Decompiled source of CustomDictionaryLoader v1.1.0

CustomDictionaryLoader.dll

Decompiled 6 days 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 BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[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("CustomDictionaryLoader")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+c62aaef49be8de56197487c0ecf0b8c9da86fff2")]
[assembly: AssemblyProduct("CustomDictionaryLoader")]
[assembly: AssemblyTitle("CustomDictionaryLoader")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace CustomDictionaryLoader;

[BepInPlugin("WordPlay.CustomDictionaryLoader", "CustomDictionaryLoader", "1.1.0")]
public class CustomDictionaryLoaderMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(WordChecker), "LoadData")]
	private class WordChecker_LoadData_Patch
	{
		[HarmonyPostfix]
		public static void WordChecker_LoadData_Postfix(WordChecker __instance)
		{
			ModLogger.LogInfo((object)"Loading custom dictionaries...");
			CustomDictionaryPaths = (from dir in Directory.GetDirectories(Paths.PluginPath, "*", SearchOption.AllDirectories)
				where string.Equals(Path.GetFileName(dir), "CustomDictionary", StringComparison.OrdinalIgnoreCase)
				select dir).ToList().SelectMany((string d) => Directory.EnumerateFiles(d, "*.txt")).ToList();
			string text = Path.Combine(Application.persistentDataPath, "customdictionary.txt");
			if (File.Exists(text))
			{
				if (LoadAppDataDictionary)
				{
					CustomDictionaryPaths.Add(text);
					ModLogger.LogInfo((object)("A dictionary was loaded from: " + text));
				}
				else
				{
					ModLogger.LogInfo((object)("This dictionary will not be loaded based on config settings: " + text));
				}
			}
			CustomValidWords = (from line in CustomDictionaryPaths.SelectMany((string file) => File.ReadAllLines(file))
				where !string.IsNullOrWhiteSpace(line)
				select line.ToUpperInvariant()).ToList();
			if (ManualConfigCustomWords.Count > 0)
			{
				CustomValidWords.AddRange(ManualConfigCustomWords);
				ModLogger.LogInfo((object)$"Added {ManualConfigCustomWords.Count} custom words from config.");
			}
			if (OverwriteVanilla)
			{
				ModLogger.LogInfo((object)"Config is set to overwrite the vanilla dictionary. Default valid words have been removed.");
				__instance.validWords = CustomValidWords.ToArray();
			}
			else
			{
				__instance.validWords = __instance.validWords.Concat(CustomValidWords).ToArray();
			}
			__instance.stringSet = new HashSet<string>(__instance.validWords);
			__instance.validWords = __instance.stringSet.ToArray();
			int count = CustomDictionaryPaths.Count;
			int count2 = CustomValidWords.Count;
			int num = __instance.validWords.Length;
			ModLogger.LogInfo((object)$"Successfully loaded {count} custom dictionaries containing {count2} custom words!");
			ModLogger.LogInfo((object)$"New total: {num} words");
		}
	}

	private const string modGUID = "WordPlay.CustomDictionaryLoader";

	private const string modName = "CustomDictionaryLoader";

	private const string modVersion = "1.1.0";

	private readonly Harmony harmony = new Harmony("WordPlay.CustomDictionaryLoader");

	internal static ManualLogSource ModLogger;

	public static CustomDictionaryLoaderMod Instance;

	public static bool OverwriteVanilla = false;

	public static bool LoadAppDataDictionary = true;

	public static List<string> CustomDictionaryPaths;

	public static List<string> CustomValidWords;

	public static List<string> ManualConfigCustomWords;

	private void Awake()
	{
		if ((Object)(object)Instance == (Object)null)
		{
			Instance = this;
		}
		harmony.PatchAll();
		ModLogger = Logger.CreateLogSource("CustomDictionaryLoader");
		ModLogger.LogInfo((object)"Plugin CustomDictionaryLoader is loaded!");
		OverwriteVanilla = ((BaseUnityPlugin)this).Config.Bind<bool>("Dictionary", "Overwrite Vanilla Dictionary", false, "If true, custom dictionaries will overwrite the vanilla dictionary instead of adding words to it.").Value;
		LoadAppDataDictionary = ((BaseUnityPlugin)this).Config.Bind<bool>("Dictionary", "Load Dictionary from AppData folder", true, "If true, a customdictionary.txt file in Word Play's AppData folder will be loaded if it exists.").Value;
		ManualConfigCustomWords = (from word in ((BaseUnityPlugin)this).Config.Bind<string>("Word List", "Manual Custom Words", "", "Comma-separated list of words to add to the valid word list.").Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
			select word.Trim().ToUpperInvariant()).ToList();
	}
}