Decompiled source of FrankysMissionLoader v1.0.0

FrankysMissionLoader.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("frankysmissionloader (frankysmissionloader)")]
[assembly: AssemblyProduct("frankysmissionloader")]
[assembly: AssemblyVersion("1.0.0.0")]
[HarmonyPatch(typeof(QuickGameSettingsScreen), "InitializeGameModeButtons")]
public static class QuickGameSettingsScrollPatch
{
	private static readonly FieldInfo GameModeToggleContainerField = AccessTools.Field(typeof(QuickGameSettingsScreen), "_gameModeToggleContainer");

	private static void Postfix(QuickGameSettingsScreen __instance)
	{
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Expected O, but got Unknown
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_016e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_020f: Unknown result type (might be due to invalid IL or missing references)
		object? value = GameModeToggleContainerField.GetValue(__instance);
		RectTransform val = (RectTransform)((value is RectTransform) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			Debug.LogError((object)"GameModeToggleContainer was not found.");
			return;
		}
		if ((Object)(object)((Transform)val).parent != (Object)null && ((Object)((Transform)val).parent).name == "GameModeViewport")
		{
			LayoutRebuilder.ForceRebuildLayoutImmediate(val);
			return;
		}
		Transform parent = ((Transform)val).parent;
		int siblingIndex = ((Transform)val).GetSiblingIndex();
		Vector2 anchorMin = val.anchorMin;
		Vector2 anchorMax = val.anchorMax;
		Vector2 pivot = val.pivot;
		Vector2 anchoredPosition = val.anchoredPosition;
		Vector2 sizeDelta = val.sizeDelta;
		GameObject val2 = new GameObject("GameModeViewport");
		RectTransform val3 = val2.AddComponent<RectTransform>();
		val2.AddComponent<RectMask2D>();
		((Transform)val3).SetParent(parent, false);
		((Transform)val3).SetSiblingIndex(siblingIndex);
		val3.anchorMin = anchorMin;
		val3.anchorMax = anchorMax;
		val3.pivot = pivot;
		val3.anchoredPosition = anchoredPosition;
		val3.sizeDelta = sizeDelta;
		((Transform)val).SetParent((Transform)(object)val3, false);
		val.anchorMin = new Vector2(0f, 0f);
		val.anchorMax = new Vector2(0f, 1f);
		val.pivot = new Vector2(0f, 0.5f);
		val.anchoredPosition = Vector2.zero;
		val.sizeDelta = new Vector2(0f, 0f);
		((Transform)val).localScale = Vector3.one;
		if (((Object)__instance).name.Contains("Survival"))
		{
			val3.anchorMax = new Vector2(val3.anchorMax.x, 1.1f);
		}
		HorizontalLayoutGroup component = ((Component)val).GetComponent<HorizontalLayoutGroup>();
		if ((Object)(object)component != (Object)null)
		{
			((HorizontalOrVerticalLayoutGroup)component).spacing = 20f;
			((LayoutGroup)component).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)component).childControlWidth = true;
			((HorizontalOrVerticalLayoutGroup)component).childControlHeight = true;
			((HorizontalOrVerticalLayoutGroup)component).childForceExpandWidth = false;
			((HorizontalOrVerticalLayoutGroup)component).childForceExpandHeight = false;
		}
		for (int i = 0; i < ((Transform)val).childCount; i++)
		{
			((Transform)val).GetChild(i).localScale = Vector3.one;
		}
		ContentSizeFitter val4 = ((Component)val).GetComponent<ContentSizeFitter>();
		if ((Object)(object)val4 == (Object)null)
		{
			val4 = ((Component)val).gameObject.AddComponent<ContentSizeFitter>();
		}
		val4.horizontalFit = (FitMode)2;
		val4.verticalFit = (FitMode)0;
		ScrollRect val5 = val2.AddComponent<ScrollRect>();
		val5.content = val;
		val5.viewport = val3;
		val5.horizontal = true;
		val5.vertical = false;
		val5.movementType = (MovementType)2;
		val5.inertia = true;
		val5.decelerationRate = 0.135f;
		val5.scrollSensitivity = 40f;
		Canvas.ForceUpdateCanvases();
		LayoutRebuilder.ForceRebuildLayoutImmediate(val);
		LayoutRebuilder.ForceRebuildLayoutImmediate(val3);
		Debug.Log((object)("Created game mode horizontal scroll view with " + ((Transform)val).childCount + " missions."));
	}
}
namespace BepInExPlugin;

[BepInPlugin("frankysmissionloader", "frankysmissionloader", "1.0.0")]
public class FrankysMissionLoader : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("FrankysMissionLoader");
		val.PatchAll();
		Debug.Log((object)"FrankysMissionLoader loaded");
	}
}
[HarmonyPatch(typeof(QuickGameSettingsScreen), "Start")]
public static class MissionLoader
{
	private static bool missionsLoaded;

	private static readonly FieldInfo QuickTemplatesField = AccessTools.Field(typeof(GameManager), "_quickGameModeTemplates");

