diff --git a/StarMap.API/Interfaces/IModMenuService.cs b/StarMap.API/Services/IModMenuService.cs similarity index 53% rename from StarMap.API/Interfaces/IModMenuService.cs rename to StarMap.API/Services/IModMenuService.cs index 6563bf4..b7beb2b 100644 --- a/StarMap.API/Interfaces/IModMenuService.cs +++ b/StarMap.API/Services/IModMenuService.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace StarMap.API.Interfaces +namespace StarMap.API.Services { public interface IModMenuService { diff --git a/StarMap.API/Interfaces/IPatchService.cs b/StarMap.API/Services/IPatchService.cs similarity index 84% rename from StarMap.API/Interfaces/IPatchService.cs rename to StarMap.API/Services/IPatchService.cs index fc7e634..6e90cca 100644 --- a/StarMap.API/Interfaces/IPatchService.cs +++ b/StarMap.API/Services/IPatchService.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; using System.Reflection; -using System.Text; -using static System.Runtime.InteropServices.JavaScript.JSType; -namespace StarMap.API.Interfaces +namespace StarMap.API.Services { public interface IPatchService diff --git a/StarMap.Core/API/ModContext.cs b/StarMap.Core/API/ModContext.cs index 33a1260..4602efa 100644 --- a/StarMap.Core/API/ModContext.cs +++ b/StarMap.Core/API/ModContext.cs @@ -1,7 +1,4 @@ using StarMap.Core.ModRepository; -using System; -using System.Collections.Generic; -using System.Text; namespace StarMap.Core.API { @@ -10,7 +7,7 @@ internal interface IModContext RuntimeMod Mod { get; set; } } - internal sealed class ModContext: IModContext + internal sealed class ModContext : IModContext { public RuntimeMod Mod { get; set; } = null!; } diff --git a/StarMap.Core/API/ModMenuService.cs b/StarMap.Core/API/ModMenuService.cs index b62c4ef..0d00552 100644 --- a/StarMap.Core/API/ModMenuService.cs +++ b/StarMap.Core/API/ModMenuService.cs @@ -1,8 +1,5 @@ -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.UI; -using System; -using System.Collections.Generic; -using System.Text; namespace StarMap.Core.API { diff --git a/StarMap.Core/API/PatchService.cs b/StarMap.Core/API/PatchService.cs index 8dba8b8..69df3ba 100644 --- a/StarMap.Core/API/PatchService.cs +++ b/StarMap.Core/API/PatchService.cs @@ -1,5 +1,4 @@ - -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.Patches; using System.Reflection; diff --git a/StarMap.Core/Config/StarMapConfig.cs b/StarMap.Core/Config/StarMapConfig.cs index 57efc81..ac78e57 100644 --- a/StarMap.Core/Config/StarMapConfig.cs +++ b/StarMap.Core/Config/StarMapConfig.cs @@ -1,15 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace StarMap.Core.Config +namespace StarMap.Core.Config { - internal class RootConfig - { + internal class RootConfig + { public required StarMapConfig StarMap { get; set; } } - internal class StarMapConfig { public required string EntryAssembly { get; set; } diff --git a/StarMap.Core/Legacy/ModManagerScreen.cs b/StarMap.Core/Legacy/ModManagerScreen.cs deleted file mode 100644 index d23490a..0000000 --- a/StarMap.Core/Legacy/ModManagerScreen.cs +++ /dev/null @@ -1,286 +0,0 @@ -/*using DummyProgram; -using DummyProgram.Screens; -using StarMap.Types.Mods; -using StarMap.Types.Proto.IPC; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace StarMap.Core -{ - internal sealed class ModManagerScreen : IScreen - { - private readonly ModManager _modManager; - - private List _managedMods = []; - private List _unmanagedMods = []; - - private List _changes = []; - - private (IPCMod mod, bool unmanaged, bool fromStore)? _currentMod; - - enum ManagerState - { - MAIN, - MOD_INFO, - MOD_STORE, - } - - private ManagerState _managerState = ManagerState.MAIN; - - private List> _actions = []; - - public ModManagerScreen(ModManager? modManager) - { - ArgumentNullException.ThrowIfNull(modManager); - - _modManager = modManager; - - RetrieveModInfo(); - } - - private void RetrieveModInfo() - { - _managedMods = [.. _modManager.GetManagedMods().Mods]; - _unmanagedMods = _modManager.GetLoadedMods().Where(loadedMod => !_managedMods.Any(managedMod => managedMod.Name == loadedMod.Name)).Select((loadedMod) => new IPCMod() { Name = loadedMod.Name}).ToList(); - - } - - public string ScreenName => "Mod manager"; - - public IScreen HandleInput(int input) - { - if (input >= _actions.Count) return new MainScreen(); - return _actions[input](); - } - - public void Render() - { - _actions = []; - switch (_managerState) - { - case ManagerState.MAIN: - { - RenderMain(); - break; - } - case ManagerState.MOD_INFO: - { - RenderModInfo(); - break; - } - case ManagerState.MOD_STORE: - { - RenderModStore(); - break; - } - } - } - - private IScreen GoToMainMenu() - { - _managerState = ManagerState.MAIN; - RetrieveModInfo(); - _changes = []; - return new MainScreen(); - } - - private ModManagerScreen GoToModManager() - { - _managerState = ManagerState.MAIN; - _currentMod = null; - - return this; - } - - private ModManagerScreen GoToStore() - { - _managerState = ManagerState.MOD_STORE; - return this; - } - - private ModManagerScreen GoToSpecificMod(IPCMod mod, bool unmanaged, bool fromStore) - { - _managerState = ManagerState.MOD_INFO; - _currentMod = (mod, unmanaged, fromStore); - - return this; - } - - private void RenderMain() - { - var index = 0; - - Console.WriteLine("Mod manager"); - Console.WriteLine("Managed mods: "); - - foreach (var mod in _managedMods) - { - var localMod = mod; - Console.WriteLine($"{index++}: {localMod.Name}:{localMod.Version}"); - _actions.Add(() => GoToSpecificMod(localMod, false, false)); - } - - Console.WriteLine("Unmanaged mods: "); - foreach (var mod in _unmanagedMods) - { - var localMod = mod; - Console.WriteLine($"{index++}: {localMod.Name}:{localMod.Version}"); - _actions.Add(() => GoToSpecificMod(mod, true, false)); - } - - Console.WriteLine($""); - - if (_changes.Count > 0) - { - Console.WriteLine($"Current changes"); - foreach (var modUpdate in _changes) - { - if (modUpdate.BeforeVersion is IPCModVersion beforeVersion && !string.IsNullOrEmpty(beforeVersion.Version)) - Console.WriteLine($"{modUpdate.Mod.Name}: {beforeVersion.Version} => {modUpdate.AfterVersion.Version}"); - else - Console.WriteLine($"{modUpdate.Mod.Name}: {modUpdate.AfterVersion.Version}"); - - } - Console.WriteLine($""); - } - - Console.WriteLine($"{index++}: Get other mods"); - _actions.Add(GoToStore); - if ( _changes.Count > 0) - { - Console.WriteLine($"{index++}: Apply"); - _actions.Add(ApplyMods); - Console.WriteLine($"{index++}: Revert"); - _actions.Add(GoToMainMenu); - } - else - { - Console.WriteLine($"{index++}: Return"); - _actions.Add(GoToMainMenu); - } - - } - - private void RenderModInfo() - { - if (_currentMod is null) return; - var modInformation = _modManager.GetModInformationAsync(_currentMod.Value.mod.Id).GetAwaiter().GetResult(); - if (modInformation is null) - { - Console.WriteLine($"Unable to retrieve information for mod: {_currentMod.Value.mod.Name}"); - Console.WriteLine($"Return"); - _actions.Add(GoToModManager); - return; - } - - var mod = _currentMod.Value.mod; - var unmanaged = _currentMod.Value.unmanaged; - - Console.WriteLine($"Alter version for mod: {mod.Name}"); - if (unmanaged) - Console.WriteLine($"Will remove the unmanaged mod and make it managed"); - - var index = 0; - - foreach (var possibleVersion in modInformation.Versions) - { - if (!unmanaged && possibleVersion.Equals(mod.Version)) - { - Console.WriteLine($"* {mod.Version}"); - continue; - } - - var localMod = mod; - var localversion = possibleVersion; - var localUnmanaged = unmanaged; - - Console.WriteLine($"{index++}: {possibleVersion.Version}"); - _actions.Add(() => SetModVersion(localMod, localversion, localUnmanaged)); - } - - Console.WriteLine($"{index}: Return"); - _actions.Add(GoToModManager); - } - - private void RenderModStore() - { - var availableMods = _modManager.GetAvailableModsAsync().GetAwaiter().GetResult(); - - var index = 0; - - foreach (var availableMod in availableMods) - { - var localMod = availableMod; - - Console.WriteLine($"{index++}: {availableMod.Name} by {availableMod.Author}"); - _actions.Add(() => GoToSpecificMod(localMod, false, true)); - } - - Console.WriteLine($"{index}: Return"); - _actions.Add(GoToModManager); - } - - private ModManagerScreen SetModVersion(IPCMod mod, IPCModVersion version, bool unmanaged) - { - _managerState = ManagerState.MAIN; - IPCModVersion? previousVersion = null; - - if (unmanaged) - { - var removedMod = _unmanagedMods.FirstOrDefault((unmanagedMod) => string.Equals(unmanagedMod.Name, mod.Name)); - if (removedMod is not null) - { - previousVersion = new IPCModVersion() { Version = removedMod.Version }; - _unmanagedMods.Remove(removedMod); - } - } - else - { - var oldModIndex = _managedMods.FindIndex(0, (modInfo) => modInfo.Id == mod.Id); - if (oldModIndex >= 0) - { - previousVersion = new IPCModVersion() { Version = _managedMods[oldModIndex].Version }; - } - } - - var newModInformation = new IPCMod() - { - Id = mod.Id, - Name = mod.Name, - Author = mod.Author, - }; - - var changesIndex = _changes.FindIndex(0, (change) => change.Mod.Id == mod.Id); - var change = new IPCUpdateModInformation() - { - Mod = newModInformation, - BeforeVersion = new IPCModVersion() { Version = previousVersion?.ToString() ?? "" }, - AfterVersion = version, - }; - - - if (changesIndex >= 0) - _changes[changesIndex] = change; - else - _changes.Add(change); - - return GoToModManager(); - } - - private IScreen ApplyMods() - { - - var message = new IPCSetManagedMods(); - message.Updates.AddRange(_changes); - - _modManager.SetModUpdates(message).GetAwaiter().GetResult(); - - return new ExitScreen(); - } - } -} -*/ \ No newline at end of file diff --git a/StarMap.Core/ModRepository/ModAssemblyLoadContext.cs b/StarMap.Core/ModRepository/ModAssemblyLoadContext.cs index ccfb44b..232e3b2 100644 --- a/StarMap.Core/ModRepository/ModAssemblyLoadContext.cs +++ b/StarMap.Core/ModRepository/ModAssemblyLoadContext.cs @@ -1,5 +1,4 @@ -using KSA; -using System.Reflection; +using System.Reflection; using System.Runtime.Loader; namespace StarMap.Core.ModRepository diff --git a/StarMap.Core/ModRepository/ModRegistry.cs b/StarMap.Core/ModRepository/ModRegistry.cs index 04ae34f..582a6ad 100644 --- a/StarMap.Core/ModRepository/ModRegistry.cs +++ b/StarMap.Core/ModRepository/ModRegistry.cs @@ -1,6 +1,4 @@ -using KSA; -using StarMap.API; -using StarMap.Core.Config; +using StarMap.API; using System.Diagnostics.CodeAnalysis; using System.Reflection; diff --git a/StarMap.Core/ModRepository/ModsLoader.cs b/StarMap.Core/ModRepository/ModsLoader.cs index 492d468..42a1ff4 100644 --- a/StarMap.Core/ModRepository/ModsLoader.cs +++ b/StarMap.Core/ModRepository/ModsLoader.cs @@ -1,26 +1,23 @@ using KSA; using Microsoft.Extensions.DependencyInjection; using StarMap.API; -using StarMap.API.Interfaces; -using StarMap.Core.API; -using StarMap.Core.Config; using StarMap.Core.Patches; +using StarMap.Core.Services; using System.Reflection; using System.Runtime.Loader; -using Tomlet; namespace StarMap.Core.ModRepository { - internal interface IModsLoader: IDisposable + internal interface IModsLoader : IDisposable { void Init(); } - internal sealed class ModsLoader:IModsLoader + internal sealed class ModsLoader : IModsLoader { - private readonly ICoreService _coreService; private readonly AssemblyLoadContext _coreAssemblyLoadContext; private readonly IPatchManager _patchManager; + private readonly IServiceProviderService _serviceProviderService; public static Dictionary RegisteredMethodAttributes = []; @@ -34,12 +31,11 @@ internal sealed class ModsLoader:IModsLoader return (attrType.Name, attrObject); } - public ModsLoader(ICoreService coreService, IPatchManager patchManager) + public ModsLoader([FromKeyedServices("Core")] AssemblyLoadContext coreAssemblyLoadContext, IPatchManager patchManager, IServiceProviderService serviceProviderService) { - _coreService = coreService; - _coreAssemblyLoadContext = coreService.CoreAssemblyLoadContext; + _coreAssemblyLoadContext = coreAssemblyLoadContext; _patchManager = patchManager; - + _serviceProviderService = serviceProviderService; Assembly coreAssembly = typeof(StarMapModAttribute).Assembly; @@ -89,7 +85,7 @@ private void PrepareMods() continue; } - if (!runtimeMod.InitializeMod(ModRegistry, _coreService, _patchManager)) + if (!runtimeMod.InitializeMod(ModRegistry, _patchManager, _serviceProviderService)) { Console.WriteLine($"StarMap - Failed to initialize mod: {runtimeMod.ModId} from manifest"); continue; @@ -98,10 +94,10 @@ private void PrepareMods() Console.WriteLine($"StarMap - Loaded mod: {runtimeMod.ModId} from manifest"); var dependentMods = runtimeMod.CheckForDependentMods(ModRegistry); - + foreach (var dependentMod in dependentMods) { - if (dependentMod.InitializeMod(ModRegistry, _coreService, _patchManager)) + if (dependentMod.InitializeMod(ModRegistry, _patchManager, _serviceProviderService)) { Console.WriteLine($"StarMap - Loaded mod: {dependentMod.ModId} after loading {runtimeMod.ModId}"); } @@ -129,7 +125,7 @@ private void TryLoadWaitingMods() loadedMod = true; ModRegistry.WaitingMods.Remove(waitingMod); - if (waitingMod.InitializeMod(ModRegistry, _coreService, _patchManager)) + if (waitingMod.InitializeMod(ModRegistry, _patchManager, _serviceProviderService)) { Console.WriteLine($"StarMap - Loaded mod: {waitingMod.ModId} after all mods were loaded, not loaded optional mods: {string.Join(",", waitingMod.NotLoadedModDependencies.Values.Select(mod => mod.ModId))}"); } diff --git a/StarMap.Core/ModRepository/RuntimeMod.cs b/StarMap.Core/ModRepository/RuntimeMod.cs index 18466e3..d158bf7 100644 --- a/StarMap.Core/ModRepository/RuntimeMod.cs +++ b/StarMap.Core/ModRepository/RuntimeMod.cs @@ -1,16 +1,13 @@ using KSA; using Microsoft.Extensions.DependencyInjection; using StarMap.API; -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.Config; using StarMap.Core.Patches; -using System; -using System.Collections.Generic; +using StarMap.Core.Services; using System.Diagnostics.CodeAnalysis; -using System.IO; using System.Reflection; using System.Runtime.Loader; -using System.Text; using Tomlet; namespace StarMap.Core.ModRepository @@ -101,7 +98,7 @@ public bool AllDependenciesLoaded(ModRegistry modRegistry) if (NotLoadedModDependencies.Count > 0) { - + modRegistry.WaitingMods.Add(this); return false; } @@ -129,14 +126,14 @@ private static IEnumerable CalculateUseableAssemblies(RuntimeMod depende return dependency.ExportedAssemblies.Intersect(dependencyInfo.ImportedAssemblies); } - public bool InitializeMod(ModRegistry modRegistry, ICoreService core, IPatchManager patchManager) + public bool InitializeMod(ModRegistry modRegistry, IPatchManager patchManager, IServiceProviderService serviceProviderService) { var modClass = ModAssembly.GetTypes().FirstOrDefault(type => type.GetCustomAttributes().Any(attr => attr.GetType().Name == typeof(StarMapModAttribute).Name)); if (modClass is null) return false; ModType = modClass; - var services = core.CreateModDependencies(this); + var services = serviceProviderService.CreateModDependencies(this); var modObject = ActivatorUtilities.CreateInstance(services, ModType); if (modObject is null) return false; ModInstance = modObject; @@ -160,12 +157,12 @@ public bool InitializeMod(ModRegistry modRegistry, ICoreService core, IPatchMana modRegistry.AddModMethod(ModId, attr, modObject, classMethod); break; case StarMapAllModsLoadedAttribute: - patchManager.RegisterStaticVoidPatch(IPatchService.PatchType.Postfix, this, typeof(KSA.ModLibrary).GetMethod(nameof(ModLibrary.LoadAll))!, + patchManager.RegisterStaticVoidPatch(IPatchService.PatchType.Postfix, this, typeof(KSA.ModLibrary).GetMethod(nameof(ModLibrary.LoadAll))!, args => { - classMethod.Invoke(modObject, args); - return true; - }); + classMethod.Invoke(modObject, args); + return true; + }); break; case StarMapImmediateLoadAttribute: diff --git a/StarMap.Core/Patches/DocumentsPathOverride.cs b/StarMap.Core/Patches/DocumentsPathOverride.cs index 88e5633..b903eda 100644 --- a/StarMap.Core/Patches/DocumentsPathOverride.cs +++ b/StarMap.Core/Patches/DocumentsPathOverride.cs @@ -1,7 +1,4 @@ -using HarmonyLib; -using KSA; - -namespace StarMap.Core.Patches +namespace StarMap.Core.Patches { internal static class DocumentsPathOverride { diff --git a/StarMap.Core/Patches/PatchManager.cs b/StarMap.Core/Patches/PatchManager.cs index b66263d..e1ed9e0 100644 --- a/StarMap.Core/Patches/PatchManager.cs +++ b/StarMap.Core/Patches/PatchManager.cs @@ -1,9 +1,7 @@ using HarmonyLib; -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.ModRepository; -using System.Diagnostics; using System.Reflection; -using System.Runtime.ExceptionServices; namespace StarMap.Core.Patches { @@ -146,7 +144,7 @@ public static void PostfixPatchEntryPoint( public void Dispose() { - foreach(var patch in _patches) + foreach (var patch in _patches) { patch.Value.Dispose(); } @@ -261,7 +259,8 @@ public void RegisterPatch(IPatchService.PatchType type, RuntimeMod? mod, IPatchC default: throw new InvalidOperationException("Unknown type " + type); - }; + } + ; } public void UnregisterPatch(IPatchService.PatchType type, RuntimeMod? mod) diff --git a/StarMap.Core/Patches/StarMapPatches.cs b/StarMap.Core/Patches/StarMapPatches.cs index 29b4fd9..05065a2 100644 --- a/StarMap.Core/Patches/StarMapPatches.cs +++ b/StarMap.Core/Patches/StarMapPatches.cs @@ -1,6 +1,6 @@ using HarmonyLib; using KSA; -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.UI.ConfirmRestart; using System.Diagnostics; diff --git a/StarMap.Core/Services/CoreService.cs b/StarMap.Core/Services/CoreService.cs new file mode 100644 index 0000000..f06c814 --- /dev/null +++ b/StarMap.Core/Services/CoreService.cs @@ -0,0 +1,19 @@ +using StarMap.Core.ModRepository; +using StarMap.Core.Patches; + +namespace StarMap.Core.Services +{ + internal interface ICoreService + { + void Init(); + } + + internal class CoreService(IStarMapPatches _starmapPatches, IModsLoader _modsLoader) : ICoreService + { + public void Init() + { + _starmapPatches.Init(); + _modsLoader.Init(); + } + } +} diff --git a/StarMap.Core/Services/ServiceProviderService.cs b/StarMap.Core/Services/ServiceProviderService.cs new file mode 100644 index 0000000..7b3daad --- /dev/null +++ b/StarMap.Core/Services/ServiceProviderService.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.DependencyInjection; +using StarMap.Core.API; +using StarMap.Core.ModRepository; + +namespace StarMap.Core.Services +{ + internal interface IServiceProviderService : IDisposable + { + IServiceProvider CreateModDependencies(RuntimeMod mod); + } + + internal class ServiceProviderService(IServiceProvider _serviceProvider) : IServiceProviderService + { + private readonly Lock _lock = new(); + private readonly List _modScopes = []; + + public IServiceProvider CreateModDependencies(RuntimeMod mod) + { + var scope = _serviceProvider.CreateScope(); + + scope.ServiceProvider.GetRequiredService().Mod = mod; + + lock (_lock) + { + _modScopes.Add(scope); + } + + return scope.ServiceProvider; + } + + public void Dispose() + { + lock (_lock) + { + foreach (var scope in _modScopes) + { + scope.Dispose(); + } + } + } + } +} diff --git a/StarMap.Core/StarMap.Core.csproj b/StarMap.Core/StarMap.Core.csproj index b83fbe3..28aa5c3 100644 --- a/StarMap.Core/StarMap.Core.csproj +++ b/StarMap.Core/StarMap.Core.csproj @@ -18,7 +18,6 @@ - diff --git a/StarMap.Core/StarMapCore.cs b/StarMap.Core/StarMapCore.cs index 332063e..5bda76a 100644 --- a/StarMap.Core/StarMapCore.cs +++ b/StarMap.Core/StarMapCore.cs @@ -1,36 +1,23 @@ using Microsoft.Extensions.DependencyInjection; -using StarMap.API.Interfaces; +using StarMap.API.Services; using StarMap.Core.API; using StarMap.Core.ModRepository; using StarMap.Core.Patches; +using StarMap.Core.Services; using StarMap.Core.UI; -using StarMap.Types; using System.Runtime.Loader; namespace StarMap.Core { - internal interface ICoreService - { - AssemblyLoadContext CoreAssemblyLoadContext { get; } - IServiceProvider CreateModDependencies(RuntimeMod mod); - } - - internal class StarMapCore : IStarMapCore, ICoreService + internal class StarMapCore : IDisposable { private readonly IServiceProvider _serviceProvider; - private readonly Lock _lock = new(); - private readonly List _modScopes = []; - - private readonly AssemblyLoadContext _coreAssemblyLoadContext; - - public AssemblyLoadContext CoreAssemblyLoadContext => _coreAssemblyLoadContext; public StarMapCore(AssemblyLoadContext coreAssemblyLoadContext) { - _coreAssemblyLoadContext = coreAssemblyLoadContext; - var serviceCollection = new ServiceCollection(); - serviceCollection.AddSingleton(this); + serviceCollection.AddKeyedSingleton("Core", coreAssemblyLoadContext); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); @@ -40,42 +27,29 @@ public StarMapCore(AssemblyLoadContext coreAssemblyLoadContext) serviceCollection.AddScoped(); serviceCollection.AddScoped(); - _serviceProvider = serviceCollection.BuildServiceProvider(); - } + serviceCollection.AddSingleton(); - public void Init() - { - _serviceProvider.GetRequiredService().Init(); - _serviceProvider.GetRequiredService().Init(); - } + _serviceProvider = serviceCollection.BuildServiceProvider(); - public void DeInit() - { - lock (_lock) + try + { + _serviceProvider.GetRequiredService().Init(); + } + catch { - foreach(var scope in _modScopes) - { - scope.Dispose(); - } + ((IDisposable)_serviceProvider).Dispose(); + throw; } - _serviceProvider.GetRequiredService().Dispose(); } - public IServiceProvider CreateModDependencies(RuntimeMod mod) + public void Dispose() { - var scope = _serviceProvider.CreateScope(); + ((IDisposable)_serviceProvider).Dispose(); + } - scope.ServiceProvider.GetRequiredService().Mod = mod; - lock (_lock) - { - _modScopes.Add(scope); - } - return scope.ServiceProvider; - } - } } \ No newline at end of file diff --git a/StarMap.Core/UI/ConfirmRestart/ConfirmRestartPopup.cs b/StarMap.Core/UI/ConfirmRestart/ConfirmRestartPopup.cs index a21a8e7..68785f0 100644 --- a/StarMap.Core/UI/ConfirmRestart/ConfirmRestartPopup.cs +++ b/StarMap.Core/UI/ConfirmRestart/ConfirmRestartPopup.cs @@ -1,5 +1,4 @@ -using Brutal.ImGuiApi; -using Brutal.Numerics; +using Brutal.Numerics; using KSA; namespace StarMap.Core.UI.ConfirmRestart @@ -29,7 +28,7 @@ internal class ConfirmRestartPopup : Popup ]; private ConfirmRestartPopup(ConfirmRestart ui) - { + { _title = "Game requires restart"; UI = ui; } diff --git a/StarMap.Core/UI/ModMenu.cs b/StarMap.Core/UI/ModMenu.cs index f09d18d..3348c64 100644 --- a/StarMap.Core/UI/ModMenu.cs +++ b/StarMap.Core/UI/ModMenu.cs @@ -1,16 +1,8 @@ using Brutal.ImGuiApi; using KSA; -using StarMap.API.Interfaces; -using StarMap.Core.ModRepository; +using StarMap.API.Services; using StarMap.Core.Patches; -using System; -using System.Collections; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.InteropServices; -using System.Text; namespace StarMap.Core.UI { diff --git a/StarMap.Types/IStarMapCore.cs b/StarMap.Types/IStarMapCore.cs deleted file mode 100644 index e205ec7..0000000 --- a/StarMap.Types/IStarMapCore.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace StarMap.Types -{ - public interface IStarMapCore - { - void Init(); - void DeInit(); - } -} diff --git a/StarMap.Types/StarMap.Types.csproj b/StarMap.Types/StarMap.Types.csproj deleted file mode 100644 index 00ec6e1..0000000 --- a/StarMap.Types/StarMap.Types.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - net10.0 - enable - enable - Debug;Release;SourceNuget - - diff --git a/StarMap.slnx b/StarMap.slnx index 9e59a21..9548234 100644 --- a/StarMap.slnx +++ b/StarMap.slnx @@ -11,5 +11,4 @@ - \ No newline at end of file diff --git a/StarMap/GameSurveyer.cs b/StarMap/GameSurveyer.cs index 3e224a1..cebe9b2 100644 --- a/StarMap/GameSurveyer.cs +++ b/StarMap/GameSurveyer.cs @@ -1,5 +1,4 @@ -using StarMap.Types; -using System.Diagnostics; +using System.Diagnostics; using System.Reflection; using System.Runtime.Loader; @@ -12,7 +11,7 @@ internal class GameSurveyer : IDisposable private readonly string[] _args; private Assembly? _game; - private IStarMapCore? _core; + private IDisposable? _core; public GameSurveyer(AssemblyLoadContext alc, string location, string[] args) @@ -26,11 +25,6 @@ public bool TryLoadCoreAndGame() { var modManagerAssembly = _gameAssemblyContext.LoadFromAssemblyPath(Path.GetFullPath("./StarMap.Core.dll")); - var starMapCore = modManagerAssembly.GetTypes().FirstOrDefault((type) => typeof(IStarMapCore).IsAssignableFrom(type) && !type.IsInterface); - if (starMapCore is null) return false; - var createdCore = Activator.CreateInstance(starMapCore, [_gameAssemblyContext]); - if (createdCore is not IStarMapCore core) return false; - _game = _gameAssemblyContext.LoadFromAssemblyPath(_gameLocation); var gameDirectory = Path.GetDirectoryName(_gameLocation); @@ -43,8 +37,13 @@ public bool TryLoadCoreAndGame() Directory.SetCurrentDirectory(gameDirectory); AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", gameDirectory + Path.DirectorySeparatorChar); + var starMapCore = modManagerAssembly.GetTypes().FirstOrDefault((type) => type.FullName == "StarMap.Core.StarMapCore" && !type.IsInterface); + if (starMapCore is null) return false; + + var createdCore = Activator.CreateInstance(starMapCore, [_gameAssemblyContext]); + if (createdCore is not IDisposable core) return false; + _core = core; - core.Init(); return true; } @@ -57,7 +56,7 @@ public void RunGame() public void Dispose() { - _core?.DeInit(); + _core?.Dispose(); } } } diff --git a/StarMap/LoaderConfig.cs b/StarMap/LoaderConfig.cs index 8037a09..3ab0634 100644 --- a/StarMap/LoaderConfig.cs +++ b/StarMap/LoaderConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json; -namespace StarMap.Types +namespace StarMap { public class LoaderConfig { @@ -42,7 +42,7 @@ public bool TryLoadConfig() GameLocation = path; GameArguments = config.GameArguments; - + return true; } diff --git a/StarMap/Program.cs b/StarMap/Program.cs index 7629222..5ae69f5 100644 --- a/StarMap/Program.cs +++ b/StarMap/Program.cs @@ -1,6 +1,4 @@ -using StarMap.Types; -using System.Diagnostics; -using System.Runtime.Loader; +using System.Runtime.Loader; namespace StarMap { @@ -18,7 +16,7 @@ static async Task Main(string[] args) AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.GetFullPath("./0Harmony.dll")); var gameAssemblyContext = new CoreAssemblyLoadContext(gameConfig.GameLocation); - using var gameSurveyer = new GameSurveyer(gameAssemblyContext, gameConfig.GameLocation, [.. args, ..gameConfig.GameArguments]); + using var gameSurveyer = new GameSurveyer(gameAssemblyContext, gameConfig.GameLocation, [.. args, .. gameConfig.GameArguments]); if (!gameSurveyer.TryLoadCoreAndGame()) { Console.WriteLine("StarMap - Unable to load mod manager and game."); diff --git a/StarMap/StarMap.csproj b/StarMap/StarMap.csproj index e7ed6d5..8fcebad 100644 --- a/StarMap/StarMap.csproj +++ b/StarMap/StarMap.csproj @@ -14,6 +14,5 @@ -