Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ on:
permissions:
pull-requests: write
contents: read
packages: read

jobs:
preview:
runs-on: ubuntu-latest
env:
DOTNET_VERSION: 9.0
PROJECT: StarMapLoader/StarMapLoader.csproj
NUGET_SOURCE: "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"

steps:
- uses: actions/checkout@v4
Expand All @@ -27,7 +29,9 @@ jobs:
dotnet-version: ${{ env.DOTNET_VERSION }}

- name: Restore dependencies
run: dotnet restore ${{ env.PROJECT }}
run: |
dotnet nuget add source --username "${{ github.actor }}" --password "${{ secrets.GITHUB_TOKEN }}" --store-password-in-clear-text --name github "${{ env.NUGET_SOURCE }}"
dotnet restore ${{ env.PROJECT }}

# - name: Run tests
# run: dotnet test --no-build --verbosity normal
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/release-zip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
PROJECT: StarMapLoader/StarMapLoader.csproj
OUTPUT_PATH: ./publish
NUGET_SOURCE: "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
EXCLUDE: "*.pdb *.xml DummyProgram.deps.json DummyProgram.runtimeconfig.json DummyProgram.pdb DummyProgram.exe Tomlyn.dll"
EXCLUDE: "*.pdb *.xml"

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -73,6 +73,14 @@ jobs:
git tag "$NEW_VERSION"
git push origin "$NEW_VERSION"

- name: Setup Github NuGet
run: |
dotnet nuget add source \
--username ${{ github.actor }} \
--password ${{ secrets.GITHUB_TOKEN }} \
--store-password-in-clear-text \
--name github "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"

- name: Build
run: dotnet publish ${{ env.PROJECT }} -c Release -o ${{ env.OUTPUT_PATH }}

Expand Down
1 change: 1 addition & 0 deletions DummyProgram/DummyProgram.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>KSA</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions DummyProgram/Mod.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using DummyProgram.Screens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace DummyProgram
namespace KSA
{
public class Mod
{
public required string DirectoryPath { get; init; }
public required AssemblyName Assembly { get; init; }
public required string Name { get; init; }

public Mod() { }
public void PrepareSystems()
{
}

public void DoSomething()
{
Console.WriteLine($"Mod {Name} is doing something!");
}
}
}
9 changes: 5 additions & 4 deletions DummyProgram/ModLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using Tomlyn.Model;
using Tomlyn;

namespace DummyProgram
namespace KSA
{
public class ModLibrary
{
private List<Mod> _mods = [];
public List<Mod> Mods = [];

public ModLibrary() { }
public void LoadAll()
Expand Down Expand Up @@ -60,11 +60,12 @@ public void FetchMods()
var mod = new Mod
{
DirectoryPath = folder,
Assembly = assemblyName
Assembly = assemblyName,
Name = assemblyName.Name
};

mod.PrepareSystems();
_mods.Add(mod);
Mods.Add(mod);

}
}
Expand Down
9 changes: 4 additions & 5 deletions DummyProgram/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using DummyProgram.Screens;

