Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ public static void UpdateSprite(string name)
}
}

Plugin.logger.LogInfo($"Registered sprite data from {mod.id} mod");
Plugin.logger.LogInfo($"Registered sprite info from {mod.id} mod");
return deserialized;
}
catch (Exception e)
{
Plugin.logger.LogError($"Error on loading sprite data from {mod.id} mod: {e.Message}");
Plugin.logger.LogError($"Error on loading sprite info from {mod.id} mod: {e.Message}");
return null;
}
}
Expand All @@ -458,6 +458,31 @@ public static void LoadAudioFile(Mod mod, Mod.File file)
// TODO: issue #71
}

public static void LoadPrefabInfoFile(Mod mod, Mod.File file)
{
try
{
string name = Path.GetFileNameWithoutExtension(file.name);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

Visual.PrefabInfo? prefab = JsonSerializer.Deserialize<Visual.PrefabInfo>(file.bytes, options);

if (prefab != null)
{
Console.WriteLine(prefab);
Registry.prefabInfos.Add(name, prefab);
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.Message}");
}
}

public static void LoadGameLogicDataPatch(Mod mod, JObject gld, JObject patch)
{
try
Expand Down
107 changes: 107 additions & 0 deletions src/Managers/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@
);
continue;
}
if (Regex.IsMatch(Path.GetFileName(file.name), @"^prefab(_.*)?\.json$"))
{
Loader.LoadPrefabInfoFile(
mod,
file
);
continue;
}

switch (Path.GetExtension(file.name))
{
Expand All @@ -381,5 +389,104 @@
}
stopwatch.Stop();
Plugin.logger.LogInfo($"Loaded all mods in {stopwatch.ElapsedMilliseconds}ms");
Unit baseUnit = PrefabManager.units[GetSkinnedHashKey(2, SkinType.Default)];
if (baseUnit != null)
{
foreach (System.Collections.Generic.KeyValuePair<string, PolyMod.Managers.Visual.PrefabInfo> valuePair in Registry.prefabInfos)
{
Console.Write(valuePair.Key);
PolyMod.Managers.Visual.PrefabInfo info = valuePair.Value;
SkinVisualsReference? skinComp = baseUnit.GetComponent<SkinVisualsReference>();
if (skinComp != null)
{
Transform head = KeepOnlyChildByName(baseUnit.transform, "Head");
skinComp.visualParts = new Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppReferenceArray<SkinVisualsReference.VisualPart>(1);
SkinVisualsReference.VisualPart visualPart = new SkinVisualsReference.VisualPart();
visualPart.DefaultSpriteName = "head";
visualPart.visualPart = head.gameObject;
skinComp.visualParts[0] = visualPart;
PrefabManager.units[GetSkinnedHashKey(53, SkinType.Default)] = baseUnit;
}
}
}
else
{
Console.Write("NULLLLLLLL");
}
}

public static Transform KeepOnlyChildByName(Transform parent, string nameToKeep)
{
Console.Write("0");
var spriteContainer = parent.GetChild(0);
Console.Write("1");
int childCount = spriteContainer.childCount;
Transform? toReturn = null;
for (int i = 0; i < childCount; i++)
{
var child = spriteContainer.GetChild(i);
Console.Write(child.gameObject.name);
if (child.gameObject.name != nameToKeep)
{
GameObject.Destroy(child.gameObject);
}
else
{
toReturn = child.transform;
}
}
return toReturn;

Check failure on line 438 in src/Managers/Main.cs

View workflow job for this annotation

GitHub Actions / CI/CD

Possible null reference return.

Check failure on line 438 in src/Managers/Main.cs

View workflow job for this annotation

GitHub Actions / CI/CD

Possible null reference return.

Check failure on line 438 in src/Managers/Main.cs

View workflow job for this annotation

GitHub Actions / CI/CD

Possible null reference return.

Check failure on line 438 in src/Managers/Main.cs

View workflow job for this annotation

GitHub Actions / CI/CD

Possible null reference return.
}

private static void GetTypeAndSkin(int hash, out int type, out SkinType skin)
{
skin = (SkinType)(hash / 1000);
type = hash % 1000;
}

private static int GetSkinnedHashKey(int type, SkinType skin)
{
return type + ((int)skin * 1000);
}

public static void GetPrefabTree(Transform transform, int depth)
{
if (transform == null) return;

GameObject obj = transform.gameObject;
string indent = new string(' ', depth * 2);
Console.WriteLine($"{indent}GameObject: {obj.name}");

var components = obj.GetComponents<Component>();
foreach (var comp in components)
{
if (comp == null) continue;

var typeName = comp.GetIl2CppType().Name;

if (typeName == "SkinVisualsReference")
{
SkinVisualsReference? skinComp = comp.TryCast<SkinVisualsReference>();
if (skinComp != null)
{
Console.WriteLine($"{indent}- Component: {typeName}");
foreach (SkinVisualsReference.VisualPart part in skinComp.visualParts)
{
if (part.visualPart == null) continue;
Console.WriteLine($"{indent}- VS: {part.visualPart.GetIl2CppType().Name}");
Console.WriteLine($"{indent}- VS: {part.DefaultSpriteName}");
Console.WriteLine($"{indent}- VS: {part.visualPart.transform.position}");
part.visualPart.transform.position = new Vector3(0, 0, 0);
}
}
}
}

int childCount = transform.childCount;
for (int i = 0; i < childCount; i++)
{
var child = transform.GetChild(i);
GetPrefabTree(child, depth + 1);
}
}
}
3 changes: 2 additions & 1 deletion src/Managers/Visual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class PreviewTile
}
public record SpriteInfo(float? pixelsPerUnit, Vector2? pivot);
public record SkinInfo(int idx, string id, SkinData? skinData);
public record PrefabInfo(string type, string name, List<VisualPartInfo> visualParts);
public record VisualPartInfo(string baseName, Vector3 coordinates);
public static Dictionary<int, int> basicPopupWidths = new();
private static bool firstTimeOpeningPreview = true;
private static UnitData.Type currentUnitTypeUI = UnitData.Type.None;
Expand Down Expand Up @@ -96,7 +98,6 @@ void GetAtlas(SpriteAtlas spriteAtlas)
private static void SpriteAtlasManager_DoSpriteLookup(ref SpriteAtlasManager.SpriteLookupResult __result, SpriteAtlasManager __instance, string baseName, TribeData.Type tribe, SkinType skin, bool checkForOutline, int level)
{
baseName = Util.FormatSpriteName(baseName);

Sprite? sprite = Registry.GetSprite(baseName, Util.GetStyle(tribe, skin), level);
if (sprite != null)
__result.sprite = sprite;
Expand Down
1 change: 1 addition & 0 deletions src/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class Registry
internal static Dictionary<string, Mod> mods = new();
public static Dictionary<string, Visual.PreviewTile[]> tribePreviews = new();
public static Dictionary<string, Visual.SpriteInfo> spriteInfos = new();
public static Dictionary<string, Visual.PrefabInfo> prefabInfos = new();
public static Dictionary<string, AssetBundle> assetBundles = new();
public static List<TribeData.Type> customTribes = new();
public static List<Visual.SkinInfo> skinInfo = new();
Expand Down
Loading