Skip to content

Commit 3f5ee17

Browse files
committed
Shell scale and offset calculation
1 parent d9eecf4 commit 3f5ee17

6 files changed

Lines changed: 482 additions & 208 deletions

File tree

Lines changed: 6 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.Logging;
32
using PG.Commons.Hashing;
43
using PG.StarWarsGame.Engine.CommandBar.Components;
5-
using PG.StarWarsGame.Engine.CommandBar.Xml;
64
using PG.StarWarsGame.Engine.ErrorReporting;
75
using PG.StarWarsGame.Engine.GameConstants;
86
using PG.StarWarsGame.Engine.IO.Repositories;
97
using PG.StarWarsGame.Engine.Rendering;
108
using PG.StarWarsGame.Engine.Rendering.Font;
11-
using PG.StarWarsGame.Files.Binary;
129
using PG.StarWarsGame.Files.MTD.Files;
1310
using PG.StarWarsGame.Files.MTD.Services;
1411
using System;
1512
using System.Collections.Generic;
16-
using System.Linq;
17-
using System.Threading;
18-
using System.Threading.Tasks;
19-
using AnakinRaW.CommonUtilities.Collections;
20-
using PG.StarWarsGame.Engine.Xml;
13+
using System.Numerics;
2114

2215
namespace PG.StarWarsGame.Engine.CommandBar;
2316

