LOREForceLoader.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using CustomStoryLogs;
using HarmonyLib;
using Newtonsoft.Json.Linq;
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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
namespace LOREForceLoad;

[BepInPlugin("Draglorr.LOEREForceLoad", "LORE Force Loader", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class LOREForceLoadPlugin : BaseUnityPlugin
{
	public const string PLUGIN_GUID = "Draglorr.LOEREForceLoad";

	public const string PLUGIN_NAME = "LORE Force Loader";

	public const string PLUGIN_VERSION = "1.0.0";

	internal static ManualLogSource Logger;

	private void Awake()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		Logger = ((BaseUnityPlugin)this).Logger;
		Logger.LogInfo((object)"LORE Force Loader v1.0.0 starting");
		try
		{
			ForceLoadLore();
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Exception while force-loading story logs: " + ex));
		}
		try
		{
			new Harmony("Draglorr.LOEREForceLoad").PatchAll(Assembly.GetExecutingAssembly());
			Logger.LogInfo((object)"Spawn safety net patched");
		}
		catch (Exception ex2)
		{
			Logger.LogError((object)("Failed to apply spawn patches: " + ex2));
		}
	}

	private void ForceLoadLore()
	{
		string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		if (string.IsNullOrEmpty(directoryName) || !Directory.Exists(directoryName))
		{
			Logger.LogWarning((object)"Could not resolve LORE plugin folder; nothing force-loaded");
			return;
		}
		List<string> list = new List<string>();
		try
		{
			list.AddRange(Directory.GetFiles(directoryName, "*.txt", SearchOption.AllDirectories));
			list.AddRange(Directory.GetFiles(directoryName, "*.json", SearchOption.AllDirectories));
		}
		catch (Exception ex)
		{
			Logger.LogError((object)("Failed to enumerate LORE folder: " + ex));
			return;
		}
		Logger.LogInfo((object)$"Scanning LORE folder ({directoryName}) found {list.Count} candidate file(s)");
		foreach (string item in list)
		{
			TryLoadFile(item);
		}
	}

	private void TryLoadFile(string path)
	{
		string text;
		try
		{
			text = File.ReadAllText(path);
		}
		catch (Exception ex)
		{
			Logger.LogWarning((object)("Skipping unreadable file " + path + ": " + ex.Message));
			return;
		}
		JObject val;
		try
		{
			val = JObject.Parse(text);
		}
		catch
		{
			return;
		}
		string text2 = (string)val["username"];
		string text3 = (string)val["modname"];
		string text4 = (string)val["version"];
		if (string.IsNullOrEmpty(text2) || string.IsNullOrEmpty(text3))
		{
			return;
		}
		JToken obj2 = val["logs"];
		JObject val2 = (JObject)(object)((obj2 is JObject) ? obj2 : null);
		int num = ((val2 != null) ? ((JContainer)val2).Count : 0);
		if (num == 0)
		{
			Logger.LogInfo((object)("File " + path + " parsed but contains no logs; skipping"));
			return;
		}
		string text5 = text2 + "-" + text3 + "-" + text4;
		if (AlreadyRegistered(text5))
		{
			Logger.LogInfo((object)("Logs for [" + text5 + "] are already registered; skipping " + path));
			return;
		}
		Logger.LogInfo((object)$"Force-registering {num} log(s) from [{text5}] at {path}");
		try
		{
			JsonLogReader.LoadLogsFromJsonFile(path);
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to load {path}: {arg}");
			return;
		}
		if (AlreadyRegistered(text5))
		{
			Logger.LogInfo((object)("Confirmed: [" + text5 + "] now registered"));
		}
		else
		{
			Logger.LogWarning((object)("File " + path + " did not produce any registered logs"));
		}
	}

	private static bool AlreadyRegistered(string modGUID)
	{
		Dictionary<int, CustomLogData> registeredLogs = CustomStoryLogs.RegisteredLogs;
		if (registeredLogs == null)
		{
			return false;
		}
		foreach (CustomLogData value in registeredLogs.Values)
		{
			if (value != null && value.ModGUID == modGUID)
			{
				return true;
			}
		}
		return false;
	}
}
[HarmonyPatch]
internal static class SpawnSafetyPatch
{
	[HarmonyPatch(typeof(CustomStoryLogs), "SpawnLogsLocally")]
	[HarmonyPostfix]
	private static void SpawnMissingLogsPostfix(string planetName)
	{
		try
		{
			EnsureSpawned(planetName);
		}
		catch (Exception arg)
		{
			LOREForceLoadPlugin.Logger.LogError((object)$"Spawn safety net error for {planetName}: {arg}");
		}
	}

	private static void EnsureSpawned(string planetName)
	{
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: Unknown result type (might be due to invalid IL or missing references)
		//IL_0177: Unknown result type (might be due to invalid IL or missing references)
		Dictionary<string, List<int>> planetCollectables = CustomStoryLogs.PlanetCollectables;
		Dictionary<int, LogCollectableData> collectables = CustomStoryLogs.Collectables;
		if (planetCollectables == null || collectables == null || string.IsNullOrEmpty(planetName) || !planetCollectables.TryGetValue(planetName, out var value) || value == null || value.Count == 0)
		{
			return;
		}
		int num = 0;
		foreach (int item in value)
		{
			if (!collectables.TryGetValue(item, out var value2) || value2 == null)
			{
				continue;
			}
			string text = "CustomStoryLog." + value2.LogID;
			if ((Object)(object)GameObject.Find(text) != (Object)null)
			{
				continue;
			}
			if ((Object)(object)CustomStoryLogs.CustomLogObj == (Object)null)
			{
				LOREForceLoadPlugin.Logger.LogWarning((object)"Custom log prefab missing; cannot force-spawn");
				return;
			}
			LOREForceLoadPlugin.Logger.LogInfo((object)$"Force-spawning missing log {value2.LogID} on {planetName}");
			GameObject val = Object.Instantiate<GameObject>(CustomStoryLogs.CustomLogObj);
			if (value2.ModelID > 0 && value2.ModelID <= CustomStoryLogs.CustomModels.Count)
			{
				GameObject val2 = Object.Instantiate<GameObject>(CustomStoryLogs.CustomModels[value2.ModelID - 1], val.transform);
				MeshRenderer component = val.GetComponent<MeshRenderer>();
				if ((Object)(object)component != (Object)null)
				{
					((Renderer)component).enabled = false;
				}
				BoxCollider component2 = val.GetComponent<BoxCollider>();
				BoxCollider component3 = val2.GetComponent<BoxCollider>();
				if ((Object)(object)component2 != (Object)null && (Object)(object)component3 != (Object)null)
				{
					((Collider)component3).enabled = false;
					component2.size = component3.size;
					component2.center = component3.center;
				}
			}
			CustomLogInteract component4 = val.GetComponent<CustomLogInteract>();
			if ((Object)(object)component4 != (Object)null)
			{
				component4.storyLogID = value2.LogID;
				component4.data = value2;
			}
			((Object)val).name = text;
			val.transform.position = value2.Position;
			val.transform.rotation = Quaternion.Euler(value2.Rotation);
			num++;
		}
		if (num > 0)
		{
			LOREForceLoadPlugin.Logger.LogInfo((object)$"Force-spawned {num} missing log(s) on {planetName}");
		}
	}
}