Skip to content

Commit d360f18

Browse files
committed
add data query project
1 parent 5ee7382 commit d360f18

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

DataQuery/DataQuery.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Dat\Dat.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

DataQuery/Program.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using OpenLoco.Common.Logging;
3+
using OpenLoco.Dat;
4+
using OpenLoco.Dat.FileParsing;
5+
using System.Reflection;
6+
7+
var dir = "Q:\\Games\\Locomotion\\Server\\Objects";
8+
var logger = new Logger();
9+
var index = ObjectIndex.LoadOrCreateIndex(dir, logger);
10+
11+
var results = new List<(ObjectIndexEntry Obj, byte CostIndex, short? RunCostIndex)>();
12+
var count = 0;
13+
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();
23+
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);
27+
28+
byte? costIndex = null;
29+
byte? runCostIndex = null;
30+
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)
36+
{
37+
costIndex = paymentIndexValue;
38+
}
39+
40+
if (runCostIndexProperty?.PropertyType == typeof(byte) && runCostIndexProperty.GetValue(struc) is byte runCostIndexValue)
41+
{
42+
runCostIndex = runCostIndexValue;
43+
}
44+
45+
if (costIndex != null)
46+
{
47+
results.Add((obj, costIndex.Value, runCostIndex));
48+
}
49+
}
50+
}
51+
catch (Exception ex)
52+
{
53+
Console.WriteLine($"{obj.Filename} - {ex.Message}");
54+
}
55+
56+
count++;
57+
58+
if (count % 500 == 0)
59+
{
60+
Console.WriteLine($"{count}/{index.Objects.Count} ({count / (float)index.Objects.Count * 100:F2}%)");
61+
}
62+
63+
//if (count > 100)
64+
//{
65+
// break;
66+
//}
67+
}
68+
69+
Console.WriteLine("writing to file");
70+
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]);
76+
77+
Console.WriteLine("done");
78+
79+
Console.ReadLine();

ObjectEditor.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaletteGenerator", "Palette
3636
EndProject
3737
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DatabaseImporter", "DatabaseImporter\DatabaseImporter.csproj", "{09C431B2-59D4-4D40-B006-A2407797FCBA}"
3838
EndProject
39+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataQuery", "DataQuery\DataQuery.csproj", "{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}"
40+
EndProject
3941
Global
4042
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4143
Debug|Any CPU = Debug|Any CPU
@@ -166,6 +168,18 @@ Global
166168
{09C431B2-59D4-4D40-B006-A2407797FCBA}.Release|x64.Build.0 = Release|Any CPU
167169
{09C431B2-59D4-4D40-B006-A2407797FCBA}.Release|x86.ActiveCfg = Release|Any CPU
168170
{09C431B2-59D4-4D40-B006-A2407797FCBA}.Release|x86.Build.0 = Release|Any CPU
171+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
172+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
173+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|x64.ActiveCfg = Debug|Any CPU
174+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|x64.Build.0 = Debug|Any CPU
175+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|x86.ActiveCfg = Debug|Any CPU
176+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Debug|x86.Build.0 = Debug|Any CPU
177+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
178+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|Any CPU.Build.0 = Release|Any CPU
179+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|x64.ActiveCfg = Release|Any CPU
180+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|x64.Build.0 = Release|Any CPU
181+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|x86.ActiveCfg = Release|Any CPU
182+
{A37DCF4D-0FC6-46F8-BF1A-24C0DB9EFEE1}.Release|x86.Build.0 = Release|Any CPU
169183
EndGlobalSection
170184
GlobalSection(SolutionProperties) = preSolution
171185
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)