Decompiled source of Asset Name Packs v1.1.0

AssetNamePacks/AssetNamePacks.dll

Decompiled 8 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AssetNamePacks.Localization;
using AssetNamePacks.Patches;
using BepInEx;
using BepInEx.Logging;
using Colossal.IO.AssetDatabase;
using Colossal.Localization;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AssetNamePacks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c38b5c903a8561fe5196bd02b6d3229eef89374d")]
[assembly: AssemblyProduct("AssetNamePacks")]
[assembly: AssemblyTitle("AssetNamePacks")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 AssetNamePacks
{
	[BepInPlugin("AssetNamePacks", "AssetNamePacks", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"[AssetNamePacks]: Loading Harmony patches.");
			MethodBase[] array = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "AssetNamePacks_Cities2Harmony").GetPatchedMethods().ToArray();
			Logger.LogInfo((object)("Plugin AssetNamePacks made patches! Patched methods: " + array.Length));
			MethodBase[] array2 = array;
			foreach (MethodBase methodBase in array2)
			{
				Logger.LogInfo((object)("[AssetNamePacks]: Patched method: " + methodBase.Module.Name + ":" + methodBase.Name));
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "AssetNamePacks";

		public const string PLUGIN_NAME = "AssetNamePacks";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace AssetNamePacks.Patches
{
	[HarmonyPatch(typeof(LocalizationManager), "AddLocale", new Type[] { typeof(LocaleAsset) })]
	internal class LocalizationManager_AddLocale
	{
		private static readonly LocalizationJS localizationJS = new LocalizationJS();

		public static readonly string AssetPackPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "packs");

		private static void Prefix(LocaleAsset asset)
		{
			AssetNamePacks.Localization.Localization.AddCustomLocal(asset);
			if (localizationJS == null || localizationJS.Localization == null)
			{
				return;
			}
			foreach (string key in asset.data.entries.Keys)
			{
				if (!localizationJS.Localization.ContainsKey(asset.localeId))
				{
					localizationJS.Localization.Add(asset.localeId, new Dictionary<string, string>());
				}
				if (!localizationJS.Localization[asset.localeId].ContainsKey(key))
				{
					localizationJS.Localization[asset.localeId].Add(key, asset.data.entries[key]);
				}
			}
			Dictionary<string, Dictionary<string, int>> dictionary = new Dictionary<string, Dictionary<string, int>>();
			foreach (string key2 in asset.data.indexCounts.Keys)
			{
				if (!dictionary.ContainsKey(asset.localeId))
				{
					dictionary.Add(asset.localeId, new Dictionary<string, int>());
				}
				if (!dictionary[asset.localeId].ContainsKey(key2))
				{
					dictionary[asset.localeId].Add(key2, asset.data.indexCounts[key2]);
				}
			}
		}
	}
}
namespace AssetNamePacks.Localization
{
	public class Localization
	{
		internal static Dictionary<string, Dictionary<string, string>> localization;

		internal static void AddCustomLocal(LocaleAsset localeAsset)
		{
			if (localization == null)
			{
				LoadLocalization();
			}
			if (localization == null)
			{
				return;
			}
			string localeId = localeAsset.localeId;
			if (!localization.ContainsKey(localeId))
			{
				return;
			}
			foreach (string key in localization[localeId].Keys)
			{
				if (localeAsset.data.entries.ContainsKey(key))
				{
					localeAsset.data.entries[key] = localization[localeId][key];
				}
				else
				{
					localeAsset.data.entries.Add(key, localization[localeId][key]);
				}
				if (!key.Contains(":"))
				{
					continue;
				}
				string[] array = key.Split(":");
				if (!int.TryParse(array[1], out var result))
				{
					continue;
				}
				result++;
				if (localeAsset.data.indexCounts.ContainsKey(array[0]))
				{
					if (localeAsset.data.indexCounts[array[0]] != result)
					{
						localeAsset.data.indexCounts[array[0]] = result;
					}
				}
				else
				{
					localeAsset.data.indexCounts.Add(array[0], result);
				}
			}
		}

		private static void CreateDefaultFile()
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(new DirectoryInfo(LocalizationManager_AddLocale.AssetPackPath).FullName, "Default"));
			if (!directoryInfo.Exists)
			{
				Plugin.Logger.LogInfo((object)("[Asset Name Packs] Creating default pack directory at " + directoryInfo.FullName));
				directoryInfo.Create();
			}
			using StreamWriter streamWriter = File.CreateText(Path.Combine(directoryInfo.FullName, "STREET_NAME.en-US.txt.example"));
			streamWriter.WriteLine("Main Street");
			streamWriter.WriteLine("Home Street");
			streamWriter.WriteLine("School Street");
		}

		private static void LoadLocalization()
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(LocalizationManager_AddLocale.AssetPackPath);
			if (!directoryInfo.Exists)
			{
				Plugin.Logger.LogWarning((object)"[Asset Name Packs] Packs directory not found");
				CreateDefaultFile();
				return;
			}
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			DirectoryInfo[] directories = directoryInfo.GetDirectories();
			foreach (DirectoryInfo directoryInfo2 in directories)
			{
				dictionary.Add(directoryInfo2.Name, directoryInfo2.FullName);
			}
			Plugin.Logger.LogInfo((object)("[Asset Name Packs] Found " + dictionary.Count + " packs"));
			if (dictionary.ContainsKey("Default"))
			{
				LoadPack(dictionary["Default"]);
				return;
			}
			Plugin.Logger.LogWarning((object)"[Asset Name Packs] Default pack not found");
			localization = new Dictionary<string, Dictionary<string, string>>();
		}

		private static void LoadPack(string path)
		{
			Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
			new DirectoryInfo(path);
			FileInfo[] files = new DirectoryInfo(path).GetFiles("*.txt");
			foreach (FileInfo fileInfo in files)
			{
				using StreamReader streamReader = new StreamReader(fileInfo.FullName);
				string[] array = fileInfo.Name.Split(".");
				string text = array[0];
				string key = array[1];
				Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
				string text2 = streamReader.ReadLine();
				int num = 0;
				while (text2 != null)
				{
					dictionary2.Add("Assets." + text.ToUpper() + ":" + num, text2);
					num++;
					text2 = streamReader.ReadLine();
				}
				dictionary.Add(key, dictionary2);
			}
			localization = dictionary;
		}
	}
	[Serializable]
	public class LocalizationLocaleJS
	{
		public Dictionary<string, string> Localization;
	}
	public class LocalizationJS
	{
		public Dictionary<string, Dictionary<string, string>> Localization = new Dictionary<string, Dictionary<string, string>>();
	}
}