Decompiled source of BountiesReworked v1.2.0

plugins/Orgetorix-BountiesReworked/Orgetorix.BountyReworked.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("Orgetorix.BountyScale")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Orgetorix.BountyScale")]
[assembly: AssemblyTitle("Orgetorix.BountyScale")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Orgetorix.BountyScale;

[BepInPlugin("orgetorix.bountyscale", "Orgetorix BountyScale", "1.2.0")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "orgetorix.bountyscale";

	public const string PluginName = "Orgetorix BountyScale";

	public const string PluginVersion = "1.2.0";

	internal static Plugin Instance;

	internal static ManualLogSource Log;

	internal static readonly Dictionary<int, float> DamageMulById = new Dictionary<int, float>();

	internal static List<ScaleRule> Rules = new List<ScaleRule>();

	internal static string ScalePath;

	private void Awake()
	{
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		ScalePath = ResolveScalePath();
		LoadRules();
		new Harmony("orgetorix.bountyscale").PatchAll();
		Log.LogInfo((object)("BountyScale 1.2.0 rules=" + Rules.Count + " file=" + ScalePath));
	}

	internal static string ResolveScalePath()
	{
		string text = Path.Combine(Paths.ConfigPath, "Orgetorix.BountyScale.json");
		if (File.Exists(text))
		{
			return text;
		}
		string text2 = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ".", "Orgetorix.BountyScale.json");
		return File.Exists(text2) ? text2 : text;
	}

	internal static void LoadRules()
	{
		Rules = new List<ScaleRule>();
		try
		{
			if (!File.Exists(ScalePath))
			{
				Log.LogWarning((object)("Keine Scale-Datei: " + ScalePath));
			}
			else
			{
				Rules = ScaleJson.Parse(File.ReadAllText(ScalePath));
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Scale-JSON: " + ex.Message));
		}
	}
}
public class ScaleRule
{
	public string id;

	public string biome;

	public string prefab;

	public int level;

	public float healthMul = 1f;

	public float damageMul = 1f;
}
internal static class ScaleJson
{
	public static List<ScaleRule> Parse(string json)
	{
		List<ScaleRule> list = new List<ScaleRule>();
		foreach (Match item in Regex.Matches(json, "\\{[^{}]*\"prefab\"[^{}]*\\}"))
		{
			string value = item.Value;
			ScaleRule scaleRule = new ScaleRule();
			scaleRule.id = Str(value, "id");
			scaleRule.biome = Str(value, "biome");
			scaleRule.prefab = Str(value, "prefab");
			scaleRule.level = Int(value, "level");
			scaleRule.healthMul = Flt(value, "healthMul", 1f);
			scaleRule.damageMul = Flt(value, "damageMul", 1f);
			if (!string.IsNullOrEmpty(scaleRule.prefab))
			{
				list.Add(scaleRule);
			}
		}
		return list;
	}

	private static string Str(string s, string key)
	{
		Match match = Regex.Match(s, "\"" + key + "\"\\s*:\\s*\"([^\"]*)\"");
		return match.Success ? match.Groups[1].Value : "";
	}

	private static int Int(string s, string key)
	{
		Match match = Regex.Match(s, "\"" + key + "\"\\s*:\\s*(-?\\d+)");
		return match.Success ? int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture) : 0;
	}

	private static float Flt(string s, string key, float def)
	{
		Match match = Regex.Match(s, "\"" + key + "\"\\s*:\\s*(-?\\d+(?:\\.\\d+)?)");
		return match.Success ? float.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture) : def;
	}
}
internal static class ScaleApply
{
	private const string Flag = "orgetorix.bountyscale";

	public static void TryApply(Character character)
	{
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)character == (Object)null || character.IsPlayer())
		{
			return;
		}
		ZNetView component = ((Component)character).GetComponent<ZNetView>();
		if ((Object)(object)component == (Object)null || !component.IsValid())
		{
			return;
		}
		ZDO zDO = component.GetZDO();
		if (zDO == null || zDO.GetBool("orgetorix.bountyscale", false))
		{
			return;
		}
		string text = Clean(((Object)((Component)character).gameObject).name);
		string text2 = BiomeName(((Component)character).transform.position);
		ScaleRule scaleRule = null;
		foreach (ScaleRule rule in Plugin.Rules)
		{
			if (rule == null || string.IsNullOrEmpty(rule.prefab) || !string.Equals(rule.prefab, text, StringComparison.OrdinalIgnoreCase) || (!string.IsNullOrEmpty(rule.biome) && !string.Equals(rule.biome, text2, StringComparison.OrdinalIgnoreCase)))
			{
				continue;
			}
			scaleRule = rule;
			break;
		}
		if (scaleRule != null)
		{
			if (scaleRule.level > 0)
			{
				character.SetLevel(Mathf.Clamp(scaleRule.level, 1, 10));
			}
			float num = ((scaleRule.healthMul > 0.01f) ? scaleRule.healthMul : 1f);
			if (Math.Abs(num - 1f) > 0.01f)
			{
				float num2 = character.GetMaxHealth() * num;
				character.SetMaxHealth(num2);
				character.SetHealth(num2);
			}
			float num3 = ((scaleRule.damageMul > 0.01f) ? scaleRule.damageMul : 1f);
			if (Math.Abs(num3 - 1f) > 0.01f)
			{
				Plugin.DamageMulById[((Object)character).GetInstanceID()] = num3;
			}
			zDO.Set("orgetorix.bountyscale", true);
			Plugin.Log.LogInfo((object)("scale " + (scaleRule.id ?? text) + " " + text + "@" + text2 + " lvl=" + character.GetLevel() + " hp×" + num + " dmg×" + num3));
		}
	}

	public static string Clean(string name)
	{
		if (string.IsNullOrEmpty(name))
		{
			return "";
		}
		return name.Replace("(Clone)", "").Trim();
	}

	public static string BiomeName(Vector3 pos)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			return ((object)Heightmap.FindBiome(pos)/*cast due to .constrained prefix*/).ToString();
		}
		catch
		{
			return "";
		}
	}
}
[HarmonyPatch(typeof(Character), "Start")]
internal static class CharacterStartPatch
{
	private static void Postfix(Character __instance)
	{
		try
		{
			ScaleApply.TryApply(__instance);
		}
		catch (Exception ex)
		{
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogError((object)ex.ToString());
			}
		}
	}
}
[HarmonyPatch(typeof(Character), "Damage")]
internal static class CharacterDamagePatch
{
	private static void Prefix(HitData hit)
	{
		if (hit != null)
		{
			Character attacker = hit.GetAttacker();
			if (!((Object)(object)attacker == (Object)null) && Plugin.DamageMulById.TryGetValue(((Object)attacker).GetInstanceID(), out var value) && !(value <= 0.01f) && !(Math.Abs(value - 1f) < 0.01f))
			{
				((DamageTypes)(ref hit.m_damage)).Modify(value);
			}
		}
	}
}