Skip to content

Commit 2ccae9c

Browse files
add doc comments and change file locations
1 parent fe55cb6 commit 2ccae9c

10 files changed

Lines changed: 253 additions & 81 deletions

File tree

src/Loader.cs

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static void AddPatchDataType(string typeId, Type type)
148148
typeMappings.Add(typeId, type);
149149
}
150150

151-
internal static void RegisterMods(Dictionary<string, Mod> mods)
151+
internal static bool RegisterMods(Dictionary<string, Mod> mods)
152152
{
153153
Directory.CreateDirectory(Plugin.MODS_PATH);
154154
string[] modContainers = Directory.GetDirectories(Plugin.MODS_PATH)
@@ -237,13 +237,11 @@ internal static void RegisterMods(Dictionary<string, Mod> mods)
237237
}
238238

239239
CheckDependencies(mods);
240+
var dependencyCycle = !SortMods(Registry.mods);
241+
return dependencyCycle;
240242
}
241-
242243
internal static void LoadMods(Dictionary<string, Mod> mods)
243244
{
244-
var dependencyCycle = !SortMods(Registry.mods);
245-
if (dependencyCycle) return;
246-
247245
StringBuilder checksumString = new();
248246
foreach (var (id, mod) in Registry.mods)
249247
{
@@ -269,6 +267,69 @@ internal static void LoadMods(Dictionary<string, Mod> mods)
269267
Compatibility.HashSignatures(checksumString);
270268

271269
}
270+
internal static void PatchGLD(JObject gameLogicdata, Dictionary<string, Mod> mods)
271+
{
272+
Loc.BuildAndLoadLocalization(
273+
JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(
274+
Plugin.GetResource("localization.json")
275+
)!
276+
);
277+
foreach (var (id, mod) in mods)
278+
{
279+
if (mod.status != Mod.Status.Success) continue;
280+
foreach (var file in mod.files)
281+
{
282+
if (Path.GetFileName(file.name) == "localization.json")
283+
{
284+
Loader.LoadLocalizationFile(mod, file);
285+
continue;
286+
}
287+
if (Regex.IsMatch(Path.GetFileName(file.name), @"^patch(_.*)?\.json$"))
288+
{
289+
var patchText = new StreamReader(new MemoryStream(file.bytes)).ReadToEnd();
290+
var template = new GldConfigTemplate(patchText, mod.id);
291+
var text = template.Render();
292+
if (text is null)
293+
{
294+
mod.status = Mod.Status.Error;
295+
continue;
296+
}
297+
Loader.LoadGameLogicDataPatch(
298+
mod,
299+
gameLogicdata,
300+
JObject.Parse(text)
301+
);
302+
continue;
303+
}
304+
if (Regex.IsMatch(Path.GetFileName(file.name), @"^prefab(_.*)?\.json$"))
305+
{
306+
Loader.LoadPrefabInfoFile(
307+
mod,
308+
file
309+
);
310+
continue;
311+
}
312+
313+
switch (Path.GetExtension(file.name))
314+
{
315+
case ".png":
316+
Loader.LoadSpriteFile(mod, file);
317+
break;
318+
case ".wav":
319+
Loader.LoadAudioFile(mod, file);
320+
break;
321+
case ".bundle":
322+
Loader.LoadAssetBundle(mod, file);
323+
break;
324+
}
325+
}
326+
}
327+
TechItem.techTierFirebaseId.Clear();
328+
for (int i = 0; i <= Main.MAX_TECH_TIER; i++)
329+
{
330+
TechItem.techTierFirebaseId.Add($"tech_research_{i}");
331+
}
332+
}
272333
private static void CheckDependencies(Dictionary<string, Mod> mods)
273334
{
274335
foreach (var (id, mod) in mods)

src/Managers/Config.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace PolyMod.Managers;
55

66
/// <summary>
7-
/// allows mods to save config.
7+
/// Allows mods to save config.
88
/// </summary>
99
public class Config<T> where T : class
1010
{
@@ -91,7 +91,7 @@ public TResult Get<TResult>(Func<T, TResult> getter)
9191
return getter(currentConfig ?? throw new InvalidOperationException("Must set default before reading config."));
9292
}
9393
/// <summary>
94-
/// writes the config to disk
94+
/// Writes the config to disk
9595
/// </summary>
9696
public void SaveChanges()
9797
{

src/Managers/GLDConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace PolyMod.Managers;
77

8-
public class GldConfigTemplate
8+
internal class GldConfigTemplate
99
{
1010
private static readonly string ConfigPath = Path.Combine(Plugin.BASE_PATH, "mods.json");
1111

src/Managers/Visual.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,93 @@
1010

1111
namespace PolyMod.Managers;
1212

13+
/// <summary>
14+
/// Manages visual aspects of the game, including sprites, UI elements, and in-game object appearances.
15+
/// </summary>
1316
public static class Visual
1417
{
18+
/// <summary>
19+
/// Represents a single tile in a tribe's world preview.
20+
/// </summary>
1521
public class PreviewTile
1622
{
23+
/// <summary>The X-coordinate of the tile.</summary>
1724
[JsonInclude]
1825
public int? x = null;
26+
/// <summary>The Y-coordinate of the tile.</summary>
1927
[JsonInclude]
2028
public int? y = null;
29+
/// <summary>The type of terrain on the tile.</summary>
2130
[JsonInclude]
2231
[JsonConverter(typeof(EnumCacheJson<Polytopia.Data.TerrainData.Type>))]
2332
public Polytopia.Data.TerrainData.Type terrainType = Polytopia.Data.TerrainData.Type.Ocean;
33+
/// <summary>The type of resource on the tile.</summary>
2434
[JsonInclude]
2535
[JsonConverter(typeof(EnumCacheJson<ResourceData.Type>))]
2636
public ResourceData.Type resourceType = ResourceData.Type.None;
37+
/// <summary>The type of unit on the tile.</summary>
2738
[JsonInclude]
2839
[JsonConverter(typeof(EnumCacheJson<UnitData.Type>))]
2940
public UnitData.Type unitType = UnitData.Type.None;
41+
/// <summary>The type of improvement on the tile.</summary>
3042
[JsonInclude]
3143
[JsonConverter(typeof(EnumCacheJson<ImprovementData.Type>))]
3244
public ImprovementData.Type improvementType = ImprovementData.Type.None;
3345
}
46+
47+
/// <summary>
48+
/// Holds information for creating a custom sprite.
49+
/// </summary>
50+
/// <param name="pixelsPerUnit">The number of pixels per unit for the sprite.</param>
51+
/// <param name="pivot">The pivot point of the sprite.</param>
3452
public record SpriteInfo(float? pixelsPerUnit, Vector2? pivot);
53+
54+
/// <summary>
55+
/// Holds information about a custom skin.
56+
/// </summary>
57+
/// <param name="idx">The index of the skin.</param>
58+
/// <param name="id">The unique identifier for the skin.</param>
59+
/// <param name="skinData">The data associated with the skin.</param>
3560
public record SkinInfo(int idx, string id, SkinData? skinData);
61+
62+
/// <summary>
63+
/// A dictionary mapping BasicPopup instance IDs to their custom widths.
64+
/// </summary>
3665
public static Dictionary<int, int> basicPopupWidths = new();
3766
private static bool firstTimeOpeningPreview = true;
3867
private static UnitData.Type currentUnitTypeUI = UnitData.Type.None;
3968
private static TribeData.Type attackerTribe = TribeData.Type.None;
69+
70+
/// <summary>
71+
/// Defines the types of prefabs that can be customized.
72+
/// </summary>
4073
public enum PrefabType
4174
{
75+
/// <summary>A unit prefab.</summary>
4276
Unit,
77+
/// <summary>An improvement prefab.</summary>
4378
Improvement,
79+
/// <summary>A resource prefab.</summary>
4480
Resource
4581
}
82+
83+
/// <summary>
84+
/// Holds information for a custom prefab.
85+
/// </summary>
86+
/// <param name="type">The type of the prefab.</param>
87+
/// <param name="name">The name of the prefab.</param>
88+
/// <param name="visualParts">A list of visual parts that make up the prefab.</param>
4689
public record PrefabInfo(PrefabType type, string name, List<VisualPartInfo> visualParts);
90+
91+
/// <summary>
92+
/// Represents a visual part of a custom prefab.
93+
/// </summary>
94+
/// <param name="gameObjectName">The name of the GameObject for this visual part.</param>
95+
/// <param name="baseName">The base name for sprite lookups.</param>
96+
/// <param name="rotation">The rotation of the visual part.</param>
97+
/// <param name="coordinates">The local position of the visual part.</param>
98+
/// <param name="scale">The local scale of the visual part.</param>
99+
/// <param name="tintable">Whether the visual part can be tinted.</param>
47100
public record VisualPartInfo(
48101
string gameObjectName,
49102
string baseName,
@@ -257,7 +310,7 @@ private static void TerrainRenderer_UpdateGraphics(TerrainRenderer __instance, T
257310
if (tile.data.terrain is Polytopia.Data.TerrainData.Type.Forest or Polytopia.Data.TerrainData.Type.Mountain)
258311
{
259312
string propertyName = terrain.ToLower();
260-
terrain = "field";
313+
terrain = "field";
261314

262315
PropertyInfo? rendererProperty = tile.GetType().GetProperty(propertyName + "Renderer",
263316
BindingFlags.Public | BindingFlags.Instance);
@@ -565,6 +618,11 @@ private static void PopupBase_Hide(PopupBase __instance)
565618
basicPopupWidths.Remove(__instance.GetInstanceID());
566619
}
567620

621+
/// <summary>
622+
/// Shows a BasicPopup and sets its width.
623+
/// </summary>
624+
/// <param name="self">The BasicPopup instance.</param>
625+
/// <param name="width">The desired width of the popup.</param>
568626
public static void ShowSetWidth(this BasicPopup self, int width)
569627
{
570628
basicPopupWidths.Add(self.GetInstanceID(), width);
@@ -594,6 +652,13 @@ private static void UpdateVisualPart(SkinVisualsReference.VisualPart visualPart,
594652
}
595653
}
596654

655+
/// <summary>
656+
/// Creates a <see cref="Sprite"/> from raw image data.
657+
/// </summary>
658+
/// <param name="data">The byte array containing the image data.</param>
659+
/// <param name="pivot">The pivot point for the sprite. Defaults to the center.</param>
660+
/// <param name="pixelsPerUnit">The number of pixels per unit for the sprite.</param>
661+
/// <returns>A new <see cref="Sprite"/> instance.</returns>
597662
public static Sprite BuildSprite(byte[] data, Vector2? pivot = null, float pixelsPerUnit = 2112f)
598663
{
599664
Texture2D texture = new(1, 1, TextureFormat.RGBA32, true);
@@ -609,6 +674,13 @@ public static Sprite BuildSprite(byte[] data, Vector2? pivot = null, float pixel
609674
return BuildSpriteWithTexture(texture, pivot, pixelsPerUnit);
610675
}
611676

677+
/// <summary>
678+
/// Creates a <see cref="Sprite"/> from an existing <see cref="Texture2D"/>.
679+
/// </summary>
680+
/// <param name="texture">The texture to create the sprite from.</param>
681+
/// <param name="pivot">The pivot point for the sprite. Defaults to the center.</param>
682+
/// <param name="pixelsPerUnit">The number of pixels per unit for the sprite.</param>
683+
/// <returns>A new <see cref="Sprite"/> instance.</returns>
612684
public static Sprite BuildSpriteWithTexture(Texture2D texture, Vector2? pivot = null, float? pixelsPerUnit = 2112f)
613685
{
614686
return Sprite.Create(

src/Mod.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,76 @@
11
namespace PolyMod;
22

3+
/// <summary>
4+
/// Represents a mod, containing its manifest information, status, and files.
5+
/// </summary>
36
public class Mod
47
{
8+
/// <summary>
9+
/// Represents a dependency for a mod.
10+
/// </summary>
11+
/// <param name="id">The unique identifier of the dependency.</param>
12+
/// <param name="min">The minimum compatible version of the dependency.</param>
13+
/// <param name="max">The maximum compatible version of the dependency.</param>
14+
/// <param name="required">Whether the dependency is required for the mod to function.</param>
515
public record Dependency(string id, Version min, Version max, bool required = true);
16+
17+
/// <summary>
18+
/// Represents the manifest of a mod, containing metadata.
19+
/// </summary>
20+
/// <param name="id">The unique identifier of the mod.</param>
21+
/// <param name="name">The display name of the mod.</param>
22+
/// <param name="description">A description of the mod.</param>
23+
/// <param name="version">The version of the mod.</param>
24+
/// <param name="authors">The authors of the mod.</param>
25+
/// <param name="dependencies">The dependencies of the mod.</param>
26+
/// <param name="client">Whether the mod is client-side only.</param>
627
public record Manifest(string id, string? name, string? description, Version version, string[] authors, Dependency[]? dependencies, bool client = false);
28+
29+
/// <summary>
30+
/// Represents a file included in a mod.
31+
/// </summary>
32+
/// <param name="name">The name of the file.</param>
33+
/// <param name="bytes">The raw byte content of the file.</param>
734
public record File(string name, byte[] bytes);
35+
36+
/// <summary>
37+
/// Represents the loading status of a mod.
38+
/// </summary>
839
public enum Status
940
{
41+
/// <summary>The mod was loaded successfully.</summary>
1042
Success,
43+
/// <summary>An error occurred while loading the mod.</summary>
1144
Error,
45+
/// <summary>The mod's dependencies were not satisfied.</summary>
1246
DependenciesUnsatisfied,
1347
}
1448

49+
/// <summary>The unique identifier of the mod.</summary>
1550
public string id;
51+
/// <summary>The display name of the mod.</summary>
1652
public string? name;
53+
/// <summary>A description of the mod.</summary>
1754
public string? description;
55+
/// <summary>The version of the mod.</summary>
1856
public Version version;
57+
/// <summary>The authors of the mod.</summary>
1958
public string[] authors;
59+
/// <summary>The dependencies of the mod.</summary>
2060
public Dependency[]? dependencies;
61+
/// <summary>Whether the mod is client-side only.</summary>
2162
public bool client;
63+
/// <summary>The loading status of the mod.</summary>
2264
public Status status;
65+
/// <summary>The files included in the mod.</summary>
2366
public List<File> files;
2467

68+
/// <summary>
69+
/// Initializes a new instance of the <see cref="Mod"/> class.
70+
/// </summary>
71+
/// <param name="manifest">The mod's manifest data.</param>
72+
/// <param name="status">The initial loading status of the mod.</param>
73+
/// <param name="files">The list of files included in the mod.</param>
2574
public Mod(Manifest manifest, Status status, List<File> files)
2675
{
2776
id = manifest.id;
@@ -34,4 +83,13 @@ public Mod(Manifest manifest, Status status, List<File> files)
3483
this.status = status;
3584
this.files = files;
3685
}
86+
87+
/// <summary>
88+
/// Returns a string that represents the current mod.
89+
/// </summary>
90+
/// <returns>A string representation of the mod.</returns>
91+
public override string ToString()
92+
{
93+
return $"mod: id={id} dependencies={dependencies} status={status}";
94+
}
3795
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace PolyMod.Managers;
77

8-
public static class Audio
8+
internal static class Audio
99
{
1010
[HarmonyPostfix]
1111
[HarmonyPatch(typeof(AudioManager), nameof(AudioManager.SetupData))]

0 commit comments

Comments
 (0)