refactor: no more dynvalues or userdatas

This commit is contained in:
Evil Factory
2021-08-29 00:16:33 -03:00
parent fdb9ca1d30
commit e24797dd05
16 changed files with 161 additions and 149 deletions
@@ -366,7 +366,7 @@ namespace Barotrauma
TaskPool.Update(); TaskPool.Update();
CoroutineManager.Update((float)Timing.Step, (float)Timing.Step); CoroutineManager.Update((float)Timing.Step, (float)Timing.Step);
GameMain.Lua.hook.Call("think", new DynValue[] { DynValue.NewNumber(elapsedTime), DynValue.NewNumber(Timing.TotalTime) }); GameMain.Lua.hook.Call("think", new object[] { elapsedTime, Timing.TotalTime });
Timing.Accumulator -= Timing.Step; Timing.Accumulator -= Timing.Step;
} }
@@ -31,28 +31,15 @@ namespace Barotrauma
private class LuaPlayer private class LuaPlayer
{ {
public static List<DynValue> GetAllCharacters() public static List<Character> GetAllCharacters()
{ {
List<DynValue> values = new List<DynValue>(); return Character.CharacterList;
foreach (Character ch in Character.CharacterList)
{
values.Add(UserData.Create(ch));
}
return values;
} }
public static List<DynValue> GetAllClients() public static List<Client> GetAllClients()
{ {
List<DynValue> values = new List<DynValue>();
foreach (Client ch in GameMain.Server.ConnectedClients) return GameMain.Server.ConnectedClients;
{
values.Add(UserData.Create(ch));
}
return values;
} }
public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced) public static CharacterInfo CreateCharacterInfo(string speciesName, string name = "", JobPrefab jobPrefab = null, string ragdollFileName = null, int variant = 0, Rand.RandSync randSync = Rand.RandSync.Unsynced)
@@ -364,12 +351,6 @@ namespace Barotrauma
env = e; env = e;
} }
public void Simple(int time, DynValue function)
{
Task.Delay(time).ContinueWith(o => { env.RunFunction(function); });
}
public static double GetTime() public static double GetTime()
{ {
return Timing.TotalTime; return Timing.TotalTime;
@@ -469,15 +450,6 @@ namespace Barotrauma
} }
} }
// hooks:
// chatMessage
// think
// update
// clientConnected
// clientDisconnected
// roundStart
// roundEnd
public class LuaHook public class LuaHook
{ {
public LuaSetup env; public LuaSetup env;
@@ -491,9 +463,9 @@ namespace Barotrauma
{ {
public string name; public string name;
public string hookName; public string hookName;
public DynValue function; public object function;
public HookFunction(string n, string hn, DynValue func) public HookFunction(string n, string hn, object func)
{ {
name = n; name = n;
hookName = hn; hookName = hn;
@@ -503,7 +475,7 @@ namespace Barotrauma
public List<HookFunction> hookFunctions = new List<HookFunction>(); public List<HookFunction> hookFunctions = new List<HookFunction>();
public void Add(string name, string hookName, DynValue function) public void Add(string name, string hookName, object function)
{ {
foreach (HookFunction hf in hookFunctions) foreach (HookFunction hf in hookFunctions)
{ {
@@ -518,7 +490,7 @@ namespace Barotrauma
hookFunctions.Add(new HookFunction(name, hookName, function)); hookFunctions.Add(new HookFunction(name, hookName, function));
} }
public DynValue Call(string name, DynValue[] args) public object Call(string name, object[] args)
{ {
foreach (HookFunction hf in hookFunctions) foreach (HookFunction hf in hookFunctions)
{ {
@@ -13,46 +13,6 @@ namespace Barotrauma
public static void RegisterAll() public static void RegisterAll()
{ {
/* // Vector 2
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Vector2),
dynVal => {
Table table = dynVal.Table;
float x = (float)((Double)table[1]);
float y = (float)((Double)table[2]);
return new Vector2(x, y);
}
);
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<Vector2>(
(script, vector) => {
DynValue x = DynValue.NewNumber((double)vector.X);
DynValue y = DynValue.NewNumber((double)vector.Y);
DynValue dynVal = DynValue.NewTable(script, new DynValue[] { x, y });
return dynVal;
}
);
// Vector3
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Vector3),
dynVal => {
Table table = dynVal.Table;
float x = (float)((Double)table[1]);
float y = (float)((Double)table[2]);
float z = (float)((Double)table[3]);
return new Vector3(x, y, z);
}
);
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<Vector3>(
(script, vector) => {
DynValue x = DynValue.NewNumber((double)vector.X);
DynValue y = DynValue.NewNumber((double)vector.Y);
DynValue z = DynValue.NewNumber((double)vector.Z);
DynValue dynVal = DynValue.NewTable(script, new DynValue[] { x, y, z });
return dynVal;
}
);*/
} }
} }
@@ -8,6 +8,7 @@ using Microsoft.Xna.Framework;
using System.Threading.Tasks; using System.Threading.Tasks;
using Barotrauma.Items.Components; using Barotrauma.Items.Components;
using System.Diagnostics; using System.Diagnostics;
using NLua;
namespace Barotrauma namespace Barotrauma
{ {
@@ -56,19 +57,6 @@ namespace Barotrauma
} }
} }
public void RunFunction(DynValue func)
{
try
{
lua.Call(func);
}
catch (Exception e)
{
HandleLuaException(e);
}
}
public DynValue DoFile(string file) public DynValue DoFile(string file)
{ {
try try
@@ -117,19 +117,18 @@ namespace Barotrauma.Networking
return; return;
} }
var should = GameMain.Lua.hook.Call("chatMessage", new DynValue[] { DynValue.NewString(txt), UserData.Create(c), UserData.Create(type) }); var should = GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type });
if(should != null) if(should != null)
{ {
if (should.CastToBool()) if (should is DynValue dyn)
{ {
return; if (dyn.CastToBool())
} {
else return;
{ }
}
}
} }
if (type == ChatMessageType.Order) if (type == ChatMessageType.Order)
@@ -310,7 +310,7 @@ namespace Barotrauma.Networking
SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient); SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient);
} }
GameMain.Lua.hook.Call("clientConnected", new MoonSharp.Interpreter.DynValue[] { MoonSharp.Interpreter.UserData.Create(newClient) }); GameMain.Lua.hook.Call("clientConnected", new object[] { newClient });
SendChatMessage($"ServerMessage.JoinedServer~[client]={clName}", ChatMessageType.Server, null, changeType: PlayerConnectionChangeType.Joined); SendChatMessage($"ServerMessage.JoinedServer~[client]={clName}", ChatMessageType.Server, null, changeType: PlayerConnectionChangeType.Joined);
@@ -2439,7 +2439,7 @@ namespace Barotrauma.Networking
roundStartTime = DateTime.Now; roundStartTime = DateTime.Now;
GameMain.Lua.hook.Call("roundStart", new MoonSharp.Interpreter.DynValue[] { }); GameMain.Lua.hook.Call("roundStart", new object[] { });
yield return CoroutineStatus.Success; yield return CoroutineStatus.Success;
@@ -2561,7 +2561,7 @@ namespace Barotrauma.Networking
Log("Ending the round...", ServerLog.MessageType.ServerMessage); Log("Ending the round...", ServerLog.MessageType.ServerMessage);
} }
GameMain.Lua.hook.Call("roundEnd", new MoonSharp.Interpreter.DynValue[] { }); GameMain.Lua.hook.Call("roundEnd", new object[] { });
string endMessage = TextManager.FormatServerMessage("RoundSummaryRoundHasEnded"); string endMessage = TextManager.FormatServerMessage("RoundSummaryRoundHasEnded");
@@ -2857,7 +2857,7 @@ namespace Barotrauma.Networking
{ {
if (client == null) return; if (client == null) return;
GameMain.Lua.hook.Call("clientDisconnected", new MoonSharp.Interpreter.DynValue[] { MoonSharp.Interpreter.UserData.Create(client) }); GameMain.Lua.hook.Call("clientDisconnected", new object[] { client });
if (gameStarted && client.Character != null) if (gameStarted && client.Character != null)
{ {
@@ -3107,10 +3107,18 @@ namespace Barotrauma.Networking
var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType); var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType);
var should = GameMain.Lua.hook.Call("modifyChatMessage", new DynValue[] { UserData.Create(hookChatMsg), LuaSetup.CreateUserDataSafe(senderRadio) }); var should = GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio });
if (should != null && should.CastToBool()) if (should != null)
return; {
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
//check which clients can receive the message and apply distance effects //check which clients can receive the message and apply distance effects
foreach (Client client in ConnectedClients) foreach (Client client in ConnectedClients)
@@ -94,10 +94,15 @@ namespace Barotrauma.Networking
ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) && ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) &&
ChatMessage.CanUseRadio(recipient.Character, out WifiComponent recipientRadio)) ChatMessage.CanUseRadio(recipient.Character, out WifiComponent recipientRadio))
{ {
var should = GameMain.Lua.hook.Call("canUseVoiceRadio", new DynValue[] { UserData.Create(sender), LuaSetup.CreateUserDataSafe(recipient) }); var should = GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient });
if (should != null) if (should != null)
return should.CastToBool(); {
if (should is DynValue dyn)
{
return dyn.CastToBool();
}
}
if (recipientRadio.CanReceive(senderRadio)) { return true; } if (recipientRadio.CanReceive(senderRadio)) { return true; }
} }
@@ -83,6 +83,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="MoonSharp.Debugger.VsCode" Version="2.0.0" /> <PackageReference Include="MoonSharp.Debugger.VsCode" Version="2.0.0" />
<PackageReference Include="NLua" Version="1.5.9" />
</ItemGroup> </ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 --> <!-- Sourced from https://stackoverflow.com/a/45248069 -->
@@ -714,11 +714,14 @@ namespace Barotrauma
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("changeFallDamage", new DynValue[] { DynValue.NewNumber(impactDamage), LuaSetup.CreateUserDataSafe(character), LuaSetup.CreateUserDataSafe(impactPos), LuaSetup.CreateUserDataSafe(velocity) }); var should = GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity });
if (should != null) if (should != null)
{ {
impactDamage = (float)should.CastToNumber(); if (should is DynValue dyn)
{
impactDamage = (float)dyn.CastToNumber();
}
} }
#endif #endif
@@ -3810,7 +3810,7 @@ namespace Barotrauma
#if SERVER #if SERVER
GameMain.Lua.hook.Call("characterDeath", new MoonSharp.Interpreter.DynValue[] { MoonSharp.Interpreter.UserData.Create(this) }); GameMain.Lua.hook.Call("characterDeath", new object[] { this });
#endif #endif
} }
partial void KillProjSpecific(CauseOfDeathType causeOfDeath, Affliction causeOfDeathAffliction, bool log); partial void KillProjSpecific(CauseOfDeathType causeOfDeath, Affliction causeOfDeathAffliction, bool log);
@@ -410,11 +410,17 @@ namespace Barotrauma
if (targetLimb == null) if (targetLimb == null)
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(affliction) }); var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -428,11 +434,17 @@ namespace Barotrauma
else else
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { UserData.Create(this), UserData.Create(affliction), UserData.Create(targetLimb) }); var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -442,11 +454,17 @@ namespace Barotrauma
else else
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { UserData.Create(this), UserData.Create(affliction)}); var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -527,11 +545,17 @@ namespace Barotrauma
} }
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(attackResult), LuaSetup.CreateUserDataSafe(hitLimb) }); var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -73,7 +73,7 @@ namespace Barotrauma
CrewManager?.Update(deltaTime); CrewManager?.Update(deltaTime);
#if SERVER #if SERVER
GameMain.Lua.hook.Call("update", new DynValue[] { DynValue.NewNumber(deltaTime) }); GameMain.Lua.hook.Call("update", new object[] { deltaTime });
#endif #endif
} }
@@ -441,7 +441,7 @@ namespace Barotrauma.Items.Components
#if SERVER #if SERVER
if (!lastSignal.ContainsValue(connection.Name) || lastSignal[connection.Name] != signal.value) if (!lastSignal.ContainsValue(connection.Name) || lastSignal[connection.Name] != signal.value)
{ {
GameMain.Lua.hook.Call("signalReceived", new DynValue[] { UserData.Create(signal), UserData.Create(connection) }); GameMain.Lua.hook.Call("signalReceived", new object[] { signal, connection });
lastSignal[connection.Name] = signal.value; lastSignal[connection.Name] = signal.value;
} }
@@ -187,10 +187,18 @@ namespace Barotrauma.Items.Components
if (senderComponent != null && !CanReceive(senderComponent)) { return; } if (senderComponent != null && !CanReceive(senderComponent)) { return; }
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("wifiSignalTransmitted", new DynValue[] { UserData.Create(this), UserData.Create(signal), DynValue.NewBoolean(sentFromChat) }); var should = GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat });
if (should != null && should.CastToBool()) if (should != null)
return; {
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif #endif
bool chatMsgSent = false; bool chatMsgSent = false;
@@ -2226,11 +2226,17 @@ namespace Barotrauma
if (condition == 0.0f) { return; } if (condition == 0.0f) { return; }
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemUse", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character), LuaSetup.CreateUserDataSafe(targetLimb) }); var should = GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -2268,11 +2274,17 @@ namespace Barotrauma
if (condition == 0.0f) { return; } if (condition == 0.0f) { return; }
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemSecondaryUse", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); var should = GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character});
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -2308,11 +2320,17 @@ namespace Barotrauma
public void ApplyTreatment(Character user, Character character, Limb targetLimb) public void ApplyTreatment(Character user, Character character, Limb targetLimb)
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemApplyTreatment", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(user), LuaSetup.CreateUserDataSafe(character), LuaSetup.CreateUserDataSafe(targetLimb) }); var should = GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -2373,11 +2391,17 @@ namespace Barotrauma
public void Drop(Character dropper, bool createNetworkEvent = true) public void Drop(Character dropper, bool createNetworkEvent = true)
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemDrop", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(dropper)}); var should = GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper});
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -2432,11 +2456,17 @@ namespace Barotrauma
public void Equip(Character character) public void Equip(Character character)
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemEquip", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); var should = GameMain.Lua.hook.Call("itemEquip", new object[] { this, character});
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -2452,11 +2482,17 @@ namespace Barotrauma
public void Unequip(Character character) public void Unequip(Character character)
{ {
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("itemUnequip", new DynValue[] { LuaSetup.CreateUserDataSafe(this), LuaSetup.CreateUserDataSafe(character)}); var should = GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character });
if (should != null && should.CastToBool()) if (should != null)
{ {
return; if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
} }
#endif #endif
@@ -651,10 +651,18 @@ namespace Barotrauma
} }
#if SERVER #if SERVER
var should = GameMain.Lua.hook.Call("gapOxygenUpdate", new DynValue[] { UserData.Create(this), UserData.Create(hull1), UserData.Create(hull2) }); var should = GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 });
if (should != null && should.CastToBool()) if (should != null)
return; {
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif #endif
float totalOxygen = hull1.Oxygen + hull2.Oxygen; float totalOxygen = hull1.Oxygen + hull2.Oxygen;