	private static readonly FieldInfo SurvivalTemplatesField = AccessTools.Field(typeof(GameManager), "_survivalTemplates");

	private static readonly List<AssetBundle> LoadedBundles = new List<AssetBundle>();

	private static void Prefix()
	{
		if (!missionsLoaded)
		{
			missionsLoaded = true;
			LoadMissionBundles();
		}
	}

	private static void LoadMissionBundles()
	{
		Debug.Log((object)"========== Mission Loader ==========");
		if (!Directory.Exists(Paths.PluginPath))
		{
			Debug.LogError((object)("Plugins folder does not exist: " + Paths.PluginPath));
			return;
		}
		string[] files = Directory.GetFiles(Paths.PluginPath, "*.mission", SearchOption.AllDirectories);
		Debug.Log((object)("Scanning: " + Paths.PluginPath));
		Debug.Log((object)("Found " + files.Length + " .mission file(s)."));
		GameManager instance = SingletonBehaviour<GameManager>.Instance;
		if ((Object)(object)instance == (Object)null)
		{
			Debug.LogError((object)"GameManager instance was not found.");
			return;
		}
		List<QuickGameModeTemplate> list = new List<QuickGameModeTemplate>();
		List<QuickGameModeTemplate> list2 = new List<QuickGameModeTemplate>();
		string[] array = files;
		foreach (string text in array)
		{
			string fileName = Path.GetFileName(text);
			bool flag = fileName.Equals("quick.mission", StringComparison.OrdinalIgnoreCase);
			bool flag2 = fileName.Equals("survival.mission", StringComparison.OrdinalIgnoreCase);
			if (!flag && !flag2)
			{
				Debug.LogWarning((object)("Skipping unknown mission file: " + text));
				continue;
			}
			Debug.Log((object)"------------------------------------");
			Debug.Log((object)("Loading mission bundle: " + text));
			AssetBundle val = AssetBundle.LoadFromFile(text);
			if ((Object)(object)val == (Object)null)
			{
				Debug.LogError((object)("Failed to load AssetBundle: " + text));
				continue;
			}
			LoadedBundles.Add(val);
			string[] allAssetNames = val.GetAllAssetNames();
			Debug.Log((object)"Bundle loaded successfully.");
			Debug.Log((object)("Assets inside bundle: " + allAssetNames.Length));
			string[] array2 = allAssetNames;
			foreach (string text2 in array2)
			{
				Debug.Log((object)("Asset: " + text2));
			}
			QuickGameModeTemplate[] array3 = val.LoadAllAssets<QuickGameModeTemplate>();
			if (array3 == null || array3.Length == 0)
			{
				Debug.LogWarning((object)("No QuickGameModeTemplate assets found in: " + text));
				continue;
			}
			QuickGameModeTemplate[] array4 = array3;
			foreach (QuickGameModeTemplate val2 in array4)
			{
				if (!((Object)(object)val2 == (Object)null))
				{
					Debug.Log((object)("Loaded mission template: " + ((Object)val2).name));
					if (flag2)
					{
						list2.Add(val2);
						Debug.Log((object)"Added to survival mission list.");
					}
					else
					{
						list.Add(val2);
						Debug.Log((object)"Added to quick mission list.");
					}
				}
			}
		}
		AppendTemplates(instance, QuickTemplatesField, list, "quick");
		AppendTemplates(instance, SurvivalTemplatesField, list2, "survival");
		Debug.Log((object)"Mission loader finished.");
		Debug.Log((object)("Custom quick missions loaded: " + list.Count));
		Debug.Log((object)("Custom survival missions loaded: " + list2.Count));
		Debug.Log((object)"====================================");
	}

	private static void AppendTemplates(GameManager gameManager, FieldInfo field, List<QuickGameModeTemplate> customTemplates, string category)
	{
		if ((object)field == null)
		{
			Debug.LogError((object)("Could not find the GameManager " + category + " template field."));
			return;
		}
		if (customTemplates.Count == 0)
		{
			Debug.Log((object)("No custom " + category + " missions to add."));
			return;
		}
		QuickGameModeTemplate[] array = field.GetValue(gameManager) as QuickGameModeTemplate[];
		if (array == null)
		{
			array = (QuickGameModeTemplate[])(object)new QuickGameModeTemplate[0];
		}
		QuickGameModeTemplate[] array2 = (QuickGameModeTemplate[])(object)new QuickGameModeTemplate[array.Length + customTemplates.Count];
		Array.Copy(array, array2, array.Length);
		for (int i = 0; i < customTemplates.Count; i++)
		{
			array2[array.Length + i] = customTemplates[i];
		}
		field.SetValue(gameManager, array2);
		Debug.Log((object)("Expanded " + category + " missions from " + array.Length + " to " + array2.Length + "."));
	}
}
internal static class PluginInfo
{
	public const string PLUGIN_NAME = "frankysmissionloader";

	public const string PLUGIN_ID = "frankysmissionloader";

	public const string PLUGIN_VERSION = "1.0.0";
}