refactor hooks with new LuaResult

This commit is contained in:
Evil Factory
2021-09-21 17:11:49 -03:00
parent 784baf550f
commit b07e5d9b0b
9 changed files with 119 additions and 206 deletions
@@ -117,19 +117,13 @@ namespace Barotrauma.Networking
return; return;
} }
var should = GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type }); var should = new LuaResult(GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type }));
if(should != null) if (should.Bool())
{ {
if (should is DynValue dyn) return;
{ }
if (dyn.CastToBool())
{
return;
}
}
}
if (type == ChatMessageType.Order) if (type == ChatMessageType.Order)
{ {
@@ -3112,18 +3112,11 @@ 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 object[] { hookChatMsg, senderRadio }); var should = new LuaResult(GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio }));
if (should != null) if (should.Bool())
{ 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,29 +94,20 @@ 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 object[] { sender, recipient }); var should = new LuaResult(GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient }));
if (should != null) if (!should.IsNull() && should.Bool())
{ return should.Bool();
if (should is DynValue dyn)
{
return dyn.CastToBool();
}
}
if (recipientRadio.CanReceive(senderRadio)) { return true; } if (recipientRadio.CanReceive(senderRadio)) { return true; }
} }
var should2 = GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient }); var should2 = new LuaResult(GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient }));
float range = 1.0f; float range = 1.0f;
if (should2 != null) if (!should2.IsNull())
{ range = should2.Float();
if (should2 is DynValue dyn)
{
range = (float)dyn.CastToNumber();
}
}
//otherwise do a distance check //otherwise do a distance check
return ChatMessage.GetGarbleAmount(recipient.Character, sender.Character, ChatMessage.SpeakRange) < range; return ChatMessage.GetGarbleAmount(recipient.Character, sender.Character, ChatMessage.SpeakRange) < range;
@@ -712,18 +712,12 @@ namespace Barotrauma
float impactDamage = Math.Min((impact - ImpactTolerance) * ImpactDamageMultiplayer, character.MaxVitality * MaxImpactDamage); float impactDamage = Math.Min((impact - ImpactTolerance) * ImpactDamageMultiplayer, character.MaxVitality * MaxImpactDamage);
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }));
var should = GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }); if (!should.IsNull())
{
if (should != null) impactDamage = should.Float();
{
if (should is DynValue dyn)
{
impactDamage = (float)dyn.CastToNumber();
}
} }
#endif
character.LastDamageSource = null; character.LastDamageSource = null;
character.AddDamage(impactPos, AfflictionPrefab.ImpactDamage.Instantiate(impactDamage).ToEnumerable(), 0.0f, true); character.AddDamage(impactPos, AfflictionPrefab.ImpactDamage.Instantiate(impactDamage).ToEnumerable(), 0.0f, true);
@@ -409,20 +409,11 @@ namespace Barotrauma
{ {
if (targetLimb == null) if (targetLimb == null)
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }));
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
if (should.Bool())
return;
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
//if a limb-specific affliction is applied to no specific limb, apply to all limbs //if a limb-specific affliction is applied to no specific limb, apply to all limbs
foreach (LimbHealth limbHealth in limbHealths) foreach (LimbHealth limbHealth in limbHealths)
@@ -433,40 +424,20 @@ namespace Barotrauma
} }
else else
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb }));
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
AddLimbAffliction(targetLimb, affliction); AddLimbAffliction(targetLimb, affliction);
} }
} }
else else
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }));
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
AddAffliction(affliction); AddAffliction(affliction);
} }
@@ -544,20 +515,10 @@ namespace Barotrauma
return; return;
} }
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb }));
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
foreach (Affliction newAffliction in attackResult.Afflictions) foreach (Affliction newAffliction in attackResult.Afflictions)
{ {
@@ -186,20 +186,10 @@ namespace Barotrauma.Items.Components
var senderComponent = signal.source?.GetComponent<WifiComponent>(); var senderComponent = signal.source?.GetComponent<WifiComponent>();
if (senderComponent != null && !CanReceive(senderComponent)) { return; } if (senderComponent != null && !CanReceive(senderComponent)) { return; }
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat }));
var should = GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
bool chatMsgSent = false; bool chatMsgSent = false;
@@ -2225,20 +2225,10 @@ namespace Barotrauma
if (condition == 0.0f) { return; } if (condition == 0.0f) { return; }
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb }));
var should = GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
bool remove = false; bool remove = false;
@@ -2273,20 +2263,11 @@ namespace Barotrauma
{ {
if (condition == 0.0f) { return; } if (condition == 0.0f) { return; }
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character}));
var should = GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character});
if (should.Bool())
return;
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
bool remove = false; bool remove = false;
@@ -2319,20 +2300,10 @@ namespace Barotrauma
public void ApplyTreatment(Character user, Character character, Limb targetLimb) public void ApplyTreatment(Character user, Character character, Limb targetLimb)
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb }));
var should = GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
//can't apply treatment to dead characters //can't apply treatment to dead characters
if (character.IsDead) return; if (character.IsDead) return;
@@ -2390,20 +2361,10 @@ namespace Barotrauma
public void Drop(Character dropper, bool createNetworkEvent = true) public void Drop(Character dropper, bool createNetworkEvent = true)
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper}));
var should = GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper});
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (createNetworkEvent) if (createNetworkEvent)
{ {
@@ -2455,20 +2416,10 @@ namespace Barotrauma
public void Equip(Character character) public void Equip(Character character)
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemEquip", new object[] { this, character}));
var should = GameMain.Lua.hook.Call("itemEquip", new object[] { this, character});
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (Removed) if (Removed)
{ {
@@ -2481,20 +2432,10 @@ namespace Barotrauma
public void Unequip(Character character) public void Unequip(Character character)
{ {
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character }));
var should = GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
foreach (ItemComponent ic in components) { ic.Unequip(character); } foreach (ItemComponent ic in components) { ic.Unequip(character); }
} }
@@ -559,8 +559,12 @@ namespace Barotrauma
try try
{ {
if (hf.function is Closure) if (hf.function is Closure)
lastResult = env.lua.Call(hf.function, args); {
// else if (hf.function is NLua.LuaFunction luaFunction) var result = env.lua.Call(hf.function, args);
if (result.CastToBool() != false)
lastResult = result;
}
//else if (hf.function is NLua.LuaFunction luaFunction)
// lastResult = luaFunction.Call(args); // lastResult = luaFunction.Call(args);
} }
catch (Exception e) catch (Exception e)
@@ -572,5 +576,60 @@ namespace Barotrauma
return lastResult; return lastResult;
} }
} }
}
public class LuaResult
{
object result;
public LuaResult(object arg)
{
result = arg;
}
public bool IsNull()
{
if (result == null)
return true;
if (result is DynValue dynValue)
return dynValue.IsNil();
return false;
}
public bool Bool()
{
if (result is DynValue dynValue)
{
return dynValue.CastToBool();
}
return false;
}
public float Float()
{
if (result is DynValue dynValue)
{
var num = dynValue.CastToNumber();
if (num == null) { return 0f; }
return (float)num;
}
return 0f;
}
public double Double()
{
if (result is DynValue dynValue)
{
var num = dynValue.CastToNumber();
if (num == null) { return 0f; }
return (double)num;
}
return 0f;
}
} }
} }
@@ -650,20 +650,10 @@ namespace Barotrauma
if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; } if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; }
} }
#if SERVER var should = new LuaResult(GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 }));
var should = GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 });
if (should != null) if (should.Bool())
{ return;
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
float totalOxygen = hull1.Oxygen + hull2.Oxygen; float totalOxygen = hull1.Oxygen + hull2.Oxygen;
float totalVolume = (hull1.Volume + hull2.Volume); float totalVolume = (hull1.Volume + hull2.Volume);