Decompiled source of Broken Ruunes Arctic Soldier Set v1.0.5

plugins/ArcticSoldierSet.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using SideLoader;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("local.dylan.arcticsoldierset", "Arctic Soldier Set", "1.0.5")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public sealed class ArcticSoldierSet : BaseUnityPlugin
{
	private sealed class PieceSettings
	{
		internal readonly int Price;

		internal readonly SL_EquipmentStats Stats;

		internal PieceSettings(ConfigFile config, string section, int price)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			Price = config.Bind<int>(section, "BuyPrice", price, new ConfigDescription("Purchase price at Loud-Hammer in silver.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 1000000), new object[0])).Value;
			int value = config.Bind<int>(section, "MaxDurability", -1, new ConfigDescription("Maximum durability. -1 keeps the original.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(-1, 100000), new object[0])).Value;
			SL_EquipmentStats val = new SL_EquipmentStats();
			val.Cold_Protection = ReadStat(config, section, "ColdWeatherDefense", 100f, "Cold Weather Defense.");
			val.Heat_Protection = ReadStat(config, section, "HotWeatherDefense", -1f, "Hot Weather Defense.");
			val.Damage_Protection = ReadStat(config, section, "Protection", -1f, "Physical Protection.");
			val.BarrierProtection = ReadStat(config, section, "Barrier", -1f, "Elemental Barrier.");
			val.Impact_Resistance = ReadStat(config, section, "ImpactResistance", -1f, "Impact Resistance.");
			((SL_ItemStats)val).RawWeight = ReadStat(config, section, "Weight", -1f, "Item weight.");
			((SL_ItemStats)val).MaxDurability = ((value < 0) ? ((int?)null) : new int?(value));
			Stats = val;
		}

		private static float? ReadStat(ConfigFile config, string section, string key, float initial, string description)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			float num = config.Bind<float>(section, key, initial, new ConfigDescription(description + " -1 keeps the original.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-1f, 10000f), new object[0])).Value;
			if (float.IsNaN(num))
			{
				num = initial;
			}
			if (!(num < 0f))
			{
				return num;
			}
			return null;
		}
	}

	private const string PluginId = "local.dylan.arcticsoldierset";

	private const int ArmorId = -847350;

	private const int HelmetId = -847351;

	private const int BootsId = -847352;

	private const int ChestPrice = 1000;

	private const int AccessoryPrice = 750;

	private const string LoudHammerId = "3Rx_R0XDLUmYaNWm66SVCQ";

	private const string MaterialName = "mat_cha_blueSandArmor";

	private static readonly Dictionary<int, PieceSettings> Settings = new Dictionary<int, PieceSettings>();

	private static ConfigEntry<string> seededInventories;

	private static HashSet<string> seededInventoryKeys;

	private static readonly int[] ShopItems = new int[3] { -847350, -847351, -847352 };

	private static readonly int MainTex = Shader.PropertyToID("_MainTex");

	private static Texture2D albedo;

	private static ManualLogSource log;

	private void Awake()
	{
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Expected O, but got Unknown
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Expected O, but got Unknown
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Expected O, but got Unknown
		//IL_0231: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Expected O, but got Unknown
		//IL_029a: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Expected O, but got Unknown
		//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_0304: Unknown result type (might be due to invalid IL or missing references)
		//IL_0311: Expected O, but got Unknown
		//IL_0311: Expected O, but got Unknown
		//IL_035e: Unknown result type (might be due to invalid IL or missing references)
		//IL_036c: Expected O, but got Unknown
		log = ((BaseUnityPlugin)this).Logger;
		Settings.Add(-847350, new PieceSettings(((BaseUnityPlugin)this).Config, "Armor", 1000));
		Settings.Add(-847351, new PieceSettings(((BaseUnityPlugin)this).Config, "Helmet", 750));
		Settings.Add(-847352, new PieceSettings(((BaseUnityPlugin)this).Config, "Boots", 750));
		seededInventories = ((BaseUnityPlugin)this).Config.Bind<string>("LoudHammer", "SeededStock", "", "Internal record of saves where the initial Arctic Soldier stock has been added.");
		seededInventoryKeys = new HashSet<string>(seededInventories.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries), StringComparer.Ordinal);
		albedo = LoadTexture("ArcticSoldierAlbedo");
		Harmony val = new Harmony("local.dylan.arcticsoldierset");
		string[] array = new string[2] { "InstantiateVisuals", "InstantiateSpecialVisuals" };
		foreach (string text in array)
		{
			val.Patch((MethodBase)AccessTools.Method(typeof(ItemManager), text, new Type[1] { typeof(Item) }, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(ArcticSoldierSet), "FinishInstance", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		string[] array2 = new string[2] { "Internal_PutBackVisual", "Internal_PutBackSpecialVisual" };
		foreach (string text2 in array2)
		{
			val.Patch((MethodBase)AccessTools.Method(typeof(ItemManager), text2, new Type[2]
			{
				typeof(int),
				typeof(Transform)
			}, (Type[])null), new HarmonyMethod(typeof(ArcticSoldierSet), "KeepVisualInOwnPool", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
		val.Patch((MethodBase)AccessTools.Method(typeof(SL_Armor), "ApplyToItem", new Type[1] { typeof(Item) }, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(ArcticSoldierSet), "ApplyConfiguredStats", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		MethodInfo methodInfo = AccessTools.Method(typeof(Item), "GetBuyValue", new Type[3]
		{
			typeof(Character),
			typeof(Merchant),
			typeof(bool)
		}, (Type[])null);
		HarmonyMethod val2 = new HarmonyMethod(typeof(ArcticSoldierSet), "SetLoudHammerPrice", (Type[])null);
		val2.priority = 0;
		val.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(typeof(MerchantPouch), "RefreshInventory", new Type[1] { typeof(Dropable) }, (Type[])null), new HarmonyMethod(typeof(ArcticSoldierSet), "BeforeRestock", (Type[])null), new HarmonyMethod(typeof(ArcticSoldierSet), "StockLoudHammer", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		val.Patch((MethodBase)AccessTools.Method(typeof(Merchant), "OnReceiveConfirmStartShopping", new Type[2]
		{
			typeof(bool),
			typeof(Character)
		}, (Type[])null), new HarmonyMethod(typeof(ArcticSoldierSet), "SeedExistingStock", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static Texture2D LoadTexture(string resource)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		Texture2D val = new Texture2D(2, 2, (TextureFormat)4, true);
		((Object)val).name = resource;
		((Texture)val).anisoLevel = 4;
		Texture2D val2 = val;
		using Stream stream = typeof(ArcticSoldierSet).Assembly.GetManifestResourceStream(resource);
		using MemoryStream memoryStream = new MemoryStream();
		stream.CopyTo(memoryStream);
		if (!ImageConversion.LoadImage(val2, memoryStream.ToArray(), true))
		{
			throw new InvalidDataException("Could not decode " + resource);
		}
		return val2;
	}

	private static void KeepVisualInOwnPool(ref int __0, Transform __1)
	{
		if (!Object.op_Implicit((Object)(object)__1))
		{
			return;
		}
		ItemVisual component = ((Component)__1).GetComponent<ItemVisual>();
		if (Object.op_Implicit((Object)(object)component))
		{
			int itemIDFromName = component.GetItemIDFromName();
			if (Array.IndexOf(ShopItems, itemIDFromName) >= 0)
			{
				__0 = itemIDFromName;
			}
		}
	}

	private static void FinishInstance(Item __0, Transform __result)
	{
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Expected O, but got Unknown
		if (!Object.op_Implicit((Object)(object)__0) || !Object.op_Implicit((Object)(object)__result) || Array.IndexOf(ShopItems, __0.ItemID) < 0)
		{
			return;
		}
		int itemID = __0.ItemID;
		ItemVisual component = ((Component)__result).GetComponent<ItemVisual>();
		if (!Object.op_Implicit((Object)(object)component) || component.ItemID != itemID)
		{
			return;
		}
		((Object)component).name = itemID.ToString(CultureInfo.InvariantCulture) + (component.IsSpecialVisual ? "_ArcticSoldier_e_v" : "_ArcticSoldier_v");
		int num = 0;
		Renderer[] componentsInChildren = ((Component)__result).GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			Material[] sharedMaterials = val.sharedMaterials;
			bool flag = false;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				Material val2 = sharedMaterials[j];
				if (Object.op_Implicit((Object)(object)val2) && ((Object)val2).name.StartsWith("mat_cha_blueSandArmor", StringComparison.OrdinalIgnoreCase) && val2.HasProperty(MainTex))
				{
					Material val3 = new Material(val2);
					((Object)val3).name = "mat_cha_ArcticSoldier";
					Material val4 = val3;
					val4.SetTexture(MainTex, (Texture)(object)albedo);
					sharedMaterials[j] = val4;
					flag = true;
					num++;
				}
			}
			if (flag)
			{
				val.sharedMaterials = sharedMaterials;
			}
		}
		if (num == 0)
		{
			log.LogWarning((object)("No Blue Sand material found on " + ((Object)__result).name));
			return;
		}
		log.LogInfo((object)("Assigned " + num + " private Arctic Soldier Set material(s) to item " + itemID + " on " + ((Object)__result).name + " (visual pool " + component.GetItemIDFromName() + ")"));
	}

	private static void ApplyConfiguredStats(Item __0)
	{
		if (Object.op_Implicit((Object)(object)__0) && Array.IndexOf(ShopItems, __0.ItemID) >= 0)
		{
			EquipmentStats component = ((Component)__0).GetComponent<EquipmentStats>();
			if (Object.op_Implicit((Object)(object)component))
			{
				((SL_ItemStats)Settings[__0.ItemID].Stats).ApplyToItem((ItemStats)(object)component);
				log.LogInfo((object)("Arctic Soldier item " + __0.ItemID + " has +" + component.ColdProtection + " Cold Weather Defense."));
			}
		}
	}

	private static bool IsLoudHammer(Merchant merchant)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)merchant))
		{
			Scene scene = ((Component)merchant).gameObject.scene;
			if (!(((Scene)(ref scene)).name != "CierzoNewTerrain"))
			{
				string text = UID.op_Implicit(merchant.HolderUID);
				return text == "3Rx_R0XDLUmYaNWm66SVCQ";
			}
		}
		return false;
	}

	private static void SetLoudHammerPrice(Item __instance, Character __0, Merchant __1, bool __2, ref int __result)
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		if (Array.IndexOf(ShopItems, __instance.ItemID) >= 0 && IsLoudHammer(__1) && (__2 || !Object.op_Implicit((Object)(object)__0) || !(EnvironmentConditions.GameTime < __instance.RebuyLimitTime) || !(__instance.PreviousOwnerUID == UID.op_Implicit(__0.UID))))
		{
			__result = Settings[__instance.ItemID].Price;
		}
	}

	private static void BeforeRestock(MerchantPouch __instance, double ___m_nextRefreshTime, out bool __state)
	{
		__state = IsLoudHammer(__instance.Merchant) && !PhotonNetwork.isNonMasterClientInRoom && EnvironmentConditions.GameTime >= ___m_nextRefreshTime;
	}

	private static Item FindStock(MerchantPouch pouch, int id)
	{
		return ((ItemContainer)pouch).GetItemsFromID(id).Find((Item item) => Object.op_Implicit((Object)(object)item) && !item.IsPendingDestroy);
	}

	private static Item EnsureStock(MerchantPouch pouch, int id)
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Expected O, but got Unknown
		Item val = FindStock(pouch, id);
		if (Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)ResourcesPrefabManager.Instance.GetItemPrefab(id)))
		{
			return val;
		}
		SL_ItemDrop val2 = new SL_ItemDrop();
		val2.DroppedItemID = id;
		val2.MinQty = 1;
		val2.MaxQty = 1;
		val2.GenerateDrop(((Component)pouch).transform);
		return FindStock(pouch, id);
	}

	private static void StockLoudHammer(MerchantPouch __instance, bool __state)
	{
		if (!__state)
		{
			return;
		}
		int[] shopItems = ShopItems;
		foreach (int num in shopItems)
		{
			Item val = EnsureStock(__instance, num);
			if (Object.op_Implicit((Object)(object)val))
			{
				log.LogInfo((object)("Loud-Hammer restock confirmed " + val.Name + " (" + num + ") in the merchant pouch."));
			}
		}
	}

	private static void SeedExistingStock(Merchant __instance, bool __0, Character __1)
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		if (!__0 || !Object.op_Implicit((Object)(object)__1) || !IsLoudHammer(__instance) || PhotonNetwork.isNonMasterClientInRoom)
		{
			return;
		}
		ItemContainer merchantPouch = __instance.MerchantPouch;
		MerchantPouch val = (MerchantPouch)(object)((merchantPouch is MerchantPouch) ? merchantPouch : null);
		Character worldHostCharacter = CharacterManager.Instance.GetWorldHostCharacter();
		if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)worldHostCharacter))
		{
			return;
		}
		string item = UID.op_Implicit(worldHostCharacter.UID) + ":" + UID.op_Implicit(__instance.HolderUID);
		if (seededInventoryKeys.Contains(item))
		{
			return;
		}
		bool flag = true;
		int[] shopItems = ShopItems;
		foreach (int num in shopItems)
		{
			Item val2 = EnsureStock(val, num);
			if (!Object.op_Implicit((Object)(object)val2))
			{
				flag = false;
				log.LogWarning((object)("Loud-Hammer's pouch is still missing item " + num + "."));
				continue;
			}
			log.LogInfo((object)("Loud-Hammer shop confirmed " + val2.Name + " (" + num + ") at " + val2.GetBuyValue(__1, __instance, true) + " silver."));
		}
		if (flag)
		{
			seededInventoryKeys.Add(item);
			seededInventories.Value = string.Join(",", seededInventoryKeys);
		}
	}
}