Decompiled source of MonsterMayhem Standalone v1.0.0

plugins/MonsterMayhemStandalone.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace MonsterMayhemStandalone;

[BepInPlugin("balrond.astafaraios.BalrondAbsoluteOverhaul", "MonsterMayhem_Standalone bridge (not BAO)", "0.0.1")]
public class Puente : BaseUnityPlugin
{
	private void Awake()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"MonsterMayhem_Standalone: BAO bridge active. Monster Mayhem runs without Balrond Absolute Overhaul. Remove this mod if you install the real BAO.");
	}
}
[BepInPlugin("kiara.MonsterMayhemStandalone", "MonsterMayhem_Standalone", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	[HarmonyPatch]
	private static class Patches
	{
		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		[HarmonyPriority(800)]
		[HarmonyPrefix]
		private static void SceneAwake(ZNetScene __instance)
		{
			try
			{
				AddSpawners(__instance.m_prefabs);
			}
			catch (Exception ex)
			{
				Log.LogError((object)("Spawners: " + ex));
			}
		}

		[HarmonyPatch(typeof(CharacterDrop), "GenerateDropList")]
		[HarmonyPostfix]
		private static void Loot(CharacterDrop __instance, List<KeyValuePair<GameObject, int>> __result)
		{
			try
			{
				AddExtras(__instance, __result);
				AddFallback(__instance, __result);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Loot: " + ex.Message));
			}
		}
	}

	public const string GUID = "kiara.MonsterMayhemStandalone";

	public const string VERSION = "1.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> spawnersOn;

	internal static ConfigEntry<bool> lootOn;

	internal static ConfigEntry<string> lootSuffix;

	internal static ConfigEntry<string> lootBases;

	internal static readonly Dictionary<Biome, ConfigEntry<string>> biomeLoot = new Dictionary<Biome, ConfigEntry<string>>();

	private static GameObject holder;

	private static readonly Dictionary<string, GameObject> made = new Dictionary<string, GameObject>();

	private static readonly object[][] Spawners = new object[6][]
	{
		new object[3]
		{
			"PoisonGeyserSpawner_bal",
			"Spawner_DraugrPile",
			(object)new Color(0.45f, 0.85f, 0.35f)
		},
		new object[3]
		{
			"SeekerBroodSpawner_bal",
			"Spawner_GreydwarfNest",
			(object)new Color(0.65f, 0.45f, 0.85f)
		},
		new object[3]
		{
			"SurtlingSpawner_bal",
			"Spawner_GreydwarfNest",
			(object)new Color(1f, 0.45f, 0.2f)
		},
		new object[3]
		{
			"DrakeSpawner_bal",
			"BonePileSpawner",
			(object)new Color(0.75f, 0.9f, 1f)
		},
		new object[3]
		{
			"DeathsquitoHive_bal",
			"Spawner_GreydwarfNest",
			(object)new Color(0.95f, 0.8f, 0.4f)
		},
		new object[3]
		{
			"RockGolemSpawn_bal",
			"BonePileSpawner",
			(object)new Color(0.6f, 0.6f, 0.62f)
		}
	};

	private static readonly Dictionary<string, List<Drop>> extras = new Dictionary<string, List<Drop>>();

	private void Awake()
	{
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		spawnersOn = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - Spawners", "Enabled", true, "Create the 6 spawners Monster Mayhem expects from BAO (cloned from vanilla nests). Restart needed. / Crea los 6 generadores que Monster Mayhem espera de BAO.");
		lootOn = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Loot", "Enabled", true, "Give Balrond creatures the drops of their vanilla counterpart that they lack, and biome materials if they drop nothing. / Botín complementario y de respaldo.");
		lootSuffix = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Loot", "OnlyNamesEndingWith", "_bal", "Only creatures whose prefab name ends with this. Empty = any creature. / Solo criaturas cuyo nombre termina así.");
		lootBases = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Loot", "VanillaCounterpart", "Hog_bal=Boar, BattleHog_bal=Boar, BoarUndead_bal=Boar, Stag_bal=Deer, StagGhost_bal=Deer, NeckBrute_bal=Neck, GreydwarfMoss_bal=Greydwarf, GreydwarfCrusher_bal=Greydwarf_Elite, PolarBear_bal=Bjorn, Frostbjorn_bal=Bjorn, FrostbjornBrown_bal=Bjorn, LeechPrimal_bal=Leech, WolfUndead_bal=Wolf, GiantCrow_bal=Crow, TrollOutcast_bal=Troll, TrollBrute_bal=Troll, TrollDraugr_bal=Troll, TrollUndead_bal=Troll", "BalrondCreature=VanillaCreature, ... Creatures not listed use their name without _bal. / Criatura de Balrond = criatura normal.");
		string text = "Item:min-max:chance, ... (chance 0-1). Used only when a creature drops nothing. / Solo si la criatura no suelta nada.";
		biomeLoot[(Biome)1] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Meadows", "LeatherScraps:1-2:1, Feathers:1-2:0.5", text);
		biomeLoot[(Biome)8] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "BlackForest", "Resin:1-3:0.8, BoneFragments:1-2:0.6, LeatherScraps:1-2:0.5", text);
		biomeLoot[(Biome)2] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Swamp", "Guck:1-2:0.6, Bloodbag:1-1:0.5, BoneFragments:1-2:0.6, Entrails:1-2:0.6", text);
		biomeLoot[(Biome)4] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Mountain", "Obsidian:1-2:0.6, Feathers:1-2:0.6, LeatherScraps:1-2:0.5", text);
		biomeLoot[(Biome)16] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Plains", "Needle:1-3:0.7, LeatherScraps:1-2:0.6, BoneFragments:1-2:0.4", text);
		biomeLoot[(Biome)512] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Mistlands", "Softtissue:1-2:0.6, Carapace:1-1:0.4, BoneFragments:1-2:0.5", text);
		biomeLoot[(Biome)32] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Ashlands", "CharredBone:1-2:0.7, Coal:1-3:0.6", text);
		biomeLoot[(Biome)64] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "DeepNorth", "BoneFragments:1-3:0.7, LeatherScraps:1-2:0.6", text);
		biomeLoot[(Biome)256] = ((BaseUnityPlugin)this).Config.Bind<string>("3 - Biome fallback loot", "Ocean", "Chitin:1-2:0.6, BoneFragments:1-2:0.4", text);
		new Harmony("kiara.MonsterMayhemStandalone").PatchAll(typeof(Patches));
	}

	private static GameObject Find(List<GameObject> l, string n)
	{
		foreach (GameObject item in l)
		{
			if ((Object)(object)item != (Object)null && ((Object)item).name == n)
			{
				return item;
			}
		}
		return null;
	}

	internal static void AddSpawners(List<GameObject> prefabs)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		if (!spawnersOn.Value)
		{
			return;
		}
		if ((Object)(object)holder == (Object)null)
		{
			holder = new GameObject("MonsterMayhemStandalone_prefabs");
			Object.DontDestroyOnLoad((Object)(object)holder);
			holder.SetActive(false);
		}
		int num = 0;
		object[][] spawners = Spawners;
		foreach (object[] array in spawners)
		{
			string text = (string)array[0];
			string text2 = (string)array[1];
			if ((Object)(object)Find(prefabs, text) != (Object)null)
			{
				continue;
			}
			if (!made.TryGetValue(text, out var value) || (Object)(object)value == (Object)null)
			{
				GameObject val = Find(prefabs, text2);
				if ((Object)(object)val == (Object)null)
				{
					Log.LogWarning((object)("Base nest " + text2 + " not found for " + text));
					continue;
				}
				value = Object.Instantiate<GameObject>(val, holder.transform);
				((Object)value).name = text;
				SpawnArea component = value.GetComponent<SpawnArea>();
				if ((Object)(object)component != (Object)null && component.m_prefabs != null)
				{
					component.m_prefabs.Clear();
				}
				DropOnDestroyed component2 = value.GetComponent<DropOnDestroyed>();
				if ((Object)(object)component2 != (Object)null && component2.m_dropWhenDestroyed != null && component2.m_dropWhenDestroyed.m_drops != null)
				{
					component2.m_dropWhenDestroyed.m_drops.Clear();
				}
				Tint(value, (Color)array[2]);
				made[text] = value;
			}
			prefabs.Add(value);
			num++;
		}
		Log.LogInfo((object)("Spawners added for Monster Mayhem: " + num));
	}

	private static void Tint(GameObject go, Color c)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Expected O, but got Unknown
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		Renderer[] componentsInChildren = go.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			Material[] sharedMaterials = val.sharedMaterials;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				if (!((Object)(object)sharedMaterials[j] == (Object)null))
				{
					Material val2 = new Material(sharedMaterials[j]);
					if (val2.HasProperty("_Color"))
					{
						val2.color *= c;
					}
					sharedMaterials[j] = val2;
				}
			}
			val.sharedMaterials = sharedMaterials;
		}
	}

	private static bool Matches(string n)
	{
		string text = lootSuffix.Value ?? "";
		if (text.Length != 0)
		{
			return n.EndsWith(text, StringComparison.OrdinalIgnoreCase);
		}
		return true;
	}

	private static List<Drop> Extras(string n, CharacterDrop cd)
	{
		if (extras.TryGetValue(n, out var value))
		{
			return value;
		}
		value = new List<Drop>();
		extras[n] = value;
		string text = null;
		string[] array = (lootBases.Value ?? "").Split(new char[1] { ',' });
		foreach (string text2 in array)
		{
			int num = text2.IndexOf('=');
			if (num > 0 && text2.Substring(0, num).Trim().Equals(n, StringComparison.OrdinalIgnoreCase))
			{
				text = text2.Substring(num + 1).Trim();
				break;
			}
		}
		if (text == null && n.EndsWith("_bal", StringComparison.OrdinalIgnoreCase))
		{
			text = n.Substring(0, n.Length - 4);
		}
		GameObject val = ((text != null && (Object)(object)ZNetScene.instance != (Object)null) ? ZNetScene.instance.GetPrefab(text) : null);
		CharacterDrop val2 = (((Object)(object)val != (Object)null) ? val.GetComponent<CharacterDrop>() : null);
		if ((Object)(object)val2 == (Object)null || val2.m_drops == null)
		{
			return value;
		}
		HashSet<string> hashSet = new HashSet<string>();
		if (cd.m_drops != null)
		{
			foreach (Drop drop in cd.m_drops)
			{
				if (drop != null && (Object)(object)drop.m_prefab != (Object)null)
				{
					hashSet.Add(((Object)drop.m_prefab).name);
				}
			}
		}
		foreach (Drop drop2 in val2.m_drops)
		{
			if (drop2 != null && (Object)(object)drop2.m_prefab != (Object)null && !hashSet.Contains(((Object)drop2.m_prefab).name))
			{
				value.Add(drop2);
			}
		}
		if (value.Count > 0)
		{
			Log.LogInfo((object)("Extra loot for " + n + " (from " + text + "): " + string.Join(", ", value.Select((Drop x) => ((Object)x.m_prefab).name).ToArray())));
		}
		return value;
	}

	internal static void AddExtras(CharacterDrop cd, List<KeyValuePair<GameObject, int>> result)
	{
		if (!lootOn.Value || (Object)(object)cd == (Object)null || result == null)
		{
			return;
		}
		string prefabName = Utils.GetPrefabName(((Component)cd).gameObject);
		if (!Matches(prefabName))
		{
			return;
		}
		Character component = ((Component)cd).GetComponent<Character>();
		if ((Object)(object)component != (Object)null && component.IsBoss())
		{
			return;
		}
		foreach (Drop item in Extras(prefabName, cd))
		{
			if (!(Random.value > item.m_chance))
			{
				int value = Random.Range(Mathf.Max(1, item.m_amountMin), Mathf.Max(item.m_amountMin, item.m_amountMax) + 1);
				result.Add(new KeyValuePair<GameObject, int>(item.m_prefab, value));
			}
		}
	}

	internal static void AddFallback(CharacterDrop cd, List<KeyValuePair<GameObject, int>> result)
	{
		//IL_00a9: 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)
		//IL_00b3: 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_0237: Unknown result type (might be due to invalid IL or missing references)
		if (!lootOn.Value || (Object)(object)cd == (Object)null || result == null || result.Count > 0)
		{
			return;
		}
		string prefabName = Utils.GetPrefabName(((Component)cd).gameObject);
		if (!Matches(prefabName))
		{
			return;
		}
		if (cd.m_drops != null)
		{
			foreach (Drop drop in cd.m_drops)
			{
				if (drop != null && (Object)(object)drop.m_prefab != (Object)null)
				{
					return;
				}
			}
		}
		Character component = ((Component)cd).GetComponent<Character>();
		if ((Object)(object)component != (Object)null && (component.IsTamed() || component.IsBoss()))
		{
			return;
		}
		Biome val = Heightmap.FindBiome(((Component)cd).transform.position);
		if (!biomeLoot.TryGetValue(val, out var value))
		{
			return;
		}
		string[] array = value.Value.Split(new char[1] { ',' });
		foreach (string text in array)
		{
			string[] array2 = text.Trim().Split(new char[1] { ':' });
			if (array2.Length < 1 || array2[0].Length == 0)
			{
				continue;
			}
			GameObject val2 = (((Object)(object)ObjectDB.instance != (Object)null) ? ObjectDB.instance.GetItemPrefab(array2[0].Trim()) : null);
			if ((Object)(object)val2 == (Object)null)
			{
				continue;
			}
			int result2 = 1;
			int result3 = 1;
			float result4 = 1f;
			if (array2.Length > 1)
			{
				string[] array3 = array2[1].Split(new char[1] { '-' });
				int.TryParse(array3[0], out result2);
				result3 = result2;
				if (array3.Length > 1)
				{
					int.TryParse(array3[1], out result3);
				}
			}
			if (array2.Length > 2)
			{
				float.TryParse(array2[2], NumberStyles.Float, CultureInfo.InvariantCulture, out result4);
			}
			if (!(Random.value > result4))
			{
				result.Add(new KeyValuePair<GameObject, int>(val2, Random.Range(Mathf.Max(1, result2), Mathf.Max(result2, result3) + 1)));
			}
		}
		if (result.Count > 0)
		{
			Log.LogInfo((object)string.Concat("Biome fallback loot for ", prefabName, " (", val, "): ", result.Count, " items"));
		}
	}
}