Decompiled source of Map Upgrade Updated v1.3.0

MapUpgrade.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using REPOLib;
using REPOLib.Modules;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("MapUpgrade")]
[assembly: AssemblyDescription("R.E.P.O. Map Upgrade Mod! Originally developed by Ardot66, updated by Jettcodey.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Ardot66, Jettcodey")]
[assembly: AssemblyProduct("MapUpgrade")]
[assembly: AssemblyCopyright("")]
[assembly: ComVisible(false)]
[assembly: Guid("0bb70cc2-9b49-4a31-8adc-e4218f4f39b5")]
[assembly: AssemblyFileVersion("1.3.0")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Ardot.Jettcodey.REPO.MapUpgrade
{
	[HarmonyPatch]
	public class MapToolChanges : MonoBehaviour
	{
		public record struct BackgroundObject(Transform Object, Vector3 OriginalScale);

		public static MapToolChanges Instance;

		public static ConfigEntry<float> DefaultSize;

		public static ConfigEntry<float> ZoomRate;

		public MapToolController MapTool;

		public Camera MapCamera;

		public BackgroundObject[] BackgroundObjects = new BackgroundObject[3];

		public const float ZoomScale = 2f;

		public float MaxZoom = 1f;

		public bool MapEnabled = true;

		private float _lastZoom = -1f;

		public static void Init()
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			ZoomRate = Plugin.Config.Bind<float>("Map", "ZoomSpeed", 1f, new ConfigDescription("Controls the speed that the map is zoomed", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
			DefaultSize = Plugin.Config.Bind<float>("Map", "DefaultSize", 1f, new ConfigDescription("The starting size of the map", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
		}

		[HarmonyPatch(typeof(PlayerAvatar), "Awake")]
		[HarmonyPostfix]
		public static void PlayerAwakePostfix(PlayerAvatar __instance)
		{
			if ((Object)(object)__instance.mapToolController != (Object)null && (Object)(object)((Component)__instance.mapToolController).gameObject.GetComponent<MapToolChanges>() == (Object)null)
			{
				((Component)__instance.mapToolController).gameObject.AddComponent<MapToolChanges>();
			}
		}

		public void Awake()
		{
			MapTool = ((Component)this).GetComponent<MapToolController>();
			if ((Object)(object)Map.Instance != (Object)null && (Object)(object)((Component)Map.Instance).transform.parent != (Object)null)
			{
				int backgroundObjectIndex = 0;
				Utils.ForObjectsInTree(((Component)Map.Instance).transform.parent, delegate(Transform transform)
				{
					//IL_0081: Unknown result type (might be due to invalid IL or missing references)
					Camera component = ((Component)transform).GetComponent<Camera>();
					if ((Object)(object)component != (Object)null)
					{
						MapCamera = component;
					}
					switch (((Object)transform).name)
					{
					case "Fog":
					case "Scanlines":
					case "Background":
						if (backgroundObjectIndex < BackgroundObjects.Length)
						{
							BackgroundObjects[backgroundObjectIndex] = new BackgroundObject(transform, transform.localScale);
							backgroundObjectIndex++;
						}
						break;
					case "Completed":
						return false;
					}
					return true;
				});
			}
			SetZoom(DefaultSize.Value);
		}

		public void Start()
		{
			PlayerAvatar componentInParent = ((Component)this).GetComponentInParent<PlayerAvatar>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				Plugin.UpdatePlayerMapTool(componentInParent);
			}
			else if ((Object)(object)PlayerAvatar.instance != (Object)null)
			{
				Plugin.UpdatePlayerMapTool(PlayerAvatar.instance);
			}
		}

		public void Update()
		{
			if ((Object)(object)MapTool == (Object)null)
			{
				return;
			}
			if (!MapEnabled)
			{
				if ((Object)(object)MapTool.DisplayMesh != (Object)null && ((Renderer)MapTool.DisplayMesh).enabled)
				{
					SetMapEnabled(enabled: false);
				}
			}
			else
			{
				if (!MapTool.Active || (Object)(object)MapCamera == (Object)null)
				{
					return;
				}
				if (SemiFunc.RunIsShop() || SemiFunc.RunIsLobby() || SemiFunc.RunIsLobbyMenu())
				{
					SetZoom(DefaultSize.Value);
					return;
				}
				float axisRaw = Input.GetAxisRaw("Mouse ScrollWheel");
				if (Mathf.Abs(axisRaw) > 0.0001f)
				{
					float zoom = Mathf.Clamp(MapCamera.orthographicSize / 2f - axisRaw * ZoomRate.Value, 0.5f, MaxZoom);
					SetZoom(zoom);
				}
			}
		}

		public void SetMapEnabled(bool enabled)
		{
			if (!((Object)(object)MapTool == (Object)null) && !((Object)(object)MapTool.DisplayMesh == (Object)null))
			{
				((Renderer)MapTool.DisplayMesh).enabled = enabled;
				Light componentInChildren = ((Component)((Component)MapTool.DisplayMesh).transform.parent).GetComponentInChildren<Light>();
				if ((Object)(object)componentInChildren != (Object)null)
				{
					((Behaviour)componentInChildren).enabled = enabled;
				}
				MapEnabled = enabled;
			}
		}

		public void SetMaxZoom(float zoom)
		{
			if (Mathf.Abs(zoom - MaxZoom) > 0.1f)
			{
				SetZoom(zoom);
			}
			MaxZoom = zoom;
		}

		public void SetZoom(float zoom)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)MapCamera == (Object)null || Mathf.Abs(zoom - _lastZoom) < 0.001f)
			{
				return;
			}
			_lastZoom = zoom;
			MapCamera.orthographicSize = zoom * 2f;
			for (int i = 0; i < BackgroundObjects.Length; i++)
			{
				BackgroundObject backgroundObject = BackgroundObjects[i];
				if ((Object)(object)backgroundObject.Object != (Object)null)
				{
					Vector3 originalScale = backgroundObject.OriginalScale;
					backgroundObject.Object.localScale = new Vector3(originalScale.x * zoom, originalScale.y, originalScale.z * zoom);
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Ardot.Jettcodey.REPO.MapUpgrade";

		public const string PLUGIN_NAME = "Map Upgrade";

		public const string PLUGIN_VERSION = "1.3.0";
	}
	[BepInPlugin("Ardot.Jettcodey.REPO.MapUpgrade", "Map Upgrade", "1.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigFile Config;

		public static Harmony Harmony;

		internal static ManualLogSource Logger;

		public static ConfigEntry<bool> MapRequiresUpgrade;

		public static ConfigEntry<float> MinCost;

		public static ConfigEntry<float> MaxCost;

		public static ConfigEntry<float> MapSizeIncrease;

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Config = ((BaseUnityPlugin)this).Config;
			Harmony = new Harmony("Ardot.Jettcodey.REPO.MapUpgrade");
			InitConfig();
			Harmony.PatchAll();
			MapToolChanges.Init();
			Logger.LogInfo((object)"Plugin Map Upgrade v1.3.0 loaded successfully.");
			Logger.LogInfo((object)"Mod originally developed by Ardot66, updated by Jettcodey!");
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "mapupgrade.file");
			BundleLoader.LoadBundle(text, (Action<AssetBundle>)delegate(AssetBundle assetBundle)
			{
				string[] array = new string[1] { "Map Upgrade" };
				string[] array2 = array;
				foreach (string text2 in array2)
				{
					try
					{
						Item val = assetBundle.LoadAsset<Item>(text2);
						if ((Object)(object)val == (Object)null)
						{
							Logger.LogError((object)("Failed loading Item asset '" + text2 + "' from bundle."));
						}
						else
						{
							GameObject val2 = assetBundle.LoadAsset<GameObject>(text2);
							if ((Object)(object)val2 == (Object)null)
							{
								Logger.LogError((object)("Failed loading prefab for upgrade '" + text2 + "'. Make sure the prefab is included in the bundle."));
							}
							else
							{
								string name = (((Object)val2).name = text2 ?? "");
								((Object)val).name = name;
								val.itemName = text2 ?? "";
								Value val3 = ScriptableObject.CreateInstance<Value>();
								val3.valueMin = MinCost.Value / 4f;
								val3.valueMax = MaxCost.Value / 4f;
								val.value = val3;
								ItemAttributes val4 = val2.GetComponent<ItemAttributes>() ?? val2.AddComponent<ItemAttributes>();
								val4.item = val;
								Upgrader upgrader = val2.GetComponent<Upgrader>() ?? val2.AddComponent<Upgrader>();
								PrefabRef val5 = Items.RegisterItem(val4);
								if (val5 == null)
								{
									Logger.LogWarning((object)("Items.RegisterItem returned null for '" + text2 + "'."));
								}
								else
								{
									Logger.LogInfo((object)("Registered upgrade item '" + text2 + "'."));
								}
								Upgrades.RegisterUpgrade(text2, val, (Action<PlayerAvatar, int>)delegate(PlayerAvatar player, int level)
								{
									UpdatePlayerMapTool(player);
								}, (Action<PlayerAvatar, int>)null);
							}
						}
					}
					catch (Exception arg)
					{
						Logger.LogError((object)$"Exception while registering upgrade '{text2}': {arg}");
					}
				}
			}, false);
			Config.SettingChanged += OnConfigChanged;
		}

		private void InitConfig()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			MapRequiresUpgrade = Config.Bind<bool>("Upgrade", "MapRequiresUpgrade", true, "If true, one map upgrade is required before the map can be used");
			MinCost = Config.Bind<float>("Upgrade", "MinCost", 7000f, new ConfigDescription("The minimum price of a MapUpgrade in the shop", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 50000f), Array.Empty<object>()));
			MaxCost = Config.Bind<float>("Upgrade", "MaxCost", 10000f, new ConfigDescription("The maximum price of a MapUpgrade in the shop", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 50000f), Array.Empty<object>()));
			MapSizeIncrease = Config.Bind<float>("Upgrade", "MapSizeIncrease", 0.5f, new ConfigDescription("The amount that the map increases in size for every map upgrade", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 10f), Array.Empty<object>()));
		}

		private void OnConfigChanged(object sender, SettingChangedEventArgs e)
		{
			if (((object)e.ChangedSetting == MapSizeIncrease || (object)e.ChangedSetting == MapRequiresUpgrade) && (Object)(object)PlayerAvatar.instance != (Object)null)
			{
				Update_MapUpgrade();
			}
		}

		public static void Update_MapUpgrade()
		{
			if (LevelGenerator.Instance.Generated && !SemiFunc.MenuLevel() && (Object)(object)PlayerAvatar.instance != (Object)null)
			{
				UpdateAllPlayersMapTool();
			}
		}

		public static void UpdateAllPlayersMapTool()
		{
			if (!((Object)(object)GameDirector.instance == (Object)null) && GameDirector.instance.PlayerList != null)
			{
				for (int i = 0; i < GameDirector.instance.PlayerList.Count; i++)
				{
					PlayerAvatar player = GameDirector.instance.PlayerList[i];
					UpdatePlayerMapTool(player);
				}
			}
		}

		public static void UpdatePlayerMapTool(PlayerAvatar player)
		{
			if ((Object)(object)player == (Object)null || (Object)(object)player.mapToolController == (Object)null)
			{
				return;
			}
			MapToolChanges component = ((Component)player.mapToolController).GetComponent<MapToolChanges>();
			if (!((Object)(object)component == (Object)null))
			{
				int stat = Upgrader.GetStat(player.steamID, "playerUpgradeMapUpgrade");
				int num = stat;
				if (!MapRequiresUpgrade.Value && num == 0)
				{
					num = 1;
				}
				component.SetMapEnabled(num > 0);
				component.SetMaxZoom(MapToolChanges.DefaultSize.Value + (float)(num - 1) * MapSizeIncrease.Value);
			}
		}
	}
	public class Upgrader : MonoBehaviourPunCallbacks
	{
		public ItemToggle _itemToggle;

		private PhotonView _photonView;

		private void Start()
		{
			_itemToggle = ((Component)this).GetComponent<ItemToggle>();
			_photonView = ((Component)this).GetComponent<PhotonView>();
			if (!StatsManager.instance.dictionaryOfDictionaries.ContainsKey("playerUpgradeMapUpgrade"))
			{
				StatsManager.instance.dictionaryOfDictionaries["playerUpgradeMapUpgrade"] = new Dictionary<string, int>();
			}
		}

		public static void UpdateStat(int amount, string steamId, string stat)
		{
			if (!StatsManager.instance.dictionaryOfDictionaries.ContainsKey(stat))
			{
				StatsManager.instance.dictionaryOfDictionaries[stat] = new Dictionary<string, int>();
			}
			Dictionary<string, int> dictionary = StatsManager.instance.dictionaryOfDictionaries[stat];
			if (!dictionary.ContainsKey(steamId))
			{
				dictionary[steamId] = 0;
			}
			dictionary[steamId] += amount;
			Plugin.Update_MapUpgrade();
		}

		public static int GetStat(string steamId, string upgradeName)
		{
			if (!StatsManager.instance.dictionaryOfDictionaries.ContainsKey(upgradeName))
			{
				return 0;
			}
			Dictionary<string, int> dictionary = StatsManager.instance.dictionaryOfDictionaries[upgradeName];
			if (!dictionary.TryGetValue(steamId, out var value))
			{
				return 0;
			}
			return value;
		}

		public void UpgradeMapUpgrade()
		{
			PlayerAvatar val = SemiFunc.PlayerAvatarGetFromPhotonID(_itemToggle.playerTogglePhotonID);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			string text = SemiFunc.PlayerGetSteamID(val);
			if (Utils.IsHost())
			{
				Plugin.Logger.LogInfo((object)("Host executing map upgrade for " + val.playerName));
				UpdateStat(1, text, "playerUpgradeMapUpgrade");
				if (SemiFunc.IsMultiplayer() && (Object)(object)_photonView != (Object)null)
				{
					_photonView.RPC("SyncUpgradeRPC", (RpcTarget)1, new object[2] { text, 1 });
				}
				if ((Object)(object)_photonView != (Object)null && _photonView.IsMine)
				{
					PhotonNetwork.Destroy(((Component)this).gameObject);
				}
			}
			else if ((Object)(object)_photonView != (Object)null && _photonView.IsMine)
			{
				Plugin.Logger.LogInfo((object)"Client requesting map upgrade from host...");
				_photonView.RPC("RequestMapUpgradeRPC", (RpcTarget)2, new object[1] { text });
			}
		}

		[PunRPC]
		public void RequestMapUpgradeRPC(string steamId)
		{
			if (Utils.IsHost())
			{
				Plugin.Logger.LogInfo((object)("Host received map upgrade request for " + steamId));
				UpdateStat(1, steamId, "playerUpgradeMapUpgrade");
				if (SemiFunc.IsMultiplayer() && (Object)(object)_photonView != (Object)null)
				{
					_photonView.RPC("SyncUpgradeRPC", (RpcTarget)1, new object[2] { steamId, 1 });
				}
				if ((Object)(object)_photonView != (Object)null && _photonView.IsMine)
				{
					PhotonNetwork.Destroy(((Component)this).gameObject);
				}
			}
		}

		[PunRPC]
		private void SyncUpgradeRPC(string steamId, int amount)
		{
			if (!Utils.IsHost())
			{
				Plugin.Logger.LogInfo((object)("Client received sync for map upgrade: " + steamId));
				UpdateStat(amount, steamId, "playerUpgradeMapUpgrade");
			}
		}
	}
	public static class Utils
	{
		public static bool IsHost()
		{
			return SemiFunc.IsMasterClientOrSingleplayer();
		}

		public static void ForObjectsInTree(Transform root, Predicate<Transform> action)
		{
			Stack<Transform> stack = new Stack<Transform>();
			stack.Push(root);
			while (stack.Count > 0)
			{
				Transform val = stack.Pop();
				if (action(val))
				{
					for (int num = val.childCount - 1; num >= 0; num--)
					{
						stack.Push(val.GetChild(num));
					}
				}
			}
		}
	}
}