Decompiled source of YggdrasilTerrain v1.0.0

plugins\YggdrasilTerrain.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using YggdrasilTerrain.Commands;
using YggdrasilTerrain.Config;
using YggdrasilTerrain.Patches;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("YggdrasilTerrain")]
[assembly: AssemblyDescription("Terrain mod allowing players to walk and build on the massive Yggdrassil Branch in the sky. Requires access to the branch and the ability to build above normal heights.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Virtualize LLC")]
[assembly: AssemblyProduct("YggdrasilTerrain")]
[assembly: AssemblyCopyright("Copyright © 2023-2024, GNU-v3 licensed")]
[assembly: Guid("7155A165-0000-40A7-8CA1-3E6B6CD7CB45")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
public abstract class YggdrasilBranch
{
	private static int? _originalBranchLayer;

	private static GameObject? _branchInstance;

	public static GameObject? BranchInstance
	{
		get
		{
			if ((Object)(object)_branchInstance != (Object)null)
			{
				return _branchInstance;
			}
			_branchInstance = GetBranchObject();
			return _branchInstance;
		}
	}

	private static MeshCollider GetOrAddMeshCollider(GameObject gameObject)
	{
		MeshCollider component = gameObject.GetComponent<MeshCollider>();
		if (Object.op_Implicit((Object)(object)component))
		{
			return component;
		}
		return gameObject.AddComponent<MeshCollider>();
	}

	private static void AddMeshCollider(GameObject gameObject)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		MeshFilter component = gameObject.GetComponent<MeshFilter>();
		if (Object.op_Implicit((Object)(object)component))
		{
			MeshCollider orAddMeshCollider = GetOrAddMeshCollider(gameObject);
			if (Object.op_Implicit((Object)(object)orAddMeshCollider))
			{
				orAddMeshCollider.convex = false;
				orAddMeshCollider.sharedMesh = component.sharedMesh;
				((Collider)orAddMeshCollider).includeLayers = LayerMask.op_Implicit(YggdrasilConfig.BranchCollisionLayerMask.Value);
			}
		}
	}

	public static void RemoveMeshCollider(GameObject gameObject)
	{
		MeshCollider component = gameObject.GetComponent<MeshCollider>();
		if (Object.op_Implicit((Object)(object)component))
		{
			Object.Destroy((Object)(object)component);
		}
	}

	public static void OnSceneReady()
	{
		OnBranchCollisionChange(YggdrasilConfig.AllowCollisionsOnYggdrasilBranch.Value);
	}

	private static GameObject? GetBranchObject()
	{
		GameObject[] array = Resources.FindObjectsOfTypeAll<GameObject>();
		GameObject val = null;
		GameObject[] array2 = array;
		foreach (GameObject val2 in array2)
		{
			if (!((Object)(object)val2.transform.parent == (Object)null))
			{
				if (((Object)val2).name == "YggdrasilBranch")
				{
					Transform obj = val2.transform.Find("branch");
					val = ((obj != null) ? ((Component)obj).gameObject : null);
					break;
				}
				if (((Object)val2.transform.parent).name == "YggdrassilBranch" && ((Object)val2).name == "branch")
				{
					val = val2;
					break;
				}
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogError((object)"Could not find branchGameObject");
		}
		return val;
	}

	private static void RemoveBranchCollision()
	{
		if (!((Object)(object)BranchInstance == (Object)null))
		{
			if (_originalBranchLayer.HasValue && BranchInstance.layer != _originalBranchLayer)
			{
				BranchInstance.layer = _originalBranchLayer.Value;
			}
			RemoveMeshCollider(BranchInstance);
		}
	}

	private static void AddBranchCollision()
	{
		if (!((Object)(object)BranchInstance == (Object)null))
		{
			int valueOrDefault = _originalBranchLayer.GetValueOrDefault();
			if (!_originalBranchLayer.HasValue)
			{
				valueOrDefault = BranchInstance.layer;
				_originalBranchLayer = valueOrDefault;
			}
			AddMeshCollider(BranchInstance);
			BranchInstance.layer = YggdrasilConfig.BranchLayer.Value;
		}
	}

	private static void OnBranchCollisionChange(bool val)
	{
		if (val)
		{
			AddBranchCollision();
		}
		else
		{
			RemoveBranchCollision();
		}
	}

	public static void OnBranchCollisionChange(object sender, EventArgs e)
	{
		OnBranchCollisionChange(YggdrasilConfig.AllowCollisionsOnYggdrasilBranch.Value);
	}
}
namespace YggdrasilTerrain
{
	[BepInPlugin("zolantris.YggdrasilTerrain", "YggdrasilTerrain", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class YggdrasilTerrainPlugin : BaseUnityPlugin
	{
		public const string Author = "zolantris";

		public const string Version = "1.0.0";

		public const string ModName = "YggdrasilTerrain";

		public const string ModGuid = "zolantris.YggdrasilTerrain";

		public const string HarmonyGuid = "zolantris.YggdrasilTerrain";

		private static Harmony _harmony;

		public const string ModDescription = "Terrain mod allowing players to walk and build on the massive Yggdrassil Branch in the sky. Requires access to the branch and the ability to build above normal heights.";

		public const string CopyRight = "Copyright © 2023-2024, GNU-v3 licensed";

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			YggdrasilConfig.BindConfig(((BaseUnityPlugin)this).Config);
			_harmony = new Harmony("zolantris.YggdrasilTerrain");
			_harmony.PatchAll(typeof(ZNetScene_Patch));
			RegisterCommands();
		}

		public void RegisterCommands()
		{
			CommandManager.Instance.AddConsoleCommand((ConsoleCommand)(object)new YggdrasilTerrainCommands());
		}
	}
}
namespace YggdrasilTerrain.Patches
{
	public class ZNetScene_Patch
	{
		[HarmonyPatch(typeof(ZNetScene), "Awake")]
		[HarmonyPostfix]
		private static void ZNetScene_Start()
		{
			YggdrasilBranch.OnSceneReady();
		}
	}
}
namespace YggdrasilTerrain.Config
{
	public static class YggdrasilConfig
	{
		public static List<string> collisionLayerNames = new List<string>(8) { "Default", "character", "piece", "terrain", "static_solid", "Default_small", "character_net", "vehicle" };

		private static readonly int DefaultCollisionMask = LayerMask.GetMask(collisionLayerNames.ToArray());

		private const string DefaultCollisionLayer = "piece";

		private const string ConfigSection = "Main";

		private const string LayersSection = "Layers";

		private const string LayerOverridesName = "LayerOverridesEnabled";

		private const string LayersSectionSharedDescription = "Will not apply an override unless Layers.$LayerOverridesEnabled = true";

		public static ConfigFile? Config { get; private set; }

		public static ConfigEntry<bool> AllowCollisionsOnYggdrasilBranch { get; private set; } = null;


		public static ConfigEntry<int> BranchCollisionLayerMask { get; private set; } = null;


		public static ConfigEntry<int> BranchLayer { get; private set; } = null;


		public static ConfigEntry<bool> Debug { get; private set; } = null;


		public static ConfigEntry<bool> OverridesSectionEnabled { get; private set; } = null;


		public static void BindConfig(ConfigFile config)
		{
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Expected O, but got Unknown
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Expected O, but got Unknown
			Config = config;
			int num = LayerMask.NameToLayer("piece");
			if (num < 0)
			{
				Logger.LogError((object)"The default collision layer piece call of LayerMask.NameToLayer failed to return a valid result, due to this issue it will default the layer to 0. Please consider overriding the layer or contacting the mod owner to fix this. This error should not happen and likely something has destructively re-named a fundamental layer value.");
			}
			AllowCollisionsOnYggdrasilBranch = config.Bind<bool>("Main", "AllowCollisionsOnYggdrasilBranch", true, ConfigHelpers.CreateConfigDescription("Allows collisions on the Yggdrasil branch. Toggling will add/remove this behavior.", isAdmin: true));
			Debug = config.Bind<bool>("Main", "Debug", false, ConfigHelpers.CreateConfigDescription("Enables debug logging and features, useful for debugging the mod, does NOT allow exploits.", isAdmin: true, isAdvanced: true));
			OverridesSectionEnabled = config.Bind<bool>("Layers", "LayerOverridesEnabled", false, ConfigHelpers.CreateConfigDescription("Enables allows overrides for collisions and other values for mod compatibility. Do not enable this unless you need to otherwise values set within overrides will require a reset of the config.", isAdmin: true, isAdvanced: true));
			BranchCollisionLayerMask = config.Bind<int>("Layers", "BranchCollisionLayerMask", DefaultCollisionMask, ConfigHelpers.CreateConfigDescription("Controls what game layers can be set as collisions. Will not apply an override unless Layers.$LayerOverridesEnabled = true", isAdmin: true, isAdvanced: true));
			BranchLayer = config.Bind<int>("Layers", "BranchLayer", LayerMask.NameToLayer("piece"), new ConfigDescription("Controls what layer the branch is on. This will affect building, do not change this unless you know what you are doing. Default should be piece. Will not apply an override unless Layers.$LayerOverridesEnabled = true", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 31), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = true
			} }));
			if (!OverridesSectionEnabled.Value)
			{
				BranchCollisionLayerMask.Value = DefaultCollisionMask;
				BranchLayer.Value = ((num >= 0) ? num : 10);
			}
			AllowCollisionsOnYggdrasilBranch.SettingChanged += YggdrasilBranch.OnBranchCollisionChange;
			if (Debug.Value)
			{
				Logger.LogDebug((object)$"collisionLayer defaultLayerNames: {collisionLayerNames} CollisionMask: ${DefaultCollisionMask}");
				Logger.LogDebug((object)string.Format("BranchLayer NameToLayer {0}, defaultLayerName: {1} defaultLayerValue: {2}", BranchLayer.Value, "piece", LayerMask.NameToLayer("piece")));
			}
		}
	}
	public class ConfigHelpers
	{
		public static ConfigDescription CreateConfigDescription(string description, bool isAdmin = false, bool isAdvanced = false)
		{
			//IL_000a: 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)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			return new ConfigDescription(description, (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = isAdmin,
				IsAdvanced = isAdvanced
			} });
		}
	}
}
namespace YggdrasilTerrain.Commands
{
	public class YggdrasilTerrainCommands : ConsoleCommand
	{
		private const string teleportCommand = "teleport";

