mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2025-12-08 19:33:27 +08:00
Work in progress
This commit is contained in:
@@ -29,6 +29,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -34,6 +34,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UXAssist", "UXAssist\UXAssi
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserCloak", "UserCloak\UserCloak.csproj", "{096D2E4B-D1CE-424D-9954-C36A23E9E279}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LuaScriptEngine", "LuaScriptEngine\LuaScriptEngine.csproj", "{E9375F0E-26DC-4CEC-80DC-9C48F23340BC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -96,5 +98,9 @@ Global
|
||||
{096D2E4B-D1CE-424D-9954-C36A23E9E279}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{096D2E4B-D1CE-424D-9954-C36A23E9E279}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{096D2E4B-D1CE-424D-9954-C36A23E9E279}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E9375F0E-26DC-4CEC-80DC-9C48F23340BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E9375F0E-26DC-4CEC-80DC-9C48F23340BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E9375F0E-26DC-4CEC-80DC-9C48F23340BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E9375F0E-26DC-4CEC-80DC-9C48F23340BC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -33,6 +33,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
|
||||
<Exec Command="del /F /Q package\$(ProjectName)-$(Version).zip
IF NOT EXIST package\plugins (mkdir package\plugins)
copy /y $(TargetPath) package\plugins\
cd package
powershell Compress-Archive -Force -DestinationPath '$(ProjectName)-$(Version).zip' -Path patchers, plugins, icon.png, manifest.json, ../README.md" />
|
||||
<Exec Command="IF NOT EXIST package\plugins (mkdir package\plugins)
copy /y $(TargetPath) package\plugins\
cd package
powershell Compress-Archive -Force -DestinationPath '$(ProjectName)-$(Version).zip' -Path patchers, plugins, icon.png, manifest.json, ../README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
|
||||
<Exec Command="del /F /Q package\$(ProjectName)-$(Version).zip
IF NOT EXIST package\plugins (mkdir package\plugins)
copy /y $(TargetPath) package\plugins\
cd package
powershell Compress-Archive -Force -DestinationPath '$(ProjectName)-$(Version).zip' -Path patchers, plugins, icon.png, manifest.json, ../README.md" />
|
||||
<Exec Command="IF NOT EXIST package\plugins (mkdir package\plugins)
copy /y $(TargetPath) package\plugins\
cd package
powershell Compress-Archive -Force -DestinationPath '$(ProjectName)-$(Version).zip' -Path patchers, plugins, icon.png, manifest.json, ../README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
7
LuaScriptEngine/CHANGELOG.md
Normal file
7
LuaScriptEngine/CHANGELOG.md
Normal file
@@ -0,0 +1,7 @@
|
||||
## Changlog
|
||||
* 1.0.0
|
||||
+ Initial release
|
||||
|
||||
## 更新日志
|
||||
* 1.0.0
|
||||
+ 初始版本
|
||||
198
LuaScriptEngine/LuaScriptEngine.cs
Normal file
198
LuaScriptEngine/LuaScriptEngine.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
{
|
||||
public Timer(LuaFunction func, long startInterval, long repeatInterval = 0L)
|
||||
{
|
||||
_func = func;
|
||||
_repeatInterval = repeatInterval;
|
||||
_nextTick = GameMain.gameTick + startInterval;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private readonly LuaFunction _func;
|
||||
private readonly long _repeatInterval;
|
||||
private long _nextTick;
|
||||
}
|
||||
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> 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 List<Timer> Timers = [];
|
||||
private static readonly List<int> RemovedTimers = [];
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
LuaState.LoadCLRPackage();
|
||||
LuaState.DoString("import('Assembly-CSharp')");
|
||||
LuaState["register_callback"] = (string tp, bool pre, LuaFunction action) =>
|
||||
{
|
||||
switch (tp)
|
||||
{
|
||||
case "update":
|
||||
(pre ? PreUpdateFuncs : PostUpdateFuncs).Add(action);
|
||||
break;
|
||||
case "game_begin":
|
||||
(pre ? PreGameBeginFuncs : PostGameBeginFuncs).Add(action);
|
||||
break;
|
||||
case "game_end":
|
||||
(pre ? PreGameEndFuncs : PostGameEndFuncs).Add(action);
|
||||
break;
|
||||
}
|
||||
};
|
||||
LuaState["add_timer"] = int(LuaFunction func, long firstInterval, long repeatInterval) =>
|
||||
{
|
||||
var timer = new Timer(func, firstInterval, repeatInterval);
|
||||
if (RemovedTimers.Count <= 0)
|
||||
{
|
||||
Timers.Add(timer);
|
||||
return Timers.Count - 1;
|
||||
}
|
||||
var index = RemovedTimers[RemovedTimers.Count - 1];
|
||||
Timers[index] = timer;
|
||||
RemovedTimers.RemoveAt(RemovedTimers.Count - 1);
|
||||
return index;
|
||||
};
|
||||
LuaState["remove_timer"] = void (int index) =>
|
||||
{
|
||||
if (index < 0 || index >= Timers.Count) return;
|
||||
Timers[index] = null;
|
||||
RemovedTimers.Add(index);
|
||||
};
|
||||
|
||||
var assemblyPath = System.IO.Path.Combine(
|
||||
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!,
|
||||
"scripts"
|
||||
);
|
||||
|
||||
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()
|
||||
{
|
||||
RemovedTimers.Clear();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HarmonyPrefix]
|
||||
[HarmonyPatch(typeof(GameMain), nameof(GameMain.FixedUpdate))]
|
||||
private static void GameMain_FixedUpdate_Prefix()
|
||||
{
|
||||
if (Timers.Count > 0)
|
||||
{
|
||||
var gameTick = GameMain.gameTick;
|
||||
for (var index = Timers.Count - 1; index >= 0; index--)
|
||||
{
|
||||
var timer = Timers[index];
|
||||
if (timer == null || !timer.Check(gameTick)) continue;
|
||||
Timers[index] = null;
|
||||
RemovedTimers.Add(index);
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
LuaScriptEngine/LuaScriptEngine.csproj
Normal file
30
LuaScriptEngine/LuaScriptEngine.csproj
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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="2018.4.12" IncludeAssets="compile" />
|
||||
</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="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>
|
||||
8
LuaScriptEngine/README.md
Normal file
8
LuaScriptEngine/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# LuaScriptEngine
|
||||
|
||||
#### Write mod functions in lua scripts
|
||||
#### 用lua脚本编写mod功能
|
||||
|
||||
## Usage
|
||||
|
||||
## 使用说明
|
||||
BIN
LuaScriptEngine/package/icon.png
Normal file
BIN
LuaScriptEngine/package/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
9
LuaScriptEngine/package/manifest.json
Normal file
9
LuaScriptEngine/package/manifest.json
Normal 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"
|
||||
]
|
||||
}
|
||||
@@ -24,6 +24,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -26,6 +26,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -37,6 +37,6 @@
|
||||
</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)", package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path "$(TargetPath)", package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -29,6 +29,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md, CHANGELOG.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
</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)', package/icon.png, package/manifest.json, README.md" />
|
||||
<Exec Command="powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', package/icon.png, package/manifest.json, README.md" />
|
||||
</Target>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user