Skip to content
Draft
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
3 changes: 2 additions & 1 deletion FodyWeavers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<Costura>
<IncludeAssemblies>
Scriban
</IncludeAssemblies>
MoonSharp.Interpreter
</IncludeAssemblies>
</Costura>
</Weavers>
1 change: 1 addition & 0 deletions PolyMod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="Costura.Fody" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="Scriban" Version="6.2.1" />
<PackageReference Include="TheBattleOfPolytopia" Version="$(PolytopiaVersion)" />
<EmbeddedResource Include="resources\*.*" />
Expand Down
34 changes: 34 additions & 0 deletions src/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Text.RegularExpressions;
using UnityEngine;
using PolytopiaBackendBase.Common;
using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Interop;

namespace PolyMod;

Expand Down Expand Up @@ -342,6 +344,10 @@ internal static void LoadMods(Dictionary<string, Mod> mods, out bool dependencyC
{
LoadAssemblyFile(mod, file);
}
if (Path.GetExtension(file.name) == ".lua")
{
LoadLuaFile(mod, file);
}
if (Path.GetFileName(file.name) == "sprites.json")
{
LoadSpriteInfoFile(mod, file);
Expand Down Expand Up @@ -509,6 +515,34 @@ public static void LoadAssemblyFile(Mod mod, Mod.File file)
}
}

/// <summary>
/// Loads a lua file from a mod.
/// </summary>
/// <param name="mod">The mod the assembly belongs to.</param>
/// <param name="file">The assembly file to load.</param>
public static void LoadLuaFile(Mod mod, Mod.File file)
{
UserData.RegistrationPolicy = InteropRegistrationPolicy.Automatic;
Script script = new();
script.Globals["import"] = (Func<string, object>)(typeName =>
{
var type = AppDomain.CurrentDomain
.GetAssemblies()
.Select(a => a.GetType(typeName))
.FirstOrDefault(t => t != null);

if (type == null)
throw new Exception($"Type not found: {typeName}");

UserData.RegisterType(type);

return UserData.CreateStatic(type);
});
script.DoString(Encoding.UTF8.GetString(file.bytes));
DynValue load = script.Globals.Get("load");
script.Call(load);
}

/// <summary>
/// Loads a localization file from a mod.
/// </summary>
Expand Down