		private Vector3 defaultYggLocation = new Vector3(9947.041f, 2118.809f, 500.9553f);

		public override string Help => OnHelp();

		public override string Name => "yggdrasilTerrain";

		private static string OnHelp()
		{
			return "Yggdrassil Terrain Mod CLI: \n<teleport>: teleports the player to the yggdrasil branch section nearest to them requires admin access";
		}

		public override void Run(string[] args)
		{
			ParseFirstArg(args);
		}

		private void ParseFirstArg(string[] args)
		{
			string text = args.First();
			if (text == null)
			{
				Logger.LogMessage((object)"Must provide a argument for `vehicle` command");
			}
			else if (text == "teleport")
			{
				Teleport(args.Skip(1).ToArray());
			}
		}

		public void Teleport(string[] args)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (!SynchronizationManager.Instance.PlayerIsAdmin)
			{
				Logger.LogMessage((object)"Player is not admin, cannot teleport to Yggdrasil branch");
			}
			else if (args.Length == 0)
			{
				((Character)Player.m_localPlayer).TeleportTo(defaultYggLocation, ((Component)Player.m_localPlayer).transform.rotation, true);
			}
			else
			{
				Logger.LogMessage((object)"Teleport commands do not currently support direct coordinates as missing a teleport will likely kill the player , use the tp command + anti-falling and add values above or near x -3652 to 10000, y~=2118.809f +-1140, +- 500f z");
			}
		}

		public override List<string> CommandOptionList()
		{
			return new List<string>(1) { "teleport" };
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		internal IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}