-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLoader.cs
More file actions
997 lines (928 loc) · 31.1 KB
/
Loader.cs
File metadata and controls
997 lines (928 loc) · 31.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
using BepInEx.Logging;
using Cpp2IL.Core.Extensions;
using Il2CppSystem.Linq;
using MonoMod.Utils;
using Newtonsoft.Json.Linq;
using PolyMod.Json;
using PolyMod.Managers;
using Polytopia.Data;
using PolytopiaBackendBase.Game;
using System.Diagnostics;
using System.Globalization;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using UnityEngine;
using PolytopiaBackendBase.Common;
namespace PolyMod;
/// <summary>
/// Handles loading of mods and their assets.
/// </summary>
public static class Loader
{
internal record TypeMapping(Type type, bool shouldCreateCache = true);
/// <summary>
/// Mappings from JSON data types to their corresponding C# types.
/// </summary>
internal static Dictionary<string, TypeMapping> typeMappings = new()
{
{ "tribeData", new TypeMapping(typeof(TribeType)) },
{ "techData", new TypeMapping(typeof(TechData.Type)) },
{ "unitData", new TypeMapping(typeof(UnitData.Type)) },
{ "improvementData", new TypeMapping(typeof(ImprovementData.Type)) },
{ "terrainData", new TypeMapping(typeof(Polytopia.Data.TerrainData.Type)) },
{ "resourceData", new TypeMapping(typeof(ResourceData.Type)) },
{ "taskData", new TypeMapping(typeof(TaskData.Type)) },
{ "tribeAbility", new TypeMapping(typeof(TribeAbility.Type)) },
{ "unitAbility", new TypeMapping(typeof(UnitAbility.Type)) },
{ "improvementAbility", new TypeMapping(typeof(ImprovementAbility.Type)) },
{ "playerAbility", new TypeMapping(typeof(PlayerAbility.Type)) },
{ "weaponData", new TypeMapping(typeof(UnitData.WeaponEnum)) },
{ "skinData", new TypeMapping(typeof(SkinData), false) }
};
/// <summary>
/// List of custom game modes to be added.
/// </summary>
internal static List<GameModeButtonsInformation> gamemodes = new();
/// <summary>
/// Handlers for processing specific data types during mod loading.
/// </summary>
internal static readonly Dictionary<Type, Action<JObject, bool>> typeHandlers = new()
{
[typeof(TribeType)] = new((token, duringEnumCacheCreation) =>
{
if (duringEnumCacheCreation)
{
Registry.customTribes.Add((TribeType)Registry.autoidx);
token["style"] = Registry.climateAutoidx;
token["climate"] = Registry.climateAutoidx;
Registry.climateAutoidx++;
}
else
{
if (token["skins"] != null)
{
JArray skins = token["skins"].Cast<JArray>();
List<JToken> skinValues = skins._values.ToArray().ToList();
foreach (var skin in skinValues)
{
string skinValue = skin.ToString();
if (!Enum.TryParse<SkinType>(skinValue, ignoreCase: true, out _))
{
EnumCache<SkinType>.AddMapping(skinValue.ToLowerInvariant(), (SkinType)Registry.autoidx);
EnumCache<SkinType>.AddMapping(skinValue.ToLowerInvariant(), (SkinType)Registry.autoidx);
Registry.skinInfo.Add(new Visual.SkinInfo(Registry.autoidx, skinValue, null));
Plugin.logger.LogInfo("Created mapping for skinType with id " + skinValue + " and index " + Registry.autoidx);
Registry.autoidx++;
}
}
Il2CppSystem.Collections.Generic.List<JToken> modifiedSkins = skins._values;
foreach (var skin in Registry.skinInfo)
{
if (modifiedSkins.Contains(skin.id))
{
modifiedSkins.Remove(skin.id);
modifiedSkins.Add(skin.idx.ToString());
}
}
JArray newSkins = new JArray();
foreach (var item in modifiedSkins)
{
newSkins.Add(item);
}
token["skins"] = newSkins;
}
if (token["preview"] != null)
{
Visual.PreviewTile[] preview = JsonSerializer.Deserialize<Visual.PreviewTile[]>(token["preview"].ToString())!;
Registry.tribePreviews[Util.GetJTokenName(token)] = preview;
}
}
}),
[typeof(UnitData.Type)] = new((token, duringEnumCacheCreation) =>
{
if (!duringEnumCacheCreation)
{
if (token["prefab"] != null)
{
Registry.prefabNames.Add((int)(UnitData.Type)(int)token["idx"], CultureInfo.CurrentCulture.TextInfo.ToTitleCase(token["prefab"]!.ToString()));
}
if (token["embarksTo"] != null)
{
string unitId = Util.GetJTokenName(token);
string embarkUnitId = token["embarksTo"].ToString();
Main.embarkNames[unitId] = embarkUnitId;
}
if (token["weapon"] != null)
{
string weaponString = token["weapon"].ToString();
if (EnumCache<UnitData.WeaponEnum>.TryGetType(weaponString, out UnitData.WeaponEnum type))
{
token["weapon"] = (int)type;
}
}
}
}),
[typeof(ImprovementData.Type)] = new((token, duringEnumCacheCreation) =>
{
if (duringEnumCacheCreation)
{
ImprovementData.Type improvementPrefabType = ImprovementData.Type.CustomsHouse;
if (token["prefab"] != null)
{
string prefabId = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(token["prefab"]!.ToString());
if (Enum.TryParse(prefabId, out ImprovementData.Type parsedType))
improvementPrefabType = parsedType;
}
PrefabManager.improvements.TryAdd((ImprovementData.Type)Registry.autoidx, PrefabManager.improvements[improvementPrefabType]);
}
else
{
if (token["attractsResource"] != null)
{
string improvementId = Util.GetJTokenName(token);
string attractsId = token["attractsResource"].ToString();
Main.attractsResourceNames[improvementId] = attractsId;
}
if (token["attractsToTerrain"] != null)
{
string improvementId = Util.GetJTokenName(token);
string attractsId = token["attractsToTerrain"].ToString();
Main.attractsTerrainNames[improvementId] = attractsId;
}
}
}),
[typeof(ResourceData.Type)] = new((token, duringEnumCacheCreation) =>
{
if (duringEnumCacheCreation)
{
ResourceData.Type resourcePrefabType = ResourceData.Type.Game;
if (token["prefab"] != null)
{
string prefabId = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(token["prefab"]!.ToString());
if (Enum.TryParse(prefabId, out ResourceData.Type parsedType))
resourcePrefabType = parsedType;
}
PrefabManager.resources.TryAdd((ResourceData.Type)Registry.autoidx, PrefabManager.resources[resourcePrefabType]);
}
}),
[typeof(SkinData)] = new((token, duringEnumCacheCreation) =>
{
var prop = token.Parent.Cast<JProperty>();
prop.Replace(new JProperty(prop.Name.ToLower(), prop.Value));
}),
};
/// <summary>
/// Represents information for a custom game mode button.
/// </summary>
/// <param name="gameModeIndex">The index of the game mode.</param>
/// <param name="action">The action to perform when the button is clicked.</param>
/// <param name="buttonIndex">The index of the button in the UI.</param>
/// <param name="sprite">The sprite for the button.</param>
/// <param name="spriteName">The name of the sprite for the button.</param>
public record GameModeButtonsInformation(int gameModeIndex, UIButtonBase.ButtonAction action, int? buttonIndex, Sprite? sprite, string? spriteName);
/// <summary>
/// Adds a new game mode button.
/// </summary>
/// <param name="id">The unique identifier for the game mode.</param>
/// <param name="action">The action to perform when the button is clicked.</param>
/// <param name="shouldShowInMenu">The boolean which decides if GameMode should appear in the menu.</param>
/// <param name="sprite">The sprite for the button.</param>
/// <param name="spriteName">The name of the sprite for the button.</param>
public static void AddGameMode(string id, UIButtonBase.ButtonAction action, bool shouldShowInMenu = true, Sprite? sprite = null, string? spriteName = null)
{
EnumCache<GameMode>.AddMapping(id, (GameMode)Registry.gameModesAutoidx);
EnumCache<GameMode>.AddMapping(id, (GameMode)Registry.gameModesAutoidx);
if(shouldShowInMenu)
gamemodes.Add(new GameModeButtonsInformation(Registry.gameModesAutoidx, action, null, sprite, spriteName));
Registry.gameModesAutoidx++;
}
/// <summary>
/// Adds a new data type for patching.
/// </summary>
/// <param name="typeId">The identifier for the data type in JSON.</param>
/// <param name="type">The C# type corresponding to the identifier.</param>
public static void AddPatchDataType(string typeId, Type type)
{
if (!typeMappings.ContainsKey(typeId))
typeMappings.Add(typeId, new TypeMapping(type));
}
public static void AddPatchDataType(string typeId, Type type, bool shouldCreateCache)
{
if (!typeMappings.ContainsKey(typeId))
typeMappings.Add(typeId, new TypeMapping(type, shouldCreateCache));
}
/// <summary>
/// Loads all mods from the mods directory.
/// </summary>
/// <param name="mods">A dictionary to populate with the loaded mods.</param>
internal static void RegisterMods(Dictionary<string, Mod> mods)
{
Directory.CreateDirectory(Plugin.MODS_PATH);
string[] modContainers = Directory.GetDirectories(Plugin.MODS_PATH)
.Union(Directory.GetFiles(Plugin.MODS_PATH, "*.polymod"))
.Union(Directory.GetFiles(Plugin.MODS_PATH, "*.zip"))
.ToArray();
foreach (var modContainer in modContainers)
{
Mod.Manifest? manifest = null;
List<Mod.File> files = new();
// Load mod from directory or zip archive
if (Directory.Exists(modContainer))
{
foreach (var file in Directory.GetFiles(modContainer))
{
if (Path.GetFileName(file) == "manifest.json")
{
manifest = JsonSerializer.Deserialize<Mod.Manifest>(
File.ReadAllBytes(file),
new JsonSerializerOptions()
{
Converters = { new VersionJson() },
}
);
continue;
}
files.Add(new(Path.GetFileName(file), File.ReadAllBytes(file)));
}
}
else
{
foreach (var entry in new ZipArchive(File.OpenRead(modContainer)).Entries)
{
if (entry.FullName == "manifest.json")
{
manifest = JsonSerializer.Deserialize<Mod.Manifest>(
entry.ReadBytes(),
new JsonSerializerOptions()
{
Converters = { new VersionJson() },
}
);
continue;
}
files.Add(new(entry.FullName, entry.ReadBytes()));
}
}
#region ValidateManifest()
if (manifest == null)
{
Plugin.logger.LogError($"Mod manifest not found in {modContainer}");
continue;
}
if (manifest.id == null)
{
Plugin.logger.LogError($"Mod id not found in {modContainer}");
continue;
}
if (!Regex.IsMatch(manifest.id, @"^(?!polytopia$)[a-z_]+$"))
{
Plugin.logger.LogError($"Mod id {manifest.id} is invalid in {modContainer}");
continue;
}
if (manifest.version == null)
{
Plugin.logger.LogError($"Mod version not found in {modContainer}");
continue;
}
if (manifest.authors == null || manifest.authors.Length == 0)
{
Plugin.logger.LogError($"Mod authors not found in {modContainer}");
continue;
}
if (mods.ContainsKey(manifest.id))
{
Plugin.logger.LogError($"Mod {manifest.id} already exists");
continue;
}
#endregion
mods.Add(manifest.id, new(
manifest,
Mod.Status.Success,
files
));
Plugin.logger.LogInfo($"Registered mod {manifest.id}");
}
CheckDependencies(mods);
}
internal static void LoadMods(Dictionary<string, Mod> mods, out bool dependencyCycle)
{
dependencyCycle = !SortMods(Registry.mods);
if (dependencyCycle) return;
StringBuilder checksumString = new();
foreach (var (id, mod) in Registry.mods)
{
if (mod.status != Mod.Status.Success) continue;
foreach (var file in mod.files)
{
checksumString.Append(JsonSerializer.Serialize(file));
if (Path.GetExtension(file.name) == ".dll")
{
LoadAssemblyFile(mod, file);
}
if (Path.GetFileName(file.name) == "sprites.json")
{
LoadSpriteInfoFile(mod, file);
}
}
if (!mod.client && id != "polytopia")
{
checksumString.Append(id);
checksumString.Append(mod.version.ToString());
}
}
Compatibility.HashSignatures(checksumString);
}
private static void CheckDependencies(Dictionary<string, Mod> mods)
{
foreach (var (id, mod) in mods)
{
foreach (var dependency in mod.dependencies ?? Array.Empty<Mod.Dependency>())
{
string? message = null;
if (!mods.ContainsKey(dependency.id))
{
message = $"Dependency {dependency.id} not found";
}
else
{
Version version = mods[dependency.id].version;
if (
(dependency.min != null && version < dependency.min)
||
(dependency.max != null && version > dependency.max)
)
{
message = $"Need dependency {dependency.id} version {dependency.min} - {dependency.max} found {version}";
}
}
if (message != null)
{
if (dependency.required)
{
Plugin.logger.LogError(message);
mod.status = Mod.Status.DependenciesUnsatisfied;
}
else
{
Plugin.logger.LogWarning(message);
}
}
}
}
}
/// <summary>
/// Sorts mods based on their dependencies using a topological sort.
/// </summary>
/// <param name="mods">The dictionary of mods to sort.</param>
/// <returns>True if the mods could be sorted (no circular dependencies), false otherwise.</returns>
private static bool SortMods(Dictionary<string, Mod> mods)
{
Stopwatch s = new();
Dictionary<string, List<string>> graph = new();
Dictionary<string, int> inDegree = new();
Dictionary<string, Mod> successfulMods = new();
Dictionary<string, Mod> unsuccessfulMods = new();
foreach (var (id, mod) in mods)
{
if (mod.status == Mod.Status.Success) successfulMods.Add(id, mod);
else unsuccessfulMods.Add(id, mod);
}
foreach (var (id, _) in successfulMods)
{
graph[id] = new();
inDegree[id] = 0;
}
foreach (var (id, mod) in successfulMods)
{
foreach (var dependency in mod.dependencies ?? Array.Empty<Mod.Dependency>())
{
if (!graph.ContainsKey(dependency.id)) continue;
graph[dependency.id].Add(id);
inDegree[id]++;
}
}
Queue<string> queue = new();
foreach (var (id, _) in successfulMods)
{
if (inDegree[id] == 0)
{
queue.Enqueue(id);
}
}
Dictionary<string, Mod> sorted = new();
while (queue.Count > 0)
{
var id = queue.Dequeue();
var mod = successfulMods[id];
sorted.Add(id, mod);
foreach (var neighbor in graph[id])
{
inDegree[neighbor]--;
if (inDegree[neighbor] == 0)
{
queue.Enqueue(neighbor);
}
}
}
if (sorted.Count != successfulMods.Count)
{
return false;
}
mods.Clear();
mods.AddRange(sorted);
mods.AddRange(unsuccessfulMods);
return true;
}
/// <summary>
/// Loads an assembly file from a mod.
/// </summary>
/// <param name="mod">The mod the assembly belongs to.</param>
/// <param name="file">The assembly file to load.</param>
public static void LoadAssemblyFile(Mod mod, Mod.File file)
{
try
{
Assembly assembly = Assembly.Load(file.bytes);
if (assembly
.GetTypes()
.FirstOrDefault(t => t.IsSubclassOf(typeof(Api.PolyScriptBase)))
is { } modType)
{
var modInstance = (Api.PolyScriptBase) Activator.CreateInstance(modType)!;
modInstance.Initialize(mod, BepInEx.Logging.Logger.CreateLogSource($"PolyMod] [{mod.id}"));
modInstance.Load();
return;
}
foreach (Type type in assembly.GetTypes())
{
MethodInfo? loadWithLogger = type.GetMethod("Load", new Type[] { typeof(ManualLogSource) });
if (loadWithLogger != null)
{
loadWithLogger.Invoke(null, new object[]
{
BepInEx.Logging.Logger.CreateLogSource($"PolyMod] [{mod.id}")
});
Plugin.logger.LogInfo($"Invoked Load method with logger from {type.FullName} from {mod.id} mod");
}
MethodInfo? load = type.GetMethod("Load", Array.Empty<Type>());
if (load != null)
{
load.Invoke(null, null);
Plugin.logger.LogInfo($"Invoked Load method from {type.FullName} from {mod.id} mod");
}
}
}
catch (TargetInvocationException exception)
{
if (exception.InnerException != null)
{
Plugin.logger.LogError($"Error on loading assembly from {mod.id} mod: {exception.InnerException.Message}");
mod.status = Mod.Status.Error;
}
}
}
/// <summary>
/// Loads a localization file from a mod.
/// </summary>
/// <param name="mod">The mod the localization file belongs to.</param>
/// <param name="file">The localization file to load.</param>
public static void LoadLocalizationFile(Mod mod, Mod.File file)
{
try
{
Loc.BuildAndLoadLocalization(JsonSerializer
.Deserialize<Dictionary<string, Dictionary<string, string>>>(file.bytes)!);
Plugin.logger.LogInfo($"Registered localization from {mod.id} mod");
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on loading locatization from {mod.id} mod: {e.StackTrace}");
}
}
/// <summary>
/// Loads a sprite file from a mod.
/// </summary>
/// <param name="mod">The mod the sprite file belongs to.</param>
/// <param name="file">The sprite file to load.</param>
public static void LoadSpriteFile(Mod mod, Mod.File file)
{
string name = Path.GetFileNameWithoutExtension(file.name);
Vector2 pivot = name.Split("_")[0] switch
{
"field" => new(0.5f, 0.0f),
"mountain" => new(0.5f, -0.375f),
_ => new(0.5f, 0.5f),
};
float pixelsPerUnit = 2112f;
if (Registry.spriteInfos.ContainsKey(name))
{
Visual.SpriteInfo spriteData = Registry.spriteInfos[name];
pivot = spriteData.pivot ?? pivot;
pixelsPerUnit = spriteData.pixelsPerUnit ?? pixelsPerUnit;
}
Sprite sprite = Visual.BuildSprite(file.bytes, pivot, pixelsPerUnit);
GameManager.GetSpriteAtlasManager().cachedSprites.TryAdd("Heads", new());
GameManager.GetSpriteAtlasManager().cachedSprites["Heads"].Add(name, sprite);
Registry.sprites.Add(name, sprite);
}
/// <summary>
/// Updates a sprite with new information.
/// </summary>
/// <param name="name">The name of the sprite to update.</param>
public static void UpdateSprite(string name)
{
if (Registry.spriteInfos.ContainsKey(name) && Registry.sprites.ContainsKey(name))
{
Visual.SpriteInfo spriteData = Registry.spriteInfos[name];
Sprite sprite = Visual.BuildSpriteWithTexture(
Registry.sprites[name].texture,
spriteData.pivot,
spriteData.pixelsPerUnit
);
GameManager.GetSpriteAtlasManager().cachedSprites["Heads"][name] = sprite;
Registry.sprites[name] = sprite;
}
}
/// <summary>
/// Loads a sprite info file from a mod.
/// </summary>
/// <param name="mod">The mod the sprite info file belongs to.</param>
/// <param name="file">The sprite info file to load.</param>
/// <returns>A dictionary of sprite information, or null if an error occurred.</returns>
public static Dictionary<string, Visual.SpriteInfo>? LoadSpriteInfoFile(Mod mod, Mod.File file)
{
try
{
var deserialized = JsonSerializer.Deserialize<Dictionary<string, Visual.SpriteInfo>>(
file.bytes,
new JsonSerializerOptions()
{
Converters = { new Vector2Json() },
}
);
if (deserialized != null)
{
foreach (var kvp in deserialized)
{
Registry.spriteInfos[kvp.Key] = kvp.Value;
}
}
Plugin.logger.LogInfo($"Registered sprite data from {mod.id} mod");
return deserialized;
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on loading sprite data from {mod.id} mod: {e.StackTrace}");
return null;
}
}
/// <summary>
/// Loads an audio file from a mod.
/// </summary>
/// <param name="mod">The mod the audio file belongs to.</param>
/// <param name="file">The audio file to load.</param>
public static void LoadAudioFile(Mod mod, Mod.File file)
{
// AudioSource audioSource = new GameObject().AddComponent<AudioSource>();
// GameObject.DontDestroyOnLoad(audioSource);
// audioSource.clip = Managers.Audio.BuildAudioClip(file.bytes);
// Registry.audioClips.Add(Path.GetFileNameWithoutExtension(file.name), audioSource);
// TODO: issue #71
}
/// <summary>
/// Loads a prefab info file from a mod and creates a new unit prefab.
/// </summary>
/// <param name="mod">The mod the prefab info file belongs to.</param>
/// <param name="file">The prefab info file to load.</param>
public static void LoadPrefabInfoFile(Mod mod, Mod.File file)
{
try
{
var prefab = JsonSerializer.Deserialize<Visual.PrefabInfo>(file.bytes, new JsonSerializerOptions
{
Converters = { new Vector2Json() },
PropertyNameCaseInsensitive = true,
});
if (prefab == null || prefab.type != Visual.PrefabType.Unit || prefab.visualParts.Count == 0)
return;
var baseUnit = PrefabManager.GetPrefab(UnitData.Type.Warrior, TribeType.Imperius, SkinType.Default);
if (baseUnit == null)
return;
var unitInstance = GameObject.Instantiate(baseUnit);
if (unitInstance == null)
return;
var spriteContainer = unitInstance.transform.GetChild(0);
var material = ClearExistingPartsAndExtractMaterial(spriteContainer);
var visualParts = ApplyVisualParts(prefab, spriteContainer, material);
Transform? headPositionMarker = null;
foreach (var vp in visualParts)
{
if (vp.visualPart.gameObject.name == prefab.headPositionMarker)
{
headPositionMarker = vp.visualPart.gameObject.transform;
break;
}
}
unitInstance.headPositionMarker = headPositionMarker ?? visualParts[0].visualPart.transform;
var svr = unitInstance.GetComponent<SkinVisualsReference>();
svr.visualParts = visualParts.ToArray();
GameObject.DontDestroyOnLoad(unitInstance.gameObject);
Registry.unitPrefabs.Add(prefab, unitInstance.GetComponent<Unit>());
Plugin.logger.LogInfo($"Registered prefab info from {mod.id} mod");
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on loading prefab info from {mod.id} mod: {e.StackTrace}");
}
}
/// <summary>
/// Clears existing visual parts from a prefab and extracts the material.
/// </summary>
/// <param name="spriteContainer">The transform containing the sprite parts.</param>
/// <returns>The material of the original sprite parts.</returns>
private static Material? ClearExistingPartsAndExtractMaterial(Transform spriteContainer)
{
Material? material = null;
for (int i = 0; i < spriteContainer.childCount; i++)
{
var child = spriteContainer.GetChild(i);
if (child.gameObject.name == "Head")
{
var renderer = child.GetComponent<SpriteRenderer>();
if (renderer != null)
material = renderer.material;
}
GameObject.Destroy(child.gameObject);
}
return material;
}
/// <summary>
/// Applies new visual parts to a prefab.
/// </summary>
/// <param name="partInfos">A list of visual part information.</param>
/// <param name="spriteContainer">The transform to add the parts to.</param>
/// <param name="material">The material to use for the parts.</param>
/// <returns>A list of the created visual parts.</returns>
private static List<SkinVisualsReference.VisualPart> ApplyVisualParts(
Visual.PrefabInfo prefab,
Transform spriteContainer,
Material? material)
{
if (prefab.visualParts.Count == 0)
return new List<SkinVisualsReference.VisualPart>();
List<SkinVisualsReference.VisualPart> parts = new();
foreach (var info in prefab.visualParts)
{
parts.Add(CreateVisualPart(info, spriteContainer, material));
}
return parts;
}
/// <summary>
/// Creates a single visual part for a prefab.
/// </summary>
/// <param name="info">The information for the visual part.</param>
/// <param name="parent">The parent transform for the part.</param>
/// <param name="material">The material to use for the part.</param>
/// <returns>The created visual part.</returns>
private static SkinVisualsReference.VisualPart CreateVisualPart(
Visual.VisualPartInfo info,
Transform parent,
Material? material)
{
var visualPartObj = new GameObject(info.gameObjectName);
visualPartObj.transform.SetParent(parent);
visualPartObj.transform.position = info.coordinates;
visualPartObj.transform.localScale = info.scale;
visualPartObj.transform.rotation = Quaternion.Euler(0f, 0f, info.rotation);
var outlineObj = new GameObject("Outline");
outlineObj.transform.SetParent(visualPartObj.transform);
outlineObj.transform.position = info.coordinates;
outlineObj.transform.localScale = info.scale;
outlineObj.transform.rotation = Quaternion.Euler(0f, 0f, info.rotation);
var visualPart = new SkinVisualsReference.VisualPart
{
DefaultSpriteName = info.baseName,
visualPart = visualPartObj,
outline = outlineObj,
tintable = info.tintable
};
var renderer = visualPartObj.AddComponent<SpriteRenderer>();
renderer.material = material;
renderer.sortingLayerName = "Units";
renderer.sortingOrder = info.tintable ? 0 : 1;
visualPart.renderer = new SkinVisualsReference.RendererUnion { spriteRenderer = renderer };
var outlineRenderer = outlineObj.AddComponent<SpriteRenderer>();
outlineRenderer.material = material;
outlineRenderer.sortingLayerName = "Units";
outlineRenderer.sortingOrder = -1;
visualPart.outlineRenderer = new SkinVisualsReference.RendererUnion { spriteRenderer = outlineRenderer };
return visualPart;
}
/// <summary>
/// Loads and applies a game logic data patch from a mod.
/// </summary>
/// <param name="mod">The mod the patch belongs to.</param>
/// <param name="gld">The original game logic data.</param>
/// <param name="patch">The patch to apply.</param>
public static void LoadGameLogicDataPatch(Mod mod, JObject gld, JObject patch)
{
try
{
// Merge the patch into the game logic data
gld = JsonMerger.Merge(gld, patch);
Plugin.logger.LogInfo($"Registered patch from {mod.id} mod");
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on loading patch from {mod.id} mod: {e.StackTrace}");
mod.status = Mod.Status.Error;
}
}
/// <summary>
/// Loads an asset bundle from a mod.
/// </summary>
/// <param name="mod">The mod the asset bundle belongs to.</param>
/// <param name="file">The asset bundle file to load.</param>
public static void LoadAssetBundle(Mod mod, Mod.File file)
{
Registry.assetBundles.Add(
Path.GetFileNameWithoutExtension(file.name),
AssetBundle.LoadFromMemory(file.bytes)
);
}
/// <summary>
/// Processes the merged game logic data after all mods have been loaded and patched.
/// </summary>
/// <param name="gameLogicData">The game logic data object to populate.</param>
/// <param name="rootObject">The root JObject of the merged game logic data.</param>
internal static void ProcessGameLogicData(GameLogicData gameLogicData, JObject rootObject)
{
try
{
CreateMappings(rootObject);
ProcessPrefabs();
ProcessEmbarkOverrides();
ProcessAttractOverrides();
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on processing modified game logic data : {e.StackTrace}");
}
}
/// <summary>
/// Creates EnumCache mappings for custom enum values and invokes type handlers.
/// </summary>
/// <param name="rootObject"></param>
internal static void CreateMappings(JObject rootObject)
{
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
{
JObject? token = jtoken.TryCast<JObject>();
if (token == null)
continue;
string dataType = Util.GetJTokenName(token, 2);
if (!typeMappings.TryGetValue(dataType, out TypeMapping? typeMapping))
continue;
if (token["idx"] == null || !typeMapping.shouldCreateCache)
continue;
Type targetType = typeMapping.type;
if (!targetType.IsEnum)
{
Plugin.logger.LogWarning($"Type {targetType.FullName} is not an enum, skipping!");
continue;
}
string id = Util.GetJTokenName(token);
if((int)token["idx"] == -1)
{
token["idx"] = Registry.autoidx;
Registry.autoidx++;
}
else if(Plugin.config.allowUnsafeIndexes)
{
Array values = Enum.GetValues(targetType);
var maxValue = values.Cast<int>().Max();
if(maxValue >= (int)token["idx"])
{
continue;
}
}
else
{
continue;
}
MethodInfo? methodInfo = typeof(EnumCache<>).MakeGenericType(targetType).GetMethod("AddMapping");
if (methodInfo == null)
{
Plugin.logger.LogWarning($"Missing AddMapping method for {targetType.FullName}");
continue;
}
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
methodInfo.Invoke(null, new object[] { id, (int)token["idx"] });
if (typeHandlers.TryGetValue(targetType, out var handler))
{
handler(token, true);
}
Plugin.logger.LogInfo("Created mapping for " + targetType.ToString() + " with id " + id + " and index " + (int)token["idx"]);
}
foreach (JToken jtoken in rootObject.SelectTokens("$.*.*").ToArray())
{
JObject? token = jtoken.TryCast<JObject>();
if (token != null)
{
string dataType = Util.GetJTokenName(token, 2);
if (typeMappings.TryGetValue(dataType, out TypeMapping? typeMapping))
{
if (typeHandlers.TryGetValue(typeMapping.type, out var handler))
{
handler(token, false);
}
}
}
}
}
/// <summary>
/// Processes the prefab registry and populates the PrefabManager with custom prefabs.
/// </summary>
internal static void ProcessPrefabs()
{
foreach (System.Collections.Generic.KeyValuePair<int, string> item in Registry.prefabNames)
{
UnitData.Type unitPrefabType = UnitData.Type.Scout;
string prefabId = item.Value;
if (Enum.TryParse(prefabId, out UnitData.Type parsedType))
{
unitPrefabType = parsedType;
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
}
else
{
KeyValuePair<Visual.PrefabInfo, Unit> prefabInfo = Registry.unitPrefabs.FirstOrDefault(kv => kv.Key.name == prefabId);
if (!EqualityComparer<Visual.PrefabInfo>.Default.Equals(prefabInfo.Key, default))
{
PrefabManager.units.TryAdd(item.Key, prefabInfo.Value);
}
else
{
PrefabManager.units.TryAdd(item.Key, PrefabManager.units[(int)unitPrefabType]);
}
}
}
}
/// <summary>
/// Processes embark overrides by mapping original embark unit types to configured overrides.
/// </summary>
internal static void ProcessEmbarkOverrides()
{
foreach (KeyValuePair<string, string> entry in Main.embarkNames)
{
try
{
UnitData.Type unit = EnumCache<UnitData.Type>.GetType(entry.Key);
UnitData.Type newUnit = EnumCache<UnitData.Type>.GetType(entry.Value);
Main.embarkOverrides[unit] = newUnit;
Plugin.logger.LogInfo($"Embark unit type for {entry.Key} is now {entry.Value}");
}
catch
{
Plugin.logger.LogError($"Embark unit type for {entry.Key} is not valid: {entry.Value}");
}
}
}
/// <summary>
/// Processes attract overrides by mapping improvements to resources and terrain types based on configured overrides.
/// </summary>
internal static void ProcessAttractOverrides()
{
foreach (KeyValuePair<string, string> entry in Main.attractsResourceNames)
{
try
{
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
ResourceData.Type resource = EnumCache<ResourceData.Type>.GetType(entry.Value);
Main.attractsResourceOverrides[improvement] = resource;
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts {entry.Value}");
}
catch
{
Plugin.logger.LogError($"Improvement {entry.Key} resource type is not valid: {entry.Value}");
}
}
foreach (KeyValuePair<string, string> entry in Main.attractsTerrainNames)
{
try
{
ImprovementData.Type improvement = EnumCache<ImprovementData.Type>.GetType(entry.Key);
Polytopia.Data.TerrainData.Type terrain = EnumCache<Polytopia.Data.TerrainData.Type>.GetType(entry.Value);
Main.attractsTerrainOverrides[improvement] = terrain;
Plugin.logger.LogInfo($"Improvement {entry.Key} now attracts on {entry.Value}");
}
catch
{
Plugin.logger.LogError($"Improvement {entry.Key} terrain type is not valid: {entry.Value}");
}
}
}
}