Decompiled source of TreeRespawn v0.8.0

TreeRespawn.dll

Decompiled 7 months ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
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.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TreeRespawn;

[BepInPlugin("aedenthorn.TreeRespawn", "Tree Respawn", "0.8.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Destructible), "Destroy")]
	private static class Destroy_Patch
	{
		private static void Prefix(Destructible __instance)
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			Dbgl("destroyed destructible " + ((Object)__instance).name);
			string value = seedsDic.FirstOrDefault((KeyValuePair<string, string> s) => ((Object)__instance).name.StartsWith(s.Key)).Value;
			if (value != null)
			{
				Dbgl("destroyed trunk " + ((Object)__instance).name + ", trying to spawn " + value);
				GameObject prefab = ZNetScene.instance.GetPrefab(value);
				if ((Object)(object)prefab != (Object)null)
				{
					Dbgl("trying to spawn new tree");
					((MonoBehaviour)context).StartCoroutine(SpawnTree(prefab, ((Component)__instance).transform.position));
				}
				else
				{
					Dbgl("prefab is null");
				}
			}
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<float> respawnDelay;

	public static Dictionary<string, string> seedsDic = new Dictionary<string, string>();

	public static void Dbgl(string str = "", bool pref = true)
	{
		if (isDebug)
		{
			Debug.Log((object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + str));
		}
	}

	private void Awake()
	{
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 37, "Nexus mod ID for updates");
		respawnDelay = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RespawnDelay", 2.5f, "Delay in seconds to spawn sapling");
		if (modEnabled.Value)
		{
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
		}
	}

	private void Start()
	{
		string path = "tree_dict.json";
		if (Chainloader.PluginInfos.ContainsKey("advize.PlantEverything"))
		{
			path = "tree_dict_Plant_Everything.json";
		}
		else if (Chainloader.PluginInfos.ContainsKey("com.Defryder.Plant_all_trees"))
		{
			path = "tree_dict_Plant_all_trees.json";
		}
		else if (Chainloader.PluginInfos.ContainsKey("com.bkeyes93.PlantingPlus"))
		{
			path = "tree_dict_PlantingPlus.json";
		}
		string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TreeRespawn", path);
		string text2 = File.ReadAllText(text);
		SeedData seedData = JsonUtility.FromJson<SeedData>(text2);
		foreach (string seed in seedData.seeds)
		{
			string[] array = seed.Split(new char[1] { ':' });
			seedsDic.Add(array[0], array[1]);
		}
		Dbgl($"Loaded {seedsDic.Count} seeds from {text}");
	}

	private static IEnumerator SpawnTree(GameObject prefab, Vector3 position)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		Dbgl("spawning new tree");
		yield return (object)new WaitForSeconds(respawnDelay.Value);
		Object.Instantiate<GameObject>(prefab, position, Quaternion.identity);
		Dbgl("created new " + ((Object)prefab).name);
	}
}
internal class SeedData
{
	public List<string> seeds = new List<string>();
}