Decompiled source of LobbyRulebookDefSaver v1.0.0

plugins/viliger-LobbyRulebookDefSaver/LobbyRulebookDefSaver.dll

Decompiled 3 weeks 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.Configuration;
using On.RoR2;
using On.RoR2.UI;
using RoR2;
using RoR2.UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

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

[BepInPlugin("com.viliger.LobbyRulebookDefSaver", "LobbyRulebookDefSaver", "1.0.0")]
public class LobbyRulebookDefSaver : BaseUnityPlugin
{
	public const string Author = "viliger";

	public const string ModName = "LobbyRulebookDefSaver";

	public const string Version = "1.0.0";

	public const string GUID = "com.viliger.LobbyRulebookDefSaver";

	private static ConfigEntry<string> SavedRulebookRules;

	private static ConfigEntry<bool> SaveOnRuleChanges;

	private static ConfigEntry<bool> AddSaveButton;

	private void Awake()
	{
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Expected O, but got Unknown
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Expected O, but got Unknown
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Expected O, but got Unknown
		SavedRulebookRules = ((BaseUnityPlugin)this).Config.Bind<string>("Rulebook Rules", "Saved Rulebook Rules", "", "Saved Rulebook rules. It is not recommended to modify this value manually.");
		SaveOnRuleChanges = ((BaseUnityPlugin)this).Config.Bind<bool>("Rulebook Rules", "Save on Rulebook Changes", false, "Saves Rulebook rules to config on any changes to rules.");
		AddSaveButton = ((BaseUnityPlugin)this).Config.Bind<bool>("Rulebook Rules", "Add Save to Config button", true, "Adds Save to config button that allows for manual rulebook saving to config.");
		LocalUserBallotPersistenceManager.OnLocalUserSignIn += new hook_OnLocalUserSignIn(LocalUserBallotPersistenceManager_OnLocalUserSignIn);
		if (SaveOnRuleChanges.Value)
		{
			LocalUserBallotPersistenceManager.OnVotesUpdated += new hook_OnVotesUpdated(LocalUserBallotPersistenceManager_OnVotesUpdated);
		}
		if (AddSaveButton.Value)
		{
			CharacterSelectController.Awake += new hook_Awake(CharacterSelectController_Awake);
		}
		Language.collectLanguageRootFolders += Language_collectLanguageRootFolders;
	}

	private void Language_collectLanguageRootFolders(List<string> folders)
	{
		folders.Add(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Language"));
	}

	private void CharacterSelectController_Awake(orig_Awake orig, CharacterSelectController self)
	{
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Expected O, but got Unknown
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_010a: Expected O, but got Unknown
		try
		{
			Transform val = ((Component)self).transform.Find("SafeArea/RightHandPanel/PopoutPanelContainer/PopoutPanelPrefab/Canvas/Main/RandomButtonContainer/RandomButton");
			GameObject gameObject = ((Component)Object.Instantiate<Transform>(((Component)self).transform.Find("SafeArea/FooterPanel/NakedButton (Quit)"), ((Component)val).transform.parent)).gameObject;
			InputSourceFilter[] components = ((Component)self).GetComponents<InputSourceFilter>();
			foreach (InputSourceFilter val2 in components)
			{
				if ((int)val2.requiredInputSource == 0)
				{
					Array.Resize(ref val2.objectsToFilter, val2.objectsToFilter.Length + 1);
					val2.objectsToFilter[val2.objectsToFilter.Length - 1] = gameObject;
					break;
				}
			}
			((Object)gameObject).name = "LobbyRulebookDefSaver_SaveToConfig";
			gameObject.AddComponent<TooltipProvider>();
			RectTransform component = gameObject.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 1.5f);
			component.anchorMax = new Vector2(1f, 1.5f);
			HGButton component2 = gameObject.GetComponent<HGButton>();
			component2.hoverToken = "LOBBYRULEBOOKDEFSAVER_SAVEBUTTON_HOVER";
			gameObject.GetComponent<LanguageTextMeshController>().token = "LOBBYRULEBOOKDEFSAVER_SAVEBUTTON_DESC";
			((Button)component2).onClick = new ButtonClickedEvent();
			AddPersistentListener((UnityEvent)(object)((Button)component2).onClick, new UnityAction(SaveToConfigInputEvent));
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"Couldn't add SaveToConfig button.");
			((BaseUnityPlugin)this).Logger.LogError((object)ex);
		}
		orig.Invoke(self);
	}

	private void LocalUserBallotPersistenceManager_OnLocalUserSignIn(orig_OnLocalUserSignIn orig, LocalUser localUser)
	{
		orig.Invoke(localUser);
		string[] array = SavedRulebookRules.Value.Split(';', StringSplitOptions.RemoveEmptyEntries);
		if (LocalUserBallotPersistenceManager.votesCache[localUser] == null)
		{
			LocalUserBallotPersistenceManager.votesCache[localUser] = PreGameRuleVoteController.CreateBallot();
		}
		string[] array2 = array;
		for (int i = 0; i < array2.Length; i++)
		{
			RuleChoiceDef val = RuleCatalog.FindChoiceDef(array2[i]);
			if (val != null)
			{
				RuleDef ruleDef = val.ruleDef;
				((Vote)(ref LocalUserBallotPersistenceManager.votesCache[localUser][ruleDef.globalIndex])).choiceValue = val.localIndex;
			}
		}
	}

	private void SaveToConfigInputEvent()
	{
		SaveToConfig();
	}

	private void LocalUserBallotPersistenceManager_OnVotesUpdated(orig_OnVotesUpdated orig)
	{
		orig.Invoke();
		SaveToConfig();
	}

	private void SaveToConfig()
	{
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		string text = "";
		foreach (KeyValuePair<LocalUser, Vote[]> item in LocalUserBallotPersistenceManager.votesCache)
		{
			Vote[] value = item.Value;
			if (value == null)
			{
				continue;
			}
			for (int i = 0; i < RuleCatalog.ruleCount; i++)
			{
				RuleDef ruleDef = RuleCatalog.GetRuleDef(i);
				int count = ruleDef.choices.Count;
				Vote val = value[i];
				if (((Vote)(ref val)).hasVoted && ((Vote)(ref val)).choiceValue < count)
				{
					RuleChoiceDef val2 = ruleDef.choices[((Vote)(ref val)).choiceValue];
					text = text + val2.globalName + ";";
				}
			}
		}
		SavedRulebookRules.Value = text;
	}

	public static void AddPersistentListener(UnityEvent unityEvent, UnityAction action)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		((UnityEventBase)unityEvent).m_PersistentCalls.AddListener(new PersistentCall
		{
			m_Target = (Object)/*isinst with value type is only supported in some contexts*/,
			m_TargetAssemblyTypeName = UnityEventTools.TidyAssemblyTypeName(((Delegate)(object)action).Method.DeclaringType.AssemblyQualifiedName),
			m_MethodName = ((Delegate)(object)action).Method.Name,
			m_CallState = (UnityEventCallState)2,
			m_Mode = (PersistentListenerMode)0
		});
	}
}