using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using DiceKingdoms.Interface;
using DiceKingdoms.Meta;
using DiceModders.ModCore;
using DiceModders.ModCore.Tools;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Steamworks;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("DiceModders")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Ukrainian language for Dice Kingdoms: a menu entry and the missing texts.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DiceModders.Ukrainian")]
[assembly: AssemblyTitle("DiceModders.Ukrainian")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace DiceModders.Ukrainian
{
internal static class LanguageMenu
{
internal const int Ukrainian = 6;
internal static string Label = "Українська";
private static IntPtr LanguageClass => Il2CppClassPointerStore<Language>.NativeClassPtr;
internal static void Install(Patcher patcher)
{
patcher.Patch(typeof(Dropdown), "SetEntries", typeof(LanguageMenu), (string)null, "EntriesPostfix");
MethodInfo methodInfo = typeof(GameSettings).GetNestedTypes(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).SelectMany((Type t) => t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)).FirstOrDefault((MethodInfo m) => m.Name.Contains("b__2_0"));
if (methodInfo != null)
{
patcher.Patch(methodInfo.DeclaringType, methodInfo.Name, typeof(LanguageMenu), "HandlerPrefix", (string)null);
}
else
{
Plugin.L.LogWarning((object)"language change handler not found: the menu entry would do nothing");
}
patcher.Patch(typeof(SteamLocaleSelector), "GetStartupLocale", typeof(LanguageMenu), (string)null, "StartupPostfix");
}
private static void EntriesPostfix(Dropdown __instance)
{
//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
//IL_00dc: Expected O, but got Unknown
try
{
Il2CppReferenceArray<Object> val = ((__instance != null) ? __instance.values : null);
if (val == null || ((Il2CppArrayBase<Object>)(object)val).Length == 0)
{
return;
}
bool flag = false;
for (int i = 0; i < ((Il2CppArrayBase<Object>)(object)val).Length; i++)
{
Object val2 = ((Il2CppArrayBase<Object>)(object)val)[i];
if (val2 != null && !(IL2CPP.il2cpp_object_get_class(((Il2CppObjectBase)val2).Pointer) != LanguageClass))
{
flag = true;
if (Unbox(val2) == 6)
{
return;
}
}
}
if (flag)
{
Il2CppReferenceArray<Object> val3 = new Il2CppReferenceArray<Object>((long)(((Il2CppArrayBase<Object>)(object)val).Length + 1));
for (int j = 0; j < ((Il2CppArrayBase<Object>)(object)val).Length; j++)
{
((Il2CppArrayBase<Object>)(object)val3)[j] = ((Il2CppArrayBase<Object>)(object)val)[j];
}
((Il2CppArrayBase<Object>)(object)val3)[((Il2CppArrayBase<Object>)(object)val).Length] = Box(6);
__instance.values = val3;
TMP_Dropdown dropdown = __instance.dropdown;
dropdown.options.Add(new OptionData(Label));
if (SavedIsUkrainian())
{
dropdown.SetValueWithoutNotify(((Il2CppArrayBase<Object>)(object)val).Length);
}
dropdown.RefreshShownValue();
}
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("language menu: " + ex.Message));
}
}
private static bool HandlerPrefix(int value)
{
if (value != 6)
{
return true;
}
Select();
return false;
}
private static void StartupPostfix(ref Locale __result)
{
try
{
bool flag = SteamClient.IsValid && SteamApps.GameLanguage == "ukrainian";
if (SavedIsUkrainian() || flag)
{
Locale val = Find();
if ((Object)(object)val != (Object)null)
{
__result = val;
}
}
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("startup language: " + ex.Message));
}
}
internal static void ApplySaved()
{
try
{
if (SavedIsUkrainian())
{
Select();
}
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("saved language: " + ex.Message));
}
}
private static bool SavedIsUkrainian()
{
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Invalid comparison between Unknown and I4
SettingSets instance = SettingSets.Instance;
object obj;
if (instance == null)
{
obj = null;
}
else
{
GameSettings gameSettings = instance.gameSettings;
obj = ((gameSettings != null) ? gameSettings.language : null);
}
Setting<Language> val = (Setting<Language>)obj;
if (val != null)
{
return (int)val.Value == 6;
}
return false;
}
private static void Select()
{
Locale val = Find();
if ((Object)(object)val == (Object)null)
{
Plugin.L.LogWarning((object)"the game has no Ukrainian locale");
return;
}
LocalizationSettings.SelectedLocale = val;
Plugin.L.LogInfo((object)"language: Ukrainian");
}
private static Locale Find()
{
ILocalesProvider availableLocales = LocalizationSettings.AvailableLocales;
List<Locale> val = ((availableLocales != null) ? availableLocales.Locales : null);
if (val == null)
{
return null;
}
Enumerator<Locale> enumerator = val.GetEnumerator();
while (enumerator.MoveNext())
{
Locale current = enumerator.Current;
if ((Object)(object)current != (Object)null && current.Identifier.Code == "uk")
{
return current;
}
}
return null;
}
private unsafe static Object Box(int value)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0018: Expected O, but got Unknown
return new Object(IL2CPP.il2cpp_value_box(LanguageClass, (IntPtr)(&value)));
}
private unsafe static int Unbox(Object value)
{
return *(int*)(void*)IL2CPP.il2cpp_object_unbox(((Il2CppObjectBase)value).Pointer);
}
}
[BepInPlugin("org.dicemodders.dicekingdoms.ukrainian", "DiceKingdoms Ukrainian", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInProcess("Dice-Kingdoms.exe")]
public sealed class Plugin : BasePlugin
{
public const string Guid = "org.dicemodders.dicekingdoms.ukrainian";
public const string Name = "DiceKingdoms Ukrainian";
public const string Version = "1.0.0";
internal static ManualLogSource L;
public override void Load()
{
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_0040: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Expected O, but got Unknown
L = ((BasePlugin)this).Log;
TextSet obj = Texts.Load("texts.json", (string)null);
LanguageMenu.Label = obj.Get("menu.label", "en");
UkTable.Install(obj);
Patcher val = new Patcher("org.dicemodders.dicekingdoms.ukrainian", ((BasePlugin)this).Log);
LanguageMenu.Install(val);
val.Summary();
Civilizations.WhenGameDataLoaded((Action)LanguageMenu.ApplySaved);
}
}
internal static class UkTable
{
private static TextSet _texts;
private static IntPtr _filled;
internal static void Install(TextSet texts)
{
_texts = texts;
Texts.LanguageChanged += Fill;
}
private static void Fill()
{
if (_texts == null || Texts.Language != "uk")
{
return;
}
try
{
StructuredTextStaticReferences instance = StructuredTextStaticReferences.instance;
if ((Object)(object)instance == (Object)null)
{
Plugin.L.LogWarning((object)"table: the game's text references are not there yet");
return;
}
StringTable table = ((LocalizedDatabase<StringTable, StringTableEntry>)(object)LocalizationSettings.StringDatabase).GetTable(instance.localizationStringTable, LocalizationSettings.SelectedLocale);
if ((Object)(object)table == (Object)null)
{
Plugin.L.LogWarning((object)"table: the Ukrainian text table could not be read");
}
else
{
if (((Il2CppObjectBase)table).Pointer == _filled)
{
return;
}
int num = 0;
int num2 = 0;
string text = default(string);
string text2 = default(string);
foreach (string key in _texts.Keys)
{
if (key.StartsWith("gamekey:", StringComparison.Ordinal) && _texts.TryGetOwn(key, "uk", ref text, ref text2) && !string.IsNullOrEmpty(text))
{
StringTableEntry entry = ((DetailedLocalizationTable<StringTableEntry>)(object)table).GetEntry(key.Substring("gamekey:".Length));
if (entry == null)
{
num2++;
}
else if (entry.Value != text)
{
entry.Value = text;
num++;
}
}
}
_filled = ((Il2CppObjectBase)table).Pointer;
Plugin.L.LogInfo((object)("table: " + num + " Ukrainian texts put into the game's table" + ((num2 > 0) ? (", " + num2 + " keys the table does not have") : "")));
}
}
catch (Exception ex)
{
Plugin.L.LogWarning((object)("table: " + ex.Message));
}
}
}
}