Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preset file system support #18

Merged
merged 11 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/KK_FolderBrowser/BrowserType.cs
Expand Up @@ -5,6 +5,7 @@ public enum BrowserType
NewGame,
FreeH,
Classroom,
HOutfit,
Maker,
Scene,
StudioChara,
Expand Down
13 changes: 11 additions & 2 deletions src/KK_FolderBrowser/KK_BrowserFolders.cs
Expand Up @@ -32,13 +32,14 @@ public class KK_BrowserFolders : BaseUnityPlugin
private IFolderBrowser _studioCharaFolders;
private IFolderBrowser _makerOutfitFolders;
private IFolderBrowser _studioOutfitFolders;
private IFolderBrowser _hOutfitFolders;


public static ConfigEntry<bool> EnableMaker { get; private set; }

public static ConfigEntry<bool> EnableMakerOutfit { get; private set; }
public static ConfigEntry<bool> EnableClassroom { get; private set; }
public static ConfigEntry<bool> EnableFreeH { get; private set; }
public static ConfigEntry<bool> EnableHOutfit { get; private set; }

public static ConfigEntry<bool> EnableStudio { get; private set; }
public static ConfigEntry<bool> EnableStudioChara { get; private set; }
Expand All @@ -63,6 +64,7 @@ internal void OnGUI()
_freeHFolders?.OnGui();
_newGameFolders?.OnGui();
_makerOutfitFolders?.OnGui();
_hOutfitFolders?.OnGui();
}
}

Expand All @@ -81,6 +83,7 @@ private void Awake()
var newGame = browsers.FirstOrDefault(x => x.Key == BrowserType.NewGame).Value;
var freeH = browsers.FirstOrDefault(x => x.Key == BrowserType.FreeH).Value;
var scene = browsers.FirstOrDefault(x => x.Key == BrowserType.Scene).Value;
var hOutfit = browsers.FirstOrDefault(x => x.Key == BrowserType.HOutfit).Value;
var studioChara = browsers.FirstOrDefault(x => x.Key == BrowserType.StudioChara).Value;
var studioOutfit = browsers.FirstOrDefault(x => x.Key == BrowserType.StudioOutfit).Value;

Expand All @@ -98,7 +101,10 @@ private void Awake()

if (freeH != null)
EnableFreeH = Config.Bind("Main game", "Enable folder browser in Free H browser", true, "Changes take effect on game restart");


if (hOutfit != null)
EnableHOutfit = Config.Bind("Main game", "Enable folder browser in H preset browser", true, "Changes take effect on game restart.\n Kplug doesn't support this and will restore previous outfit when not main or out of H.");

