Decompiled source of burmaldinskylanguage v1.0.1

plugins/BurmaldynREPO.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Burmaldyn.Repo;

[BepInPlugin("perent.repo.burmaldyn", "Burmaldyn REPO", "1.0.1")]
public sealed class BurmaldynPlugin : BaseUnityPlugin
{
	private LocalizedStringDatabase database;

	private RussianTableProcessor processor;

	private void Awake()
	{
		try
		{
			Dictionary<string, Dictionary<string, string>> translations = RussianTableProcessor.LoadTranslations();
			database = LocalizationSettings.StringDatabase;
			processor = new RussianTableProcessor(translations, ((LocalizedDatabase<StringTable, StringTableEntry>)(object)database).TablePostprocessor, ((BaseUnityPlugin)this).Logger);
			((LocalizedDatabase<StringTable, StringTableEntry>)(object)database).TablePostprocessor = (ITablePostprocessor)(object)processor;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Burmaldyn is ready. Select Russian in the game settings.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Could not initialize Burmaldyn: " + ex));
		}
	}

	private void OnDestroy()
	{
		if (database != null && processor != null && object.ReferenceEquals(((LocalizedDatabase<StringTable, StringTableEntry>)(object)database).TablePostprocessor, processor))
		{
			((LocalizedDatabase<StringTable, StringTableEntry>)(object)database).TablePostprocessor = processor.Previous;
		}
	}
}
internal sealed class RussianTableProcessor : ITablePostprocessor
{
	private static readonly string[] TableNames = new string[5] { "Game", "HUD", "Menu", "Subtitles", "TTS" };

	private readonly IDictionary<string, Dictionary<string, string>> translations;

	private readonly ManualLogSource logger;

	internal ITablePostprocessor Previous { get; private set; }

	internal RussianTableProcessor(IDictionary<string, Dictionary<string, string>> translations, ITablePostprocessor previous, ManualLogSource logger)
	{
		this.translations = translations;
		this.logger = logger;
		Previous = previous;
	}

	public void PostprocessTable(LocalizationTable table)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		if (Previous != null)
		{
			Previous.PostprocessTable(table);
		}
		StringTable val = (StringTable)(object)((table is StringTable) ? table : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		LocaleIdentifier localeIdentifier = ((LocalizationTable)val).LocaleIdentifier;
		if (!string.Equals(((LocaleIdentifier)(ref localeIdentifier)).Code, "ru-RU", StringComparison.OrdinalIgnoreCase) || (Object)(object)((LocalizationTable)val).SharedData == (Object)null)
		{
			return;
		}
		string tableCollectionName = ((LocalizationTable)val).TableCollectionName;
		if (!translations.TryGetValue(tableCollectionName, out var value))
		{
			return;
		}
		int num = 0;
		foreach (KeyValuePair<string, string> item in value)
		{
			StringTableEntry entry = ((DetailedLocalizationTable<StringTableEntry>)(object)val).GetEntry(item.Key);
			if (entry != null && entry.Value != item.Value)
			{
				entry.Value = item.Value;
				num++;
			}
		}
		logger.LogDebug((object)("Applied " + num + " Burmaldyn strings to " + tableCollectionName + "."));
	}

	internal static Dictionary<string, Dictionary<string, string>> LoadTranslations()
	{
		Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>(StringComparer.Ordinal);
		Assembly assembly = typeof(RussianTableProcessor).Assembly;
		UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
		string[] tableNames = TableNames;
		foreach (string text in tableNames)
		{
			string text2 = "Burmaldyn.Repo." + text + ".tsv";
			using Stream stream = assembly.GetManifestResourceStream(text2);
			if (stream == null)
			{
				throw new InvalidDataException("Missing translation resource " + text2);
			}
			Dictionary<string, string> dictionary2 = new Dictionary<string, string>(StringComparer.Ordinal);
			using (StreamReader streamReader = new StreamReader(stream, encoding))
			{
				string text3;
				while ((text3 = streamReader.ReadLine()) != null)
				{
					int num = text3.IndexOf('\t');
					if (num <= 0 || text3.IndexOf('\t', num + 1) >= 0)
					{
						throw new InvalidDataException("Invalid translation row in " + text2);
					}
					dictionary2.Add(text3.Substring(0, num), text3.Substring(num + 1).Replace("\\n", "\n"));
				}
			}
			dictionary.Add(text, dictionary2);
		}
		return dictionary;
	}
}