update logging and add pl splash as the main one
All checks were successful
Build / Build Launcher (push) Successful in 2m31s

This commit is contained in:
zznty
2024-05-31 17:12:08 +07:00
parent fc69ee8e83
commit 9fb29d2011
28 changed files with 364 additions and 318 deletions

View File

@@ -24,7 +24,7 @@ public class Main : IHandleInputPlugin
{
var sw = Stopwatch.StartNew();
Splash = new();
RunSplash();
Instance = this;
@@ -32,10 +32,9 @@ public class Main : IHandleInputPlugin
Cursor.Current = Cursors.AppStarting;
var pluginsDir = LoaderTools.PluginsDir;
Directory.CreateDirectory(pluginsDir);
LogFile.Init(pluginsDir);
LogFile.WriteLine("Starting - v" + Assembly.GetExecutingAssembly().GetName().Version.ToString(3));
LogFile.Init(Directory.CreateDirectory(pluginsDir).FullName);
LogFile.Log.Debug("Starting - v{Version}", Assembly.GetExecutingAssembly().GetName().Version.ToString(3));
// Fix tls 1.2 not supported on Windows 7 - github.com is tls 1.2 only
try
@@ -44,10 +43,10 @@ public class Main : IHandleInputPlugin
}
catch (NotSupportedException e)
{
LogFile.WriteLine("An error occurred while setting up networking, web requests will probably fail: " + e);
LogFile.Log.Warn(e, "An error occurred while setting up networking, web requests will probably fail");
}
Splash.SetText("Finding references...");
Splash?.SetText("Finding references...");
RoslynReferences.GenerateAssemblyList();
AppDomain.CurrentDomain.AssemblyResolve += ResolveDependencies;
@@ -59,13 +58,13 @@ public class Main : IHandleInputPlugin
StatsClient.OverrideBaseUrl(Config.StatsServerBaseUrl);
Splash.SetText("Patching...");
LogFile.WriteLine("Patching");
Splash?.SetText("Patching...");
LogFile.Log.Debug("Patching");
new Harmony("avaness.PluginLoader").PatchAll(Assembly.GetExecutingAssembly());
Splash.SetText("Instantiating plugins...");
LogFile.WriteLine("Instantiating plugins");
Splash?.SetText("Instantiating plugins...");
LogFile.Log.Debug("Instantiating plugins");
foreach (var id in Config)
{
var data = List[id];
@@ -84,17 +83,14 @@ public class Main : IHandleInputPlugin
// FIXME: It can potentially run in the background speeding up the game's startup
//ReportEnabledPlugins();
LogFile.WriteLine($"Finished startup. Took {sw.ElapsedMilliseconds}ms");
LogFile.Log.Debug("Finished startup. Took {Time}ms", sw.ElapsedMilliseconds);
Cursor.Current = temp;
Splash.Delete();
Splash = null;
}
public PluginList List { get; }
public PluginConfig Config { get; }
public SplashScreen Splash { get; }
public SplashScreen? Splash { get; set; }
/// <summary>
/// True if a local plugin was loaded
@@ -106,7 +102,7 @@ public class Main : IHandleInputPlugin
public void Init(object gameInstance)
{
LogFile.WriteLine($"Initializing {plugins.Count} plugins");
LogFile.Log.Debug("Initializing {PluginsCount} plugins", plugins.Count);
for (var i = plugins.Count - 1; i >= 0; i--)
{
var p = plugins[i];
@@ -146,7 +142,6 @@ public class Main : IHandleInputPlugin
plugins.Clear();
AppDomain.CurrentDomain.AssemblyResolve -= ResolveDependencies;
LogFile.Dispose();
Instance = null;
}
@@ -166,25 +161,47 @@ public class Main : IHandleInputPlugin
return false;
}
private void RunSplash()
{
var resetEvent = new ManualResetEventSlim();
var thread = new Thread(() =>
{
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Splash = new();
resetEvent.Set();
Task.Run(() =>
{
Sandbox.MySandboxGame.m_windowCreatedEvent.WaitOne();
Splash.Invoke(() => Splash.Delete());
});
Application.Run(Splash);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
resetEvent.Wait();
}
private void ReportEnabledPlugins()
{
if (!PlayerConsent.ConsentGiven)
return;
Splash.SetText("Reporting plugin usage...");
LogFile.WriteLine("Reporting plugin usage");
Splash?.SetText("Reporting plugin usage...");
LogFile.Log.Debug("Reporting plugin usage");
// Config has already been validated at this point so all enabled plugins will have list items
// FIXME: Move into a background thread
if (StatsClient.Track(TrackablePluginIds))
LogFile.WriteLine("List of enabled plugins has been sent to the statistics server");
LogFile.Log.Debug("List of enabled plugins has been sent to the statistics server");
else
LogFile.WriteLine("Failed to send the list of enabled plugins to the statistics server");
LogFile.Log.Debug("Failed to send the list of enabled plugins to the statistics server");
}
public void RegisterComponents()
{
LogFile.WriteLine($"Registering {plugins.Count} components");
LogFile.Log.Debug("Registering {PluginsCount} components", plugins.Count);
foreach (var plugin in plugins)
plugin.RegisterSession(MySession.Static);
}
@@ -193,12 +210,12 @@ public class Main : IHandleInputPlugin
{
Config.Disable();
plugins.Clear();
LogFile.WriteLine("Disabled all plugins");
LogFile.Log.Debug("Disabled all plugins");
}
public void InstantiatePlugins()
{
LogFile.WriteLine($"Loading {plugins.Count} plugins");
LogFile.Log.Debug($"Loading {plugins.Count} plugins");
for (var i = plugins.Count - 1; i >= 0; i--)
{
var p = plugins[i];
@@ -208,27 +225,30 @@ public class Main : IHandleInputPlugin
}
private Assembly ResolveDependencies(object sender, ResolveEventArgs args)
private Assembly? ResolveDependencies(object? sender, ResolveEventArgs args)
{
var assembly = args.RequestingAssembly?.GetName().ToString();
if (args.Name.Contains("0Harmony"))
var requestedName = new AssemblyName(args.Name);
switch (requestedName.Name)
{
if (assembly != null)
LogFile.WriteLine("Resolving 0Harmony for " + assembly);
else
LogFile.WriteLine("Resolving 0Harmony");
return typeof(Harmony).Assembly;
case "0Harmony":
{
if (assembly != null)
LogFile.Log.Debug("Resolving 0Harmony for {AssemblyName}", assembly);
else
LogFile.Log.Debug("Resolving 0Harmony");
return typeof(Harmony).Assembly;
}
case "SEPluginManager":
{
if (assembly != null)
LogFile.Log.Debug("Resolving SEPluginManager for {AssemblyName}", assembly);
else
LogFile.Log.Debug("Resolving SEPluginManager");
return typeof(SEPMPlugin).Assembly;
}
default:
return null;
}
if (args.Name.Contains("SEPluginManager"))
{
if (assembly != null)
LogFile.WriteLine("Resolving SEPluginManager for " + assembly);
else
LogFile.WriteLine("Resolving SEPluginManager");
return typeof(SEPMPlugin).Assembly;
}
return null;
}
}