Skip to content

Commit abaf82a

Browse files
committed
add data query for cargo categories
1 parent db97fb9 commit abaf82a

File tree

1 file changed

+80
-51
lines changed

1 file changed

+80
-51
lines changed

DataQuery/Program.cs

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,108 @@
11
// See https://aka.ms/new-console-template for more information
22
using OpenLoco.Common.Logging;
33
using OpenLoco.Dat;
4+
using OpenLoco.Dat.Data;
45
using OpenLoco.Dat.FileParsing;
6+
using OpenLoco.Dat.Objects;
57
using System.Reflection;
68

79
var dir = "Q:\\Games\\Locomotion\\Server\\Objects";
810
var logger = new Logger();
911
var index = ObjectIndex.LoadOrCreateIndex(dir, logger);
1012

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);
1315

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");
2317

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();
2719

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)>();
3023

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)
3630
{
37-
costIndex = paymentIndexValue;
38-
}
31+
var struc = (CargoObject)o.Value.LocoObject.Object;
3932

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);
4435

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));
4837
}
4938
}
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+
}
5443
}
5544

56-
count++;
45+
Console.WriteLine("writing to file");
5746

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)
5959
{
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();
6267

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);
6871

69-
Console.WriteLine("writing to file");
72+
byte? costIndex = null;
73+
byte? runCostIndex = null;
7074

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+
}
7683

77-
Console.WriteLine("done");
84+
if (runCostIndexProperty?.PropertyType == typeof(byte) && runCostIndexProperty.GetValue(struc) is byte runCostIndexValue)
85+
{
86+
runCostIndex = runCostIndexValue;
87+
}
7888

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

Comments
 (0)