Skip to content

Commit 5813bd5

Browse files
committed
unify CostIndex validation
1 parent fe5e813 commit 5813bd5

File tree

16 files changed

+39
-42
lines changed

16 files changed

+39
-42
lines changed

DataSanitiser/Program.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ static void QueryCostIndex()
9393
//var index = ObjectIndex.CreateIndex(dir, logger);
9494
//index.SaveIndexAsync(Path.Combine(dir, "objectIndex.json")).Wait();
9595

96-
var results = new List<(ObjectIndexEntry Obj, ObjectSource ObjectSource, byte CostIndex)>();
97-
98-
// Pseudocode plan:
99-
// 1. Get all loaded assemblies (or just the relevant ones, e.g., Assembly.GetExecutingAssembly()).
100-
// 2. For each type, check if it's a class, not abstract, implements ILocoStruct, and has a property named "CostIndex".
101-
// 3. Iterate over these types in a foreach loop.
96+
var results = new List<(ObjectIndexEntry Obj, ObjectSource ObjectSource, ObjectType ObjectType, byte CostIndex)>();
10297

10398
var locoStructTypesWithCostIndex = AppDomain.CurrentDomain.GetAssemblies()
10499
.SelectMany(a => a.GetTypes())
@@ -115,9 +110,8 @@ static void QueryCostIndex()
115110
Console.WriteLine($"Type: {type} implements ILocoStruct and has a CostIndex property.");
116111
}
117112

118-
//foreach (var obj in index.Objects.Where(x => x.ObjectSource is ObjectSource.LocomotionSteam or ObjectSource.LocomotionGoG && locoStructTypesWithCostIndex.Contains(x.ObjectType)))
119-
120-
foreach (var obj in index.Objects.Where(x => locoStructTypesWithCostIndex.Contains(x.ObjectType)))
113+
foreach (var obj in index.Objects.Where(x => x.ObjectSource is ObjectSource.LocomotionSteam or ObjectSource.LocomotionGoG && locoStructTypesWithCostIndex.Contains(x.ObjectType)))
114+
//foreach (var obj in index.Objects.Where(x => locoStructTypesWithCostIndex.Contains(x.ObjectType)))
121115
{
122116
try
123117
{
@@ -132,7 +126,7 @@ static void QueryCostIndex()
132126
var header = o.DatFileInfo.S5Header;
133127
var source = OriginalObjectFiles.GetFileSource(header.Name, header.Checksum);
134128

135-
results.Add((obj, source, costIndex));
129+
results.Add((obj, source, o.LocoObject.ObjectType, costIndex));
136130
}
137131
}
138132
catch (Exception ex)
@@ -145,7 +139,7 @@ static void QueryCostIndex()
145139
foreach (var result in results.OrderBy(x => x.CostIndex))
146140
{
147141
// Print object index entry and its enabled flags
148-
Console.WriteLine($"{result.Obj.DisplayName} - {result.ObjectSource} - CostIndex={result.CostIndex}");
142+
Console.WriteLine($"{result.Obj.DisplayName} - {result.ObjectSource} - {result.ObjectType} - CostIndex={result.CostIndex}");
149143
}
150144
}
151145
QueryCostIndex();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Definitions.ObjectModels;
2+
3+
public static class Constants
4+
{
5+
public const int CurrencyMultiplicationFactorArraySize = 32; // this is the loco GameState::currencyMultiplicationFactor array size
6+
}

Definitions/ObjectModels/Objects/Airport/AirportObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
3333
yield return result;
3434
}
3535

36-
if (CostIndex > 32)
36+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
3737
{
38-
yield return new ValidationResult($"{nameof(CostIndex)} must be between 0 and 32.", [nameof(CostIndex)]);
38+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3939
}
4040

4141
if (-SellCostFactor > BuildCostFactor)

Definitions/ObjectModels/Objects/Bridge/BridgeObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class BridgeObject : ILocoStruct
2525

2626
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2727
{
28-
if (CostIndex > 32)
28+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
2929
{
30-
yield return new ValidationResult($"{nameof(CostIndex)} must be between 0 and 32 inclusive.", [nameof(CostIndex)]);
30+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3131
}
3232

3333
if (-SellCostFactor > BaseCostFactor)

Definitions/ObjectModels/Objects/Dock/DockObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
2525
yield return result;
2626
}
2727

28-
if (CostIndex > 32)
28+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
2929
{
30-
yield return new ValidationResult($"{nameof(CostIndex)} must be between 0 and 32 (inclusive).", [nameof(CostIndex)]);
30+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3131
}
3232

3333
if (-SellCostFactor > BuildCostFactor)

Definitions/ObjectModels/Objects/Land/LandObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class LandObject : ILocoStruct
2020

2121
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2222
{
23-
if (CostIndex > 32)
23+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
2424
{
25-
yield return new ValidationResult($"{nameof(CostIndex)} must be in the range 0-32.", [nameof(CostIndex)]);
25+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
2626
}
2727

2828
if (CostFactor <= 0)

Definitions/ObjectModels/Objects/Road/RoadObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class RoadObject : ILocoStruct
2424

2525
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2626
{
27-
if (CostIndex >= 32)
27+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
2828
{
29-
yield return new ValidationResult("CostIndex must be less than 32", [nameof(CostIndex)]);
29+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3030
}
3131

3232
if (-SellCostFactor > BuildCostFactor)

Definitions/ObjectModels/Objects/RoadExtra/RoadExtraObject.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
1818
yield return new ValidationResult("PaintStyle must be 0 or 1", [nameof(PaintStyle)]);
1919
}
2020

21-
// This check missing from vanilla
22-
if (CostIndex >= 32)
21+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
2322
{
24-
yield return new ValidationResult("CostIndex must be less than 32", [nameof(CostIndex)]);
23+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
2524
}
2625

2726
if (-SellCostFactor > BuildCostFactor)

Definitions/ObjectModels/Objects/RoadStation/RoadStationObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public class RoadStationObject : ILocoStruct
2626

2727
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
2828
{
29-
if (CostIndex >= 32)
29+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
3030
{
31-
yield return new ValidationResult($"{nameof(CostIndex)} must be between 0 and 31 inclusive.", [nameof(CostIndex)]);
31+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3232
}
3333

3434
if (-SellCostFactor > BuildCostFactor)

Definitions/ObjectModels/Objects/Track/TrackObject.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
3030
yield return new ValidationResult($"{nameof(var_06)} must be 0, 1, or 2.", [nameof(var_06)]);
3131
}
3232

33-
// vanilla missed this check
34-
if (CostIndex > 32)
33+
if (CostIndex >= Constants.CurrencyMultiplicationFactorArraySize)
3534
{
36-
yield return new ValidationResult($"{nameof(CostIndex)} must be between 0 and 32 inclusive.", [nameof(CostIndex)]);
35+
yield return new ValidationResult($"{nameof(CostIndex)} must be less than {Constants.CurrencyMultiplicationFactorArraySize}", [nameof(CostIndex)]);
3736
}
3837

3938
if (-SellCostFactor > BuildCostFactor)

0 commit comments

Comments
 (0)