Decompiled source of AssetPlacer v0.1.0

Tomatobird.AssetPlacer.dll

Decompiled 6 hours ago
using System;
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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Tomatobird.AssetPlacer")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+fbd6a1f387bdf1fa9d5e9604bc3815faead57b15")]
[assembly: AssemblyProduct("AssetPlacer")]
[assembly: AssemblyTitle("Tomatobird.AssetPlacer")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.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;
		}
	}
}
namespace AssetPlacer
{
	[BepInPlugin("Tomatobird.AssetPlacer", "AssetPlacer", "0.1.0")]
	public class AssetPlacer : BaseUnityPlugin
	{
		internal static string objectPrefix = "TBAssetPlacer_";

		private static string contentFolder = "AssetPlacer";

		public static AssetPlacer Instance { get; private set; } = null;


		internal static ManualLogSource Logger { get; private set; } = null;


		internal Dictionary<int, string[]> PrefabSettings { get; private set; } = new Dictionary<int, string[]>();


		internal static List<GameObject> GameObjects { get; private set; } = new List<GameObject>();


		private string ConfigContentPath => Path.Combine(Paths.ConfigPath, "AssetPlacer");

		private void Awake()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Instance = this;
			int num = ((BaseUnityPlugin)this).Config.Bind<int>("General", "PrefabCount", 1, new ConfigDescription("How many prefab configurations should be created?", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 1000), Array.Empty<object>())).Value + 1;
			bool value = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UseConfigFolder", true, "Should assets be looked up from the config folder? Creates the folder if doesnt exist yet.").Value;
			bool value2 = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsePluginsFolder", true, "Should assets be looked up from the plugins folder?").Value;
			contentFolder = ((BaseUnityPlugin)this).Config.Bind<string>("General", "FolderName", "AssetPlacer", "Name of folder to look for assets from in plugins.").Value;
			Logger.LogDebug((object)("ConfigContent path is: " + ConfigContentPath));
			if (value && !CreatePaths(ConfigContentPath))
			{
				AddAssets(ConfigContentPath);
			}
			if (value2)
			{
				string[] directories = Directory.GetDirectories(Paths.PluginPath, contentFolder, SearchOption.AllDirectories);
				string[] array = directories;
				foreach (string text in array)
				{
					Logger.LogDebug((object)("Adding assets from " + text));
					AddAssets(text);
				}
			}
			for (int j = 1; j < num; j++)
			{
				string value3 = ((BaseUnityPlugin)this).Config.Bind<string>($"Prefab{j}", $"Prefab{j.ToString()}Name", "name", $"Name of Prefab {j.ToString()}. Names of the prefabs can be seen ").Value;
				string value4 = ((BaseUnityPlugin)this).Config.Bind<string>($"Prefab{j}", $"Prefab{j.ToString()}SceneList", "CompanyBuilding", $"Scenes that Prefab {j.ToString()} should spawn on. ex: (CompanyBuilding, Level2Assurance)").Value;
				string value5 = ((BaseUnityPlugin)this).Config.Bind<string>($"Prefab{j}", $"Prefab{j.ToString()}Position", "0, 0, 0", $"The position of Prefab {j.ToString()} (x, y, z)").Value;
				string value6 = ((BaseUnityPlugin)this).Config.Bind<string>($"Prefab{j}", $"Prefab{j.ToString()}Rotation", "0, 0, 0", $"The rotation of Prefab {j.ToString()} (x, y, z)").Value;
				string value7 = ((BaseUnityPlugin)this).Config.Bind<string>($"Prefab{j}", $"Prefab{j.ToString()}Scale", "1, 1, 1", $"The scale of Prefab {j.ToString()} (x, y, z)").Value;
				PrefabSettings.Add(j, new string[5] { value3, value4, value5, value6, value7 });
			}
			Logger.LogDebug((object)("GameObjects in array: " + GameObjects.Count()));
			Logger.LogDebug((object)("PrefabSettings in dictionary: " + PrefabSettings.Count()));
			if (GameObjects.Count() != 0 && PrefabSettings.Count() != 0 && (value || value2))
			{
				SceneManager.sceneLoaded += onSceneLoaded;
				SceneManager.sceneUnloaded += onSceneUnloaded;
			}
			Logger.LogInfo((object)"Tomatobird.AssetPlacer v0.1.0 has loaded!");
		}

		private void AddAssets(string path)
		{
			try
			{
				string[] files = Directory.GetFiles(path);
				Logger.LogDebug((object)"Iterating assets");
				string[] array = files;
				foreach (string text in array)
				{
					Logger.LogDebug((object)("Iterating... " + text));
					GameObject[] array2 = AssetBundle.LoadFromFile(Path.Combine(path, text)).LoadAllAssets<GameObject>();
					Logger.LogDebug((object)("Assets length: " + array2.Length));
					GameObject[] array3 = array2;
					foreach (GameObject val in array3)
					{
						Logger.LogInfo((object)$"Inserting '{((Object)val).name}' to the GameObjects list.");
						GameObjects.Add(val);
					}
				}
			}
			catch (Exception ex)
			{
				Logger.LogError((object)("There was an exception while trying to iterate through the files to add assets to placeable assets list. " + ex.Message));
			}
		}

		private bool CreatePaths(string path)
		{
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
				Logger.LogInfo((object)"AssetBundles directory created.");
				return true;
			}
			return false;
		}

		private void onSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00d2: Unknown result type (might be due to invalid IL or missing references)
			Logger.LogDebug((object)("Scene Loaded: " + ((Scene)(ref scene)).name));
			foreach (KeyValuePair<int, string[]> prefabSetting in PrefabSettings)
			{
				if (!prefabSetting.Value[1].Contains(((Scene)(ref scene)).name))
				{
					continue;
				}
				foreach (GameObject gameObject in GameObjects)
				{
					if (prefabSetting.Value[0] == ((Object)gameObject).name)
					{
						Quaternion identity = Quaternion.identity;
						((Quaternion)(ref identity)).eulerAngles = ParseVector3FromString(prefabSetting.Value[3]);
						GameObject val = Object.Instantiate<GameObject>(gameObject, ParseVector3FromString(prefabSetting.Value[2]), identity);
						val.transform.localScale = ParseVector3FromString(prefabSetting.Value[4]);
						((Object)val).name = objectPrefix + ((Object)val).name;
					}
				}
			}
		}

		private void onSceneUnloaded(Scene scene)
		{
			GameObject[] array = Object.FindObjectsOfType<GameObject>();
			GameObject[] array2 = array;
			foreach (GameObject val in array2)
			{
				if (((Object)val).name.Contains(objectPrefix))
				{
					Logger.LogDebug((object)("Destroying object: " + ((Object)val).name));
					Object.Destroy((Object)(object)val);
				}
			}
		}

		private static Vector3 ParseVector3FromString(string s)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				string[] array = s.Split(',');
				return new Vector3(float.Parse(array[0].Trim()), float.Parse(array[1].Trim()), float.Parse(array[2].Trim()));
			}
			catch (IndexOutOfRangeException ex)
			{
				Logger.LogError((object)("Specified string could not be parsed for a Vector3. Please use the correct format. Example: '-14.6, 4.6, 0' " + ex.Message));
			}
			catch (ArgumentOutOfRangeException ex2)
			{
				Logger.LogError((object)("Specified string could not be parsed for a Vector3. Please use the correct format. Example: '-14.6, 4.6, 0' " + ex2.Message));
			}
			catch (ArgumentNullException ex3)
			{
				Logger.LogError((object)("ArgumentNullException while trying to parse Vector3. " + ex3.Message));
			}
			catch (FormatException ex4)
			{
				Logger.LogError((object)("FormatException while trying to parse Vector3. " + ex4.Message));
			}
			return default(Vector3);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Tomatobird.AssetPlacer";

		public const string PLUGIN_NAME = "AssetPlacer";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}