Decompiled source of FileAccessPlugin v1.7.2

FileAccessPlugin.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using Newtonsoft.Json;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FileAccessPlugin")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FileAccessPlugin")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("FileAccessPlugin")]
[assembly: ComVisible(false)]
[assembly: Guid("c303405d-e66c-4316-9cdb-4e3ca15c6360")]
[assembly: AssemblyFileVersion("1.7.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.7.2.0")]
namespace LordAshes;

[BepInPlugin("org.lordashes.plugins.fileaccess", "File Access Plugin", "1.7.2.0")]
public class FileAccessPlugin : BaseUnityPlugin
{
	public static class AssetBundle
	{
		public static AssetBundle Load(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			return AssetBundle.LoadFromMemory(File.ReadAllBytes(source, cacheSettings));
		}
	}

	public static class Cache
	{
		public static void ReloadCache()
		{
			CacheType cacheType = FileAccessPlugin.cacheType;
			SetCacheStyle(CacheType.CacheFullListing);
			FileAccessPlugin.cacheType = cacheType;
		}

		public static void SetCacheStyle(CacheType cacheSettings)
		{
			cache.Clear();
			GetFolders(ref cache, dirPlugin, cacheSettings == CacheType.CacheCustomData || cacheSettings == CacheType.NoCacheCustomData);
			if (useDirCommon)
			{
				GetFiles(ref cache, dirCommon);
			}
			cacheType = cacheSettings;
		}

		private static void GetFolders(ref List<string> files, string root, bool limit = false)
		{
			foreach (string item in from s in Directory.EnumerateDirectories(root)
				orderby s.ToString()
				select s)
			{
				GetFiles(ref files, item, limit);
			}
		}

		private static void GetFiles(ref List<string> files, string root, bool limit = false)
		{
			if (!limit)
			{
				foreach (string item in from s in Directory.EnumerateFiles(root)
					orderby s.ToString()
					select s)
				{
					files.Add(item.Replace("\\", "/"));
				}
				{
					foreach (string item2 in from s in Directory.EnumerateDirectories(root)
						orderby s.ToString()
						select s)
					{
						GetFiles(ref files, item2);
					}
					return;
				}
			}
			if (Directory.Exists(root + "/Assets"))
			{
				GetFiles(ref files, root + "/Assets");
			}
			if (Directory.Exists(root + "/CustomData"))
			{
				GetFiles(ref files, root + "/CustomData");
			}
		}
	}

	public static class File
	{
		public static void AppendAllText(string source, string content, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						Debug.LogWarning((object)"File Access Plugin: Append Is Not Supported For Non Local File Sources. Effects Will Be Same As Write.");
						webClient.UploadString(source.Replace("\\", "/"), content);
						return;
					}
					catch (Exception innerException)
					{
						Debug.LogException(new Exception("File Access Plugin: AppendAllText(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
						return;
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					System.IO.File.AppendAllText(Find(source, cacheSettings)[0], content);
				}
				else
				{
					System.IO.File.AppendAllText(source, content);
				}
			}
			catch (Exception innerException2)
			{
				Debug.LogException(new Exception("File Access Plugin: AppendAllText(File) Exception (" + source + ")", innerException2));
			}
		}

