-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuiDialogsVerifier.cs
More file actions
166 lines (139 loc) · 6.09 KB
/
GuiDialogsVerifier.cs
File metadata and controls
166 lines (139 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using AET.ModVerify.Reporting;
using AET.ModVerify.Settings;
using AET.ModVerify.Verifiers.Commons;
using Microsoft.Extensions.DependencyInjection;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.GuiDialog;
using PG.StarWarsGame.Files.MTD.Binary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace AET.ModVerify.Verifiers.GuiDialogs;
sealed class GuiDialogsVerifier : GameVerifier
{
internal const string DefaultComponentIdentifier = "<<DEFAULT>>";
private static readonly GuiComponentType[] GuiComponentTypes =
Enum.GetValues(typeof(GuiComponentType)).OfType<GuiComponentType>().ToArray();
private readonly IAlreadyVerifiedCache? _cache;
private readonly TextureVeifier _textureVerifier;
public GuiDialogsVerifier(IStarWarsGameEngine gameEngine,
GameVerifySettings settings,
IServiceProvider serviceProvider) : base(null, gameEngine, settings, serviceProvider)
{
_cache = serviceProvider.GetService<IAlreadyVerifiedCache>();
_textureVerifier = new TextureVeifier(this, gameEngine, settings, serviceProvider);
}
public override void Verify(CancellationToken token)
{
try
{
_textureVerifier.Error += OnTextureError;
VerifyMegaTexturesExist(token);
VerifyGuiTextures();
}
finally
{
_textureVerifier.Error -= OnTextureError;
}
}
private void VerifyGuiTextures()
{
var components = new List<string>
{
DefaultComponentIdentifier
};
components.AddRange(GameEngine.GuiDialogManager.Components);
// TODO: Verify no double definitions for textures and components exit
foreach (var component in components)
VerifyGuiComponentTexturesExist(component);
}
private void VerifyMegaTexturesExist(CancellationToken token)
{
var megaTextureName = GameEngine.GuiDialogManager.GuiDialogsXml?.TextureData.MegaTexture;
if (GameEngine.GuiDialogManager.MtdFile is null)
{
var mtdFileName = megaTextureName ?? "<<MTD_NOT_SPECIFIED>>";
VerificationError.Create(VerifierChain, VerifierErrorCodes.FileNotFound, $"MtdFile '{mtdFileName}.mtd' could not be found",
VerificationSeverity.Critical, mtdFileName);
}
if (megaTextureName is not null)
{
var megaTextureFileName = $"{megaTextureName}.tga";
_textureVerifier.Verify(megaTextureFileName, ["GUIDIALOGS.XML"], token);
}
var compressedMegaTextureName = GameEngine.GuiDialogManager.GuiDialogsXml?.TextureData.CompressedMegaTexture;
if (compressedMegaTextureName is not null)
{
var compressedMegaTextureFieName = $"{compressedMegaTextureName}.dds";
_textureVerifier.Verify(compressedMegaTextureFieName, ["GUIDIALOGS.XML"], token);
}
}
private void VerifyGuiComponentTexturesExist(string component)
{
var middleButtonInRepoMode = false;
var entriesForComponent = GetTextureEntriesForComponents(component, out var defined);
if (!defined)
return;
foreach (var componentType in GuiComponentTypes)
{
try
{
if (!entriesForComponent.TryGetValue(componentType, out var texture))
continue;
if (_cache?.TryAddEntry(texture.Texture) == false)
{
// If we are in a special case we don't want to skip
if (!middleButtonInRepoMode &&
componentType is not GuiComponentType.ButtonMiddle &&
componentType is not GuiComponentType.Scanlines &&
componentType is not GuiComponentType.FrameBackground)
continue;
}
if (!GameEngine.GuiDialogManager.TextureExists(
texture,
out var origin,
out var isNone,
middleButtonInRepoMode)
&& !isNone)
{
if (origin == GuiTextureOrigin.MegaTexture && texture.Texture.Length > MtdFileConstants.MaxFileNameSize)
{
AddError(VerificationError.Create(VerifierChain, VerifierErrorCodes.FilePathTooLong,
$"The filename is too long. Max length is {MtdFileConstants.MaxFileNameSize} characters.",
VerificationSeverity.Error, texture.Texture));
}
else
{
var message = $"Could not find GUI texture '{texture.Texture}' at location '{origin}'.";
if (texture.Texture.Length > PGConstants.MaxMegEntryPathLength)
message += " The file name is too long.";
AddError(VerificationError.Create(VerifierChain, VerifierErrorCodes.FileNotFound,
message, VerificationSeverity.Error,
[component, origin.ToString()], texture.Texture));
}
}
if (componentType is GuiComponentType.ButtonMiddle && origin is GuiTextureOrigin.Repository)
middleButtonInRepoMode = true;
}
finally
{
if (componentType >= GuiComponentType.ButtonRightDisabled)
middleButtonInRepoMode = false;
}
}
}
private IReadOnlyDictionary<GuiComponentType, ComponentTextureEntry> GetTextureEntriesForComponents(string component, out bool defined)
{
if (component == DefaultComponentIdentifier)
{
defined = true;
return GameEngine.GuiDialogManager.DefaultTextureEntries;
}
return GameEngine.GuiDialogManager.GetTextureEntries(component, out defined);
}
private void OnTextureError(object sender, VerificationErrorEventArgs e)
{
AddError(e.Error);
}
}