if (scene != null || studioChara != null || studioOutfit != null)
{
EnableStudio = Config.Bind("Chara Studio", "Enable folder browser in scene browser", true, "Changes take effect on game restart");
Expand Down Expand Up @@ -129,6 +135,9 @@ private void Awake()
if (makerOutfit != null && EnableMakerOutfit.Value)
_makerOutfitFolders = (IFolderBrowser)Activator.CreateInstance(makerOutfit);

if (hOutfit != null && EnableHOutfit.Value)
_hOutfitFolders = (IFolderBrowser)Activator.CreateInstance(hOutfit);

if (EnableClassroom != null && EnableClassroom.Value)
{
if (classroom != null) _classroomFolders = (IFolderBrowser)Activator.CreateInstance(classroom);
Expand Down
130 changes: 130 additions & 0 deletions src/KK_Hooks_KK/HOutfitFolders.cs
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using ChaCustom;
using HarmonyLib;
using KKAPI.Utilities;
using UnityEngine;
using UnityEngine.UI;

namespace BrowserFolders.Hooks.KK
{
[BrowserType(BrowserType.HOutfit)]
public class HOutfitFolders : IFolderBrowser
{
private static clothesFileControl _customCoordinateFile;
private static FolderTreeView _folderTreeView;

private static string _currentRelativeFolder;
private static bool _hToggle;//doesn't initialize to true at start or at least in "public HOutfitFolders()" as true as it crashes the game on startup

public HOutfitFolders()
{
_folderTreeView = new FolderTreeView(Utils.NormalizePath(UserData.Path), Utils.NormalizePath(UserData.Path));
_folderTreeView.CurrentFolderChanged = OnFolderChanged;

Harmony.CreateAndPatchAll(typeof(HOutfitFolders));
}

[HarmonyPrefix]
[HarmonyPatch(typeof(clothesFileControl), "Start")]
internal static void InitHook(clothesFileControl __instance)
{
_folderTreeView.DefaultPath = Path.Combine((UserData.Path), "coordinate/");
_folderTreeView.CurrentFolder = _folderTreeView.DefaultPath;

_customCoordinateFile = __instance;

_hToggle = true; //weirdly enough required so file system would open the first time you use the preset per encounter
GameObject.Find("Canvas/SubMenu/DressCategory/ClothChange").GetComponent<Button>().onClick.AddListener(EnablePreset);
GameObject.Find("Canvas/clothesFileWindow/Window/WinRect/Load/btnCancel").GetComponent<Button>().onClick.AddListener(DisablePreset);
GameObject.Find("Canvas/clothesFileWindow/Window/BasePanel/MenuTitle/btnClose").GetComponent<Button>().onClick.AddListener(DisablePreset);
}

[HarmonyTranspiler]
[HarmonyPatch(typeof(clothesFileControl), "Initialize")]
internal static IEnumerable<CodeInstruction> InitializePatch(IEnumerable<CodeInstruction> instructions)
{
foreach (var instruction in instructions)
{
if (string.Equals(instruction.operand as string, "coordinate/", StringComparison.OrdinalIgnoreCase))
{
//0x7E ldsfld <field> Push the value of the static field on the stack.
instruction.opcode = OpCodes.Ldsfld;
instruction.operand = typeof(HOutfitFolders).GetField(nameof(_currentRelativeFolder), BindingFlags.NonPublic | BindingFlags.Static);
}

yield return instruction;
}
}

public void OnGui()
{
var guiShown = false;
if (_hToggle) //if preset window is active draw file select
{
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.F1))//if right click or F1 close
{
GameObject.Find("Canvas/clothesFileWindow").SetActive(false);
DisablePreset();
}
var screenRect = new Rect((int)(Screen.width * 0.04), (int)(Screen.height * 0.57f), (int)(Screen.width * 0.125), (int)(Screen.height * 0.35));
IMGUIUtils.DrawSolidBox(screenRect);
GUILayout.Window(362, screenRect, TreeWindow, "Select outfit folder");
IMGUIUtils.EatInputInRect(screenRect);
guiShown = true;
}
if (!guiShown) _folderTreeView?.StopMonitoringFiles();
}

private static void OnFolderChanged()
{
_currentRelativeFolder = _folderTreeView.CurrentRelativeFolder;

if (_customCoordinateFile == null) return; //if failed not initializing in "start"

Traverse.Create(_customCoordinateFile).Method("Initialize").GetValue();
}

private static void TreeWindow(int id)
{
GUILayout.BeginVertical();
{
_folderTreeView.DrawDirectoryTree();

GUILayout.BeginVertical(GUI.skin.box, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
{
if (GUILayout.Button("Refresh thumbnails"))
{
_folderTreeView.ResetTreeCache();
OnFolderChanged();
}

GUILayout.Space(1);

GUILayout.Label("Open in explorer...");
if (GUILayout.Button("Current folder"))
Utils.OpenDirInExplorer(_folderTreeView.CurrentFolder);
if (GUILayout.Button("Screenshot folder"))
Utils.OpenDirInExplorer(Path.Combine(Utils.NormalizePath(UserData.Path), "cap"));
if (GUILayout.Button("Main game folder"))
Utils.OpenDirInExplorer(Path.GetDirectoryName(Utils.NormalizePath(UserData.Path)));
}
GUILayout.EndVertical();
}
GUILayout.EndVertical();
}

private static void EnablePreset()//listen to preset button
{
_hToggle = true;
}

private static void DisablePreset()//exit if either close button is clicked or right click
{
_hToggle = false;
}
}
}
1 change: 1 addition & 0 deletions src/KK_Hooks_KK/KK_BrowserFolders_Hooks_KK.csproj
Expand Up @@ -66,6 +66,7 @@
<ItemGroup>
<Compile Include="ClassroomFolders.cs" />
<Compile Include="FreeHFolders.cs" />
<Compile Include="HOutfitFolders.cs" />
<Compile Include="MakerOutfitFolders.cs" />
<Compile Include="MakerFolders.cs" />
<Compile Include="NewGameFolders.cs" />
Expand Down
108 changes: 108 additions & 0 deletions src/KK_Hooks_KKP/HOutfitFolders.cs
@@ -0,0 +1,108 @@
using System.IO;
using ChaCustom;
using HarmonyLib;
using KKAPI.Utilities;
using UnityEngine;
using UnityEngine.UI;

namespace BrowserFolders.Hooks.KKP
{
[BrowserType(BrowserType.HOutfit)]
public class HOutfitFolders : IFolderBrowser
{
private static clothesFileControl _customCoordinateFile;
private static FolderTreeView _folderTreeView;

public static string CurrentRelativeFolder => _folderTreeView?.CurrentRelativeFolder;
private static bool _hToggle;//doesn't initialize to true here and at least in "public HOutfitFolders()" as true as it crashes the game on startup

public HOutfitFolders()
{
_folderTreeView = new FolderTreeView(Utils.NormalizePath(UserData.Path), Utils.NormalizePath(UserData.Path));
_folderTreeView.CurrentFolderChanged = OnFolderChanged;

Harmony.CreateAndPatchAll(typeof(HOutfitFolders));
}

[HarmonyPrefix]
[HarmonyPatch(typeof(clothesFileControl), "Start")]
public static void InitHook(clothesFileControl __instance)
{
_folderTreeView.DefaultPath = Path.Combine((UserData.Path), "coordinate/");
_folderTreeView.CurrentFolder = _folderTreeView.DefaultPath;

_customCoordinateFile = __instance;

_hToggle = true; //weirdly enough required so file system would open the first time you use the preset per encounter
GameObject.Find("Canvas/SubMenu/DressCategory/ClothChange").GetComponent<Button>().onClick.AddListener(EnablePreset);//guessing cause listener is added after first click and doesnt execute but the other two seem fine
GameObject.Find("Canvas/clothesFileWindow/Window/WinRect/Load/btnCancel").GetComponent<Button>().onClick.AddListener(DisablePreset);//maybe cause there are two disables
GameObject.Find("Canvas/clothesFileWindow/Window/BasePanel/MenuTitle/btnClose").GetComponent<Button>().onClick.AddListener(DisablePreset);
}

public void OnGui()
{
bool guiShown = false;
if (_hToggle)
{
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.F1))//if right click click or F1 close
{
GameObject.Find("Canvas/clothesFileWindow").SetActive(false);
DisablePreset();
}
var screenRect = new Rect((int)(Screen.width * 0.004), (int)(Screen.height * 0.57f), (int)(Screen.width * 0.125), (int)(Screen.height * 0.35));
IMGUIUtils.DrawSolidBox(screenRect);
GUILayout.Window(36, screenRect, TreeWindow, "Select outfit folder");
IMGUIUtils.EatInputInRect(screenRect);
guiShown = true;
}
if (!guiShown) _folderTreeView?.StopMonitoringFiles();
}

private static void OnFolderChanged()
{
if (_customCoordinateFile == null) return;
Traverse.Create(_customCoordinateFile).Method("Initialize").GetValue();
}

private static void TreeWindow(int id)
{
GUILayout.BeginVertical();
{
_folderTreeView.DrawDirectoryTree();

GUILayout.BeginVertical(GUI.skin.box, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(false));
{
if (Overlord.DrawDefaultCardsToggle())
OnFolderChanged();

if (GUILayout.Button("Refresh thumbnails"))
{
_folderTreeView.ResetTreeCache();
OnFolderChanged();
}

GUILayout.Space(1);

GUILayout.Label("Open in explorer...");
if (GUILayout.Button("Current folder"))
Utils.OpenDirInExplorer(_folderTreeView.CurrentFolder);
if (GUILayout.Button("Screenshot folder"))
Utils.OpenDirInExplorer(Path.Combine(Utils.NormalizePath(UserData.Path), "cap"));
if (GUILayout.Button("Main game folder"))
Utils.OpenDirInExplorer(Path.GetDirectoryName(Utils.NormalizePath(UserData.Path)));
}
GUILayout.EndVertical();
}
GUILayout.EndVertical();
}

private static void EnablePreset()//listen to preset button
{
_hToggle = true;
}
private static void DisablePreset()//exit if either close button is clicked or right click
{
_hToggle = false;
}
}
}
1 change: 1 addition & 0 deletions src/KK_Hooks_KKP/KK_BrowserFolders_Hooks_KKP.csproj
Expand Up @@ -65,6 +65,7 @@
<ItemGroup>
<Compile Include="ClassroomFolders.cs" />
<Compile Include="FreeHFolders.cs" />
<Compile Include="HOutfitFolders.cs" />
<Compile Include="MakerOutfitFolder.cs" />
<Compile Include="MakerFolders.cs" />
<Compile Include="NewGameFolders.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/KK_Hooks_KKP/Overlord.cs
Expand Up @@ -86,6 +86,13 @@ internal static void FilenameHook(ref string path, ref bool useDefaultData)
if (!string.IsNullOrEmpty(overridePath))
path = overridePath;
}
var HOutfit = Object.FindObjectOfType<clothesFileControl>();
if (HOutfit != null)
{
var overridePath = HOutfitFolders.CurrentRelativeFolder;
if (!string.IsNullOrEmpty(overridePath))
path = overridePath;
}
}
}

Expand Down