1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-02-04 15:12:17 +08:00

work in progress

This commit is contained in:
2025-12-18 19:39:33 +08:00
parent 0049629fd1
commit b537ff22be
21 changed files with 453 additions and 83 deletions

View File

@@ -0,0 +1,7 @@
## Changlog
* 1.0.0
+ Initial release
## 更新日志
* 1.0.0
+ 初始版本

View File

@@ -0,0 +1,232 @@
using System;
using System.Collections.Generic;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using NLua;
namespace LuaScriptEngine;
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class LuaScriptEngine : BaseUnityPlugin
{
private class Timer(LuaFunction func, long startInterval, long repeatInterval = 0L)
{
public bool Check(long gameTick)
{
if (gameTick < _nextTick) return false;
try
{
_func.Call();
}
catch (Exception e)
{
Logger.LogError($"Error in Lua script: {e}");
}
if (_repeatInterval <= 0L) return true;
_nextTick += _repeatInterval;
if (_nextTick < gameTick)
_nextTick = gameTick + 1;
return false;
}
public bool Reset(long gameTick)
{
if (_repeatInterval <= 0L) return true;
_nextTick = gameTick + _repeatInterval;
return false;
}
private readonly LuaFunction _func = func;
private readonly long _repeatInterval = repeatInterval;
private long _nextTick = GameMain.gameTick + startInterval;
}
public new static readonly ManualLogSource Logger =
BepInEx.Logging.Logger.CreateLogSource(PluginInfo.PLUGIN_NAME);
private Harmony _harmony;
private static readonly Lua LuaState = new();
private static readonly List<LuaFunction> PostDataLoadedFuncs = [];
private static readonly List<LuaFunction> PreUpdateFuncs = [];
private static readonly List<LuaFunction> PostUpdateFuncs = [];
private static readonly List<LuaFunction> PreGameBeginFuncs = [];
private static readonly List<LuaFunction> PostGameBeginFuncs = [];
private static readonly List<LuaFunction> PreGameEndFuncs = [];
private static readonly List<LuaFunction> PostGameEndFuncs = [];
private static readonly HashSet<Timer> Timers = [];
private static readonly List<Timer> TimersToRemove = [];
private void Awake()
{
LuaState.State.Encoding = Encoding.UTF8;
LuaState.LoadCLRPackage();
LuaState.DoString("import('Assembly-CSharp')");
LuaState["register_callback"] = (string tp, LuaFunction action) =>
{
switch (tp)
{
case "data_loaded":
PostDataLoadedFuncs.Add(action);
break;
case "pre_update":
PreUpdateFuncs.Add(action);
break;
case "post_update":
PostUpdateFuncs.Add(action);
break;
case "pre_game_begin":
PreGameBeginFuncs.Add(action);
break;
case "post_game_begin":
PostGameBeginFuncs.Add(action);
break;
case "pre_game_end":
PreGameEndFuncs.Add(action);
break;
case "post_game_end":
PostGameEndFuncs.Add(action);
break;
}
};
LuaState["add_timer"] = Timer(LuaFunction func, long firstInterval, long repeatInterval) =>
{
var timer = new Timer(func, firstInterval, repeatInterval);
Timers.Add(timer);
return timer;
};
LuaState["remove_timer"] = void (Timer timer) =>
{
Timers.Remove(timer);
};
var assemblyPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!,
"scripts"
);
LuaState.DoString($"package.path = '{assemblyPath.Replace('\\', '/')}/?.lua'");
foreach (var file in System.IO.Directory.GetFiles(assemblyPath, "*.lua"))
{
Logger.LogInfo($"Loading Lua script: {file}");
LuaState.DoFile(file);
}
_harmony = Harmony.CreateAndPatchAll(typeof(Patches));
}
private void OnDestroy()
{
Timers.Clear();
PreUpdateFuncs.Clear();
PostUpdateFuncs.Clear();
PreGameBeginFuncs.Clear();
PostGameBeginFuncs.Clear();
PreGameEndFuncs.Clear();
PostGameEndFuncs.Clear();
_harmony?.UnpatchSelf();
LuaState.Dispose();
}
private static class Patches
{
private static void LoopCall(List<LuaFunction> funcs)
{
foreach (var func in funcs)
{
try
{
func.Call();
}
catch (Exception e)
{
Logger.LogError($"Error in Lua script: {e}");
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(VFPreload), nameof(VFPreload.InvokeOnLoadWorkEnded))]
private static void VFPreload_InvokeOnLoadWorkEnded_Postfix()
{
LoopCall(PostDataLoadedFuncs);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.FixedUpdate))]
private static void GameMain_FixedUpdate_Prefix()
{
if (Timers.Count > 0)
{
var gameTick = GameMain.gameTick;
foreach (var timer in Timers)
{
if (timer == null || !timer.Check(gameTick)) continue;
TimersToRemove.Add(timer);
}
if (TimersToRemove.Count > 0)
{
foreach (var timer in TimersToRemove)
{
Timers.Remove(timer);
}
TimersToRemove.Clear();
}
}
LoopCall(PreUpdateFuncs);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.FixedUpdate))]
private static void GameMain_FixedUpdate_Postfix()
{
LoopCall(PostUpdateFuncs);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.Begin))]
private static void GameMain_Begin_Prefix()
{
var tick = GameMain.gameTick;
foreach (var timer in Timers)
{
if (timer.Reset(tick))
{
TimersToRemove.Add(timer);
}
}
if (TimersToRemove.Count > 0)
{
foreach (var timer in TimersToRemove)
{
Timers.Remove(timer);
}
TimersToRemove.Clear();
}
LoopCall(PreGameBeginFuncs);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.Begin))]
private static void GameMain_Begin_Postfix()
{
LoopCall(PostGameBeginFuncs);
}
[HarmonyPrefix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.End))]
private static void GameMain_End_Prefix()
{
LoopCall(PreGameEndFuncs);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameMain), nameof(GameMain.End))]
private static void GameMain_End_Postfix()
{
LoopCall(PostGameEndFuncs);
}
}
}

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>LuaScriptEngine</AssemblyName>
<BepInExPluginGuid>org.soardev.luascriptengine</BepInExPluginGuid>
<Description>DSP MOD - LuaScriptEngine</Description>
<Version>1.0.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<RestoreAdditionalProjectSources>https://nuget.bepinex.dev/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLua" Version="1.*" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
<!-- <PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" /> -->
<PackageReference Include="UnityEngine.Modules" Version="2022.3.53" IncludeAssets="compile" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\AssemblyFromGame\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>..\AssemblyFromGame\UnityEngine.UI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
<Exec Command="del /F /Q package\$(ProjectName)-$(Version).zip
powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', '$(TargetDir)/KeraLua.dll', '$(TargetDir)/lua54.dll', '$(TargetDir)/NLua.dll', package/icon.png, package/manifest.json, README.md" />
</Target>
</Project>

View File

@@ -0,0 +1,8 @@
# LuaScriptEngine
#### Write mod functions in lua scripts
#### 用lua脚本编写mod功能
## Usage
## 使用说明

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

View File

@@ -0,0 +1,9 @@
{
"name": "LuaScriptEngine",
"version_number": "1.0.0",
"website_url": "https://github.com/soarqin/DSP_Mods/tree/master/LuaScriptEngine",
"description": "Write mod functions in lua scripts / 用lua脚本编写mod功能",
"dependencies": [
"xiaoye97-BepInEx-5.4.17"
]
}