Skip to content
Open
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
7 changes: 4 additions & 3 deletions mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
84 changes: 51 additions & 33 deletions source/CommanderPortraitLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class CommanderPortraitLoader
public static List<CustomVoice> customVoices;
public static List<string> searchablePaths;
public static List<string> jsonSearchablePaths;
public static readonly List<string> supportedSuffixes = new List<string>() { ".png", ".jpeg", ".gif", ".dds" };
public static void FinishedLoading(List<string> loadOrder) {
Logger.LogLine("FinishedLoading");
try {
Expand Down Expand Up @@ -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<string>();
searchablePaths.Add($"{ModDirectory}/Portraits/");
searchablePaths.Add(Path.Combine(ModDirectory,"Portraits"));
jsonSearchablePaths = new List<string>();
jsonSearchablePaths.Add($"{ ModDirectory}/../CPLHelper/portraits/");
jsonSearchablePaths.Add(Path.Combine(ModDirectory,"..","CPLHelper","portraits"));
disableCreatePilotPatch = true;
CreateJsons();
}
Expand All @@ -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(Path.Combine(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)
Expand Down
42 changes: 34 additions & 8 deletions source/CommanderPortraitLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,38 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\ModTek\0Harmony.dll</HintPath>
<HintPath>$(BattleTechGameDir)\Mods\ModTek\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="IRBTModUtils">
<HintPath>$(BattleTechGameDir)\Mods\IRBTModUtils\IRBTModUtils.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\BattleTech_Data\Managed\Newtonsoft.Json.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System">
<HintPath>..\..\..\BattleTech_Data\Managed\System.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\System.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Core">
<HintPath>..\..\..\BattleTech_Data\Managed\System.Core.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\System.Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\..\..\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.ImageConversionModule">
<HintPath>..\..\..\BattleTech_Data\Managed\UnityEngine.ImageConversionModule.dll</HintPath>
<HintPath>$(BattleTechGameDir)\BattleTech_Data\Managed\UnityEngine.ImageConversionModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
Expand All @@ -87,4 +90,27 @@
<Compile Include="VersionManifestUtilities_Patches.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>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"
)
</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion source/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
Expand Down
45 changes: 30 additions & 15 deletions source/RenderedPortraitResult_GetItemPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UnityEngine;
using System.IO;
using CommanderPortraitLoader;
using IRBTModUtils;

namespace CommanderPortraitLoader
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion source/SGBarracksMWCustomizationPopup_Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion source/VersionManifestUtilities_Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down