Decompiled source of LethalWithFriends v0.0.2

patchers/LcPluginInstaller.dll

Decompiled 10 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
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 Mono.Cecil;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LcPluginInstaller")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("LcPluginInstaller")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: AssemblyInformationalVersion("0.0.2")]
[assembly: AssemblyProduct("LcPluginInstaller")]
[assembly: AssemblyTitle("LcPluginInstaller")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.2.0")]
[module: UnverifiableCode]
namespace LcPluginInstaller;

public static class Installer
{
	public static class Settings
	{
		public static ConfigFile Config;

		public static ConfigEntry<string> PluginRepositoryUrl;

		public static ConfigEntry<string> PluginListId;

		public static void Load()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			Config = new ConfigFile(Path.Combine(Paths.ConfigPath, "LcPluginInstaller.cfg"), true);
			PluginRepositoryUrl = Config.Bind<string>("General", "PluginRepositoryUrl", (string)null, (ConfigDescription)null);
			PluginListId = Config.Bind<string>("General", "PluginListId", (string)null, (ConfigDescription)null);
		}
	}

	private enum MaskPluginName
	{
		None,
		Abbreviation1,
		Abbreviation2,
		Abbreviation3
	}

	private static string repositoryUrl;

	private static string pluginListId;

	private static MaskPluginName maskPluginName = MaskPluginName.Abbreviation2;

	private static string downloadDirectory;

	private static bool installed = false;

	public static void RunSync()
	{
		if (installed)
		{
			return;
		}
		installed = true;
		ILogListener preloaderConsoleListener = LcPluginInstaller.PreloaderConsoleListener;
		bool flag = preloaderConsoleListener != null && !Logger.Listeners.Contains(preloaderConsoleListener);
		if (flag)
		{
			Logger.Listeners.Add(preloaderConsoleListener);
		}
		try
		{
			RunSyncWithLogging();
		}
		finally
		{
			if (flag)
			{
				Logger.Listeners.Remove(preloaderConsoleListener);
			}
		}
	}

	private static void RunSyncWithLogging()
	{
		Settings.Load();
		downloadDirectory = Path.Combine(Path.GetTempPath(), "LcPluginInstallerDownloads");
		repositoryUrl = Settings.PluginRepositoryUrl.Value;
		if (string.IsNullOrWhiteSpace(repositoryUrl))
		{
			LcPluginInstaller.Log.LogInfo((object)"The repository url is missing, unable to download any plugins");
			return;
		}
		pluginListId = Settings.PluginListId.Value;
		if (string.IsNullOrWhiteSpace(pluginListId))
		{
			LcPluginInstaller.Log.LogInfo((object)"The plugin list id is missing, unable to download any plugins");
			return;
		}
		try
		{
			IEnumerator enumerator = Run();
			while (enumerator.MoveNext())
			{
			}
		}
		catch (Exception ex)
		{
			LcPluginInstaller.Log.LogError((object)ex);
		}
	}

	public static IEnumerator Run()
	{
		LcPluginInstaller.Log.LogInfo((object)"Running LcPluginInstaller");
		string pluginsDirectory = Path.Combine(Paths.PluginPath, "LcPluginInstaller/Plugins");
		string configsDirectory = Paths.ConfigPath;
		Directory.CreateDirectory(pluginsDirectory);
		List<string> pluginsInstalled = new List<string>();
		string[] directories = Directory.GetDirectories(pluginsDirectory);
		for (int i = 0; i < directories.Length; i++)
		{
			string fileName = Path.GetFileName(directories[i]);
			if (!string.IsNullOrWhiteSpace(fileName))
			{
				pluginsInstalled.Add(fileName);
			}
		}
		yield return null;
		UnityWebRequest request3 = UnityWebRequest.Get(repositoryUrl + "/" + pluginListId);
		List<string> list;
		try
		{
			yield return request3.SendWebRequest();
			while (!request3.isDone)
			{
				yield return null;
			}
			Result result = request3.result;
			if (result - 2 <= 2)
			{
				LcPluginInstaller.Log.LogError((object)("Failed to get plugin list, try again later. " + request3.error));
				yield break;
			}
			list = (from line in request3.downloadHandler.text.Trim().Split("\n")
				where !string.IsNullOrWhiteSpace(line)
				where !line.StartsWith("#")
				select line).ToList();
		}
		finally
		{
			((IDisposable)request3)?.Dispose();
		}
		List<string> pluginsToRemove = new List<string>();
		List<string> pluginsToDownload = new List<string>();
		List<string> pluginsToUpdateConfig = new List<string>();
		foreach (string item in list)
		{
			pluginsToUpdateConfig.Add(item);
			if (!pluginsInstalled.Contains(item))
			{
				pluginsToDownload.Add(item);
			}
		}
		foreach (string item2 in pluginsInstalled)
		{
			if (!list.Contains(item2))
			{
				pluginsToRemove.Add(item2);
			}
		}
		if (maskPluginName != 0)
		{
			LcPluginInstaller.Log.LogInfo((object)"Plugin names are obfuscated to avoid spoilers!");
		}
		if (pluginsToRemove.Count > 0)
		{
			LcPluginInstaller.Log.LogInfo((object)"Plugins to remove:");
			foreach (string item3 in pluginsToRemove)
			{
				LcPluginInstaller.Log.LogInfo((object)("   " + DisplayPluginName(item3)));
			}
		}
		if (pluginsToDownload.Count > 0)
		{
			LcPluginInstaller.Log.LogInfo((object)"Plugins to download:");
			foreach (string item4 in pluginsToDownload)
			{
				LcPluginInstaller.Log.LogInfo((object)("   " + DisplayPluginName(item4)));
			}
		}
		if (pluginsToUpdateConfig.Count > 0)
		{
			LcPluginInstaller.Log.LogInfo((object)"Plugins to update config:");
			foreach (string item5 in pluginsToUpdateConfig)
			{
				LcPluginInstaller.Log.LogInfo((object)("   " + DisplayPluginName(item5)));
			}
		}
		Directory.CreateDirectory(downloadDirectory);
		string pluginDownloadDirectory = Path.Combine(downloadDirectory, "plugins");
		Directory.CreateDirectory(pluginDownloadDirectory);
		string pluginConfigDownloadDirectory = Path.Combine(downloadDirectory, "configs");
		Directory.CreateDirectory(pluginConfigDownloadDirectory);
		List<string> downloadedPlugins = new List<string>();
		List<string> downloadedPluginConfigs = new List<string>();
		long time = DateTime.UtcNow.Ticks;
		foreach (string plugin3 in pluginsToDownload)
		{
			LcPluginInstaller.Log.LogInfo((object)("Downloading plugin " + DisplayPluginName(plugin3)));
			string text = plugin3.Split("@")[0];
			string text2 = plugin3.Split("@")[1];
			request3 = UnityWebRequest.Get(repositoryUrl + "/Plugins/" + text + "/" + text2 + "/" + text + "@" + text2 + ".zip");
			try
			{
				yield return request3.SendWebRequest();
				while (!request3.isDone)
				{
					long ticks = DateTime.UtcNow.Ticks;
					if (ticks - time > 10000000)
					{
						time = ticks;
						int num = (int)(request3.downloadProgress * 10f);
						int num2 = (int)(request3.downloadProgress * 100f);
						string text3 = $"[{new string('#', num)}{new string('-', 10 - num)}] {num2,3}%";
						LcPluginInstaller.Log.LogInfo((object)text3);
					}
					yield return null;
				}
				Result result = request3.result;
				if (result - 2 <= 2)
				{
					LcPluginInstaller.Log.LogError((object)("Failed to download plugin " + DisplayPluginName(plugin3) + ", skipping. " + request3.error));
					continue;
				}
				using (FileStream fileStream = File.Open(GetPluginDownloadFile(plugin3), FileMode.Create, FileAccess.Write))
				{
					fileStream.Write(request3.downloadHandler.data);
				}
				downloadedPlugins.Add(plugin3);
				LcPluginInstaller.Log.LogInfo((object)"Download complete!");
			}
			finally
			{
				((IDisposable)request3)?.Dispose();
			}
		}
		foreach (string plugin3 in pluginsToUpdateConfig)
		{
			LcPluginInstaller.Log.LogInfo((object)("Downloading plugin config for " + DisplayPluginName(plugin3)));
			string text4 = plugin3.Split("@")[0];
			string text5 = plugin3.Split("@")[1];
			request3 = UnityWebRequest.Get(repositoryUrl + "/Plugins/" + text4 + "/" + text5 + "/" + text4 + "@" + text5 + ".cfg");
			try
			{
				yield return request3.SendWebRequest();
				while (!request3.isDone)
				{
					yield return null;
				}
				Result result = request3.result;
				if (result - 2 <= 2)
				{
					LcPluginInstaller.Log.LogError((object)("Failed to download plugin config for " + DisplayPluginName(plugin3) + ", skipping. " + request3.error));
					continue;
				}
				using (FileStream fileStream2 = File.Open(GetPluginConfigDownloadFile(plugin3), FileMode.Create, FileAccess.Write))
				{
					fileStream2.Write(request3.downloadHandler.data);
				}
				downloadedPluginConfigs.Add(plugin3);
				LcPluginInstaller.Log.LogInfo((object)"Download complete!");
			}
			finally
			{
				((IDisposable)request3)?.Dispose();
			}
		}
		LcPluginInstaller.Log.LogInfo((object)"Installing plugins...");
		if (pluginsToRemove.Count > 0)
		{
			LcPluginInstaller.Log.LogInfo((object)$"Removing {pluginsToRemove.Count} plugins which are no longer required");
			foreach (string item6 in pluginsToRemove)
			{
				LcPluginInstaller.Log.LogInfo((object)("Removing plugin " + DisplayPluginName(item6)));
				string path = GetPluginDirectory(item6);
				if (Directory.Exists(path))
				{
					LcPluginInstaller.Log.LogInfo((object)"   Deleting plugin directory");
					Directory.Delete(path, recursive: true);
				}
				string path2 = GetPluginConfigFile(item6);
				if (File.Exists(path2))
				{
					LcPluginInstaller.Log.LogInfo((object)"   Deleting config file");
					File.Delete(path2);
				}
			}
		}
		foreach (string item7 in downloadedPlugins)
		{
			string destinationDirectoryName = GetPluginDirectory(item7);
			using (FileStream stream = File.Open(GetPluginDownloadFile(item7), FileMode.Open))
			{
				using ZipArchive source = new ZipArchive(stream);
				source.ExtractToDirectory(destinationDirectoryName);
			}
			LcPluginInstaller.Log.LogInfo((object)("Installed plugin " + DisplayPluginName(item7)));
		}
		foreach (string item8 in downloadedPluginConfigs)
		{
			string sourceFileName = GetPluginConfigDownloadFile(item8);
			string destFileName = GetPluginConfigFile(item8);
			File.Copy(sourceFileName, destFileName, overwrite: true);
			LcPluginInstaller.Log.LogInfo((object)("Installed plugin config for " + DisplayPluginName(item8)));
		}
		LcPluginInstaller.Log.LogInfo((object)"Finished installing!");
		foreach (string item9 in pluginsToDownload)
		{
			if (!downloadedPlugins.Contains(item9))
			{
				LcPluginInstaller.Log.LogWarning((object)("Could not download plugin " + DisplayPluginName(item9) + ", this may cause issues"));
			}
		}
		foreach (string item10 in pluginsToUpdateConfig)
		{
			if (!downloadedPluginConfigs.Contains(item10))
			{
				LcPluginInstaller.Log.LogWarning((object)("Could not update config for " + DisplayPluginName(item10) + ", this may cause issues"));
			}
		}
		string GetPluginConfigDownloadFile(string plugin)
		{
			return Path.Combine(pluginConfigDownloadDirectory, plugin + ".cfg");
		}
		string GetPluginConfigFile(string plugin)
		{
			string text6 = plugin.Split("@")[0];
			return Path.Combine(configsDirectory, text6 + ".cfg");
		}
		string GetPluginDirectory(string plugin)
		{
			return Path.Combine(pluginsDirectory, plugin ?? "");
		}
		string GetPluginDownloadFile(string plugin)
		{
			return Path.Combine(pluginDownloadDirectory, plugin + ".zip");
		}
	}

	public static string DisplayPluginName(string plugin)
	{
		if (maskPluginName == MaskPluginName.None)
		{
			return plugin;
		}
		string text = plugin.Split("@")[0];
		string text2 = plugin.Split("@")[1];
		int num = maskPluginName switch
		{
			MaskPluginName.Abbreviation1 => 1, 
			MaskPluginName.Abbreviation2 => 2, 
			MaskPluginName.Abbreviation3 => 3, 
			_ => 0, 
		};
		if (num == 0)
		{
			throw new InvalidOperationException();
		}
		string text3 = "";
		for (int i = 0; i < text.Length; i++)
		{
			char c = text[i];
			if (!char.IsUpper(c))
			{
				continue;
			}
			text3 += c;
			for (int j = 1; j < num; j++)
			{
				if (text.Length <= i + 1)
				{
					break;
				}
				char c2 = text[i + 1];
				if (char.IsUpper(c2))
				{
					break;
				}
				text3 += c2;
				i++;
			}
		}
		return text3 + "@" + text2;
	}
}
public static class LcPluginInstaller
{
	public static readonly ManualLogSource Log = Logger.CreateLogSource("LcPluginInstaller");

	public static ILogListener PreloaderConsoleListener;

	public static IEnumerable<string> TargetDLLs => new string[0];

	public static void Patch(AssemblyDefinition _)
	{
	}

	public static void Finish()
	{
		foreach (ILogListener listener in Logger.Listeners)
		{
			if (((object)listener).GetType().Name == "PreloaderConsoleListener")
			{
				PreloaderConsoleListener = listener;
			}
		}
		AppDomain.CurrentDomain.AssemblyLoad += delegate(object sender, AssemblyLoadEventArgs e)
		{
			if (e.LoadedAssembly.GetName().Name == "netstandard")
			{
				Installer.RunSync();
			}
		};
	}
}
public static class PluginInfo
{
	public const string PLUGIN_GUID = "LcPluginInstaller";

	public const string PLUGIN_NAME = "LcPluginInstaller";

	public const string PLUGIN_VERSION = "0.0.2";
}