Skip to content

Commit 6caf136

Browse files
committed
GameObjectType PostLoadFixup
1 parent f3cdad8 commit 6caf136

5 files changed

Lines changed: 57 additions & 18 deletions

File tree

src/PetroglyphTools/PG.StarWarsGame.Engine/ErrorReporting/EngineAssert.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PG.StarWarsGame.Engine.ErrorReporting;
99

1010
public sealed class EngineAssert
1111
{
12-
private static readonly string ThisNameSpace = typeof(EngineAssert).Namespace!;
12+
private static readonly string ThisNamespace = typeof(EngineAssert).Namespace!;
1313
private const string NullLiteral = "NULL";
1414

1515
public string Value { get; }
@@ -66,7 +66,7 @@ internal static EngineAssert Create(EngineAssertKind kind, object? value, IEnume
6666
{
6767
var frame = trace.GetFrame(i);
6868
var method = frame.GetMethod();
69-
if (method.DeclaringType is null || method.DeclaringType.Namespace?.Equals(ThisNameSpace) == false)
69+
if (method.DeclaringType is null || method.DeclaringType.Namespace?.Equals(ThisNamespace) == false)
7070
return frame;
7171
}
7272
return null;

src/PetroglyphTools/PG.StarWarsGame.Engine/GameObjects/GameObject.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,6 @@ internal GameObject(
6363
ClassificationName = classification ?? throw new ArgumentNullException(nameof(classification));
6464
LandTerrainModelMappingValues = new ReadOnlyCollection<(string, string)>(InternalLandTerrainModelMapping);
6565
}
66-
67-
public void PostLoadFixup()
68-
{
69-
// TODO:
70-
// MaxSpeed *= 1.0;
71-
// MaxThrust *= 1.0;
72-
73-
// The engine loads references for scripts, images, hardpoints, etc.,
74-
// but we don't do that here.
75-
}
7666

7767
internal void ApplyBaseType(GameObject baseType)
7868
{
@@ -91,4 +81,10 @@ internal void ApplyBaseType(GameObject baseType)
9181
DamagedSmokeAssetModel = baseType.DamagedSmokeAssetModel;
9282
InternalLandTerrainModelMapping.ClearAddRange(baseType.InternalLandTerrainModelMapping);
9383
}
84+
85+
internal void PostLoadFixup()
86+
{
87+
// This method is different than the fixup that is performed after parsing the object.
88+
// IDK why there are two separate fixups. This fixup performs more value coercions
89+
}
9490
}

src/PetroglyphTools/PG.StarWarsGame.Engine/GameObjects/GameObjectTypeGameManager.Initialization.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using System;
2+
using Microsoft.Extensions.Logging;
23
using PG.StarWarsGame.Engine.ErrorReporting;
34
using PG.StarWarsGame.Engine.Xml;
45
using PG.StarWarsGame.Engine.Xml.Parsers;
@@ -70,13 +71,39 @@ private void ParseGameObjectDatabases()
7071
}
7172

7273

73-
74-
//GameObjectTypeClass::Static_Post_Load_Fixup();
74+
PostLoadFixup();
7575
//SFXEventReferenceClass::Static_Post_Load_Fixup();
7676
//SpeechEventReferenceClass::Static_Post_Load_Fixup();
7777
//MusicEventReferenceClass::Static_Post_Load_Fixup();
7878
//FactionReferenceClass::Static_Post_Load_Fixup();
7979
//...
80+
81+
allLoaded = true;
82+
83+
foreach (var gameObject in _gameObjects)
84+
{
85+
gameObject.PostLoadFixup();
86+
if (!gameObject.IsLoadingComplete)
87+
allLoaded = false;
88+
}
89+
}
90+
}
91+
92+
private void PostLoadFixup()
93+
{
94+
// In the engine, this is the so-called static post load fixup.
95+
foreach (var gameObject in _gameObjects)
96+
{
97+
var baseTypeName = gameObject.VariantOfExistingTypeName;
98+
if (string.IsNullOrEmpty(baseTypeName) || baseTypeName.Equals("None", StringComparison.OrdinalIgnoreCase))
99+
continue;
100+
101+
// At this point the engine would assert, if the base type was not found.
102+
// We do not, because the game reports this error for every iteration of the loading loop,
103+
// which would bloat error logs.
104+
var baseNameHash = _hashingService.GetCrc32Upper(baseTypeName, PGConstants.DefaultPGEncoding);
105+
NamedEntries.TryGetFirstValue(baseNameHash, out var baseType);
106+
gameObject.VariantOfExistingType = baseType;
80107
}
81108
}
82109

src/PetroglyphTools/PG.StarWarsGame.Engine/GameObjects/GameObjectTypeGameManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
using PG.StarWarsGame.Engine.ErrorReporting;
1+
using PG.Commons.Hashing;
2+
using PG.StarWarsGame.Engine.ErrorReporting;
23
using PG.StarWarsGame.Engine.IO.Repositories;
34
using System;
45
using System.Collections.Generic;
56
using System.Collections.ObjectModel;
7+
using Microsoft.Extensions.DependencyInjection;
68

79
namespace PG.StarWarsGame.Engine.GameObjects;
810

911
internal partial class GameObjectTypeGameManager : GameManagerBase<GameObject>, IGameObjectTypeGameManager
1012
{
1113
private readonly List<GameObject> _gameObjects;
14+
private readonly ICrc32HashingService _hashingService;
1215

1316
public GameObjectTypeGameManager(
1417
GameRepository repository,
1518
GameEngineErrorReporterWrapper errorReporter,
1619
IServiceProvider serviceProvider)
1720
: base(repository, errorReporter, serviceProvider)
1821
{
22+
_hashingService = serviceProvider.GetRequiredService<ICrc32HashingService>();
1923
_gameObjects = [];
2024
GameObjects = new ReadOnlyCollection<GameObject>(_gameObjects);
2125
}

src/PetroglyphTools/PG.StarWarsGame.Engine/Xml/Parsers/NamedObjects/GameObjectParser.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ protected override void ValidateAndFixupValues(GameObject gameObject, XElement e
7575
{
7676
if (!OverlayLoad)
7777
{
78+
// TODO
7879
//BehaviorClass.AddImpliedBehaviors(this, BehaviorNames);
7980
//InitBehaviorMap();
8081

81-
gameObject.PostLoadFixup();
82+
PostLoadFixup(gameObject);
8283
if (string.IsNullOrEmpty(gameObject.VariantOfExistingTypeName))
8384
gameObject.IsLoadingComplete = true;
8485
else
@@ -113,7 +114,7 @@ private void OverlayType(GameObject baseType, GameObject derivedType, XElement e
113114

114115
ParseTags(derivedType, element, true, ReadOnlyFrugalValueListDictionary<Crc32, GameObject>.Empty);
115116

116-
derivedType.PostLoadFixup();
117+
PostLoadFixup(derivedType);
117118
derivedType.IsLoadingComplete = true;
118119
}
119120

@@ -130,6 +131,17 @@ protected override bool ParseTag(
130131
return true;
131132
}
132133

134+
private void PostLoadFixup(GameObject gameObject)
135+
{
136+
// TODO:
137+
// MaxSpeed *= 1.0;
138+
// MaxThrust *= 1.0;
139+
// Asserts and some coercions
140+
141+
// The engine loads references for scripts, images, hardpoints, etc.,
142+
// but we don't do that here.
143+
}
144+
133145
private sealed class GameObjectXmlTagMapper(IServiceProvider serviceProvider) : XmlTagMapper<GameObject>(serviceProvider)
134146
{
135147
protected override void BuildMappings()

0 commit comments

Comments
 (0)