Decompiled source of Ragnarok Tweaks v1.0.0

plugins/Ragnarok_Tweaks.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("Ragnarok_Tweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+304efa162f1debd7dbb0a8cce2d83a024428b11f")]
[assembly: AssemblyProduct("Ragnarok_Tweaks")]
[assembly: AssemblyTitle("Ragnarok_Tweaks")]
[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 RagnarokTweaks
{
	[BepInPlugin("ragnarok.tweaks", "Ragnarok_Tweaks", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		private static class ObjectDBAwakePatch
		{
			[HarmonyPriority(0)]
			private static void Postfix(ObjectDB __instance)
			{
				_instance?.Apply(__instance);
			}
		}

		[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
		private static class ObjectDBCopyPatch
		{
			[HarmonyPriority(0)]
			private static void Postfix(ObjectDB __instance)
			{
				_instance?.Apply(__instance);
			}
		}

		[HarmonyPatch(typeof(Player), "OnSpawned")]
		private static class PlayerSpawnPatch
		{
			private static void Postfix()
			{
				_instance?.Apply(ObjectDB.instance);
			}
		}

		public const string Guid = "ragnarok.tweaks";

		public const string Version = "1.0.0";

		private const BindingFlags All = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static Plugin _instance;

		private readonly Dictionary<string, List<KeyValuePair<string, string>>> _rules = new Dictionary<string, List<KeyValuePair<string, string>>>();

		private void Awake()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			_instance = this;
			LoadRules(Path.Combine(Paths.ConfigPath, "Ragnarok_Tweaks.SetEffects.tsv"));
			new Harmony("ragnarok.tweaks").PatchAll(typeof(Plugin).Assembly);
		}

		private void LoadRules(string path)
		{
			if (!File.Exists(path))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)(path + " not found - no status effects will be changed"));
				return;
			}
			string[] array = File.ReadAllLines(path);
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length == 0 || text.StartsWith("#"))
				{
					continue;
				}
				int num = text.IndexOf('\t');
				if (num <= 0)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Bad rule line: " + text));
					continue;
				}
				string key = text.Substring(0, num).Trim();
				List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
				string[] array2 = text.Substring(num + 1).Split(new char[1] { ';' });
				foreach (string text2 in array2)
				{
					int num2 = text2.IndexOf('=');
					if (num2 > 0)
					{
						list.Add(new KeyValuePair<string, string>(text2.Substring(0, num2).Trim(), text2.Substring(num2 + 1).Trim()));
					}
				}
				_rules[key] = list;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"Loaded {_rules.Count} status effect rules");
		}

		internal void Apply(ObjectDB db)
		{
			if ((Object)(object)db == (Object)null || _rules.Count == 0)
			{
				return;
			}
			HashSet<StatusEffect> hashSet = new HashSet<StatusEffect>();
			if (db.m_StatusEffects != null)
			{
				foreach (StatusEffect statusEffect in db.m_StatusEffects)
				{
					if ((Object)(object)statusEffect != (Object)null)
					{
						hashSet.Add(statusEffect);
					}
				}
			}
			if (db.m_items != null)
			{
				foreach (GameObject item in db.m_items)
				{
					SharedData val = ((!((Object)(object)item != (Object)null)) ? null : item.GetComponent<ItemDrop>()?.m_itemData?.m_shared);
					if ((Object)(object)val?.m_setStatusEffect != (Object)null)
					{
						hashSet.Add(val.m_setStatusEffect);
					}
					if ((Object)(object)val?.m_equipStatusEffect != (Object)null)
					{
						hashSet.Add(val.m_equipStatusEffect);
					}
				}
			}
			int num = 0;
			int num2 = 0;
			HashSet<string> found = new HashSet<string>();
			foreach (StatusEffect item2 in hashSet)
			{
				if (!_rules.TryGetValue(((Object)item2).name, out var value))
				{
					continue;
				}
				found.Add(((Object)item2).name);
				num++;
				foreach (KeyValuePair<string, string> item3 in value)
				{
					if (SetField(item2, item3.Key, item3.Value))
					{
						num2++;
					}
				}
			}
			List<string> list = _rules.Keys.Where((string k) => !found.Contains(k)).ToList();
			((BaseUnityPlugin)this).Logger.LogInfo((object)($"Applied {num2} field changes to {num} status effects" + ((list.Count > 0) ? ("; not present: " + string.Join(", ", list)) : "")));
		}

		private bool SetField(object target, string fieldName, string value)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			FieldInfo fieldInfo = null;
			Type type = target.GetType();
			while (type != null && fieldInfo == null)
			{
				fieldInfo = type.GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				type = type.BaseType;
			}
			if (fieldInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)(((Object)target).name + ": field " + fieldName + " not found"));
				return false;
			}
			try
			{
				Type fieldType = fieldInfo.FieldType;
				object value2;
				if (fieldType == typeof(float))
				{
					value2 = float.Parse(value, CultureInfo.InvariantCulture);
				}
				else if (fieldType == typeof(int))
				{
					value2 = int.Parse(value, CultureInfo.InvariantCulture);
				}
				else if (fieldType == typeof(bool))
				{
					value2 = bool.Parse(value);
				}
				else if (fieldType.IsEnum)
				{
					value2 = Enum.Parse(fieldType, value, ignoreCase: true);
				}
				else
				{
					if (!(fieldType == typeof(DamageTypes)) || !(value == "zero"))
					{
						((BaseUnityPlugin)this).Logger.LogWarning((object)(fieldName + ": unsupported type " + fieldType.Name));
						return false;
					}
					value2 = (object)default(DamageTypes);
				}
				fieldInfo.SetValue(target, value2);
				return true;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)(fieldName + "=" + value + ": " + ex.Message));
				return false;
			}
		}
	}
}