Decompiled source of LogisCharges v0.5.3

plugins\LogisCharges\LogisCharges.dll

Decompiled 5 days 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 BepInEx;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("LogisCharges")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LogisCharges")]
[assembly: AssemblyTitle("LogisCharges")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 LogisCharges
{
	[BepInPlugin("com.logi.charges", "Logi's Charges", "0.5.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.logi.charges";

		public const string PluginName = "Logi's Charges";

		public const string PluginVersion = "0.5.3";

		private bool _registered;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Logi's Charges 0.5.3 loading.");
			AddLocalizations();
			PrefabManager.OnVanillaPrefabsAvailable += RegisterCharges;
		}

		private void AddLocalizations()
		{
			CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
			string text = "English";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				{ "$item_resincharge", "Resin Charge" },
				{ "$item_resincharge_desc", "A crude explosive charge made with resin." },
				{ "$item_surtlingcharge", "Surtling Charge" },
				{ "$item_surtlingcharge_desc", "An explosive charge infused with Surtling energy." },
				{ "$item_guckcharge", "Guck Charge" },
				{ "$item_guckcharge_desc", "An explosive charge infused with volatile guck." },
				{ "$item_fuelcharge", "Fuel Charge" },
				{ "$item_fuelcharge_desc", "An explosive charge made with volatile tar." },
				{ "$item_wispcharge", "Wisp Charge" },
				{ "$item_wispcharge_desc", "An explosive charge infused with wisp energy." }
			});
		}

		private Sprite LoadIcon(string fileName)
		{
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "Assets", "Icons", fileName);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Looking for icon: " + text));
			if (!File.Exists(text))
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("ICON FILE NOT FOUND: " + text));
				return null;
			}
			try
			{
				Texture2D val = AssetUtils.LoadTexture(text, true);
				if ((Object)(object)val == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Failed to load icon: " + text));
					return null;
				}
				Sprite result = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Custom icon loaded: " + fileName + " " + $"({((Texture)val).width}x{((Texture)val).height})"));
				return result;
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Error loading icon {fileName}: {arg}");
				return null;
			}
		}

		private void RegisterCharges()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			if (_registered)
			{
				return;
			}
			try
			{
				RegisterCharge("ResinCharge", "$item_resincharge", "$item_resincharge_desc", "ResinCharge.png", "LeatherScraps", 2, "Resin", 1, 35f, 25f, 12.5f, new Color(2f, 1.2f, 0f, 1f), new Color(1f, 0.75f, 0.05f, 1f));
				RegisterCharge("SurtlingCharge", "$item_surtlingcharge", "$item_surtlingcharge_desc", "SurtlingCharge.png", "BjornHide", 2, "SurtlingCore", 1, 56f, 40f, 20f, new Color(2f, 0.08f, 0.02f, 1f), new Color(1f, 0.08f, 0.03f, 1f));
				RegisterCharge("GuckCharge", "$item_guckcharge", "$item_guckcharge_desc", "GuckCharge.png", "WolfPelt", 2, "Guck", 1, 77f, 55f, 27.5f, new Color(0.05f, 2f, 0.08f, 1f), new Color(0.1f, 1f, 0.08f, 1f));
				RegisterCharge("FuelCharge", "$item_fuelcharge", "$item_fuelcharge_desc", "FuelCharge.png", "LoxPelt", 2, "Tar", 1, 98f, 70f, 35f, new Color(1.5f, 0.05f, 2f, 1f), new Color(0.65f, 0.08f, 1f, 1f));
				RegisterCharge("WispCharge", "$item_wispcharge", "$item_wispcharge_desc", "WispCharge.png", "ScaleHide", 2, "Wisp", 1, 119f, 85f, 42.5f, new Color(0.05f, 1.4f, 2f, 1f), new Color(0.1f, 0.75f, 1f, 1f));
				_registered = true;
				PrefabManager.OnVanillaPrefabsAvailable -= RegisterCharges;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Logi's Charges 0.5.3 registration completed.");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Error registering Logi's Charges: {arg}");
			}
		}

		private void RegisterCharge(string prefabName, string itemName, string description, string iconFile, string requirement1, int amount1, string requirement2, int amount2, float bluntDamage, float chopDamage, float pickaxeDamage, Color emissionColor, Color lightColor)
		{
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Expected O, but got Unknown
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Expected O, but got Unknown
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Sprite val = LoadIcon(iconFile);
				if ((Object)(object)val == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)(prefabName + " registration skipped: icon unavailable."));
					return;
				}
				GameObject val2 = CreateChargeExplosion(prefabName, bluntDamage, chopDamage, pickaxeDamage);
				if ((Object)(object)val2 == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)(prefabName + ": explosion prefab creation failed."));
					return;
				}
				GameObject val3 = CreateChargeProjectile(prefabName, val2);
				if ((Object)(object)val3 == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)(prefabName + ": projectile prefab creation failed."));
					return;
				}
				ItemConfig val4 = new ItemConfig
				{
					Name = itemName,
					Description = description,
					CraftingStation = "piece_workbench",
					MinStationLevel = 1,
					Amount = 10,
					Icon = val
				};
				val4.AddRequirement(requirement1, amount1, 0);
				val4.AddRequirement(requirement2, amount2, 0);
				CustomItem val5 = new CustomItem(prefabName, "BombDynamite", val4);
				SetItemProjectile(val5.ItemPrefab, val3);
				SetEmissionColor(val5.ItemPrefab, emissionColor);
				SetChargeLightColor(val5.ItemPrefab, lightColor);
				SetEmissionColor(val3, emissionColor);
				SetChargeLightColor(val3, lightColor);
				ItemManager.Instance.AddItem(val5);
				((BaseUnityPlugin)this).Logger.LogInfo((object)(prefabName + " registered. " + $"Damage: Blunt {bluntDamage}, " + $"Chop {chopDamage}, Pickaxe {pickaxeDamage}. " + $"Requirements: {amount1} {requirement1}, " + $"{amount2} {requirement2}. Output: 10."));
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to register {prefabName}: {arg}");
			}
		}

		private GameObject CreateChargeExplosion(string chargeName, float bluntDamage, float chopDamage, float pickaxeDamage)
		{
			string text = chargeName + "_explosion";
			GameObject val = PrefabManager.Instance.CreateClonedPrefab(text, "bombdynamite_explosion");
			if ((Object)(object)val == (Object)null)
			{
				throw new Exception("Could not clone bombdynamite_explosion.");
			}
			((Object)val).name = text;
			Component val2 = FindComponentByName(val, "Aoe");
			if ((Object)(object)val2 == (Object)null)
			{
				throw new Exception("Aoe component not found on cloned explosion.");
			}
			SetNestedFloatField(val2, "m_damage", "m_blunt", bluntDamage);
			SetNestedFloatField(val2, "m_damage", "m_chop", chopDamage);
			SetNestedFloatField(val2, "m_damage", "m_pickaxe", pickaxeDamage);
			PrefabManager.Instance.AddPrefab(val);
			((BaseUnityPlugin)this).Logger.LogInfo((object)(text + " created with independent Aoe damage."));
			return val;
		}

		private GameObject CreateChargeProjectile(string chargeName, GameObject explosionPrefab)
		{
			string text = chargeName + "_projectile";
			GameObject val = PrefabManager.Instance.CreateClonedPrefab(text, "bombdynamite_projectile");
			if ((Object)(object)val == (Object)null)
			{
				throw new Exception("Could not clone bombdynamite_projectile.");
			}
			((Object)val).name = text;
			Component val2 = FindComponentByName(val, "Projectile");
			if ((Object)(object)val2 == (Object)null)
			{
				throw new Exception("Projectile component not found on cloned projectile.");
			}
			SetMemberValue(val2, "m_spawnOnHit", explosionPrefab);
			PrefabManager.Instance.AddPrefab(val);
			((BaseUnityPlugin)this).Logger.LogInfo((object)(text + " created with dedicated explosion reference."));
			return val;
		}

		private void SetItemProjectile(GameObject itemPrefab, GameObject projectilePrefab)
		{
			if ((Object)(object)itemPrefab == (Object)null)
			{
				throw new Exception("Item prefab is null.");
			}
			Component val = FindComponentByName(itemPrefab, "ItemDrop");
			if ((Object)(object)val == (Object)null)
			{
				throw new Exception("ItemDrop component not found on custom item.");
			}
			object memberValue = GetMemberValue(val, "m_itemData");
			object memberValue2 = GetMemberValue(memberValue, "m_shared");
			object memberValue3 = GetMemberValue(memberValue2, "m_attack");
			if (memberValue3 == null)
			{
				throw new Exception("Shared attack data is missing.");
			}
			SetMemberValue(memberValue3, "m_attackProjectile", projectilePrefab);
			((BaseUnityPlugin)this).Logger.LogInfo((object)(((Object)itemPrefab).name + " now uses " + ((Object)projectilePrefab).name + "."));
		}

		private Component FindComponentByName(GameObject prefab, string componentName)
		{
			if ((Object)(object)prefab == (Object)null)
			{
				return null;
			}
			Component[] componentsInChildren = prefab.GetComponentsInChildren<Component>(true);
			foreach (Component val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null && ((object)val).GetType().Name == componentName)
				{
					return val;
				}
			}
			return null;
		}

		private object GetMemberValue(object target, string memberName)
		{
			if (target == null)
			{
				throw new Exception("Cannot read " + memberName + ": target is null.");
			}
			Type type = target.GetType();
			FieldInfo field = type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				return field.GetValue(target);
			}
			PropertyInfo property = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null)
			{
				return property.GetValue(target, null);
			}
			throw new Exception("Member " + memberName + " was not found on " + type.FullName + ".");
		}

		private void SetMemberValue(object target, string memberName, object value)
		{
			if (target == null)
			{
				throw new Exception("Cannot set " + memberName + ": target is null.");
			}
			Type type = target.GetType();
			FieldInfo field = type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(target, value);
				return;
			}
			PropertyInfo property = type.GetProperty(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.CanWrite)
			{
				property.SetValue(target, value, null);
				return;
			}
			throw new Exception("Writable member " + memberName + " was not found on " + type.FullName + ".");
		}

		private void SetNestedFloatField(object component, string parentFieldName, string nestedFieldName, float value)
		{
			object memberValue = GetMemberValue(component, parentFieldName);
			SetMemberValue(memberValue, nestedFieldName, value);
			SetMemberValue(component, parentFieldName, memberValue);
		}

		private void SetEmissionColor(GameObject prefab, Color color)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)prefab == (Object)null))
			{
				MeshRenderer[] componentsInChildren = prefab.GetComponentsInChildren<MeshRenderer>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					Renderer renderer = (Renderer)(object)componentsInChildren[i];
					ApplyEmission(renderer, color);
				}
				SkinnedMeshRenderer[] componentsInChildren2 = prefab.GetComponentsInChildren<SkinnedMeshRenderer>(true);
				for (int i = 0; i < componentsInChildren2.Length; i++)
				{
					Renderer renderer2 = (Renderer)(object)componentsInChildren2[i];
					ApplyEmission(renderer2, color);
				}
			}
		}

		private void ApplyEmission(Renderer renderer, Color color)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)renderer == (Object)null)
			{
				return;
			}
			Material[] materials = renderer.materials;
			Material[] array = materials;
			foreach (Material val in array)
			{
				if ((Object)(object)val != (Object)null && val.HasProperty("_EmissionColor"))
				{
					val.EnableKeyword("_EMISSION");
					val.SetColor("_EmissionColor", color);
				}
			}
			renderer.materials = materials;
		}

		private void SetChargeLightColor(GameObject prefab, Color color)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)prefab == (Object)null))
			{
				Light[] componentsInChildren = prefab.GetComponentsInChildren<Light>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					componentsInChildren[i].color = color;
				}
			}
		}
	}
}