24-
internal class CommandBarGameManager(
17+
internal partial class CommandBarGameManager(
2518
GameRepository repository,
2619
PGRender pgRender,
2720
IGameConstants gameConstants,
@@ -33,7 +26,6 @@ internal class CommandBarGameManager(
3326
private readonly ICrc32HashingService _hashingService = serviceProvider.GetRequiredService<ICrc32HashingService>();
3427
private readonly IMtdFileService _mtdFileService = serviceProvider.GetRequiredService<IMtdFileService>();
3528
private readonly Dictionary<string, CommandBarComponentGroup> _groups = new();
36-
private readonly PGRender _pgRender = pgRender;
3729

3830
private bool _megaTextureExists;
3931
private FontData? _defaultFont;
@@ -42,7 +34,7 @@ internal class CommandBarGameManager(
4234

4335
public IReadOnlyDictionary<string, CommandBarComponentGroup> Groups => _groups;
4436

45-
public FontData? DefaultFont
37+
public FontData? DefaultFont
4638
{
4739
get
4840
{
@@ -70,200 +62,7 @@ private set
7062
}
7163
}
7264

73-
protected override async Task InitializeCoreAsync(CancellationToken token)
74-
{
75-
Logger?.LogInformation("Creating command bar components...");
76-
77-
var contentParser = new PetroglyphStarWarsGameXmlParser(GameRepository, new PetroglyphStarWarsGameXmlParseSettings
78-
{
79-
GameManager = ToString(),
80-
InvalidObjectXmlFailsInitialization = true,
81-
InvalidFilesListXmlFailsInitialization = true
82-
}, ServiceProvider, ErrorReporter);
83-
84-
var parsedCommandBarComponents = new FrugalValueListDictionary<Crc32, CommandBarComponentData>();
85-
86-
await Task.Run(() => contentParser.ParseEntriesFromFileListXml(
87-
".\\Data\\XML\\CommandBarComponentFiles.xml",
88-
".\\DATA\\XML\\",
89-
parsedCommandBarComponents,
90-
VerifyFilePathLength),
91-
token);
92-
93-
// Create Scene
94-
// Create Camera
95-
// Resize(true)
96-
97-
foreach (var parsedCommandBarComponent in parsedCommandBarComponents.Values)
98-
{
99-
var component = CommandBarBaseComponent.Create(parsedCommandBarComponent, ErrorReporter);
100-
if (component is not null)
101-
{
102-
var crc = _hashingService.GetCrc32(component.Name, PGConstants.DefaultPGEncoding);
103-
NamedEntries.Add(crc, component);
104-
}
105-
}
106-
107-
SetComponentGroup(Components);
108-
SetMegaTexture();
109-
SetDefaultFont();
110-
111-
LinkComponentsToShell();
112-
LinkComponentsWithActions();
113-
}
114-
115-
private void LinkComponentsWithActions()
116-
{
117-
var nameLookup = SupportedCommandBarComponentData.GetComponentIdsForEngine(GameRepository.EngineType);
118-
119-
foreach (var idPair in nameLookup)
120-
{
121-
var crc = _hashingService.GetCrc32(idPair.Value, PGConstants.DefaultPGEncoding);
122-
if (NamedEntries.TryGetFirstValue(crc, out var component))
123-
component.Id = idPair.Key;
124-
}
125-
}
126-
127-
private void LinkComponentsToShell()
128-
{
129-
if (!Groups.TryGetValue(CommandBarConstants.ShellGroupName, out var shellGroup))
130-
return;
131-
132-
var modelCache = new Dictionary<string, ModelClass?>();
133-
foreach (var component in Components)
134-
{
135-
if (component.Type == CommandBarComponentType.Shell)
136-
continue;
137-
138-
foreach (var shellComponent in shellGroup.Components)
139-
{
140-
if (LinkToShell(component, shellComponent as CommandBarShellComponent, modelCache))
141-
break;
142-
}
143-
}
144-
145-
foreach (var model in modelCache.Values)
146-
model?.Dispose();
147-
}
148-
149-
private bool LinkToShell(
150-
CommandBarBaseComponent component,
151-
CommandBarShellComponent? shell,
152-
IDictionary<string, ModelClass?> modelCache)
153-
{
154-
if (shell is null)
155-
{
156-
ErrorReporter.Assert(
157-
EngineAssert.FromNullOrEmpty(
158-
[component.Name], $"Cannot link component '{component}' because shell component is null."));
159-
return false;
160-
}
161-
162-
var componentName = component.Name;
163-
if (string.IsNullOrEmpty(componentName))
164-
return false;
165-
166-
var modelPath = shell.ModelPath;
167-
if (string.IsNullOrEmpty(modelPath))
168-
return false;
169-
170-
if (!modelCache.TryGetValue(shell.Name, out var model))
171-
{
172-
model = _pgRender.LoadModelAndAnimations(modelPath.AsSpan(), null, true);
173-
modelCache.Add(shell.Name, model);
174-
}
175-
176-
if (model is null)
177-
{
178-
ErrorReporter.Assert(
179-
EngineAssert.FromNullOrEmpty(
180-
[$"component='{component.Name}'", $"shell='{shell.Name}'"],
181-
$"Cannot link component '{componentName}' to shell '{shell.Name}' because model '{modelPath}' could not be loaded."));
182-
return false;
183-
}
184-
185-
if (!model.IsModel)
186-
{
187-
ErrorReporter.Assert(
188-
EngineAssert.FromNullOrEmpty(
189-
[$"component='{component.Name}'", $"shell='{shell.Name}'"],
190-
$"Cannot link component '{componentName}' to shell '{shell.Name}' because the loaded file '{modelPath}' is not a model."));
191-
return false;
192-
}
193-
194-
var boneIndex = model.IndexOfBone(componentName);
195-
196-
if (boneIndex == -1)
197-
return false;
198-
component.Bone = boneIndex;
199-
component.ParentShell = shell;
200-
return true;
201-
}
202-
203-
private void SetDefaultFont()
204-
{
205-
// The code is only triggered iff at least one Text CommandbarBar component existed
206-
if (Components.FirstOrDefault(x => x is CommandBarTextComponent or CommandBarTextButtonComponent) is null)
207-
return;
208-
209-
if (_defaultFont is null)
210-
{
211-
// TODO: From GameConstants
212-
var fontName = PGConstants.DefaultUnicodeFontName;
213-
var size = 11;
214-
var font = fontManager.CreateFont(fontName, size, true, false, false, 1.0f);
215-
if (font is null)
216-
ErrorReporter.Assert(EngineAssert.FromNullOrEmpty([ToString()], $"Unable to create Default from name {fontName}"));
217-
DefaultFont = font;
218-
}
219-
}
220-
221-
private void SetMegaTexture()
222-
{
223-
// The code is only triggered iff at least one Shell CommandbarBar component existed
224-
if (Components.FirstOrDefault(x => x is CommandBarShellComponent) is null)
225-
return;
226-
// Note: The tag <Mega_Texture_Name> is not used by the engine
227-
var mtdPath = FileSystem.Path.Combine("DATA\\ART\\TEXTURES", $"{CommandBarConstants.MegaTextureBaseName}.mtd");
228-
using var megaTexture = GameRepository.TryOpenFile(mtdPath);
229-
230-
try
231-
{
232-
MegaTextureFile = megaTexture is null ? null : _mtdFileService.Load(megaTexture);
233-
}
234-
catch (BinaryCorruptedException e)
235-
{
236-
var message = $"Failed to load MTD file '{mtdPath}': {e.Message}";
237-
Logger?.LogError(e, message);
238-
ErrorReporter.Assert(EngineAssert.Create(EngineAssertKind.CorruptBinary, mtdPath, [], message));
239-
}
240-
_megaTextureExists = GameRepository.TextureRepository.FileExists($"{CommandBarConstants.MegaTextureBaseName}.tga");
241-
}
242-
243-
private void SetComponentGroup(IEnumerable<CommandBarBaseComponent> components)
244-
{
245-
var groupData = components
246-
.Where(x => !string.IsNullOrEmpty(x.XmlData.Group))
247-
.GroupBy(x => x.XmlData.Group!, StringComparer.Ordinal);
65+
public Vector3 CommandBarScale { get; }
24866

249-
foreach (var grouping in groupData)
250-
{
251-
var group = new CommandBarComponentGroup(grouping.Key, grouping);
252-
_groups.Add(grouping.Key, group);
253-
foreach (var component in grouping)
254-
component.Group = group;
255-
}
256-
}
257-
258-
private void VerifyFilePathLength(string filePath)
259-
{
260-
if (filePath.Length > PGConstants.MaxCommandBarDatabaseFileName)
261-
{
262-
ErrorReporter.Report(new InitializationError
263-
{
264-
GameManager = ToString(),
265-
Message = $"CommandBar file '{filePath}' is longer than {PGConstants.MaxCommandBarDatabaseFileName} characters."
266-
});
267-
}
268-
}
269-
}
67+
public Vector3 CommandBarOffset { get; internal set; }
68+
}

0 commit comments

Comments
 (0)