namespace DummyProgram
namespace KSA
{
public class Program
{
public static ModLibrary ModLibrary = new ModLibrary();

private static IScreen _currentScreen = new MainScreen();

static void Main(string[] args)
{
var library = new ModLibrary();
library.LoadAll();
ModLibrary.LoadAll();

while (true)
{
Expand Down
2 changes: 1 addition & 1 deletion DummyProgram/Screens/IScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace DummyProgram.Screens
namespace KSA
{
public interface IScreen
{
Expand Down
4 changes: 2 additions & 2 deletions DummyProgram/Screens/Screens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using System.Text;
using System.Threading.Tasks;

namespace DummyProgram.Screens
namespace KSA
{
public class MainScreen : IScreen
{

public static IScreen[] Screens { get; set; } = [new GameScreen(), new ExitScreen()];

public string ScreenName => "Main";
Expand Down Expand Up @@ -59,6 +58,7 @@ public IScreen HandleInput(int input)

public void DoSomething()
{
Program.ModLibrary.Mods.ForEach((mod) => mod.DoSomething());
Console.WriteLine("GameScreen.DoSomething");
Console.ReadLine();
}
Expand Down
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 StarMap

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
# StarMap
A POC/test modloader primarly for Kitten Space Agency
This was inspired by https://github.com/cheese3660/KsaLoader for me to learn about assemblyloading.
It very heavily relies on AssemblyLoadContexts to load both the game and then the mods in the game.
The advantage of this is that different mods can use different versions of the same dependencies.

The main goal was to be able to unload the whole game and the modding, change the mods/dlls around and then load it again.
This could result in a simular system to how Factory feels where the mod manager lives within the game.
Sadly it did not work out in the current way it is implemented because Harmony does not play well ALC's and it keeps references to objects within the Assemblies.
This results in the assemblies not unloading which means I can not swap the dll's (it locks them).
A probable way forward is using a seperate process for the game itself so the whole process can shut down, however communication between processes forms a barrier to doing it this way.

A POC/Prototype arbitrary code modloader for Kitten Space Agency.
The idea is to get to a Factorio like experience where mods can be managed in game and mods can be synced by restarting the game.
Currently this loader can be ran with this functionality in the background, or as a dumb loader just loading mods.
It makes use of Assembly Load Contexts to ensure mod dependencies are managed seperatly, reducing conflicts

## Installation

- Download and unzip release from [Releases](https://github.com/StarMapLoader/StarMap/releases/latest).
- Run StarMapLoader.exe, this will fail and create a StarMapLoader.json.
- Open StarMapLoader.json and set the location of your KSA installation.
- Run StarMapLoader.exe again, this should launch KSA and load your mods.

## Running as dumb loader

If you do not want the seperate process and mod manager functionality, StarMap.exe can also be ran seperatly, this will just load the installed mods

## Mod location

Mods should be installed in the mods folder in the KSA installation, any KSA mod that has a dll with the same name as the mod that impelements the IStarMapMod interface will be loaded.

## Mod creation

For more information on mod creation, check out the example mods: [StarMap-ExampleMods](https://github.com/StarMapLoader/StarMap-ExampleMods).

## Credits

- Lexi - [KSALoader](https://github.com/cheese3660/KsaLoader)
15 changes: 4 additions & 11 deletions StarMap.Core/ModAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using DummyProgram;
using System;
using System.Collections.Generic;
using System.Linq;
using KSA;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;

namespace StarMap.Core
{
Expand All @@ -20,29 +15,27 @@ public ModAssemblyLoadContext(Mod mod, AssemblyLoadContext coreAssemblyContext)
_coreAssemblyLoadContext = coreAssemblyContext;

_modDependencyResolver = new AssemblyDependencyResolver(
Path.GetFullPath(Path.Combine(mod.DirectoryPath, mod.Assembly.Name + ".dll"))
Path.GetFullPath(Path.Combine(mod.DirectoryPath, mod.Name + ".dll"))
);
}

protected override Assembly? Load(AssemblyName assemblyName)
{
var existingInDefault = Default.Assemblies
.FirstOrDefault(a => a.FullName == assemblyName.FullName);
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
if (existingInDefault != null)
return existingInDefault;

var existingInGameContext = _coreAssemblyLoadContext?.Assemblies
.FirstOrDefault(a => a.FullName == assemblyName.FullName);
.FirstOrDefault(a => string.Equals(a.GetName().Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
if (existingInGameContext != null)
return existingInGameContext;

var foundPath = _modDependencyResolver.ResolveAssemblyToPath(assemblyName);

if (foundPath is null)
return null;

var path = Path.GetFullPath(foundPath);

return path != null ? LoadFromAssemblyPath(path) : null;
}
}
Expand Down
9 changes: 4 additions & 5 deletions StarMap.Core/ModLoaderPatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DummyProgram;
using DummyProgram.Screens;
using KSA;
using HarmonyLib;

namespace StarMap.Core
Expand All @@ -19,7 +18,7 @@ public static void Patch(ModManager modManager)
public static void Unload()
{
_modManager = null;
MainScreen.Screens = [];
//MainScreen.Screens = [];
_harmony?.UnpatchAll();
}

Expand All @@ -35,10 +34,10 @@ public static void OnLoadMod(this Mod __instance)
public static void AfterLoad()
{
_modManager?.OnAllModsLoaded();
var screens = MainScreen.Screens.ToList();
/*var screens = MainScreen.Screens.ToList();
screens.Insert(MainScreen.Screens.Length - 1, new ModManagerScreen(_modManager));

MainScreen.Screens = screens.ToArray();
MainScreen.Screens = screens.ToArray();*/
_harmony?.UnpatchAll(_harmony.Id);
}
}
Expand Down
Loading
Loading