Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
c0re100 committed Aug 2, 2024
1 parent 9401706 commit 75aa79e
Show file tree
Hide file tree
Showing 11 changed files with 955 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/bin/
/obj/
/*.DotSettings.user
/*.otf
92 changes: 92 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Text.Json;
using Il2CppSystem.IO;
using Il2CppSystem.Text;

namespace IMYSHook;

public class IMYSConfig
{
public static double Speed;
public static int FPS;
public static bool TranslationEnabled;

public static void Read()
{
if (File.Exists("./BepInEx/plugins/config.json"))
{
var content = File.InternalReadAllText("./BepInEx/plugins/config.json", Encoding.UTF8);
var doc = JsonDocument.Parse(content);
var config = doc.RootElement;

var needWrite = false;

if (config.TryGetProperty("speed", out var sValue))
{
Speed = sValue.GetDouble();
}
else
{
Speed = 0.5;
needWrite = true;
}

if (config.TryGetProperty("fps", out var fValue))
{
FPS = fValue.GetInt32();
}
else
{
FPS = 60;
needWrite = true;
}

if (config.TryGetProperty("translation", out var tValue))
{
TranslationEnabled = tValue.GetBoolean();
}
else
{
TranslationEnabled = true;
needWrite = true;
}

if (needWrite) WriteJsonFile(Speed, FPS, TranslationEnabled);

Plugin.Global.Log.LogInfo("Current setting:");
Plugin.Global.Log.LogInfo("Game speed(each step): " + Speed);
Plugin.Global.Log.LogInfo("FPS: " + FPS);
Plugin.Global.Log.LogInfo("Translation: " + (TranslationEnabled ? "Enabled" : "Disabled"));
}
else
{
Plugin.Global.Log.LogWarning("config.json not found!!!");
Plugin.Global.Log.LogWarning("Using default config.");
Speed = 0.5;
FPS = 60;
TranslationEnabled = true;

// Create default JSON file
WriteJsonFile(0.5, 60, true);
}
}

public static void WriteJsonFile(double speed, int fps, bool enabled)
{
var config = new config
{
speed = speed,
fps = fps,
translation = enabled
};

var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText("./BepInEx/plugins/config.json", json);
}

public class config
{
public double speed { get; set; }
public int fps { get; set; }
public bool translation { get; set; }
}
}
377 changes: 377 additions & 0 deletions IMYSHook.csproj

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions IMYSHook.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IMYSHook", "IMYSHook.csproj", "{ECCA29DF-52F0-47C8-B3EF-2E9072DC696B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ECCA29DF-52F0-47C8-B3EF-2E9072DC696B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECCA29DF-52F0-47C8-B3EF-2E9072DC696B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECCA29DF-52F0-47C8-B3EF-2E9072DC696B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECCA29DF-52F0-47C8-B3EF-2E9072DC696B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FFF19899-6EE7-4266-9123-87E30E6015B7}
EndGlobalSection
EndGlobal
37 changes: 37 additions & 0 deletions Notification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Diagnostics;

namespace IMYSHook;

public class Notification
{
public static void Popup(string title, string text)
{
var script = string.Format("$headlineText = '{0}';", title) +
string.Format("$bodyText = '{0}';", text) +
"$ToastText02 = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText02;" +
"$TemplateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText02);" +
"$TemplateContent.SelectSingleNode('//text[@id=\"1\"]').InnerText = $headlineText;" +
"$TemplateContent.SelectSingleNode('//text[@id=\"2\"]').InnerText = $bodyText;" +
"$AppId = 'Iris Mysteria';" +
"[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($TemplateContent);";

var start = new ProcessStartInfo("powershell.exe")
{
UseShellExecute = false,
Arguments = script
};
Process.Start(start);
}

public static void SsPopup(string location)
{
var scriptArgs = "-F ./BepInEx/plugins/SS_Notification.ps1 " + location + "";

var start = new ProcessStartInfo("powershell.exe")
{
UseShellExecute = false,
Arguments = scriptArgs
};
Process.Start(start);
}
}
140 changes: 140 additions & 0 deletions Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Text.RegularExpressions;
using BepInEx;
using Hachiroku.Novel;
using Hachiroku.Novel.UI;
using HarmonyLib;
using Il2CppSystem.IO;
using TMPro;
using UnityEngine;

namespace IMYSHook;

public class Patch
{
private static string currentAdvId;
public static string fontName = "notosanscjktc";
public static TMP_FontAsset TMPTranslateFont;

public static void Initialize()
{
Harmony.CreateAndPatchAll(typeof(Patch));
}

[HarmonyPostfix]
[HarmonyPatch(typeof(NovelRoot), "Start")]
public static void NovelStart(ref NovelRoot __instance)
{
if (!IMYSConfig.TranslationEnabled) return;

if (TMPTranslateFont == null && File.Exists($"{Paths.PluginPath}/font/{fontName}"))
{
var ab = AssetBundle.LoadFromFile($"{Paths.PluginPath}/font/{fontName}");
TMPTranslateFont = ab.LoadAsset<TMP_FontAsset>(fontName + " SDF");
ab.Unload(false);
}

currentAdvId = __instance.Linker.ScenarioId;

if (!Translation.chapterDicts.ContainsKey(currentAdvId)) Translation.FetchChapterTranslation(currentAdvId);
Plugin.Global.Log.LogInfo(currentAdvId);
}

// Message
[HarmonyPrefix]
[HarmonyPatch(typeof(BurikoParseScript), "_SetMssageCommand")]
public static void Novel_SetMssageCommand(ref int lineNum, ref string line, ref bool isSelectedCaseArea,
ref int caseCount)
{
if (!IMYSConfig.TranslationEnabled) return;

if (Translation.chapterDicts.ContainsKey(currentAdvId) && line.Contains("「"))
{
var idx = line.IndexOf('「');
var name = line.Substring(0, idx);
var text = line.Substring(idx);

var full = "";

string name_replace;
if (Translation.nameDicts.TryGetValue(name, out name_replace))
full = name_replace.IsNullOrWhiteSpace() ? text : name_replace;

string text_replace;
if (Translation.chapterDicts[currentAdvId].TryGetValue(text, out text_replace))
full += text_replace.IsNullOrWhiteSpace() ? text : text_replace;

line = full;
}
else
{
string text_replace;
if (Translation.chapterDicts.ContainsKey(currentAdvId) &&
Translation.chapterDicts[currentAdvId].TryGetValue(line, out text_replace))
line = text_replace.IsNullOrWhiteSpace() ? line : text_replace;
}
}

// Option
[HarmonyPrefix]
[HarmonyPatch(typeof(BurikoParseScript), "_ToParamList")]
public static void Novel_ToParamList(ref string param)
{
if (!IMYSConfig.TranslationEnabled) return;

var re = new Regex(@"{(.*)}");
var match = re.Match(param);
if (match.Success)
for (var i = 0; i < match.Groups.Count; i++)
{
var options = match.Groups[i].Value.Split(",");

for (var i2 = 0; i2 < options.Length; i2++)
{
string text_replace;
if (Translation.chapterDicts.ContainsKey(currentAdvId) && Translation.chapterDicts[currentAdvId]
.TryGetValue(options[i2], out text_replace))
{
var option_tr = text_replace.IsNullOrWhiteSpace() ? options[i2] : text_replace;
param = param.Replace(options[i2], option_tr);
}
}
}
}

[HarmonyPrefix]
[HarmonyPatch(typeof(MessageScrollView), "CreateItem")]
public static void CreateItem(ref MessageScrollViewItem item)
{
if (!IMYSConfig.TranslationEnabled) return;

if (TMPTranslateFont != null)
{
item._name.font = TMPTranslateFont;
item._message.font = TMPTranslateFont;
}
}

[HarmonyPostfix]
[HarmonyPatch(typeof(ChoicesContent), "SetChoiceButtonText")]
public static void SetChoiceButtonText(ref ChoicesContent __instance)
{
if (!IMYSConfig.TranslationEnabled) return;

if (TMPTranslateFont != null)
for (var i = 0; i < __instance.choiceTextList.Length; i++)
__instance.choiceTextList[i].font = TMPTranslateFont;
}

[HarmonyPostfix]
[HarmonyPatch(typeof(TextRoot), "DeleteRuby")]
public static void DeleteRuby(ref TextRoot __instance)
{
if (!IMYSConfig.TranslationEnabled) return;

if (TMPTranslateFont != null)
{
if (__instance.CharaName) __instance.CharaName.font = TMPTranslateFont;
if (__instance.Message) __instance.Message.font = TMPTranslateFont;
}
}
}
60 changes: 60 additions & 0 deletions Plugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Il2CppSystem.IO;

namespace IMYSHook;

[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class Plugin : BasePlugin
{
public override void Load()
{
Console.OutputEncoding = Encoding.UTF8;

if (File.Exists("IMYSProxy.exe"))
{
if (Process.GetProcessesByName("IMYSProxy").Length == 0)
{
var start = new ProcessStartInfo("IMYSProxy.exe")
{
UseShellExecute = true,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Normal
};
Process.Start(start);
}

Environment.SetEnvironmentVariable("http_proxy", "http://127.0.0.1:8765",
EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("https_proxy", "http://127.0.0.1:8765",
EnvironmentVariableTarget.Process);
}
else
{
Environment.SetEnvironmentVariable("http_proxy", "http://127.0.0.1:5678",
EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("https_proxy", "http://127.0.0.1:5678",
EnvironmentVariableTarget.Process);
}


var log = Log;
Global.Log = log;
Log.LogInfo($"Plugin {MyPluginInfo.PLUGIN_GUID} is loaded!");

IMYSConfig.Read();
Translation.Init();
Patch.Initialize();

AddComponent<PluginBehavior>();
}

public class Global
{
public static ManualLogSource Log { get; set; }
}
}
Loading

0 comments on commit 75aa79e

Please sign in to comment.