using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using Newtonsoft.Json.Linq;
using R2API;
using R2API.Utils;
using RoR2;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FixJPTranslations")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FixJPTranslations")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d9e39f98-5a1f-466e-8058-73ab4ea683f6")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace FixJPTranslations;
[BepInPlugin("com.example.fixjp", "Fix JP Translations", "1.2.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
public class FixJpPlugin : BaseUnityPlugin
{
internal static ManualLogSource Logger;
private string JsonPath => Path.Combine(Paths.ConfigPath, "FixJPTranslations.ja-JP.json");
public void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
EnsureJsonExists();
Language.onCurrentLanguageChanged += Apply;
RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(Apply));
Run.onRunStartGlobal += delegate
{
Apply();
};
Apply();
}
private void OnDestroy()
{
Language.onCurrentLanguageChanged -= Apply;
RoR2Application.onLoad = (Action)Delegate.Remove(RoR2Application.onLoad, new Action(Apply));
Run.onRunStartGlobal -= delegate
{
Apply();
};
}
private void Apply()
{
try
{
if (!File.Exists(JsonPath))
{
Logger.LogWarning((object)("JSON が見つかりません: " + JsonPath));
return;
}
string json = File.ReadAllText(JsonPath, Encoding.UTF8);
Dictionary<string, string> dictionary = LoadTokenMap(json);
if (dictionary.Count == 0)
{
Logger.LogInfo((object)"上書き対象が 0 件です(strings が空の可能性)。");
return;
}
LanguageAPI.AddOverlay(dictionary, "ja");
Logger.LogInfo((object)$"FixJPTranslations: overlay 適用 {dictionary.Count} 件 (current={Language.currentLanguage?.name})");
}
catch (Exception ex)
{
Logger.LogWarning((object)("Apply 失敗: " + ex.Message));
}
}
private static Dictionary<string, string> LoadTokenMap(string json)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
try
{
JObject val = JObject.Parse(json);
JToken val2 = default(JToken);
if (val.TryGetValue("strings", ref val2))
{
JObject val3 = (JObject)(object)((val2 is JObject) ? val2 : null);
if (val3 != null)
{
foreach (JProperty item in val3.Properties())
{
string name = item.Name;
string value = ((object)item.Value)?.ToString() ?? "";
if (!string.IsNullOrWhiteSpace(name))
{
dictionary[name] = value;
}
}
}
}
}
catch (Exception ex)
{
Logger.LogWarning((object)("JSONパース失敗: " + ex.Message));
}
return dictionary;
}
private void EnsureJsonExists()
{
try
{
string directoryName = Path.GetDirectoryName(JsonPath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
if (!File.Exists(JsonPath))
{
File.WriteAllText(JsonPath, "{\r\n \"strings\": {\r\n \"SAMPLE\": \"サンプル: ここに翻訳を入れてください\"\r\n }\r\n}", Encoding.UTF8);
Logger.LogInfo((object)("JSONファイルを自動生成しました: " + JsonPath));
}
}
catch (Exception ex)
{
Logger.LogWarning((object)("JSON自動生成に失敗: " + ex.Message));
}
}
[ConCommand(/*Could not decode attribute arguments.*/)]
private static void CmdFixJpTest(ConCommandArgs args)
{
if (((ConCommandArgs)(ref args)).Count < 2)
{
Debug.Log((object)"usage: fixjp_test <TOKEN>");
return;
}
string text = ((ConCommandArgs)(ref args))[1];
string @string = Language.GetString(text);
Debug.Log((object)("TOKEN=" + text + " >> '" + @string + "' (lang=" + Language.currentLanguage?.name + ")"));
}
[ConCommand(/*Could not decode attribute arguments.*/)]
private static void CmdFixJpFind(ConCommandArgs args)
{
if (((ConCommandArgs)(ref args)).Count < 2)
{
Debug.Log((object)"usage: fixjp_find <needle>");
return;
}
string value = ((ConCommandArgs)(ref args))[1];
Language currentLanguage = Language.currentLanguage;
if (currentLanguage == null)
{
Debug.Log((object)"currentLanguage is null");
return;
}
int num = 0;
foreach (KeyValuePair<string, string> item in currentLanguage.stringsByToken)
{
string value2 = item.Value;
if (!string.IsNullOrEmpty(value2) && value2.Contains(value))
{
Debug.Log((object)("HIT: " + item.Key + " = " + value2));
if (++num >= 50)
{
break;
}
}
}
Debug.Log((object)$"search done. hits={num}, lang={currentLanguage.name}");
}
}