Skip to content

Commit

Permalink
Added a user interface so I can finally release it
Browse files Browse the repository at this point in the history
  • Loading branch information
DeathWeasel1337 committed Jan 4, 2019
1 parent f34bb48 commit cb118c8
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions KK_UncensorSelector/KK_UncensorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
using Harmony;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;
using Logger = BepInEx.Logger;
/// <summary>
/// Proof of concept plugin for assigning uncensors to characters individually
/// Plugin for assigning uncensors to characters individually
/// </summary>
namespace KK_UncensorSelector
{
[BepInPlugin("com.deathweasel.bepinex.uncensorselector", "Uncensor Selector", Version)]
public class KK_UncensorSelector : BaseUnityPlugin
{
public const string Version = "0.2";
public const string Version = "1.0";
private static string CharacterName = "";
private static readonly string UncensorSelectorFilePath = Path.Combine(Paths.PluginPath, "KK_UncensorSelector.csv");
private static Dictionary<string, string> UncensorList = new Dictionary<string, string>();

void Main()
{
Expand All @@ -28,6 +29,31 @@ void Main()
Type LoadAsyncIterator = typeof(ChaControl).GetNestedTypes(AccessTools.all).Where(x => x.Name.StartsWith("<LoadAsync>c__Iterator")).First();
MethodInfo LoadAsyncIteratorMoveNext = LoadAsyncIterator.GetMethod("MoveNext");
harmony.Patch(LoadAsyncIteratorMoveNext, null, null, new HarmonyMethod(typeof(KK_UncensorSelector).GetMethod(nameof(LoadAsyncTranspiler), BindingFlags.Static | BindingFlags.Public)));

if (File.Exists(UncensorSelectorFilePath))
GenerateUncensorList();
}
/// <summary>
/// Generate the dictionary of CharacterName,UncensorName from KK_UncensorSelector.csv
/// </summary>
private static void GenerateUncensorList()
{
using (StreamReader reader = new StreamReader(UncensorSelectorFilePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
try
{
string[] parts = line.Split(',');
UncensorList.Add(parts[0], parts[1]);
}
catch
{
Logger.Log(LogLevel.Error, $"Error reading KK_UncensorSelector.csv line, skipping.");
}
}
}
}

[HarmonyPrefix]
Expand All @@ -42,25 +68,14 @@ public static IEnumerable<CodeInstruction> LoadAsyncTranspiler(IEnumerable<CodeI
List<CodeInstruction> instructionsList = instructions.ToList();
List<int> OOBaseIndex = new List<int>();

int IndexMaleBody = instructionsList.FindIndex(instruction => instruction.opcode == OpCodes.Ldstr && instruction.operand.ToString() == "p_cm_body_00");

for (int i = 0; i < instructionsList.Count; i++)
foreach (var x in instructionsList)
{
if (instructionsList[i].operand?.ToString() == "chara/oo_base.unity3d")
if (x.operand?.ToString() == "chara/oo_base.unity3d")
{
Logger.Log(LogLevel.Info, $"i:{i} opcode:[{instructionsList[i].opcode}] operand:[{instructionsList[i].operand?.ToString()}]");
OOBaseIndex.Add(i);
x.opcode = OpCodes.Call;
x.operand = typeof(KK_UncensorSelector).GetMethod(nameof(SetOOBase), AccessTools.all);
}
}
Logger.Log(LogLevel.Info, IndexMaleBody);

//p_cf_body_00, p_cm_body_00, and _low variants
instructionsList[OOBaseIndex[2]].opcode = OpCodes.Call;
instructionsList[OOBaseIndex[2]].operand = typeof(KK_UncensorSelector).GetMethod(nameof(SetOOBase), BindingFlags.NonPublic | BindingFlags.Static);

//p_cf_body_00_Nml
instructionsList[OOBaseIndex[3]].opcode = OpCodes.Call;
instructionsList[OOBaseIndex[3]].operand = typeof(KK_UncensorSelector).GetMethod(nameof(SetOOBase), BindingFlags.NonPublic | BindingFlags.Static);

return instructions;
}
Expand All @@ -73,11 +88,10 @@ public static IEnumerable<CodeInstruction> CreateBodyTextureTranspiler(IEnumerab
//cf_body_00_t, cf_body_00_mc, cm_body_00_mc, and _low variants
foreach (var x in instructionsList)
{
Logger.Log(LogLevel.Info, $"opcode:[{x.opcode}] operand:[{x.operand?.ToString()}]");
if (x.operand?.ToString() == "chara/oo_base.unity3d")
{
x.opcode = OpCodes.Call;
x.operand = typeof(KK_UncensorSelector).GetMethod(nameof(SetOOBase), BindingFlags.NonPublic | BindingFlags.Static);
x.operand = typeof(KK_UncensorSelector).GetMethod(nameof(SetOOBase), AccessTools.all);
}
}

Expand All @@ -86,11 +100,12 @@ public static IEnumerable<CodeInstruction> CreateBodyTextureTranspiler(IEnumerab

private static string SetOOBase()
{
Logger.Log(LogLevel.Info, $"SetOOBase {CharacterName}");
if (CharacterName == "Bessie")
return "chara/oo_base_KK_LO.unity3d";
if (UncensorList.TryGetValue(CharacterName, out string Uncensor) && AssetBundleCheck.IsFile(Uncensor))
return Uncensor;
else if (UncensorList.TryGetValue("*", out string DefaultUncensor) && AssetBundleCheck.IsFile(DefaultUncensor))
return DefaultUncensor;

return "chara/oo_base.unity3d";
}

}
}

0 comments on commit cb118c8

Please sign in to comment.