Decompiled source of RecipeManager v1.0.0

DSP_RecipeManager.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("starfi5h.plugin.RecipeManager")]
[assembly: AssemblyProduct("RecipeManager")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace RecipeManager;

[Serializable]
public class DictWrapper
{
	public List<int> recipeId;

	public List<int> gridIndex;

	public void FromDict(Dictionary<int, int> dict)
	{
		recipeId = new List<int>();
		gridIndex = new List<int>();
		foreach (KeyValuePair<int, int> item in dict)
		{
			recipeId.Add(item.Key);
			gridIndex.Add(item.Value);
		}
	}

	public Dictionary<int, int> ToDict()
	{
		Dictionary<int, int> dictionary = new Dictionary<int, int>();
		if (recipeId == null || gridIndex == null)
		{
			return dictionary;
		}
		int num = Math.Min(recipeId.Count, gridIndex.Count);
		for (int i = 0; i < num; i++)
		{
			dictionary.Add(recipeId[i], gridIndex[i]);
		}
		return dictionary;
	}
}
[BepInPlugin("starfi5h.plugin.RecipeManager", "RecipeManager", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "starfi5h.plugin.RecipeManager";

	public const string NAME = "RecipeManager";

	public const string VERSION = "1.0.0";

	private static Harmony harmony;

	private static ManualLogSource Log;

	private static ConfigEntry<string> GridIndexDictionaryJSON;

	private static int selectedRecipeID = -1;

	private static int GridIndex = 0;

	private static Dictionary<int, int> GridIndexDictionary;

	private static Dictionary<int, int> GridIndexDictionaryOrigin;

	private static GameObject editModeTextObj;

	private static GameObject destRecipeSelImageObj;

	private static GameObject resetButtonObj;

	private static GameObject editButtonObj;

	private static GameObject selectedRecipeIcon;

	private static Button resetButton;

	private static Button editButton;

	private static Text resetText;

	private static Text editText;

	private static bool editMode = false;

	public static void WriteGridIndexDictionary()
	{
		DictWrapper dictWrapper = new DictWrapper();
		dictWrapper.FromDict(GridIndexDictionary);
		string value = JsonUtility.ToJson((object)dictWrapper);
		GridIndexDictionaryJSON.Value = value;
	}

	public static void ReadGridIndexDictionary()
	{
		GridIndexDictionary = ((DictWrapper)JsonUtility.FromJson(GridIndexDictionaryJSON.Value, typeof(DictWrapper))).ToDict();
		Log.LogDebug((object)("ReadGridIndexDictionary: " + GridIndexDictionary.Count));
	}

	public void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		harmony = new Harmony("starfi5h.plugin.RecipeManager");
		harmony.PatchAll(typeof(Plugin));
		GridIndexDictionaryJSON = ((BaseUnityPlugin)this).Config.Bind<string>("General", "GridIndexDictionary", "{}", "JSON format string of custom recipe grid index pair");
		ReadGridIndexDictionary();
	}

	private static void CreateUI()
	{
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Expected O, but got Unknown
		//IL_020e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Expected O, but got Unknown
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_0400: Unknown result type (might be due to invalid IL or missing references)
		GameObject obj = GameObject.Find("UI Root/Overlay Canvas/In Game/Windows/Station Window/storage-box-0/popup-box/sd-option-button-0");
		GameObject val = GameObject.Find("UI Root/Overlay Canvas/In Game/Windows/Replicator Window/recipe-group");
		resetButtonObj = Object.Instantiate<GameObject>(obj);
		editButtonObj = Object.Instantiate<GameObject>(obj);
		resetButtonObj.SetActive(true);
		editButtonObj.SetActive(true);
		((Object)resetButtonObj).name = "resetButtonObj";
		((Object)editButtonObj).name = "editButtonObj";
		resetButtonObj.transform.SetParent(val.transform, false);
		editButtonObj.transform.SetParent(val.transform, false);
		resetButtonObj.transform.localPosition = new Vector3(265f, 55f, 0f);
		editButtonObj.transform.localPosition = new Vector3(245f, 25f, 0f);
		RectTransform component = resetButtonObj.GetComponent<RectTransform>();
		RectTransform component2 = editButtonObj.GetComponent<RectTransform>();
		component.sizeDelta = new Vector2(80f, 25f);
		component2.sizeDelta = new Vector2(100f, 25f);
		resetButton = resetButtonObj.GetComponent<Button>();
		editButton = editButtonObj.GetComponent<Button>();
		Image component3 = resetButtonObj.GetComponent<Image>();
		Image component4 = editButtonObj.GetComponent<Image>();
		((Graphic)component3).color = new Color(1f, 0.68f, 0.45f, 0.7f);
		((Graphic)component4).color = new Color(0.24f, 0.55f, 0.65f, 0.7f);
		resetText = resetButtonObj.GetComponentInChildren<Text>();
		editText = editButtonObj.GetComponentInChildren<Text>();
		resetText.text = Localization.Translate("Reset All");
		editText.text = Localization.Translate("Enter Edit Mode");
		((Object)resetText).name = "resetText";
		((Object)editText).name = "editText";
		((UnityEvent)resetButton.onClick).AddListener(new UnityAction(OnClickResetButton));
		((UnityEvent)editButton.onClick).AddListener(new UnityAction(OnClickEditButton));
		destRecipeSelImageObj = Object.Instantiate<GameObject>(GameObject.Find("UI Root/Overlay Canvas/In Game/Windows/Replicator Window/recipe-group/sel-img"));
		destRecipeSelImageObj.transform.SetParent(val.transform, false);
		destRecipeSelImageObj.SetActive(false);
		selectedRecipeIcon = Object.Instantiate<GameObject>(GameObject.Find("UI Root/Overlay Canvas/In Game/Windows/Replicator Window/recipe-tree/center-icon"));
		selectedRecipeIcon.transform.SetParent(val.transform, false);
		selectedRecipeIcon.transform.localPosition = new Vector3(150f, 36f, 0f);
		((Component)selectedRecipeIcon.transform.Find("place-text")).GetComponentInChildren<Text>().text = Localization.Translate("Selected Recipe");
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("vline-m")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("hline-0")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("hline-1")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("icon 2")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("text 1")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("text 2")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("time-text")).gameObject);
		Object.Destroy((Object)(object)((Component)selectedRecipeIcon.transform.Find("time-text")).gameObject);
		selectedRecipeIcon.SetActive(false);
		editModeTextObj = Object.Instantiate<GameObject>(GameObject.Find("UI Root/Overlay Canvas/In Game/Function Panel/bg-trans/mode-text"));
		editModeTextObj.transform.SetParent(val.transform, false);
		editModeTextObj.transform.localPosition = new Vector3(0f, -3f, 0f);
		Object.Destroy((Object)(object)editModeTextObj.GetComponent<Localizer>());
		editModeTextObj.GetComponent<Text>().text = Localization.Translate("Edit mode");
	}

	public static void OnClickResetButton()
	{
		Log.LogInfo((object)"Reset All");
		RecipeProto[] dataArray = ((ProtoSet<RecipeProto>)(object)LDB.recipes).dataArray;
		foreach (RecipeProto val in dataArray)
		{
			val.GridIndex = GridIndexDictionaryOrigin[((Proto)val).ID];
		}
		GridIndexDictionary.Clear();
		WriteGridIndexDictionary();
		UIRoot.instance.uiGame.replicator.RefreshRecipeIcons();
	}

	public static void OnClickEditButton()
	{
		if (!editMode)
		{
			editModeTextObj.SetActive(true);
			resetButtonObj.SetActive(true);
			editText.text = Localization.Translate("Exit Edit Mode");
			editMode = true;
			destRecipeSelImageObj.gameObject.SetActive(true);
			((Component)UIRoot.instance.uiGame.replicator.recipeSelImage).gameObject.SetActive(false);
			selectedRecipeIcon.SetActive(true);
			((Component)selectedRecipeIcon.transform.Find("icon 1")).gameObject.SetActive(false);
			destRecipeSelImageObj.gameObject.SetActive(false);
			UIRoot.instance.uiGame.replicator.selectedRecipe = null;
			UIRoot.instance.uiGame.replicator.treeTweener1.Play1To0Continuing();
			UIRoot.instance.uiGame.replicator.treeTweener2.Play1To0Continuing();
			((Component)UIRoot.instance.uiGame.replicator.instantItemSwitch).gameObject.SetActive(false);
			UIRoot.instance.uiGame.replicator.RefreshRecipeIcons();
		}
		else
		{
			editModeTextObj.SetActive(false);
			resetButtonObj.SetActive(false);
			editText.text = Localization.Translate("Enter Edit Mode");
			editMode = false;
			selectedRecipeID = -1;
			destRecipeSelImageObj.gameObject.SetActive(false);
			selectedRecipeIcon.SetActive(false);
			((Component)UIRoot.instance.uiGame.replicator.instantItemSwitch).gameObject.SetActive(GameMain.sandboxToolsEnabled);
			UIRoot.instance.uiGame.replicator.RefreshRecipeIcons();
			WriteGridIndexDictionary();
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIReplicatorWindow), "RefreshRecipeIcons")]
	private static void RefreshRecipeIcons_Postfix(UIReplicatorWindow __instance)
	{
		if (!editMode)
		{
			return;
		}
		Array.Clear(__instance.recipeIndexArray, 0, __instance.recipeIndexArray.Length);
		Array.Clear(__instance.recipeStateArray, 0, __instance.recipeStateArray.Length);
		Array.Clear(__instance.recipeProtoArray, 0, __instance.recipeProtoArray.Length);
		int num = __instance.queueNumTexts.Length;
		RecipeProto[] dataArray = ((ProtoSet<RecipeProto>)(object)LDB.recipes).dataArray;
		IconSet iconSet = GameMain.iconSet;
		HashSet<int> hashSet = new HashSet<int>();
		for (int i = 0; i < dataArray.Length; i++)
		{
			int num2 = dataArray[i].GridIndex / 1000;
			int num3 = (dataArray[i].GridIndex - num2 * 1000) / 100 - 1;
			int num4 = dataArray[i].GridIndex % 100 - 1;
			int num5 = num3 * num + num4;
			if (num2 == __instance.currentType && num5 < __instance.recipeIndexArray.Length)
			{
				int iD = ((Proto)dataArray[i]).ID;
				__instance.recipeIndexArray[num5] = iconSet.recipeIconIndex[iD];
				__instance.recipeProtoArray[num5] = dataArray[i];
				if (hashSet.Contains(dataArray[i].GridIndex))
				{
					__instance.recipeStateArray[num5] |= 8u;
				}
				else if (GridIndexDictionary.ContainsKey(iD))
				{
					__instance.recipeStateArray[num5] |= 4u;
				}
				hashSet.Add(dataArray[i].GridIndex);
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIReplicatorWindow), "TestMouseRecipeIndex")]
	private static bool TestMouseRecipeIndex_Prefix(UIReplicatorWindow __instance)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
		if (selectedRecipeID == -1 || !editMode)
		{
			return true;
		}
		Vector2 val = default(Vector2);
		if (__instance.mouseInRecipe && UIRoot.ScreenPointIntoRect(Input.mousePosition, ((Graphic)__instance.recipeBg).rectTransform, ref val))
		{
			int num = Mathf.FloorToInt(val.x / 46f);
			int num2 = Mathf.FloorToInt((0f - val.y) / 46f);
			int num3 = __instance.queueNumTexts.Length;
			if (num >= 0 && num2 >= 0 && num < num3 && num2 < 8)
			{
				__instance.mouseRecipeIndex = num + num2 * num3;
				destRecipeSelImageObj.gameObject.SetActive(true);
				destRecipeSelImageObj.GetComponent<RectTransform>().anchoredPosition = new Vector2((float)(num * 46 - 1), (float)(-num2 * 46 + 1));
			}
		}
		return false;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIReplicatorWindow), "SetSelectedRecipeIndex")]
	private static bool SetSelectedRecipeIndex_Prefix(UIReplicatorWindow __instance, int index)
	{
		//IL_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		GridIndex = 0;
		if (!editMode || index == -1)
		{
			return true;
		}
		int num = __instance.queueNumTexts.Length;
		if (selectedRecipeID == -1)
		{
			if (__instance.selectedRecipe == null)
			{
				return true;
			}
			((Graphic)__instance.recipeSelImage).rectTransform.anchoredPosition = new Vector2((float)(index % num * 46 - 1), (0f - (float)(index / num)) * 46f + 1f);
			((Component)__instance.recipeSelImage).gameObject.SetActive(true);
			selectedRecipeID = ((Proto)__instance.selectedRecipe).ID;
			Log.LogDebug((object)("origin grid selecteed : " + selectedRecipeID));
			((Component)selectedRecipeIcon.transform.Find("icon 1")).gameObject.SetActive(true);
			((Component)selectedRecipeIcon.transform.Find("icon 1")).GetComponentInChildren<Image>().sprite = ((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(selectedRecipeID).iconSprite;
		}
		else
		{
			destRecipeSelImageObj.GetComponent<RectTransform>().anchoredPosition = new Vector2((float)(index % num * 46 - 1), (float)(-(index / num) * 46 + 1));
			VFAudio.Create("ui-click-0", (Transform)null, Vector3.zero, true, 0, -1, -1L);
			int num2 = index / num + 1;
			int num3 = index % num + 1;
			GridIndex = __instance.currentType * 1000 + num2 * 100 + num3;
			Log.LogDebug((object)("destiantion grid selecteed : " + GridIndex));
			if (__instance.selectedRecipe != null)
			{
				Log.LogInfo((object)("Recipe" + selectedRecipeID + " => " + GridIndex + "(orignal recipe" + ((Proto)__instance.selectedRecipe).ID + ")"));
				int gridIndex = ((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(selectedRecipeID).GridIndex;
				((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(selectedRecipeID).GridIndex = ((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(((Proto)__instance.selectedRecipe).ID).GridIndex;
				((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(((Proto)__instance.selectedRecipe).ID).GridIndex = gridIndex;
				GridIndexDictionary[selectedRecipeID] = GridIndex;
				GridIndexDictionary[((Proto)__instance.selectedRecipe).ID] = gridIndex;
			}
			else
			{
				Log.LogInfo((object)("Recipe" + selectedRecipeID + " => " + GridIndex + "(empty)"));
				((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(selectedRecipeID).GridIndex = GridIndex;
				__instance.selectedRecipe = ((ProtoSet<RecipeProto>)(object)LDB.recipes).Select(selectedRecipeID);
				GridIndexDictionary[selectedRecipeID] = GridIndex;
			}
			destRecipeSelImageObj.gameObject.SetActive(false);
			((Component)__instance.recipeSelImage).gameObject.SetActive(false);
			UIRoot.instance.uiGame.replicator.RefreshRecipeIcons();
			selectedRecipeID = -1;
			((Component)selectedRecipeIcon.transform.Find("icon 1")).gameObject.SetActive(false);
		}
		return false;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIReplicatorWindow), "_OnOpen")]
	[HarmonyPatch(typeof(UIReplicatorWindow), "OnInstantSwitchClick")]
	private static void ShowButtons(UIReplicatorWindow __instance)
	{
		if ((Object)(object)resetButtonObj == (Object)null)
		{
			CreateUI();
			UIRoot.instance.uiGame.replicator.okButton.onRightClick += OnOkButtonRightClick;
		}
		resetButtonObj.SetActive(false);
		editButtonObj.SetActive(!__instance.isInstantItem);
	}

	[HarmonyPostfix]
	[HarmonyPriority(200)]
	[HarmonyPatch(typeof(VFPreload), "InvokeOnLoadWorkEnded")]
	private static void SetRecipeGridIndex()
	{
		if (GridIndexDictionaryOrigin != null)
		{
			return;
		}
		GridIndexDictionaryOrigin = new Dictionary<int, int>();
		int num = 0;
		RecipeProto[] dataArray = ((ProtoSet<RecipeProto>)(object)LDB.recipes).dataArray;
		foreach (RecipeProto val in dataArray)
		{
			if (val != null)
			{
				GridIndexDictionaryOrigin[((Proto)val).ID] = val.GridIndex;
				if (GridIndexDictionary.ContainsKey(((Proto)val).ID))
				{
					Log.LogDebug((object)("Recipe" + ((Proto)val).ID + ": " + val.GridIndex + " => " + GridIndexDictionary[((Proto)val).ID]));
					val.GridIndex = GridIndexDictionary[((Proto)val).ID];
					num++;
				}
			}
		}
		Log.LogInfo((object)("Recipe grid index changed: " + num));
	}

	private static void OnOkButtonRightClick(int _)
	{
		MechaForge forge = GameMain.mainPlayer.mecha.forge;
		List<ForgeTask> list = new List<ForgeTask>();
		int tick = 0;
		if (forge.tasks.Count > 0)
		{
			tick = forge.tasks[0].tick;
		}
		for (int i = 0; i < forge.tasks.Count; i++)
		{
			if (forge.tasks[i].parentTaskIndex < 0)
			{
				list.Add(forge.tasks[i]);
			}
		}
		forge.CancelAllTasks();
		UIRoot.instance.uiGame.replicator.OnOkButtonClick(0, true);
		int count = forge.tasks.Count;
		for (int j = 0; j < list.Count; j++)
		{
			if (forge.AddTask(list[j].recipeId, list[j].count) == null)
			{
				Log.LogWarning((object)$"Can't AddTask {list[j].recipeId} {list[j].count}");
			}
		}
		if (count < forge.tasks.Count)
		{
			forge.tasks[count].tick = tick;
		}
	}
}