using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using SimpleJson;
using UnityEngine;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ValheimJP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimJP")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d359d9fb-e6d8-48f3-8ecb-2508e72c0d48")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ValheimJP;
[BepInPlugin("Gedevan-Aleksizde.mods.ValheimJP", "Correct Localization JP for Valheim", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class TextCorrector : BaseUnityPlugin
{
private class CorrectFontPatch
{
private static CorrectFontPatch _instance;
public List<string> defaultFontNames = new List<string>();
public Dictionary<string, string[]> newFontNamesStr = new Dictionary<string, string[]>();
public Dictionary<(string, int), Font> FontAssets = new Dictionary<(string, int), Font>();
public static CorrectFontPatch Instance
{
get
{
if (_instance == null)
{
_instance = new CorrectFontPatch();
}
return _instance;
}
private set
{
_instance = value;
}
}
public CorrectFontPatch()
{
Logger.LogInfo((object)"Correct Font Patch Instance Created.");
foreach (KeyValuePair<string, ConfigEntry<string>> newFontName in newFontNames)
{
newFontNamesStr.Add(newFontName.Key, Regex.Split(newFontName.Value.Value, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))").Concat(new string[1] { fallbackFont.Value }).ToArray());
}
}
[HarmonyPatch(typeof(Text), "OnEnable")]
[HarmonyPostfix]
private static void CorrectCorrectCorrect(Text __instance)
{
ChangeFont(__instance);
}
private static void KeepOriginalFontOnRune(TextViewer __instance, bool __runOriginal, Style style, string topic, string text, bool autoHide)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
if ((int)style == 0)
{
string text2 = Localization.instance.Localize(text);
__runOriginal = false;
__instance.m_topic.text = topic;
__instance.m_text.text = text2;
__instance.m_runeText.text = text2;
__instance.m_animator.SetBool(TextViewer.m_visibleID, true);
__instance.m_autoHide = true;
__instance.m_openPlayerPos = ((Component)Player.m_localPlayer).transform.position;
__instance.m_showTime = 0f;
ZLog.Log((object)("Show text " + topic + ":" + text));
}
}
private static void ChangeFont(Text text)
{
if ((text.text.Contains("塞がっている") || text.text.Contains("blocked") || text.text.Contains("Cargo")) && debugMode.Value)
{
Logger.LogDebug((object)$"text={text.text}, go={((Object)((Component)text).gameObject).name}(root={((Component)text).gameObject.transform.root}), font={((Object)text.font).name}");
}
if (!((Object)(object)text.font != (Object)null))
{
return;
}
if (!Instance.newFontNamesStr.TryGetValue(((Object)text.font).name, out var value))
{
value = new string[1] { fallbackFont.Value };
}
if (howToReplace.Value != "Override")
{
value = new string[1] { text.font.fontNames[0] }.Concat(value).ToArray();
}
if (text.font.fontNames != value)
{
if (debugMode.Value)
{
Logger.LogDebug((object)("new fallbacks for '" + ((Object)text.font).name + "' = " + string.Join(", ", value)));
}
text.font.fontNames = value;
}
}
}
public const string PluginGUID = "Gedevan-Aleksizde.mods.ValheimJP";
public static readonly Dictionary<string, string> defaultFontCorrespondence = new Dictionary<string, string>
{
{ "Norse", "Yu Mincho" },
{ "Norsebold", "Yu Mincho Bold" },
{ "prstart", "Yu Mincho" },
{ "prstartk", "Yu Mincho" },
{ "rune", "Yu Mincho" },
{ "Arial", "Yu Mincho" },
{ "AveriaSerifLibre-Regular", "Yu Mincho" },
{ "AveriaSerifLibre-Light", "Yu Mincho" },
{ "AveriaSerifLibre-Bold", "Yu Mincho Bold" },
{ "AveriaSerifLibre-Italic", "Yu Mincho" },
{ "AveriaSerifLibre-BoldItalic", "Yu Mincho Bold" },
{ "AveriaSansLibre-Regular", "Yu Gothic" },
{ "AveriaSansLibre-Light", "Yu Gothic" },
{ "AveriaSansLibre-Bold", "Yu Gothic Bold" },
{ "AveriaSansLibre-Italic", "Yu Gothic" },
{ "AveriaSansLibre-BoldItalic", "Yu Gothic Bold" }
};
public static ConfigEntry<string> fallbackFont;
public static ConfigEntry<bool> correctFont;
public static ConfigEntry<bool> debugMode;
public static ConfigEntry<bool> textOverflow;
public static ConfigEntry<string> howToReplace;
public static Dictionary<string, ConfigEntry<string>> newFontNames = new Dictionary<string, ConfigEntry<string>>();
internal static ManualLogSource Logger;
private void Awake()
{
Logger = ((BaseUnityPlugin)this).Logger;
LoadCfg();
Harmony.CreateAndPatchAll(typeof(TextCorrector), (string)null);
if (correctFont.Value)
{
Harmony.CreateAndPatchAll(typeof(CorrectFontPatch), (string)null);
}
AddLocalizationText();
}
private void Start()
{
Localization.instance.m_endChars = Localization.instance.m_endChars.Concat(new char[10] { '、', '。', '〈', '〉', '「', '」', '『', '』', '《', '》' }).ToArray();
}
private void LoadCfg()
{
((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
correctFont = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "CorrectFont", true, "Enable to replace to the correct fonts");
debugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugMode", false, "Enable to output verbose log");
textOverflow = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "TextOverflow", false, "Enable to overflow all texts");
howToReplace = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HowToReplace", "Override", "'Override', or 'Fallback' ");
foreach (KeyValuePair<string, string> item in defaultFontCorrespondence)
{
ConfigEntry<string> value = ((BaseUnityPlugin)this).Config.Bind<string>("NewFonts", item.Key, item.Value, "font name which is replaced with " + item.Key);
newFontNames.Add(item.Key, value);
}
fallbackFont = ((BaseUnityPlugin)this).Config.Bind<string>("NewFonts", "FallbackFont", "Yu Mincho", "a fallback font name");
Logger.LogInfo((object)("fallbackfont = " + fallbackFont.Value));
Logger.LogInfo((object)$"correctFont = {correctFont.Value}");
Logger.LogInfo((object)$"textOverflow {textOverflow.Value}");
}
private void AddLocalizationText()
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Expected O, but got Unknown
CustomLocalization val = new CustomLocalization();
Dictionary<string, string> dictionary = SimpleJson.DeserializeObject<Dictionary<string, string>>(AssetUtils.LoadAssetBundle("ValheimJP/Assets/correcttext_japanese").LoadAsset<TextAsset>("CorrectText_Japanese").text);
string text = "Japanese";
val.AddTranslation(ref text, dictionary);
LocalizationManager.Instance.AddLocalization(val);
}
private static void TweakLocalizationSettings(Localization __instance)
{
__instance.m_endChars = __instance.m_endChars.Concat(new char[2] { '、', '。' }).ToArray();
if (debugMode.Value)
{
Logger.LogDebug((object)$"m_endChars={__instance.m_endChars}");
}
}
[HarmonyPatch(typeof(FejdStartup), "SetupGui")]
[HarmonyPostfix]
private static void HarmonyTest()
{
if (debugMode.Value)
{
Logger.LogDebug((object)"----- Harmony Test: FejdStartup.SetupGui called ----");
}
}
}