embed plugin loader directly into the launcher

This commit is contained in:
zznty
2022-10-29 01:50:14 +07:00
parent 7204815c0c
commit 66d3dc2ead
53 changed files with 5689 additions and 10 deletions

View File

@@ -0,0 +1,92 @@
using System.Diagnostics;
using System.Reflection;
using Sandbox.Graphics.GUI;
using VRage;
namespace PluginLoader.Data;
public class LocalPlugin : PluginData
{
private LocalPlugin()
{
}
public LocalPlugin(string dll)
{
Id = dll;
Status = PluginStatus.None;
}
public override string Source => MyTexts.GetString(MyCommonTexts.Local);
public override string Id
{
get => base.Id;
set
{
base.Id = value;
if (File.Exists(value))
FriendlyName = Path.GetFileName(value);
}
}
public override Assembly GetAssembly()
{
if (File.Exists(Id))
{
AppDomain.CurrentDomain.AssemblyResolve += LoadFromSameFolder;
var a = Assembly.LoadFile(Id);
Version = a.GetName().Version;
return a;
}
return null;
}
public override string ToString()
{
return Id;
}
public override void Show()
{
var file = Path.GetFullPath(Id);
if (File.Exists(file))
Process.Start("explorer.exe", $"/select, \"{file}\"");
}
private Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
if (args.RequestingAssembly.IsDynamic)
return null;
if (args.Name.Contains("0Harmony") || args.Name.Contains("SEPluginManager"))
return null;
var location = args.RequestingAssembly.Location;
if (string.IsNullOrWhiteSpace(location) || !Path.GetFullPath(location)
.StartsWith(Path.GetDirectoryName(Id),
StringComparison.OrdinalIgnoreCase))
return null;
var folderPath = Path.GetDirectoryName(location);
var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
if (!File.Exists(assemblyPath))
return null;
var assembly = Assembly.LoadFile(assemblyPath);
LogFile.WriteLine("Resolving " + assembly.GetName().Name + " for " + args.RequestingAssembly.FullName);
var main = Main.Instance;
if (!main.Config.IsEnabled(assemblyPath))
main.List.Remove(assemblyPath);
return assembly;
}
public override void GetDescriptionText(MyGuiControlMultilineText textbox)
{
textbox.Visible = false;
textbox.Clear();
}
}