|
1 | 1 | // See https://aka.ms/new-console-template for more information
|
2 | 2 | using OpenLoco.Common.Logging;
|
3 | 3 | using OpenLoco.Dat;
|
| 4 | +using OpenLoco.Dat.Data; |
4 | 5 | using OpenLoco.Dat.FileParsing;
|
| 6 | +using OpenLoco.Dat.Objects; |
5 | 7 | using System.Reflection;
|
6 | 8 |
|
7 | 9 | var dir = "Q:\\Games\\Locomotion\\Server\\Objects";
|
8 | 10 | var logger = new Logger();
|
9 | 11 | var index = ObjectIndex.LoadOrCreateIndex(dir, logger);
|
10 | 12 |
|
11 |
| -var results = new List<(ObjectIndexEntry Obj, byte CostIndex, short? RunCostIndex)>(); |
12 |
| -var count = 0; |
| 13 | +//QueryCostIndices(dir, logger, index); |
| 14 | +QueryCargoCategories(dir, logger, index); |
13 | 15 |
|
14 |
| -foreach (var obj in index.Objects) |
15 |
| -{ |
16 |
| - try |
17 |
| - { |
18 |
| - var o = SawyerStreamReader.LoadFullObjectFromFile(Path.Combine(dir, obj.Filename), logger); |
19 |
| - if (o?.LocoObject != null) |
20 |
| - { |
21 |
| - var struc = o.Value.LocoObject.Object; |
22 |
| - var type = struc.GetType(); |
| 16 | +Console.WriteLine("done"); |
23 | 17 |
|
24 |
| - var costIndexProperty = type.GetProperty("CostIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
25 |
| - var paymentIndexProperty = type.GetProperty("PaymentIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
26 |
| - var runCostIndexProperty = type.GetProperty("RunCostIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
| 18 | +Console.ReadLine(); |
27 | 19 |
|
28 |
| - byte? costIndex = null; |
29 |
| - byte? runCostIndex = null; |
| 20 | +static void QueryCargoCategories(string dir, Logger logger, ObjectIndex index) |
| 21 | +{ |
| 22 | + var results = new List<(ObjectIndexEntry Obj, CargoCategory CargoCategory, string LocalisedName, ObjectSource ObjectSource)>(); |
30 | 23 |
|
31 |
| - if (costIndexProperty?.PropertyType == typeof(byte) && costIndexProperty.GetValue(struc) is byte costIndexValue) |
32 |
| - { |
33 |
| - costIndex = costIndexValue; |
34 |
| - } |
35 |
| - else if (paymentIndexProperty?.PropertyType == typeof(byte) && paymentIndexProperty.GetValue(struc) is byte paymentIndexValue) |
| 24 | + foreach (var obj in index.Objects.Where(x => x.ObjectType == ObjectType.Cargo)) |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + var o = SawyerStreamReader.LoadFullObjectFromFile(Path.Combine(dir, obj.Filename), logger); |
| 29 | + if (o?.LocoObject != null) |
36 | 30 | {
|
37 |
| - costIndex = paymentIndexValue; |
38 |
| - } |
| 31 | + var struc = (CargoObject)o.Value.LocoObject.Object; |
39 | 32 |
|
40 |
| - if (runCostIndexProperty?.PropertyType == typeof(byte) && runCostIndexProperty.GetValue(struc) is byte runCostIndexValue) |
41 |
| - { |
42 |
| - runCostIndex = runCostIndexValue; |
43 |
| - } |
| 33 | + var header = o.Value.DatFileInfo.S5Header; |
| 34 | + var source = OriginalObjectFiles.GetFileSource(header.Name, header.Checksum); |
44 | 35 |
|
45 |
| - if (costIndex != null) |
46 |
| - { |
47 |
| - results.Add((obj, costIndex.Value, runCostIndex)); |
| 36 | + results.Add((obj, struc.CargoCategory, o.Value.LocoObject.StringTable.Table["Name"][LanguageId.English_UK], source)); |
48 | 37 | }
|
49 | 38 | }
|
50 |
| - } |
51 |
| - catch (Exception ex) |
52 |
| - { |
53 |
| - Console.WriteLine($"{obj.Filename} - {ex.Message}"); |
| 39 | + catch (Exception ex) |
| 40 | + { |
| 41 | + Console.WriteLine($"{obj.Filename} - {ex.Message}"); |
| 42 | + } |
54 | 43 | }
|
55 | 44 |
|
56 |
| - count++; |
| 45 | + Console.WriteLine("writing to file"); |
57 | 46 |
|
58 |
| - if (count % 500 == 0) |
| 47 | + const string csvHeader = "DatName, CargoCategory, LocalisedName, ObjectSource"; |
| 48 | + var lines = results |
| 49 | + .OrderBy(x => x.Obj.DatName) |
| 50 | + .Select(x => string.Join(',', x.Obj.DatName, (int)x.CargoCategory, x.LocalisedName, x.ObjectSource)); |
| 51 | + File.WriteAllLines("cargoCategories.csv", [csvHeader, .. lines]); |
| 52 | +} |
| 53 | + |
| 54 | +static void QueryCostIndices(string dir, Logger logger, ObjectIndex index) |
| 55 | +{ |
| 56 | + var results = new List<(ObjectIndexEntry Obj, byte CostIndex, short? RunCostIndex)>(); |
| 57 | + |
| 58 | + foreach (var obj in index.Objects) |
59 | 59 | {
|
60 |
| - Console.WriteLine($"{count}/{index.Objects.Count} ({count / (float)index.Objects.Count * 100:F2}%)"); |
61 |
| - } |
| 60 | + try |
| 61 | + { |
| 62 | + var o = SawyerStreamReader.LoadFullObjectFromFile(Path.Combine(dir, obj.Filename), logger); |
| 63 | + if (o?.LocoObject != null) |
| 64 | + { |
| 65 | + var struc = o.Value.LocoObject.Object; |
| 66 | + var type = struc.GetType(); |
62 | 67 |
|
63 |
| - //if (count > 100) |
64 |
| - //{ |
65 |
| - // break; |
66 |
| - //} |
67 |
| -} |
| 68 | + var costIndexProperty = type.GetProperty("CostIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
| 69 | + var paymentIndexProperty = type.GetProperty("PaymentIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
| 70 | + var runCostIndexProperty = type.GetProperty("RunCostIndex", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); |
68 | 71 |
|
69 |
| -Console.WriteLine("writing to file"); |
| 72 | + byte? costIndex = null; |
| 73 | + byte? runCostIndex = null; |
70 | 74 |
|
71 |
| -var header = "DatName, ObjectType, CostIndex, RunCostIndex"; |
72 |
| -var lines = results |
73 |
| - .OrderBy(x => x.Obj.DatName) |
74 |
| - .Select(x => string.Join(',', x.Obj.DatName, x.Obj.ObjectType, x.CostIndex, x.RunCostIndex)); |
75 |
| -File.WriteAllLines("costIndex.csv", [header, .. lines]); |
| 75 | + if (costIndexProperty?.PropertyType == typeof(byte) && costIndexProperty.GetValue(struc) is byte costIndexValue) |
| 76 | + { |
| 77 | + costIndex = costIndexValue; |
| 78 | + } |
| 79 | + else if (paymentIndexProperty?.PropertyType == typeof(byte) && paymentIndexProperty.GetValue(struc) is byte paymentIndexValue) |
| 80 | + { |
| 81 | + costIndex = paymentIndexValue; |
| 82 | + } |
76 | 83 |
|
77 |
| -Console.WriteLine("done"); |
| 84 | + if (runCostIndexProperty?.PropertyType == typeof(byte) && runCostIndexProperty.GetValue(struc) is byte runCostIndexValue) |
| 85 | + { |
| 86 | + runCostIndex = runCostIndexValue; |
| 87 | + } |
78 | 88 |
|
79 |
| -Console.ReadLine(); |
| 89 | + if (costIndex != null) |
| 90 | + { |
| 91 | + results.Add((obj, costIndex.Value, runCostIndex)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + Console.WriteLine($"{obj.Filename} - {ex.Message}"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + Console.WriteLine("writing to file"); |
| 102 | + |
| 103 | + const string header = "DatName, ObjectType, CostIndex, RunCostIndex"; |
| 104 | + var lines = results |
| 105 | + .OrderBy(x => x.Obj.DatName) |
| 106 | + .Select(x => string.Join(',', x.Obj.DatName, x.Obj.ObjectType, x.CostIndex, x.RunCostIndex)); |
| 107 | + File.WriteAllLines("costIndex.csv", [header, .. lines]); |
| 108 | +} |
0 commit comments