|
| 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(); |
0 commit comments