Skip to content

Commit

Permalink
*cleanup and finished loading portraits without configuring game files
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphyum committed May 6, 2018
1 parent 3ae880f commit 7195bca
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 204 deletions.
240 changes: 36 additions & 204 deletions ClassLibrary2/CommanderPortraitLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,197 +14,61 @@
using HBS.Collections;
using BattleTech.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace CommanderPortraitLoader {
[HarmonyPatch(typeof(RenderedPortraitResult), "get_Item")]
public static class RenderedPortraitResult_get_Item_Patch {
static void Postfix(RenderedPortraitResult __instance, ref Texture2D __result) {
Logger.LogLine("RenderedPortraitResult Postfix start");
if (!string.IsNullOrEmpty(__instance.settings.Description.Icon)) {
Logger.LogLine("Icon Found");
try {
Texture2D texture2D = new Texture2D(2, 2);
byte[] array = File.ReadAllBytes("mods/CommanderPortraitLoader/Portraits/" + __instance.settings.Description.Icon + ".png");
texture2D.LoadImage(array);
__result = texture2D;
}
catch (Exception e) {
Logger.LogError(e);
}
}
}
}

[HarmonyPatch(typeof(SGCharacterCreationWidget), "CreatePilot")]
public static class SGCharacterCreationWidget_CreatePilot_Patch {
static void Postfix(ref SGCharacterCreationWidget __instance, ref Pilot __result) {
Logger.LogLine("CreatePilot Postfix start");
try {
if (!string.IsNullOrEmpty(__result.pilotDef.Description.Icon)) {
PilotDef pilotDef = new PilotDef(new HumanDescriptionDef(__result.Description.Id, __result.Description.Callsign, __result.Description.FirstName, __result.Description.LastName,
__result.Description.Callsign, __result.Description.Gender, Faction.NoFaction, __result.Description.Age, __result.Description.Details, __result.pilotDef.Description.Icon),
__result.Gunnery, __result.Piloting, __result.Guts, __result.Guts, 0, 3, false, 0, string.Empty, CommanderPortraitLoader.GetAbilities(__result.Gunnery, __result.Piloting, __result.Guts, __result.Guts), AIPersonality.Undefined, 0, __result.pilotDef.PilotTags, 0, 0);
pilotDef.PortraitSettings = null;
pilotDef.SetHiringHallStats(true, false, true, false);
__result = new Pilot(pilotDef, "commander", false);
Logger.LogLine("Pilot modified");
}
}
catch (Exception e) {
Logger.LogError(e);
}
}
}

[HarmonyPatch(typeof(PilotDef), "GetPortraitSprite")]
public static class PilotDef_GetPortraitSprite_Patch {
static void Postfix(ref PilotDef __instance, ref Sprite __result) {
Logger.LogLine("GetPortraitSprite Postfix start");
try {
if (__result == null) {
Texture2D texture2D2 = new Texture2D(2, 2);
byte[] data = File.ReadAllBytes("mods/CommanderPortraitLoader/Portraits/" + __instance.Description.Icon + ".png");
texture2D2.LoadImage(data);
Sprite sprite = new Sprite();
sprite = Sprite.Create(texture2D2, new Rect(0f, 0f, (float)texture2D2.width, (float)texture2D2.height), new Vector2(0f, 0f), 100f);
__result = sprite;
}
}
catch (Exception e) {
Logger.LogError(e);
}
}
}

[HarmonyPatch(typeof(PilotDef), "GetPortraitSpriteThumb")]
public static class PilotDef_GetPortraitSpriteThumb_Patch {
static void Postfix(ref PilotDef __instance, ref Sprite __result) {
Logger.LogLine("GetPortraitSpriteThumb Postfix start");
try {
if (__result == null) {
Texture2D texture2D2 = new Texture2D(2, 2);
byte[] data = File.ReadAllBytes("mods/CommanderPortraitLoader/Portraits/" + __instance.Description.Icon + ".png");
texture2D2.LoadImage(data);
Sprite sprite = new Sprite();
sprite = Sprite.Create(texture2D2, new Rect(0f, 0f, (float)texture2D2.width, (float)texture2D2.height), new Vector2(0f, 0f), 100f);
__result = CommanderPortraitLoader.DownsampleSprite(sprite);
}
}
catch (Exception e) {
Logger.LogError(e);
}
}
}

/*[HarmonyPatch(typeof(SGCharacterCreationPortraitSelectionPanel), "PopulateList")]
public static class SGCharacterCreationPortraitSelectionPanel_PopulateList_Patch {
static void Postfix(SGCharacterCreationPortraitSelectionPanel __instance) {
Logger.LogLine("PopulateList Prefix start");
try {
PortraitSettings settings = new PortraitSettings();
string json;
using (StreamReader r = new StreamReader("mods/CommanderPortraitLoader/Portraits/PortraitPreset_Kara.json")) {
json = r.ReadToEnd();
}
settings.FromJSON(json);
DataManager data = LazySingletonBehavior<UnityGameInstance>.Instance.Game.DataManager;
KeyValuePair<string, PortraitSettings> pair = new KeyValuePair<string, PortraitSettings>(settings.Description.Id, settings);
data.PortraitSettings.Add(pair);
}
catch (Exception e) {
Logger.LogError(e);
}
}
}*/

[HarmonyPatch(typeof(VersionManifestUtilities), "LoadDefaultManifest")]
public static class VersionManifestUtilitiesPatch {
// ReSharper disable once RedundantAssignment
public static void Postfix(ref VersionManifest __result) {
try {
var addendum = VersionManifestUtilities.ManifestFromCSV("mods/CommanderPortraitLoader/VersionManifest.csv");
foreach (var entry in addendum.Entries) {
__result.AddOrUpdate(entry.Id, entry.FilePath, entry.Type, entry.AddedOn, entry.AssetBundleName, entry.IsAssetBundlePersistent);
}
}
catch (Exception e) {
Logger.LogError(e);
}

}
}

public static class CommanderPortraitLoader {

public static void Init() {
var harmony = HarmonyInstance.Create("de.morphyum.CommanderPortraitLoader");
harmony.PatchAll(Assembly.GetExecutingAssembly());
CreateJsons();
AddOrUpdateJSONToManifest();
}



// Token: 0x0600564D RID: 22093 RVA: 0x0023F158 File Offset: 0x0023D358
public static Sprite DownsampleSprite(Sprite oldSprite) {
Texture2D texture = oldSprite.texture;
RenderTexture temporary = RenderTexture.GetTemporary(texture.width / 2, texture.height / 2, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
RenderTexture temporary2 = RenderTexture.GetTemporary(texture.width / 4, texture.height / 4, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
Graphics.Blit(texture, temporary);
Graphics.Blit(temporary, temporary2);
Texture2D texture2D = new Texture2D(texture.width / 4, texture.height / 4, TextureFormat.ARGB32, false);
RenderTexture.active = temporary2;
texture2D.ReadPixels(new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), 0, 0);
texture2D.Apply(false, true);
Texture2D texture2D2 = texture2D;
texture2D2.name = texture2D2.name + texture.name + " Thumb";
RenderTexture.ReleaseTemporary(temporary);
RenderTexture.ReleaseTemporary(temporary2);
return Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f), 100f, 0u, SpriteMeshType.FullRect, Vector4.zero);
}

public static List<string> GetAbilities(int gunnery, int piloting, int guts, int tactics) {
List<string> list = new List<string>();
SimPilotProgressionConstantsDef progression = LazySingletonBehavior<UnityGameInstance>.Instance.Game.Simulation.Constants.Progression;
int abilityLevel = gunnery;
int abilityLevel2 = piloting;
int abilityLevel3 = guts;
int abilityLevel4 = tactics;
list.AddRange(CommanderPortraitLoader.GetAbilityDefsForSkill(progression.GunnerySkills, abilityLevel));
list.AddRange(CommanderPortraitLoader.GetAbilityDefsForSkill(progression.PilotingSkills, abilityLevel2));
list.AddRange(CommanderPortraitLoader.GetAbilityDefsForSkill(progression.GutsSkills, abilityLevel3));
list.AddRange(CommanderPortraitLoader.GetAbilityDefsForSkill(progression.TacticsSkills, abilityLevel4));
return list;
}

public static List<string> GetAbilityDefsForSkill(string[][] abilityDefConsts, int abilityLevel) {
List<string> list = new List<string>();
for (int i = 0; i < abilityLevel; i++) {
list.AddRange(abilityDefConsts[i]);
public static void CreateJsons() {
try {
string filePath = "mods/CommanderPortraitLoader/Portraits/";
DirectoryInfo d1 = new DirectoryInfo(filePath);
FileInfo[] f1 = d1.GetFiles("*.png");
foreach (FileInfo info in f1) {
if (!File.Exists(info.FullName.Replace(".png", ".json"))) {
CustomPreset preset = new CustomPreset();
preset.isCommander = true;
preset.Description = new CustomDescription();
preset.Description.Id = info.Name.Replace(".png", "");
preset.Description.Icon = info.Name.Replace(".png", "");
preset.Description.Name = info.Name.Replace(".png", "");
preset.Description.Details = "";
JObject o = (JObject)JToken.FromObject(preset);
using (StreamWriter writer = new StreamWriter(filePath + info.Name.Replace(".png", ".json"), false)) {
writer.WriteLine(o);
}
}
}
}
catch (Exception e) {
Logger.LogError(e);
}
return list;
}

public static void CreateJsons() {
private static void AddOrUpdateJSONToManifest() {
try {
string filePath = "mods/CommanderPortraitLoader/Portraits/";
VersionManifest manifest = VersionManifestUtilities.ManifestFromCSV("mods/CommanderPortraitLoader/VersionManifest.csv");
DirectoryInfo d1 = new DirectoryInfo(filePath);
FileInfo[] f1 = d1.GetFiles("*.png");
FileInfo[] f1 = d1.GetFiles("*.json");
CustomPreset preset = new CustomPreset();
foreach (FileInfo info in f1) {
CustomPreset preset = new CustomPreset();
preset.isCommander = true;
preset.Description = new CustomDescription();
preset.Description.Id = info.Name.Replace(".png", "");
preset.Description.Icon = info.Name.Replace(".png", "");
preset.Description.Name = info.Name.Replace(".png", "");
preset.Description.Details = "";
JObject o = (JObject)JToken.FromObject(preset);
using (StreamWriter writer = new StreamWriter(filePath + info.Name.Replace(".png", ".json"), true)) {
writer.WriteLine(o);
using (StreamReader r = new StreamReader(info.FullName)) {
string json = r.ReadToEnd();
preset = JsonConvert.DeserializeObject<CustomPreset>(json);
}
manifest.AddOrUpdate(preset.Description.Id, info.FullName, "PortraitSettings", DateTime.Now, null, false);
}
} catch(Exception e) {
VersionManifestUtilities.ManifestToCSV(manifest, "mods/CommanderPortraitLoader/VersionManifest.csv");
}
catch (Exception e) {
Logger.LogError(e);
}
}
Expand Down Expand Up @@ -232,36 +96,4 @@ public static void CreateJsons() {
public float RecoveryChance;
}*/

public class CustomPreset
{
public CustomDescription Description;
public bool isCommander;

}

public class CustomDescription {
public string Id;
public string Name;
public string Details;
public string Icon;

}
public class Logger {
public static void LogError(Exception ex) {
string filePath = "mods/CommanderPortraitLoader/Log.txt";
using (StreamWriter writer = new StreamWriter(filePath, true)) {
writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
"" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}

public static void LogLine(String line) {
string filePath = "mods/CommanderPortraitLoader/Log.txt";
using (StreamWriter writer = new StreamWriter(filePath, true)) {
writer.WriteLine(line + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}
}
}
}
8 changes: 8 additions & 0 deletions ClassLibrary2/CommanderPortraitLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\BATTLETECH\BattleTech_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="CsvHelper, Version=7.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823, processorArchitecture=MSIL">
<HintPath>..\packages\CsvHelper.7.1.0\lib\net45\CsvHelper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand All @@ -54,6 +58,10 @@
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</ItemGroup>
<ItemGroup>
<Compile Include="HolderClasses.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Patch.cs" />
<Compile Include="Logger.cs" />
<Compile Include="CommanderPortraitLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
61 changes: 61 additions & 0 deletions ClassLibrary2/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Harmony;
using BattleTech;
using System.Reflection;
using System.IO;
using BattleTech.Portraits;
using UnityEngine;
using UnityEngine.Rendering;
using BattleTech.UI;
using HBS;
using HBS.Collections;
using BattleTech.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace CommanderPortraitLoader {
public class Helper{
// Token: 0x0600564D RID: 22093 RVA: 0x0023F158 File Offset: 0x0023D358
public static Sprite DownsampleSprite(Sprite oldSprite) {
Texture2D texture = oldSprite.texture;
RenderTexture temporary = RenderTexture.GetTemporary(texture.width / 2, texture.height / 2, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
RenderTexture temporary2 = RenderTexture.GetTemporary(texture.width / 4, texture.height / 4, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
Graphics.Blit(texture, temporary);
Graphics.Blit(temporary, temporary2);
Texture2D texture2D = new Texture2D(texture.width / 4, texture.height / 4, TextureFormat.ARGB32, false);
RenderTexture.active = temporary2;
texture2D.ReadPixels(new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), 0, 0);
texture2D.Apply(false, true);
Texture2D texture2D2 = texture2D;
texture2D2.name = texture2D2.name + texture.name + " Thumb";
RenderTexture.ReleaseTemporary(temporary);
RenderTexture.ReleaseTemporary(temporary2);
return Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f), 100f, 0u, SpriteMeshType.FullRect, Vector4.zero);
}

public static List<string> GetAbilities(int gunnery, int piloting, int guts, int tactics) {
List<string> list = new List<string>();
SimPilotProgressionConstantsDef progression = LazySingletonBehavior<UnityGameInstance>.Instance.Game.Simulation.Constants.Progression;
int abilityLevel = gunnery;
int abilityLevel2 = piloting;
int abilityLevel3 = guts;
int abilityLevel4 = tactics;
list.AddRange(GetAbilityDefsForSkill(progression.GunnerySkills, abilityLevel));
list.AddRange(GetAbilityDefsForSkill(progression.PilotingSkills, abilityLevel2));
list.AddRange(GetAbilityDefsForSkill(progression.GutsSkills, abilityLevel3));
list.AddRange(GetAbilityDefsForSkill(progression.TacticsSkills, abilityLevel4));
return list;
}

public static List<string> GetAbilityDefsForSkill(string[][] abilityDefConsts, int abilityLevel) {
List<string> list = new List<string>();
for (int i = 0; i < abilityLevel; i++) {
list.AddRange(abilityDefConsts[i]);
}
return list;
}
}
}
33 changes: 33 additions & 0 deletions ClassLibrary2/HolderClasses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Harmony;
using BattleTech;
using System.Reflection;
using System.IO;
using BattleTech.Portraits;
using UnityEngine;
using UnityEngine.Rendering;
using BattleTech.UI;
using HBS;
using HBS.Collections;
using BattleTech.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace CommanderPortraitLoader {
public class CustomPreset {
public CustomDescription Description = new CustomDescription();
public bool isCommander;

}

public class CustomDescription {
public string Id;
public string Name;
public string Details;
public string Icon;

}
}
Loading

0 comments on commit 7195bca

Please sign in to comment.