From c8fef663810e8cb092d6fc36eef8e74e2d8065a5 Mon Sep 17 00:00:00 2001 From: CMiSSioN <47270663+CMiSSioN@users.noreply.github.com> Date: Mon, 6 Dec 2021 12:59:14 +0300 Subject: [PATCH 1/3] add basic gif support --- source/CommanderPortraitLoader.cs | 84 +++++++++++-------- source/CommanderPortraitLoader.csproj | 42 ++++++++-- source/Logger.cs | 2 +- source/RenderedPortraitResult_GetItemPatch.cs | 45 ++++++---- .../SGBarracksMWCustomizationPopup_Patches.cs | 2 +- source/VersionManifestUtilities_Patches.cs | 2 +- 6 files changed, 118 insertions(+), 59 deletions(-) diff --git a/source/CommanderPortraitLoader.cs b/source/CommanderPortraitLoader.cs index 74b08b4..76e411a 100644 --- a/source/CommanderPortraitLoader.cs +++ b/source/CommanderPortraitLoader.cs @@ -19,6 +19,7 @@ public static class CommanderPortraitLoader public static List customVoices; public static List searchablePaths; public static List jsonSearchablePaths; + public static readonly List supportedSuffixes = new List() { ".png", ".jpeg", ".gif", ".dds" }; public static void FinishedLoading(List loadOrder) { Logger.LogLine("FinishedLoading"); try { @@ -62,9 +63,9 @@ public static void Init(string directory, string settingsJSON) harmony.Patch(genericMethod, null, null, new HarmonyMethod(transpiler)); harmony.PatchAll(Assembly.GetExecutingAssembly()); searchablePaths = new List(); - searchablePaths.Add($"{ModDirectory}/Portraits/"); + searchablePaths.Add(Path.Combine(ModDirectory,"Portraits")); jsonSearchablePaths = new List(); - jsonSearchablePaths.Add($"{ ModDirectory}/../CPLHelper/portraits/"); + jsonSearchablePaths.Add(Path.Combine(ModDirectory,"..","CPLHelper","portraits")); disableCreatePilotPatch = true; CreateJsons(); } @@ -74,53 +75,70 @@ public static void CreateJsons() try { //Create a path for the Json files if it does not already exist - string jsonPath = $"{ CommanderPortraitLoader.ModDirectory}/../CPLHelper/portraits/"; + string jsonPath = Path.Combine(ModDirectory, "..", "CPLHelper", "portraits"); + //$"{ CommanderPortraitLoader.ModDirectory}/../CPLHelper/portraits/"; Directory.CreateDirectory(jsonPath); - string filePath = $"{ CommanderPortraitLoader.ModDirectory}/Portraits/"; + string filePath = Path.Combine(ModDirectory, "Portraits");//$"{ CommanderPortraitLoader.ModDirectory}/Portraits/"; DirectoryInfo d1 = new DirectoryInfo(filePath); FileInfo[] f1 = d1.GetFiles(); foreach (FileInfo info in f1) { - if (info.Name.EndsWith(".png")) - { - if (!File.Exists(info.FullName.Replace(".png", ".json"))) + foreach(string ext in supportedSuffixes) { + if (info.Name.EndsWith(ext)) { + if (!File.Exists(info.FullName.Replace(ext, ".json"))) { PortraitSettings portait = new PortraitSettings(); portait.headMesh = 0.5f; portait.Randomize(true); - portait.Description.SetName(info.Name.Replace(".png", "")); - portait.Description.SetID(info.Name.Replace(".png", "")); - portait.Description.SetIcon(info.Name.Replace(".png", "")); + portait.Description.SetName(info.Name.Replace(ext, "")); + portait.Description.SetID(info.Name.Replace(ext, "")); + portait.Description.SetIcon(info.Name.Replace(ext, "")); portait.isCommander = true; - using (StreamWriter writer = - new StreamWriter(jsonPath + info.Name.Replace(".png", ".json"), false)) + using (StreamWriter writer = new StreamWriter(jsonPath + info.Name.Replace(ext, ".json"), false)) { writer.WriteLine(portait.ToJSON()); } } + break; + } } - else - { - if (info.Name.EndsWith(".dds")) - { - if (!File.Exists(info.FullName.Replace(".dds", ".json"))) - { - PortraitSettings portait = new PortraitSettings(); - portait.headMesh = 0.5f; - portait.Randomize(true); - portait.Description.SetName(info.Name.Replace(".dds", "")); - portait.Description.SetID(info.Name.Replace(".dds", "")); - portait.Description.SetIcon(info.Name.Replace(".dds", "")); - portait.isCommander = true; - using (StreamWriter writer = - new StreamWriter(jsonPath + info.Name.Replace(".dds", ".json"), false)) - { - writer.WriteLine(portait.ToJSON()); - } - } - } - } + //if (info.Name.EndsWith(".png")) + //{ + // if (!File.Exists(info.FullName.Replace(".png", ".json"))) + // { + // PortraitSettings portait = new PortraitSettings(); + // portait.headMesh = 0.5f; + // portait.Randomize(true); + // portait.Description.SetName(info.Name.Replace(".png", "")); + // portait.Description.SetID(info.Name.Replace(".png", "")); + // portait.Description.SetIcon(info.Name.Replace(".png", "")); + // portait.isCommander = true; + // using (StreamWriter writer = + // new StreamWriter(jsonPath + info.Name.Replace(".png", ".json"), false)) + // { + // writer.WriteLine(portait.ToJSON()); + // } + // } + //} + //else if (info.Name.EndsWith(".dds")) + //{ + // if (!File.Exists(info.FullName.Replace(".dds", ".json"))) + // { + // PortraitSettings portait = new PortraitSettings(); + // portait.headMesh = 0.5f; + // portait.Randomize(true); + // portait.Description.SetName(info.Name.Replace(".dds", "")); + // portait.Description.SetID(info.Name.Replace(".dds", "")); + // portait.Description.SetIcon(info.Name.Replace(".dds", "")); + // portait.isCommander = true; + // using (StreamWriter writer = + // new StreamWriter(jsonPath + info.Name.Replace(".dds", ".json"), false)) + // { + // writer.WriteLine(portait.ToJSON()); + // } + // } + //} } } catch (Exception e) diff --git a/source/CommanderPortraitLoader.csproj b/source/CommanderPortraitLoader.csproj index fe3722a..5fd3b48 100644 --- a/source/CommanderPortraitLoader.csproj +++ b/source/CommanderPortraitLoader.csproj @@ -35,35 +35,38 @@ - ..\..\ModTek\0Harmony.dll + $(BattleTechGameDir)\Mods\ModTek\0Harmony.dll False - ..\..\..\BattleTech_Data\Managed\Assembly-CSharp.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\Assembly-CSharp.dll False + + $(BattleTechGameDir)\Mods\IRBTModUtils\IRBTModUtils.dll + - ..\..\..\BattleTech_Data\Managed\Newtonsoft.Json.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\Newtonsoft.Json.dll False - ..\..\..\BattleTech_Data\Managed\System.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\System.dll False - ..\..\..\BattleTech_Data\Managed\System.Core.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\System.Core.dll False - ..\..\..\BattleTech_Data\Managed\UnityEngine.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.dll False - ..\..\..\BattleTech_Data\Managed\UnityEngine.CoreModule.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.CoreModule.dll False - ..\..\..\BattleTech_Data\Managed\UnityEngine.ImageConversionModule.dll + $(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.ImageConversionModule.dll False @@ -87,4 +90,27 @@ + + if exist "$(BattleTechGameDir)\Mods_CBDE\CommanderPortraitLoader" ( + echo %25BattleTechGameDir%25 + copy /Y "$(TargetPath)" "$(BattleTechGameDir)\Mods_CBDE\CommanderPortraitLoader" +) +if exist "$(BattleTechGameDir)\Mods\CommanderPortraitLoader" ( + echo %25BattleTechGameDir%25 + copy /Y "$(TargetPath)" "$(BattleTechGameDir)\Mods\CommanderPortraitLoader" +) +if exist "$(RogueTechRepo)\CommanderPortraitLoader" ( + echo %25BattleTechGameDir%25 + copy /Y "$(TargetPath)" "$(RogueTechRepo)\CommanderPortraitLoader" +) +if exist "$(BTARepo)\CommanderPortraitLoader" ( + echo %25BattleTechGameDir%25 + copy /Y "$(TargetPath)" "$(BTARepo)\CommanderPortraitLoader" +) +if exist "$(BattleTechGameDir)\Release_CAC\CustomBundle\CommanderPortraitLoader" ( + echo %25BattleTechGameDir%25 + copy /Y "$(TargetPath)" "$(BattleTechGameDir)\Release_CAC\Release\CustomBundle\CommanderPortraitLoader" +) + + \ No newline at end of file diff --git a/source/Logger.cs b/source/Logger.cs index fd2e42b..90458a8 100644 --- a/source/Logger.cs +++ b/source/Logger.cs @@ -3,7 +3,7 @@ namespace CommanderPortraitLoader { public class Logger { - static string filePath = $"{CommanderPortraitLoader.ModDirectory}/Log.txt"; + static string filePath = Path.Combine(CommanderPortraitLoader.ModDirectory,"Log.txt"); public static void LogError(Exception ex) { using (var writer = new StreamWriter(filePath, true)) { writer.WriteLine("Message :" + ex.Message + "
" + Environment.NewLine + "StackTrace :" + ex.StackTrace + diff --git a/source/RenderedPortraitResult_GetItemPatch.cs b/source/RenderedPortraitResult_GetItemPatch.cs index 15a2d99..683facb 100644 --- a/source/RenderedPortraitResult_GetItemPatch.cs +++ b/source/RenderedPortraitResult_GetItemPatch.cs @@ -9,6 +9,7 @@ using UnityEngine; using System.IO; using CommanderPortraitLoader; +using IRBTModUtils; namespace CommanderPortraitLoader { @@ -21,25 +22,39 @@ static void Postfix(RenderedPortraitResult __instance, ref Texture2D __result) { try { - Texture2D texture2D = new Texture2D(2, 2); + Texture2D texture2D = null; + Logger.LogLine("RenderedPortraitResult.Item "+ __instance.settings.Description.Icon); foreach (string path in CommanderPortraitLoader.searchablePaths) { - string filePath = path + __instance.settings.Description.Icon; - byte[] array; - if (File.Exists(filePath + ".dds")) - { - array = File.ReadAllBytes(filePath + ".dds"); - texture2D = TextureManager.LoadTextureDXT(array); - break; - } - else - { - array = File.ReadAllBytes(filePath + ".png"); - texture2D.LoadImage(array); - break; + string[] files = Directory.GetFiles(path, __instance.settings.Description.Icon + ".*"); + foreach (string filePath in files) { + //string filePath = Path.Combine(path, __instance.settings.Description.Icon); + foreach (string ext in CommanderPortraitLoader.supportedSuffixes) { + if (filePath.EndsWith(ext)) { + byte[] data = File.ReadAllBytes(filePath); + if (TextureManager.IsDDS(data)) { + texture2D = TextureManager.LoadTextureDXT(data); + }else if (TextureManager.IsPNG(data) || TextureManager.IsJPG(data)) { + texture2D = new Texture2D(2, 2, TextureFormat.DXT5, false); + if (texture2D.LoadImage(data) == false) { texture2D = null; } + } else if (data.IsGIF()) { + UniGif.GifImage gif = UniGif.GetTexturesList(data); + if(gif.frames.Count > 0) { + Logger.LogLine(" frames:" + gif.frames.Count + " loop:" + gif.loopCount + " size:" + gif.width + "x" + gif.height); + gif.Register(__instance.settings.Description.Icon); + UniGif.GifSprites gifSprites = new UniGif.GifSprites(gif); + gifSprites.Register(__instance.settings.Description.Icon); + texture2D = gif.frames.Count > 0 ? gif.frames[0].m_texture2d:null; + } + } + } + if (texture2D != null) { break; } + } + if (texture2D != null) { break; } } + if(texture2D != null) { break; } } - + if(texture2D == null) { texture2D = new Texture2D(2, 2); } __result = texture2D; } catch (Exception e) diff --git a/source/SGBarracksMWCustomizationPopup_Patches.cs b/source/SGBarracksMWCustomizationPopup_Patches.cs index b4570f5..233780a 100644 --- a/source/SGBarracksMWCustomizationPopup_Patches.cs +++ b/source/SGBarracksMWCustomizationPopup_Patches.cs @@ -32,7 +32,7 @@ static void Prefix(ref SGBarracksMWCustomizationPopup __instance, ref PortraitSe { - string filePath = path + __instance.pilot.pilotDef.Description.Icon + ".json"; + string filePath = Path.Combine(path,__instance.pilot.pilotDef.Description.Icon + ".json"); if (File.Exists(filePath)) { portraitSettingsData = new PortraitSettings(); diff --git a/source/VersionManifestUtilities_Patches.cs b/source/VersionManifestUtilities_Patches.cs index 90d41b5..45ab48f 100644 --- a/source/VersionManifestUtilities_Patches.cs +++ b/source/VersionManifestUtilities_Patches.cs @@ -16,7 +16,7 @@ public static void Postfix(ref VersionManifest __result) { try { - string filePath = $"{ CommanderPortraitLoader.ModDirectory}/../CPLHelper/portraits/"; + string filePath = Path.Combine(CommanderPortraitLoader.ModDirectory, "..", "CPLHelper", "portraits");//$"{ CommanderPortraitLoader.ModDirectory}/../CPLHelper/portraits/"; DirectoryInfo d1 = new DirectoryInfo(filePath); FileInfo[] f1 = d1.GetFiles("*.json"); From 87d7fc2b8c6ad4783b7ea698bf207dcf471c918a Mon Sep 17 00:00:00 2001 From: CMiSSioN <47270663+CMiSSioN@users.noreply.github.com> Date: Mon, 6 Dec 2021 13:18:28 +0300 Subject: [PATCH 2/3] Update mod.json --- mod.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mod.json b/mod.json index c4ac3a9..2028b75 100644 --- a/mod.json +++ b/mod.json @@ -3,11 +3,12 @@ "Enabled": true, "Version": "2.0.0", - "Description": "Loads your custom PNGs as a Commander avatar and allows voice selection.", - "Author": "Morphyum & Callyste & Jamie Wolf", + "Description": "Loads your custom PNG,DDS,JPEG and GIF as a Commander avatar and allows voice selection.", + "Author": "Morphyum & Callyste & Jamie Wolf & KMiSSioN", "Website": "https://gitlab.com/JWolf16/jw-commanderportraitloader", "Contact": "", - + "DependsOn": [ "IRBTModUtils" ], + "ConflictsWith": [ "RogueTech Enhanced", "RogueTech Enhanced Clans"], "DLL": "CommanderPortraitLoader.dll", From 9b47e1f7f1c76f1d13cbcf5aac56b0c09bbf4717 Mon Sep 17 00:00:00 2001 From: CMiSSioN <47270663+CMiSSioN@users.noreply.github.com> Date: Tue, 14 Dec 2021 09:26:13 +0300 Subject: [PATCH 3/3] portraits path fix --- source/CommanderPortraitLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/CommanderPortraitLoader.cs b/source/CommanderPortraitLoader.cs index 76e411a..50a1a89 100644 --- a/source/CommanderPortraitLoader.cs +++ b/source/CommanderPortraitLoader.cs @@ -95,7 +95,7 @@ public static void CreateJsons() portait.Description.SetID(info.Name.Replace(ext, "")); portait.Description.SetIcon(info.Name.Replace(ext, "")); portait.isCommander = true; - using (StreamWriter writer = new StreamWriter(jsonPath + info.Name.Replace(ext, ".json"), false)) + using (StreamWriter writer = new StreamWriter(Path.Combine(jsonPath,info.Name.Replace(ext, ".json")), false)) { writer.WriteLine(portait.ToJSON()); }