		public static string ReadAllText(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using URL: " + source.Replace("\\", "/")));
				}
				using WebClient webClient = new WebClient();
				try
				{
					return webClient.DownloadString(source.Replace("\\", "/"));
				}
				catch (Exception ex)
				{
					Debug.LogException(new Exception("File Access Plugin: ReadAllText(URL) Exception (" + source.Replace("\\", "/") + ")", ex));
					return ex.ToString();
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using (Partial) File Name: " + source));
					}
					return System.IO.File.ReadAllText(Find(source, cacheSettings)[0]);
				}
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using Full Path File Name: " + source));
				}
				return System.IO.File.ReadAllText(source);
			}
			catch (Exception ex2)
			{
				Debug.LogException(new Exception("File Access Plugin: ReadAllText(File) Exception (" + source + ")", ex2));
				return ex2.ToString();
			}
		}

		public static void WriteAllText(string source, string content, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						webClient.UploadString(source.Replace("\\", "/"), content);
						return;
					}
					catch (Exception innerException)
					{
						Debug.LogException(new Exception("File Access Plugin: WriteAllText(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
						return;
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					System.IO.File.WriteAllText(Find(source, cacheSettings)[0], content);
				}
				else
				{
					System.IO.File.WriteAllText(source, content);
				}
			}
			catch (Exception innerException2)
			{
				Debug.LogException(new Exception("File Access Plugin: WriteAllText(File) Exception (" + source + ")", innerException2));
			}
		}

		public static void AppendAllLines(string source, string[] content, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						Debug.LogWarning((object)"File Access Plugin: Append Is Not Supported For Non Local File Sources. Effects Will Be Same As Write.");
						webClient.UploadString(source.Replace("\\", "/"), string.Join("\r\n", content));
						return;
					}
					catch (Exception innerException)
					{
						Debug.LogException(new Exception("File Access Plugin: AppendAllLines(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
						return;
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using (Partial) File Name: " + source));
					}
					System.IO.File.AppendAllLines(Find(source, cacheSettings)[0], content);
				}
				else
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using Full Path File Name: " + source));
					}
					System.IO.File.AppendAllLines(source, content);
				}
			}
			catch (Exception innerException2)
			{
				Debug.LogException(new Exception("File Access Plugin: AppendAllLines(File) Exception (" + source + ")", innerException2));
			}
		}

		public static string[] ReadAllLines(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						return webClient.DownloadString(source.Replace("\\", "/").Replace("\r\n", "\n")).Split(new char[1] { '\n' });
					}
					catch (Exception ex)
					{
						Debug.LogException(new Exception("File Access Plugin: ReadAllLines(URL) Exception (" + source.Replace("\\", "/") + ")", ex));
						return new string[2]
						{
							ex.ToString(),
							ex.InnerException.ToString()
						};
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using (Partial) File Name: " + source));
					}
					return System.IO.File.ReadAllLines(Find(source, cacheSettings)[0]);
				}
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using Full Path File Name: " + source));
				}
				return System.IO.File.ReadAllLines(source);
			}
			catch (Exception ex2)
			{
				Debug.LogException(new Exception("File Access Plugin: ReadAllLines(File) Exception (" + source + ")", ex2));
				return new string[2]
				{
					ex2.ToString(),
					ex2.InnerException.ToString()
				};
			}
		}

		public static void WriteAllLines(string source, string[] content, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						webClient.UploadString(source.Replace("\\", "/"), string.Join("\r\n", content));
						return;
					}
					catch (Exception innerException)
					{
						Debug.LogException(new Exception("File Access Plugin: WriteAllLines(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
						return;
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					System.IO.File.WriteAllLines(Find(source, cacheSettings)[0], content);
				}
				else
				{
					System.IO.File.WriteAllLines(source, content);
				}
			}
			catch (Exception innerException2)
			{
				Debug.LogException(new Exception("File Access Plugin: WriteAllLines(File) Exception (" + source + ")", innerException2));
			}
		}

		public static byte[] ReadAllBytes(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						return webClient.DownloadData(source.Replace("\\", "/"));
					}
					catch (Exception ex)
					{
						Debug.LogException(new Exception("File Access Plugin: ReadAllBytes(URL) Exception (" + source.Replace("\\", "/") + ")", ex));
						Debug.LogException(ex);
						return new byte[0];
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using (Partial) File Name: " + source));
					}
					return System.IO.File.ReadAllBytes(Find(source, cacheSettings)[0]);
				}
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using Full Path File Name: " + source));
				}
				return System.IO.File.ReadAllBytes(source);
			}
			catch (Exception innerException)
			{
				Debug.LogException(new Exception("File Access Plugin: ReadAllBytes(File) Exception (" + source + ")", innerException));
				return new byte[0];
			}
		}

		public static void WriteAllBytes(string source, byte[] content, CacheType cacheSettings = CacheType.NoChange)
		{
			if (cacheSettings == CacheType.NoChange)
			{
				cacheSettings = cacheType;
			}
			string protocol = GetProtocol(source);
			if (protocol != "")
			{
				using (WebClient webClient = new WebClient())
				{
					try
					{
						webClient.UploadData(source.Replace("\\", "/"), content);
						return;
					}
					catch (Exception innerException)
					{
						Debug.LogException(new Exception("File Access Plugin: WriteAllBytes(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
						return;
					}
				}
			}
			try
			{
				if (source.Substring(1, 1) != ":")
				{
					System.IO.File.WriteAllBytes(Find(source, cacheSettings)[0], content);
				}
				else
				{
					System.IO.File.WriteAllBytes(source, content);
				}
			}
			catch (Exception innerException2)
			{
				Debug.LogException(new Exception("File Access Plugin: WriteAllBytes(File) Exception (" + source + ")", innerException2));
			}
		}

		public static bool Exists(string source)
		{
			if (GetProtocol(source.Replace("\\", "/")) == "")
			{
				if (source.Substring(1, 1) != ":")
				{
					if (diagnostics.Value)
					{
						Debug.Log((object)("File Access Plugin: Using (Partial) File Name: " + source));
					}
					return Find(source).Length != 0;
				}
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using Full Path File Name: " + source));
				}
				return System.IO.File.Exists(source);
			}
			try
			{
				if (diagnostics.Value)
				{
					Debug.Log((object)("File Access Plugin: Using URL: " + source.Replace("\\", "/")));
				}
				using (WebClient webClient = new WebClient())
				{
					webClient.DownloadData(source.Replace("\\", "/"));
				}
				return true;
			}
			catch (Exception innerException)
			{
				if (diagnostics.Value)
				{
					Debug.LogException(new Exception("File Access Plugin: Exists(URL) Exception (" + source.Replace("\\", "/") + ")", innerException));
				}
				return false;
			}
		}

		public static string[] Find(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			string[] array;
			if (GetProtocol(source) == "" && source.Substring(1, 1) != ":")
			{
				if (cacheSettings == CacheType.NoChange)
				{
					cacheSettings = cacheType;
				}
				if (cacheSettings == CacheType.NoCacheFullListing || cacheSettings == CacheType.NoCacheCustomData || cacheSettings != cacheType)
				{
					Cache.SetCacheStyle(cacheSettings);
				}
				Regex regEx = new Regex(Regex.Escape(source.Replace("\\", "/")), RegexOptions.IgnoreCase);
				array = cache.Where((string item) => regEx.IsMatch(item)).ToArray();
				if (diagnostics.Value)
				{
					string[] array2 = array;
					foreach (string text in array2)
					{
						Debug.Log((object)("FindFile('" + source + "'," + cacheSettings.ToString() + ") found '" + text + "'"));
					}
					Debug.Log((object)("FindFile('" + source + "'," + cacheSettings.ToString() + ") found " + array.Length + " results"));
				}
			}
			else
			{
				array = new string[1] { source };
			}
			return array;
		}

		[Obsolete]
		public static void SetCacheType(CacheType cacheSettings)
		{
			Debug.LogWarning((object)"File Access Plugin: SetCacheType is obsolete. Use File Access Plugin to set the cache type for all plugins.");
		}

		public static string[] Catalog(bool extendedInfo = false)
		{
			List<string> list = new List<string>();
			foreach (string item in cache)
			{
				string text = "";
				text = ((!item.Contains("TaleSpire_CustomData")) ? ((!item.Contains("/Assets/")) ? ((!item.Contains("/CustomData/")) ? ("|" + item) : ((!extendedInfo) ? item.Substring(item.IndexOf("CustomData")) : (item.Substring(item.IndexOf("CustomData")) + " (" + item.Substring(0, item.IndexOf("CustomData")) + ")"))) : ((!extendedInfo) ? item.Substring(item.IndexOf("Assets")) : (item.Substring(item.IndexOf("Assets")) + " (" + item.Substring(0, item.IndexOf("Assets")) + ")"))) : ((!extendedInfo) ? item.Substring(item.IndexOf("TaleSpire_CustomData")) : (item.Substring(item.IndexOf("TaleSpire_CustomData")) + " (" + item.Substring(0, item.IndexOf("TaleSpire_CustomData")) + ")")));
				list.Add(text);
			}
			list.Sort();
			return list.ToArray();
		}
	}

	public enum CacheType
	{
		NoCacheFullListing = 0,
		NoCacheCustomData = 1,
		CacheFullListing = 2,
		CacheCustomData = 3,
		NoChange = 999
	}

	public static class Image
	{
		public static Texture2D LoadTexture(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Expected O, but got Unknown
			Texture2D val = new Texture2D(1, 1);
			ImageConversion.LoadImage(val, File.ReadAllBytes(source, cacheSettings));
			return val;
		}

		public static Sprite LoadSprite(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = LoadTexture(source, cacheSettings);
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
		}
	}

	public static class Plugin
	{
		public static string WhoAmI(string pluginName)
		{
			return Type.GetType(pluginName, throwOnError: false, ignoreCase: true).Assembly.Location;
		}

		public static string Location(string pluginName)
		{
			string location = Type.GetType(pluginName, throwOnError: false, ignoreCase: true).Assembly.Location;
			return location.Substring(0, location.LastIndexOf("\\"));
		}

		public static Dictionary<string, object> Manifest(string pluginName)
		{
			Dictionary<string, object> result = new Dictionary<string, object>();
			if (File.Exists(Location(pluginName) + "\\manifest.json"))
			{
				result = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(Location(pluginName) + "\\manifest.json"));
			}
			return result;
		}
	}

	public static class Resource
	{
		public static Font LoadFont(string source, CacheType cacheSettings = CacheType.NoChange)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			return (Font)Resources.Load(File.Find(source, cacheSettings)[0], typeof(Font));
		}
	}

	public const string Name = "File Access Plugin";

	public const string Guid = "org.lordashes.plugins.fileaccess";

	public const string Version = "1.7.2.0";

	public const string Author = "Lord Ashes";

	private static string dirPlugin = Paths.PluginPath;

	private static string dirCommon = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/")) + "/TaleSpire_CustomData";

	private ConfigEntry<KeyboardShortcut> triggerKey;

	public static ConfigEntry<bool> diagnostics;

	private static bool useDirCommon = false;

	private static List<string> cache = new List<string>();

	private static CacheType cacheType = CacheType.CacheCustomData;

	private void Awake()
	{
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		useDirCommon = Directory.Exists(dirCommon);
		Debug.Log((object)("File Access Plugin: " + dirCommon + " " + (useDirCommon ? "is" : "is not") + " present"));
		triggerKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Dump Asset Catalog", new KeyboardShortcut((KeyCode)47, (KeyCode[])(object)new KeyCode[1] { (KeyCode)306 }), (ConfigDescription)null);
		diagnostics = ((BaseUnityPlugin)this).Config.Bind<bool>("Diagnostics", "Include diagnostic logs", false, (ConfigDescription)null);
		cacheType = ((BaseUnityPlugin)this).Config.Bind<CacheType>("Settings", "Cache", CacheType.CacheCustomData, (ConfigDescription)null).Value;
		Cache.SetCacheStyle(cacheType);
	}

	private void Update()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		KeyboardShortcut value = triggerKey.Value;
		if (((KeyboardShortcut)(ref value)).IsUp())
		{
			Debug.Log((object)"File Access Plugin: Asset Catalog:");
			string[] array = File.Catalog();
			foreach (string text in array)
			{
				Debug.Log((object)text);
			}
		}
	}

	public static string GetProtocol(string source)
	{
		if (!source.Contains(":"))
		{
			return "";
		}
		if (source.Substring(1, 1) == ":")
		{
			return "";
		}
		return source.Substring(0, source.IndexOf(":"));
	}
}