From a2f8b9582931da2e80d7b3b861e1c9f83527f4ad Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Wed, 12 Apr 2023 02:51:19 +0430 Subject: [PATCH 01/14] Update name property description --- SlipeServer.Server/Elements/Element.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SlipeServer.Server/Elements/Element.cs b/SlipeServer.Server/Elements/Element.cs index 914423e9..715ce343 100644 --- a/SlipeServer.Server/Elements/Element.cs +++ b/SlipeServer.Server/Elements/Element.cs @@ -83,7 +83,7 @@ public ElementId Id private string name = ""; /// - /// The element name, for Player's this is the nickname, for other elements it's mostly unused. + /// The element name, for Player's this is the nickname, for custom elements this will be used as type /// public string Name { From 3853a95b3d4a286c02ca56b9b8d45b2c5fbfd490 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Wed, 12 Apr 2023 03:21:04 +0430 Subject: [PATCH 02/14] First steps --- .../Definitions/ElementScriptDefinitions.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 218f2e7b..0229643f 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -1,5 +1,6 @@ using SlipeServer.Server.Elements; using System.Numerics; +using MoonSharp.Interpreter; namespace SlipeServer.Scripting.Definitions; @@ -25,4 +26,11 @@ public class ElementScriptDefinitions [ScriptFunctionDefinition("getElementType")] public string GetElementType(Element element) => element.ElementType.ToString().ToLower(); + + [ScriptFunctionDefinition("createElement")] + public Element CreateElement(string type) + { + + return new object() as Element; + } } From 1c4c7880c3a89c6c24cdded82e1c75b5372f716e Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Wed, 12 Apr 2023 12:31:57 +0430 Subject: [PATCH 03/14] Some minor changes --- .../Definitions/ElementScriptDefinitions.cs | 13 ++++++++++++- SlipeServer.Scripting/SlipeServer.Scripting.csproj | 4 ++++ SlipeServer.Server/Elements/Enums/ElementType.cs | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 0229643f..ae2a5e79 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -1,6 +1,10 @@ using SlipeServer.Server.Elements; using System.Numerics; using MoonSharp.Interpreter; +using SlipeServer.Server.ElementCollections; +using System.Collections.Generic; +using System.ComponentModel.Design; +using System.Linq; namespace SlipeServer.Scripting.Definitions; @@ -28,8 +32,15 @@ public class ElementScriptDefinitions public string GetElementType(Element element) => element.ElementType.ToString().ToLower(); [ScriptFunctionDefinition("createElement")] - public Element CreateElement(string type) + public Element CreateElement(BasicCompoundElementCollection elementCollection, string type, int? elementID = null) { + IEnumerable allElements= elementCollection.GetAll(); + IEnumerable? elementType = from element in allElements where element.Name == type select element; + + if (elementType != null) + { + var actualElementType = elementType.FirstOrDefault(); + } return new object() as Element; } diff --git a/SlipeServer.Scripting/SlipeServer.Scripting.csproj b/SlipeServer.Scripting/SlipeServer.Scripting.csproj index 6bd82fcc..1882fd37 100644 --- a/SlipeServer.Scripting/SlipeServer.Scripting.csproj +++ b/SlipeServer.Scripting/SlipeServer.Scripting.csproj @@ -31,4 +31,8 @@ + + + + diff --git a/SlipeServer.Server/Elements/Enums/ElementType.cs b/SlipeServer.Server/Elements/Enums/ElementType.cs index ef8cb7cf..cd50baf3 100644 --- a/SlipeServer.Server/Elements/Enums/ElementType.cs +++ b/SlipeServer.Server/Elements/Enums/ElementType.cs @@ -23,5 +23,5 @@ public enum ElementType Weapon, DatabaseConnection, Root, - Unknown + Unknown, } From 3bf98a3f9c9fb21feb6e388a49192f4e3891c352 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 09:11:08 +0430 Subject: [PATCH 04/14] add CustomElement class --- .../Resources/SecondTestResource/test.lua | 2 + .../Definitions/ElementScriptDefinitions.cs | 54 ++++++- SlipeServer.Server/Elements/CustomElement.cs | 31 ++++ .../Elements/Enums/ElementType.cs | 1 + .../Extensions/StringExtensions.cs | 140 +++++++++++++++++- 5 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 SlipeServer.Server/Elements/CustomElement.cs diff --git a/SlipeServer.Console/Resources/SecondTestResource/test.lua b/SlipeServer.Console/Resources/SecondTestResource/test.lua index 0a529bba..4bd5b255 100644 --- a/SlipeServer.Console/Resources/SecondTestResource/test.lua +++ b/SlipeServer.Console/Resources/SecondTestResource/test.lua @@ -1 +1,3 @@ outputChatBox("I am a second resource") + + diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index ae2a5e79..d25530e5 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -5,11 +5,45 @@ using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; +using SlipeServer.Server; +using System.Text.RegularExpressions; +using System; +using System.Collections; +using SlipeServer.Server.Elements.ColShapes; +using System.Runtime.InteropServices; namespace SlipeServer.Scripting.Definitions; public class ElementScriptDefinitions { + private readonly MtaServer server; + private readonly IDictionary elementsForVariants; + + public ElementScriptDefinitions(MtaServer _server) + { + this.server = _server; + this.elementsForVariants = new Dictionary(Enum.GetNames(typeof(ElementType)).Length) + { + [ElementType.Player] = typeof(Player), + [ElementType.Weapon] = typeof(WeaponObject), + [ElementType.Pickup] = typeof(Pickup), + [ElementType.Blip] = typeof(Blip), + [ElementType.Colshape] = typeof(CollisionShape), + [ElementType.Console] = typeof(Console), + [ElementType.Dummy] = typeof(DummyElement), + [ElementType.Marker] = typeof(Marker), + [ElementType.Object] = typeof(Object), + [ElementType.Ped] = typeof(Ped), + [ElementType.RadarArea] = typeof(RadarArea), + [ElementType.Team] = typeof(Team), + [ElementType.Vehicle] = typeof(Vehicle), + [ElementType.Water] = typeof(Water), + // TBD: [ElementType.WorldMeshUnused] = ? + // TBD: [ElementType.PathNodeUnused] = + // TBD: [ElementType.DatabaseConnection] = ? + }; + } + [ScriptFunctionDefinition("destroyElement")] public void DestroyElement(Element element) => element.Destroy(); @@ -32,16 +66,26 @@ public class ElementScriptDefinitions public string GetElementType(Element element) => element.ElementType.ToString().ToLower(); [ScriptFunctionDefinition("createElement")] - public Element CreateElement(BasicCompoundElementCollection elementCollection, string type, int? elementID = null) + public Element CreateElement(string type, int? elementID = null) { + BasicCompoundElementCollection elementCollection = (BasicCompoundElementCollection)this.server.GetRequiredService(); + IEnumerable allElements= elementCollection.GetAll(); - IEnumerable? elementType = from element in allElements where element.Name == type select element; - if (elementType != null) + Element newElement; + + if (Enum.IsDefined(typeof(ElementType), type)) + { + Type classType = (Type)this.elementsForVariants[Enum.Parse(type)]; + newElement = (Element)Activator.CreateInstance(classType); + // TODO: Maybe there's a better alternative to handle this + if (newElement is null) + newElement = new CustomElement(type); + } else { - var actualElementType = elementType.FirstOrDefault(); + newElement = new CustomElement(type); } - return new object() as Element; + return newElement; } } diff --git a/SlipeServer.Server/Elements/CustomElement.cs b/SlipeServer.Server/Elements/CustomElement.cs new file mode 100644 index 00000000..359f2f0b --- /dev/null +++ b/SlipeServer.Server/Elements/CustomElement.cs @@ -0,0 +1,31 @@ +using SlipeServer.Server.Elements.Events; +using System.Drawing; +using System.Numerics; + +namespace SlipeServer.Server.Elements; + +/// +/// A radar area element +/// Radar areas are visual rectangles on the F11 map and ingame radar. +/// +public class CustomElement : Element +{ + public override ElementType ElementType => ElementType.Custom; + + public CustomElement() + { + this.Name = "UnknownCustom"; + } + + public CustomElement(string type) + { + this.Name = type; + } + + public new CustomElement AssociateWith(MtaServer server) + { + base.AssociateWith(server); + return this; + } + +} diff --git a/SlipeServer.Server/Elements/Enums/ElementType.cs b/SlipeServer.Server/Elements/Enums/ElementType.cs index cd50baf3..20a467d7 100644 --- a/SlipeServer.Server/Elements/Enums/ElementType.cs +++ b/SlipeServer.Server/Elements/Enums/ElementType.cs @@ -24,4 +24,5 @@ public enum ElementType DatabaseConnection, Root, Unknown, + Custom, } diff --git a/SlipeServer.Server/Extensions/StringExtensions.cs b/SlipeServer.Server/Extensions/StringExtensions.cs index 2f198efb..7ebec467 100644 --- a/SlipeServer.Server/Extensions/StringExtensions.cs +++ b/SlipeServer.Server/Extensions/StringExtensions.cs @@ -1,4 +1,7 @@ -using System.Text.RegularExpressions; +using System; +using System.Drawing; +using System.Reflection.PortableExecutable; +using System.Text.RegularExpressions; namespace SlipeServer.Server.Extensions; @@ -16,4 +19,139 @@ public static string StripColorCode(this string value) temp = Regex.Replace(temp, colorCodeRegex, ""); return temp; } + /// + /// Calculate and return this strings hash + /// + /// Returns Hash for this string + public static nuint GetHash(this string value) + { + UIntPtr a, b, c; // Temporary variables + int len = value.Length; // Length of the string left + + a = b = 0x9e3779b9; + c = 0xabcdef89; + + int start = 0; + int end = 12; + + + while (len >= 12) + { + var _values = value.ToCharArray(start, end); + a += (_values[0] + ((uint)_values[1] << 8) + (uint)(_values[2] << 16) + (uint)(_values[3] << 24)); + b += (_values[4] + ((uint)_values[5] << 8) + (uint)(_values[6] << 16) + (uint)(_values[7] << 24)); + c += (_values[8] + ((uint)_values[9] << 8) + (uint)(_values[10] << 16) + (uint)(_values[11] << 24)); + + // Mix + a -= b; + a -= c; + a ^= (c >> 13); + b -= c; + b -= a; + b ^= (a << 8); + c -= a; + c -= b; + c ^= (b >> 13); + a -= b; + a -= c; + a ^= (c >> 12); + b -= c; + b -= a; + b ^= (a << 16); + c -= a; + c -= b; + c ^= (b >> 5); + a -= b; + a -= c; + a ^= (c >> 3); + b -= c; + b -= a; + b ^= (a << 10); + c -= a; + c -= b; + c ^= (b >> 15); + + start += 12; + end += 12; + len -= 12; + } + + // Handle the last 11 remaining bytes + // Note: All cases fall through + + c += (nuint)value.Length; // Lower byte of c gets used for length + + var values = value.ToCharArray(0, 12); + + switch (len) + { + case 11: + c += ((uint)values[10] << 24); + goto case 10; + case 10: + c += ((uint)values[9] << 16); + goto case 9; + case 9: + c += ((uint)values[8] << 8); + goto case 8; + case 8: + b += ((uint)values[7] << 24); + goto case 7; + case 7: + b += ((uint)values[6] << 16); + goto case 6; + case 6: + b += ((uint)values[5] << 8); + goto case 5; + case 5: + b += values[4]; + goto case 4; + case 4: + a += ((uint)values[3] << 24); + goto case 3; + case 3: + a += ((uint)values[2] << 16); + goto case 2; + case 2: + a += ((uint)values[1] << 8); + goto case 1; + case 1: + a += values[0]; + goto default; + default: + break; + } + + + // Mix + a -= b; + a -= c; + a ^= (c >> 13); + b -= c; + b -= a; + b ^= (a << 8); + c -= a; + c -= b; + c ^= (b >> 13); + a -= b; + a -= c; + a ^= (c >> 12); + b -= c; + b -= a; + b ^= (a << 16); + c -= a; + c -= b; + c ^= (b >> 5); + a -= b; + a -= c; + a ^= (c >> 3); + b -= c; + b -= a; + b ^= (a << 10); + c -= a; + c -= b; + c ^= (b >> 15); + + return c; + } } From d08a8d28dffa9e6e3affd16fa24f7c2c80579878 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 18:23:19 +0430 Subject: [PATCH 05/14] Add lua tests for createElement --- SlipeServer.Console/test.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/SlipeServer.Console/test.lua b/SlipeServer.Console/test.lua index 024097cc..a3f3cdb9 100644 --- a/SlipeServer.Console/test.lua +++ b/SlipeServer.Console/test.lua @@ -1,4 +1,9 @@ if(isSlipeServer)then + local customElement = createElement("MyCustomType") + iprint("Custom Element ", customElement) + + iprint("This is a message on server printed using iprint", "This is the second Argument from iprint") + local object = createObject(321, 5, 5, 5) setElementPosition(object, 50, 50, 250) setElementRotation(object, 180, 180, 90) @@ -20,6 +25,8 @@ outputDebugString("Debug message, elapsed time: "..getTickCount()) + print("iprint is ", iprint) + print("base64 test:", base64Encode("sample text"), base64Decode(base64Encode("sample text"))) print("Some color: ", tocolor(235,23,77,159), tocolor(235,23,77,159) == -1611983027) print("getColorFromString: ", getColorFromString("#ff0000")); @@ -81,4 +88,5 @@ addCommandHandler("foo3", commandHandler3) addCommandHandler("foo4", commandHandler2) addCommandHandler("foo4", commandHandler3) removeCommandHandler("foo3", commandHandler2) -removeCommandHandler("foo4") \ No newline at end of file +removeCommandHandler("foo4") + From 4c92d66f8a2722251ddb5cd567895e4e87be84c5 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 18:25:30 +0430 Subject: [PATCH 06/14] Use partial class and allow for more resources --- .../AdditionalResources/Parachute/ServerBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SlipeServer.Console/AdditionalResources/Parachute/ServerBuilderExtensions.cs b/SlipeServer.Console/AdditionalResources/Parachute/ServerBuilderExtensions.cs index fbed20f7..41e7012b 100644 --- a/SlipeServer.Console/AdditionalResources/Parachute/ServerBuilderExtensions.cs +++ b/SlipeServer.Console/AdditionalResources/Parachute/ServerBuilderExtensions.cs @@ -2,7 +2,7 @@ using SlipeServer.Server.ServerBuilders; namespace SlipeServer.Console.AdditionalResources; -public static class ServerBuilderExtensions +public static partial class ServerBuilderExtensions { public static void AddParachuteResource(this ServerBuilder builder) { From 99cd537c81f18ccc7c60cd310b55453e88e655e9 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 18:27:10 +0430 Subject: [PATCH 07/14] add createElement Lua Definition --- SlipeServer.Console/Logic/LuaTestLogic.cs | 3 +- SlipeServer.Console/Program.cs | 1 + .../Resources/SecondTestResource/test.lua | 4 +-- SlipeServer.Lua/LuaTranslator.cs | 35 +++++++++++++++++++ .../Definitions/DebugScriptDefinitions.cs | 27 +++++++++++++- .../Definitions/ElementScriptDefinitions.cs | 10 +++--- 6 files changed, 71 insertions(+), 9 deletions(-) diff --git a/SlipeServer.Console/Logic/LuaTestLogic.cs b/SlipeServer.Console/Logic/LuaTestLogic.cs index 5b2e1577..5384c47d 100644 --- a/SlipeServer.Console/Logic/LuaTestLogic.cs +++ b/SlipeServer.Console/Logic/LuaTestLogic.cs @@ -24,7 +24,8 @@ ILogger logger this.eventRuntime = eventRuntime; this.luaService = luaService; this.logger = logger; - commandService.AddCommand("lua").Triggered += (source, args) => Init(); + //commandService.AddCommand("lua").Triggered += (source, args) => Init(); + Init(); } private void Init() diff --git a/SlipeServer.Console/Program.cs b/SlipeServer.Console/Program.cs index c83cbddb..b2d6dc1e 100644 --- a/SlipeServer.Console/Program.cs +++ b/SlipeServer.Console/Program.cs @@ -14,6 +14,7 @@ using SlipeServer.Server; using SlipeServer.Server.Loggers; using SlipeServer.Server.PacketHandling.Handlers.Middleware; +using SlipeServer.Server.Resources.Providers; using SlipeServer.Server.ServerBuilders; using System; using System.Threading; diff --git a/SlipeServer.Console/Resources/SecondTestResource/test.lua b/SlipeServer.Console/Resources/SecondTestResource/test.lua index 4bd5b255..51183cc1 100644 --- a/SlipeServer.Console/Resources/SecondTestResource/test.lua +++ b/SlipeServer.Console/Resources/SecondTestResource/test.lua @@ -1,3 +1 @@ -outputChatBox("I am a second resource") - - +outputChatBox("This is the second resource") diff --git a/SlipeServer.Lua/LuaTranslator.cs b/SlipeServer.Lua/LuaTranslator.cs index cace3cbc..75954b86 100644 --- a/SlipeServer.Lua/LuaTranslator.cs +++ b/SlipeServer.Lua/LuaTranslator.cs @@ -77,6 +77,7 @@ public IEnumerable ToDynValues(object? obj) if (obj is IEnumerable enumerable) return enumerable.Select(x => ToDynValues(x)).SelectMany(x => x).ToArray(); + throw new NotImplementedException($"Conversion to Lua for {obj.GetType()} not implemented"); } @@ -133,6 +134,39 @@ public object FromDynValue(Type targetType, Queue dynValues) return GetTableFromDynValue(dynValues.Dequeue()); if (typeof(Element).IsAssignableFrom(targetType)) return dynValues.Dequeue().UserData.Object; + if (targetType == typeof(IEnumerable)) + { + List args = new List { }; + + foreach (DynValue arg in dynValues) + { + args.Add(GetStringFromDynValue(arg)); + } + return args; + } + if (targetType == typeof(IEnumerable)) + { + List args = new List(); + + foreach (DynValue arg in dynValues) + { + switch (arg.Type) + { + case DataType.String: + args.Add(GetStringFromDynValue(arg)); + break; + case DataType.UserData: + var obj = (Element)arg.UserData.Object; + args.Add(obj.Name + ":" + obj.GetHashCode()); + break; + case DataType.Number: + // TODO: This may result in data loss [Add more cases of number conversion] + args.Add(GetInt32FromDynValue(arg)); + break; + } + } + return args; + } if (targetType == typeof(ScriptCallbackDelegateWrapper)) { var callback = dynValues.Dequeue().Function; @@ -144,6 +178,7 @@ public object FromDynValue(Type targetType, Queue dynValues) return (EventDelegate)((element, parameters) => callback.Call(new DynValue[] { UserData.Create(element) }.Concat(ToDynValues(parameters)))); } + throw new NotImplementedException($"Conversion from Lua for {targetType} not implemented"); } } diff --git a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs index ad97134b..efc061f0 100644 --- a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs @@ -1,5 +1,10 @@ using Microsoft.Extensions.Logging; +using MoonSharp.Interpreter; using SlipeServer.Server.Services; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; namespace SlipeServer.Scripting.Definitions; @@ -21,5 +26,25 @@ public void OutputDebugString(string message) this.logger.LogDebug(message); } - + [ScriptFunctionDefinition("iprint")] + public void Print(IEnumerable toPrint) + { + toPrint = (List)toPrint; + StringBuilder sb = new StringBuilder(); + int i = 0; + int len = toPrint.Count(); + foreach (var printable in toPrint) + { + string? stringRepresentation = printable.ToString(); + sb.Append(stringRepresentation ?? "NoStringRepresentation"); + if (i != len-1) + { + sb.Append(", "); + } + i++; + } + string message = sb.ToString(); + this.debugLog.Output(message); + this.logger.LogInformation(message); + } } diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index d25530e5..881f877f 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -18,10 +18,12 @@ public class ElementScriptDefinitions { private readonly MtaServer server; private readonly IDictionary elementsForVariants; + private readonly IElementCollection elementCollection; - public ElementScriptDefinitions(MtaServer _server) + public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection) { this.server = _server; + this.elementCollection = (RTreeCompoundElementCollection)elementCollection; this.elementsForVariants = new Dictionary(Enum.GetNames(typeof(ElementType)).Length) { [ElementType.Player] = typeof(Player), @@ -68,9 +70,7 @@ public ElementScriptDefinitions(MtaServer _server) [ScriptFunctionDefinition("createElement")] public Element CreateElement(string type, int? elementID = null) { - BasicCompoundElementCollection elementCollection = (BasicCompoundElementCollection)this.server.GetRequiredService(); - - IEnumerable allElements= elementCollection.GetAll(); + //BasicCompoundElementCollection elementCollection = (BasicCompoundElementCollection)this.server.GetRequiredService(); Element newElement; @@ -86,6 +86,8 @@ public Element CreateElement(string type, int? elementID = null) newElement = new CustomElement(type); } + elementCollection.Add(newElement); + return newElement; } } From 8a537242cfc5bc318a3b2bdbe36556475b60eda4 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 18:44:42 +0430 Subject: [PATCH 08/14] Add detachElements --- .../Definitions/ElementScriptDefinitions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 881f877f..2beefc8d 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -90,4 +90,10 @@ public Element CreateElement(string type, int? elementID = null) return newElement; } + + [ScriptFunctionDefinition("detachElements")] + public void DeatchElements(Element childElement, Element? detachFrom = null) + { + childElement.DetachFrom(detachFrom ?? null); + } } From 08e4860f972f0563a8af417b6961a2186547a527 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Mon, 17 Apr 2023 19:52:45 +0430 Subject: [PATCH 09/14] Add getAllElementData --- SlipeServer.Console/Logic/LuaTestLogic.cs | 2 +- SlipeServer.Lua/LuaService.cs | 6 ++++-- .../Definitions/ElementScriptDefinitions.cs | 19 ++++++++++++++++++- SlipeServer.Server/Elements/Element.cs | 5 +++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/SlipeServer.Console/Logic/LuaTestLogic.cs b/SlipeServer.Console/Logic/LuaTestLogic.cs index 5384c47d..1fbdc307 100644 --- a/SlipeServer.Console/Logic/LuaTestLogic.cs +++ b/SlipeServer.Console/Logic/LuaTestLogic.cs @@ -32,7 +32,7 @@ private void Init() { this.eventRuntime.LoadDefaultEvents(); - this.luaService.LoadDefaultDefinitions(); + //this.luaService.LoadDefaultDefinitions(); this.luaService.LoadDefinitions(); this.luaService.LoadDefinitions(); diff --git a/SlipeServer.Lua/LuaService.cs b/SlipeServer.Lua/LuaService.cs index 9a0069d9..e8af513b 100644 --- a/SlipeServer.Lua/LuaService.cs +++ b/SlipeServer.Lua/LuaService.cs @@ -84,14 +84,14 @@ public void LoadDefinitions() LoadDefinitions(this.server.Instantiate()!); } - public void LoadDefaultDefinitions() + public void LoadDefaultDefinitions(Script script) { foreach (var type in typeof(ScriptFunctionDefinitionAttribute).Assembly.DefinedTypes .Where(type => type.GetMethods() .Any(method => method.CustomAttributes .Any(attribute => attribute.AttributeType == typeof(ScriptFunctionDefinitionAttribute))))) { - LoadDefinitions(this.server.Instantiate(type)); + LoadDefinitions(this.server.Instantiate(type, script)); } } @@ -145,6 +145,8 @@ private void LoadDefinitions(Script script) script.Globals["real" + definition.Key] = definition.Value; stringBuilder.AppendLine($"function {definition.Key}(...) return table.unpack(real{definition.Key}({{...}})) end"); } + LoadDefaultDefinitions(script); + script.DoString(stringBuilder.ToString(), codeFriendlyName: "SlipeDefinitions"); } diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 2beefc8d..d7d4c482 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -19,11 +19,13 @@ public class ElementScriptDefinitions private readonly MtaServer server; private readonly IDictionary elementsForVariants; private readonly IElementCollection elementCollection; + private readonly Script ownerScript; - public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection) + public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection, Script ownerScript) { this.server = _server; this.elementCollection = (RTreeCompoundElementCollection)elementCollection; + this.ownerScript = ownerScript; this.elementsForVariants = new Dictionary(Enum.GetNames(typeof(ElementType)).Length) { [ElementType.Player] = typeof(Player), @@ -96,4 +98,19 @@ public void DeatchElements(Element childElement, Element? detachFrom = null) { childElement.DetachFrom(detachFrom ?? null); } + + [ScriptFunctionDefinition("getAllElementData")] + public Table GetAllElementData(Element element) + { + var elementDatas = element.GetAllElementData(); + DynValue theTable = DynValue.NewTable(ownerScript); + + foreach ( var elementData in elementDatas ) + { + theTable.Table.Set(elementData.Key, DynValue.FromObject(ownerScript, elementData.Value)); + } + + + return theTable.Table; + } } diff --git a/SlipeServer.Server/Elements/Element.cs b/SlipeServer.Server/Elements/Element.cs index 715ce343..2c9c8b88 100644 --- a/SlipeServer.Server/Elements/Element.cs +++ b/SlipeServer.Server/Elements/Element.cs @@ -679,6 +679,11 @@ public void SetData(string key, LuaValue value, DataSyncType syncType = DataSync return null; } + public IDictionary GetAllElementData() + { + return this.ElementData; + } + /// /// Subscribes a player to changes to a specific element data value /// From ce19e8bc91080f3c9aa6f96b62c8d5a8c42c2b92 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Tue, 18 Apr 2023 03:22:16 +0430 Subject: [PATCH 10/14] Add setElementData --- .../Definitions/ElementScriptDefinitions.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index d7d4c482..2606b629 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -11,6 +11,9 @@ using System.Collections; using SlipeServer.Server.Elements.ColShapes; using System.Runtime.InteropServices; +using Microsoft.Extensions.Primitives; +using SlipeServer.Server.Elements.Enums; +using System.ComponentModel; namespace SlipeServer.Scripting.Definitions; @@ -113,4 +116,15 @@ public Table GetAllElementData(Element element) return theTable.Table; } + + [ScriptFunctionDefinition("setElementData")] + public void SetElementData(Element element, string key, DynValue dynValue, bool synchronizeType = true) + { + Dictionary syncTypes = new() + { + [true] = DataSyncType.Broadcast, + [false] = DataSyncType.Local, + }; + element.SetData(key, dynValue.String, syncTypes[synchronizeType]); + } } From a8b9509d5c8efc2a8b2d5c6aae16a67d04d748dd Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Tue, 18 Apr 2023 03:47:05 +0430 Subject: [PATCH 11/14] Some changes for Scripting project --- SlipeServer.Console/test.lua | 6 + .../Definitions/DebugScriptDefinitions.cs | 2 +- .../Definitions/ElementScriptDefinitions.cs | 18 +- .../Definitions/EventScriptDefinitions.cs | 5 +- .../Definitions/ExplosionScriptDefinitions.cs | 5 +- .../Definitions/InputScriptDefinitions.cs | 6 +- .../Definitions/ObjectScriptDefinitions.cs | 5 +- .../Definitions/RadarAreaScriptDefinitions.cs | 5 +- .../Definitions/UtilityScriptDefinitions.cs | 5 +- SlipeServer.Scripting/LuaTranslatorUtility.cs | 177 ++++++++++++++++++ 10 files changed, 214 insertions(+), 20 deletions(-) create mode 100644 SlipeServer.Scripting/LuaTranslatorUtility.cs diff --git a/SlipeServer.Console/test.lua b/SlipeServer.Console/test.lua index a3f3cdb9..c0b2f835 100644 --- a/SlipeServer.Console/test.lua +++ b/SlipeServer.Console/test.lua @@ -4,6 +4,12 @@ iprint("This is a message on server printed using iprint", "This is the second Argument from iprint") + setElementData(customElement, "dummyKey", "dummyValue") + + local elementData = getElementData(customElement, "dummyKey") + + iprint("Element Data Is ", elementData) + local object = createObject(321, 5, 5, 5) setElementPosition(object, 50, 50, 250) setElementRotation(object, 180, 180, 90) diff --git a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs index efc061f0..34940289 100644 --- a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs @@ -13,7 +13,7 @@ public class DebugScriptDefinitions private readonly DebugLog debugLog; private readonly ILogger logger; - public DebugScriptDefinitions(DebugLog debugLog, ILogger logger) + public DebugScriptDefinitions(DebugLog debugLog, ILogger logger, Script _) { this.debugLog = debugLog; this.logger = logger; diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 2606b629..976d28ff 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -3,17 +3,12 @@ using MoonSharp.Interpreter; using SlipeServer.Server.ElementCollections; using System.Collections.Generic; -using System.ComponentModel.Design; -using System.Linq; using SlipeServer.Server; -using System.Text.RegularExpressions; using System; -using System.Collections; using SlipeServer.Server.Elements.ColShapes; -using System.Runtime.InteropServices; -using Microsoft.Extensions.Primitives; using SlipeServer.Server.Elements.Enums; -using System.ComponentModel; +using SlipeServer.Packets.Definitions.Lua; +using System.Linq; namespace SlipeServer.Scripting.Definitions; @@ -127,4 +122,13 @@ public void SetElementData(Element element, string key, DynValue dynValue, bool }; element.SetData(key, dynValue.String, syncTypes[synchronizeType]); } + + [ScriptFunctionDefinition("getElementData")] + public DynValue GetElementData(Element element, string key, bool inherit = true) + { + LuaValue? data = element.GetData(key); + var result = LuaTranslatorUtility.ToDynValues(data).First(); + + return result ?? DynValue.NewBoolean(false); + } } diff --git a/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs index 78718131..981d7993 100644 --- a/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs @@ -1,4 +1,5 @@ -using SlipeServer.Server.Elements; +using MoonSharp.Interpreter; +using SlipeServer.Server.Elements; namespace SlipeServer.Scripting.Definitions; @@ -6,7 +7,7 @@ public class EventScriptDefinitions { private readonly IScriptEventRuntime eventRuntime; - public EventScriptDefinitions(IScriptEventRuntime eventRuntime) + public EventScriptDefinitions(IScriptEventRuntime eventRuntime, Script _) { this.eventRuntime = eventRuntime; } diff --git a/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs index a7675d2d..3f5cfcac 100644 --- a/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs @@ -1,4 +1,5 @@ -using SlipeServer.Server.Elements; +using MoonSharp.Interpreter; +using SlipeServer.Server.Elements; using SlipeServer.Server.Enums; using SlipeServer.Server.Services; using System; @@ -10,7 +11,7 @@ public class ExplosionScriptDefinitions { private readonly ExplosionService explosionService; - public ExplosionScriptDefinitions(ExplosionService explosionService) + public ExplosionScriptDefinitions(ExplosionService explosionService, Script _) { this.explosionService = explosionService; } diff --git a/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs index a49efb92..fc8bd08b 100644 --- a/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs @@ -1,10 +1,12 @@ -namespace SlipeServer.Scripting.Definitions; +using MoonSharp.Interpreter; + +namespace SlipeServer.Scripting.Definitions; public class InputScriptDefinitions { private readonly IScriptInputRuntime inputRuntime; - public InputScriptDefinitions(IScriptInputRuntime inputRuntime) + public InputScriptDefinitions(IScriptInputRuntime inputRuntime, Script _) { this.inputRuntime = inputRuntime; } diff --git a/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs index 4b3cf6c1..2362009e 100644 --- a/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs @@ -1,4 +1,5 @@ -using SlipeServer.Server; +using MoonSharp.Interpreter; +using SlipeServer.Server; using SlipeServer.Server.Elements; using System.Numerics; @@ -8,7 +9,7 @@ public class ObjectScriptDefinitions { private readonly MtaServer server; - public ObjectScriptDefinitions(MtaServer server) + public ObjectScriptDefinitions(MtaServer server, Script _) { this.server = server; } diff --git a/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs index 3704d58c..94e6a3ad 100644 --- a/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs @@ -1,4 +1,5 @@ -using SlipeServer.Server; +using MoonSharp.Interpreter; +using SlipeServer.Server; using SlipeServer.Server.Elements; using System.Drawing; using System.Numerics; @@ -9,7 +10,7 @@ public class RadarAreaScriptDefinitions { private readonly MtaServer server; - public RadarAreaScriptDefinitions(MtaServer server) + public RadarAreaScriptDefinitions(MtaServer server, Script _) { this.server = server; } diff --git a/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs index 9e81c858..12c5c1d9 100644 --- a/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs @@ -1,4 +1,5 @@ -using SlipeServer.Server; +using MoonSharp.Interpreter; +using SlipeServer.Server; using System; using System.Drawing; using System.Security.Cryptography; @@ -10,7 +11,7 @@ public class UtilityScriptDefinitions { private readonly MtaServer server; - public UtilityScriptDefinitions(MtaServer server) + public UtilityScriptDefinitions(MtaServer server, Script _) { this.server = server; } diff --git a/SlipeServer.Scripting/LuaTranslatorUtility.cs b/SlipeServer.Scripting/LuaTranslatorUtility.cs new file mode 100644 index 00000000..23618ced --- /dev/null +++ b/SlipeServer.Scripting/LuaTranslatorUtility.cs @@ -0,0 +1,177 @@ +using MoonSharp.Interpreter; +using SlipeServer.Server.Elements; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Numerics; + +namespace SlipeServer.Scripting; +public static class LuaTranslatorUtility +{ + public static IEnumerable ToDynValues(object? obj) + { + if (obj == null) + return new DynValue[] { DynValue.Nil }; + if (obj is Element element) + return new DynValue[] { UserData.Create(element) }; + if (obj is byte int8) + return new DynValue[] { DynValue.NewNumber(int8) }; + if (obj is short int16) + return new DynValue[] { DynValue.NewNumber(int16) }; + if (obj is int int32) + return new DynValue[] { DynValue.NewNumber(int32) }; + if (obj is long int64) + return new DynValue[] { DynValue.NewNumber(int64) }; + if (obj is ushort uint16) + return new DynValue[] { DynValue.NewNumber(uint16) }; + if (obj is uint uint32) + return new DynValue[] { DynValue.NewNumber(uint32) }; + if (obj is ulong uint64) + return new DynValue[] { DynValue.NewNumber(uint64) }; + if (obj is float single) + return new DynValue[] { DynValue.NewNumber(single) }; + if (obj is double dub) + return new DynValue[] { DynValue.NewNumber(dub) }; + if (obj is bool boolean) + return new DynValue[] { DynValue.NewBoolean(boolean) }; + if (obj is string str) + return new DynValue[] { DynValue.NewString(str) }; + if (obj is Color color) + return new DynValue[] + { + DynValue.NewNumber(color.R), + DynValue.NewNumber(color.G), + DynValue.NewNumber(color.B), + DynValue.NewNumber(color.A), + }; + if (obj is Vector2 vector2) + return new DynValue[] + { + DynValue.NewNumber(vector2.X), + DynValue.NewNumber(vector2.Y) + }; + if (obj is Vector3 vector3) + return new DynValue[] + { + DynValue.NewNumber(vector3.X), + DynValue.NewNumber(vector3.Y), + DynValue.NewNumber(vector3.Z) + }; + if (obj is Delegate del) + return new DynValue[] { DynValue.NewCallback((context, arguments) => ToDynValues(del.DynamicInvoke(arguments.GetArray())!).First()) }; + if (obj is Table table) + return new DynValue[] { DynValue.NewTable(table) }; + if (obj is DynValue dynValue) + return new DynValue[] { dynValue }; + + if (obj is IEnumerable stringEnumerable) + return stringEnumerable.Select(x => DynValue.NewString(x)).ToArray(); + if (obj is IEnumerable enumerable) + return enumerable.Select(x => ToDynValues(x)).SelectMany(x => x).ToArray(); + + + throw new NotImplementedException($"Conversion to Lua for {obj.GetType()} not implemented"); + } + + public static float GetSingleFromDynValue(DynValue dynValue) => (float)dynValue.Number; + public static double GetDoubleFromDynValue(DynValue dynValue) => dynValue.Number; + public static byte GetByteFromDynValue(DynValue dynValue) => (byte)dynValue.Number; + public static short GetInt16FromDynValue(DynValue dynValue) => (short)dynValue.Number; + public static int GetInt32FromDynValue(DynValue dynValue) => (int)dynValue.Number; + public static long GetInt64FromDynValue(DynValue dynValue) => (long)dynValue.Number; + public static ushort GetUInt16FromDynValue(DynValue dynValue) => (ushort)dynValue.Number; + public static uint GetUInt32FromDynValue(DynValue dynValue) => (uint)dynValue.Number; + public static ulong GetUInt64FromDynValue(DynValue dynValue) => (ulong)dynValue.Number; + public static string GetStringFromDynValue(DynValue dynValue) => dynValue.String; + public static bool GetBooleanFromDynValue(DynValue dynValue) => dynValue.Boolean; + public static Table GetTableFromDynValue(DynValue dynValue) => dynValue.Table; + + public static object FromDynValue(Type targetType, Queue dynValues) + { + if (targetType == typeof(Color) || targetType == typeof(Color?)) + { + byte red = GetByteFromDynValue(dynValues.Dequeue()); + byte green = GetByteFromDynValue(dynValues.Dequeue()); + byte blue = GetByteFromDynValue(dynValues.Dequeue()); + byte alpha = GetByteFromDynValue(dynValues.Dequeue()); + return Color.FromArgb(alpha, red, green, blue); + } + if (targetType == typeof(Vector3)) + return new Vector3(GetSingleFromDynValue(dynValues.Dequeue()), GetSingleFromDynValue(dynValues.Dequeue()), GetSingleFromDynValue(dynValues.Dequeue())); + if (targetType == typeof(Vector2)) + return new Vector2(GetSingleFromDynValue(dynValues.Dequeue()), GetSingleFromDynValue(dynValues.Dequeue())); + if (targetType == typeof(float)) + return GetSingleFromDynValue(dynValues.Dequeue()); + if (targetType == typeof(double)) + return GetDoubleFromDynValue(dynValues.Dequeue()); + if (targetType == typeof(byte)) + return GetByteFromDynValue(dynValues.Dequeue()); + if (targetType == typeof(short)) + return GetInt16FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(ushort)) + return GetUInt16FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(int)) + return GetInt32FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(uint)) + return GetUInt32FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(long)) + return GetInt64FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(ulong)) + return GetUInt64FromDynValue(dynValues.Dequeue()); + if (targetType == typeof(string)) + return GetStringFromDynValue(dynValues.Dequeue()); + if (targetType == typeof(bool)) + return GetBooleanFromDynValue(dynValues.Dequeue()); + if (targetType == typeof(Table)) + return GetTableFromDynValue(dynValues.Dequeue()); + if (typeof(Element).IsAssignableFrom(targetType)) + return dynValues.Dequeue().UserData.Object; + if (targetType == typeof(IEnumerable)) + { + List args = new List { }; + + foreach (DynValue arg in dynValues) + { + args.Add(GetStringFromDynValue(arg)); + } + return args; + } + if (targetType == typeof(IEnumerable)) + { + List args = new List(); + + foreach (DynValue arg in dynValues) + { + switch (arg.Type) + { + case DataType.String: + args.Add(GetStringFromDynValue(arg)); + break; + case DataType.UserData: + var obj = (Element)arg.UserData.Object; + args.Add(obj.Name + ":" + obj.GetHashCode()); + break; + case DataType.Number: + // TODO: This may result in data loss [Add more cases of number conversion] + args.Add(GetInt32FromDynValue(arg)); + break; + } + } + return args; + } + if (targetType == typeof(ScriptCallbackDelegateWrapper)) + { + var callback = dynValues.Dequeue().Function; + return new ScriptCallbackDelegateWrapper(parameters => callback.Call(ToDynValues(parameters)), callback); + } + if (targetType == typeof(EventDelegate)) + { + var callback = dynValues.Dequeue().Function; + return (EventDelegate)((element, parameters) => callback.Call(new DynValue[] { UserData.Create(element) }.Concat(ToDynValues(parameters)))); + } + + + throw new NotImplementedException($"Conversion from Lua for {targetType} not implemented"); + } +} From b1398bcf44d42112060014dbcc2e3c75f668b7ce Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Tue, 18 Apr 2023 14:47:11 +0430 Subject: [PATCH 12/14] Revert some changes --- SlipeServer.Console/Logic/LuaTestLogic.cs | 2 +- SlipeServer.Console/test.lua | 11 ++++++----- SlipeServer.Lua/LuaService.cs | 6 +++--- .../Definitions/DebugScriptDefinitions.cs | 2 +- .../Definitions/ElementScriptDefinitions.cs | 16 ++++++++-------- .../Definitions/EventScriptDefinitions.cs | 2 +- .../Definitions/ExplosionScriptDefinitions.cs | 2 +- .../Definitions/InputScriptDefinitions.cs | 2 +- .../Definitions/ObjectScriptDefinitions.cs | 2 +- .../Definitions/RadarAreaScriptDefinitions.cs | 2 +- .../Definitions/UtilityScriptDefinitions.cs | 2 +- 11 files changed, 25 insertions(+), 24 deletions(-) diff --git a/SlipeServer.Console/Logic/LuaTestLogic.cs b/SlipeServer.Console/Logic/LuaTestLogic.cs index 1fbdc307..5384c47d 100644 --- a/SlipeServer.Console/Logic/LuaTestLogic.cs +++ b/SlipeServer.Console/Logic/LuaTestLogic.cs @@ -32,7 +32,7 @@ private void Init() { this.eventRuntime.LoadDefaultEvents(); - //this.luaService.LoadDefaultDefinitions(); + this.luaService.LoadDefaultDefinitions(); this.luaService.LoadDefinitions(); this.luaService.LoadDefinitions(); diff --git a/SlipeServer.Console/test.lua b/SlipeServer.Console/test.lua index c0b2f835..6bdbf7bc 100644 --- a/SlipeServer.Console/test.lua +++ b/SlipeServer.Console/test.lua @@ -1,14 +1,15 @@ if(isSlipeServer)then - local customElement = createElement("MyCustomType") - iprint("Custom Element ", customElement) iprint("This is a message on server printed using iprint", "This is the second Argument from iprint") - setElementData(customElement, "dummyKey", "dummyValue") + local customElement = createElement("MyCustomType") + iprint("Custom Element ", customElement) + + --setElementData(customElement, "dummyKey", "dummyValue") - local elementData = getElementData(customElement, "dummyKey") + --local elementData = getElementData(customElement, "dummyKey") - iprint("Element Data Is ", elementData) + --iprint("Element Data Is ", elementData) local object = createObject(321, 5, 5, 5) setElementPosition(object, 50, 50, 250) diff --git a/SlipeServer.Lua/LuaService.cs b/SlipeServer.Lua/LuaService.cs index e8af513b..3b63caac 100644 --- a/SlipeServer.Lua/LuaService.cs +++ b/SlipeServer.Lua/LuaService.cs @@ -84,14 +84,14 @@ public void LoadDefinitions() LoadDefinitions(this.server.Instantiate()!); } - public void LoadDefaultDefinitions(Script script) + public void LoadDefaultDefinitions() { foreach (var type in typeof(ScriptFunctionDefinitionAttribute).Assembly.DefinedTypes .Where(type => type.GetMethods() .Any(method => method.CustomAttributes .Any(attribute => attribute.AttributeType == typeof(ScriptFunctionDefinitionAttribute))))) { - LoadDefinitions(this.server.Instantiate(type, script)); + LoadDefinitions(this.server.Instantiate(type)); } } @@ -145,7 +145,7 @@ private void LoadDefinitions(Script script) script.Globals["real" + definition.Key] = definition.Value; stringBuilder.AppendLine($"function {definition.Key}(...) return table.unpack(real{definition.Key}({{...}})) end"); } - LoadDefaultDefinitions(script); + //LoadDefaultDefinitions(script); script.DoString(stringBuilder.ToString(), codeFriendlyName: "SlipeDefinitions"); } diff --git a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs index 34940289..efc061f0 100644 --- a/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/DebugScriptDefinitions.cs @@ -13,7 +13,7 @@ public class DebugScriptDefinitions private readonly DebugLog debugLog; private readonly ILogger logger; - public DebugScriptDefinitions(DebugLog debugLog, ILogger logger, Script _) + public DebugScriptDefinitions(DebugLog debugLog, ILogger logger) { this.debugLog = debugLog; this.logger = logger; diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index 976d28ff..b8c4acfd 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -17,13 +17,13 @@ public class ElementScriptDefinitions private readonly MtaServer server; private readonly IDictionary elementsForVariants; private readonly IElementCollection elementCollection; - private readonly Script ownerScript; + //private readonly Script ownerScript; - public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection, Script ownerScript) + public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection /*Script ownerScript*/) { this.server = _server; this.elementCollection = (RTreeCompoundElementCollection)elementCollection; - this.ownerScript = ownerScript; + //this.ownerScript = ownerScript; this.elementsForVariants = new Dictionary(Enum.GetNames(typeof(ElementType)).Length) { [ElementType.Player] = typeof(Player), @@ -100,16 +100,16 @@ public void DeatchElements(Element childElement, Element? detachFrom = null) [ScriptFunctionDefinition("getAllElementData")] public Table GetAllElementData(Element element) { - var elementDatas = element.GetAllElementData(); - DynValue theTable = DynValue.NewTable(ownerScript); + /*var elementDatas = element.GetAllElementData(); + DynValue theTable = DynValue.NewTable(); foreach ( var elementData in elementDatas ) { - theTable.Table.Set(elementData.Key, DynValue.FromObject(ownerScript, elementData.Value)); + theTable.Table.Set(elementData.Key, DynValue.FromObject(, elementData.Value)); } - - return theTable.Table; + */ + return new object() as Table; } [ScriptFunctionDefinition("setElementData")] diff --git a/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs index 981d7993..c80ce5a9 100644 --- a/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/EventScriptDefinitions.cs @@ -7,7 +7,7 @@ public class EventScriptDefinitions { private readonly IScriptEventRuntime eventRuntime; - public EventScriptDefinitions(IScriptEventRuntime eventRuntime, Script _) + public EventScriptDefinitions(IScriptEventRuntime eventRuntime) { this.eventRuntime = eventRuntime; } diff --git a/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs index 3f5cfcac..4e91dc86 100644 --- a/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ExplosionScriptDefinitions.cs @@ -11,7 +11,7 @@ public class ExplosionScriptDefinitions { private readonly ExplosionService explosionService; - public ExplosionScriptDefinitions(ExplosionService explosionService, Script _) + public ExplosionScriptDefinitions(ExplosionService explosionService) { this.explosionService = explosionService; } diff --git a/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs index fc8bd08b..b0657c10 100644 --- a/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/InputScriptDefinitions.cs @@ -6,7 +6,7 @@ public class InputScriptDefinitions { private readonly IScriptInputRuntime inputRuntime; - public InputScriptDefinitions(IScriptInputRuntime inputRuntime, Script _) + public InputScriptDefinitions(IScriptInputRuntime inputRuntime) { this.inputRuntime = inputRuntime; } diff --git a/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs index 2362009e..1c0bf910 100644 --- a/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ObjectScriptDefinitions.cs @@ -9,7 +9,7 @@ public class ObjectScriptDefinitions { private readonly MtaServer server; - public ObjectScriptDefinitions(MtaServer server, Script _) + public ObjectScriptDefinitions(MtaServer server) { this.server = server; } diff --git a/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs index 94e6a3ad..f02356f8 100644 --- a/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/RadarAreaScriptDefinitions.cs @@ -10,7 +10,7 @@ public class RadarAreaScriptDefinitions { private readonly MtaServer server; - public RadarAreaScriptDefinitions(MtaServer server, Script _) + public RadarAreaScriptDefinitions(MtaServer server) { this.server = server; } diff --git a/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs index 12c5c1d9..9920e05c 100644 --- a/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/UtilityScriptDefinitions.cs @@ -11,7 +11,7 @@ public class UtilityScriptDefinitions { private readonly MtaServer server; - public UtilityScriptDefinitions(MtaServer server, Script _) + public UtilityScriptDefinitions(MtaServer server) { this.server = server; } From 01d8a8237c5977d518ad2baed70ea31e904a7483 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Tue, 18 Apr 2023 14:54:42 +0430 Subject: [PATCH 13/14] Some cleanup --- SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs index b8c4acfd..b6ccdf78 100644 --- a/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs +++ b/SlipeServer.Scripting/Definitions/ElementScriptDefinitions.cs @@ -17,13 +17,11 @@ public class ElementScriptDefinitions private readonly MtaServer server; private readonly IDictionary elementsForVariants; private readonly IElementCollection elementCollection; - //private readonly Script ownerScript; - public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection /*Script ownerScript*/) + public ElementScriptDefinitions(MtaServer _server, IElementCollection elementCollection) { this.server = _server; this.elementCollection = (RTreeCompoundElementCollection)elementCollection; - //this.ownerScript = ownerScript; this.elementsForVariants = new Dictionary(Enum.GetNames(typeof(ElementType)).Length) { [ElementType.Player] = typeof(Player), From 457a808afc8aa23afac4c17ba4d4fca3e48edcb8 Mon Sep 17 00:00:00 2001 From: ERAGON007 Date: Tue, 18 Apr 2023 15:43:07 +0430 Subject: [PATCH 14/14] Maybe temporary workaround for LuaTranslator --- SlipeServer.Console/Logic/LuaTestLogic.cs | 12 ++--- SlipeServer.Lua/LuaService.cs | 46 +++++++++++-------- SlipeServer.Lua/LuaTranslator.cs | 12 ++--- .../Definitions/ElementScriptDefinitions.cs | 5 +- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/SlipeServer.Console/Logic/LuaTestLogic.cs b/SlipeServer.Console/Logic/LuaTestLogic.cs index 5384c47d..c283f1fc 100644 --- a/SlipeServer.Console/Logic/LuaTestLogic.cs +++ b/SlipeServer.Console/Logic/LuaTestLogic.cs @@ -32,16 +32,16 @@ private void Init() { this.eventRuntime.LoadDefaultEvents(); - this.luaService.LoadDefaultDefinitions(); - - this.luaService.LoadDefinitions(); - this.luaService.LoadDefinitions(); - using FileStream testLua = File.OpenRead("test.lua"); using StreamReader reader = new StreamReader(testLua); try { - this.luaService.LoadScript("test.lua", reader.ReadToEnd()); + Script createdScript = this.luaService.LoadScript("test.lua", reader.ReadToEnd()); + + this.luaService.LoadDefaultDefinitions(createdScript); + this.luaService.LoadDefinitions(createdScript); + this.luaService.LoadDefinitions(createdScript); + } catch (InterpreterException ex) { diff --git a/SlipeServer.Lua/LuaService.cs b/SlipeServer.Lua/LuaService.cs index 3b63caac..0c887637 100644 --- a/SlipeServer.Lua/LuaService.cs +++ b/SlipeServer.Lua/LuaService.cs @@ -19,7 +19,7 @@ public class LuaService private readonly ILogger logger; private readonly RootElement root; private readonly Dictionary scripts; - private readonly Dictionary methods; + private readonly Dictionary> methods; private readonly LuaTranslator translator; public LuaService(MtaServer server, ILogger logger, RootElement root) @@ -28,11 +28,11 @@ public LuaService(MtaServer server, ILogger logger, RootElement root) this.logger = logger; this.root = root; this.scripts = new Dictionary(); - this.methods = new Dictionary(); + this.methods = new Dictionary>(); this.translator = new LuaTranslator(); } - public void LoadDefinitions(object methodSet) + public void LoadDefinitions(object methodSet, Script owner) { foreach (var method in methodSet.GetType().GetMethods() .Where(method => method.CustomAttributes @@ -40,11 +40,12 @@ public void LoadDefinitions(object methodSet) { var attribute = method.GetCustomAttribute(); - if (this.methods.ContainsKey(attribute!.NiceName)) + if (methods.TryGetValue(owner, out Dictionary? _) && this.methods[owner].ContainsKey(attribute!.NiceName)) throw new Exception($"Lua name conflict for '{attribute.NiceName}'"); var methodParameters = method.GetParameters(); - this.methods[attribute.NiceName] = (values) => + this.methods.TryAdd(owner, new Dictionary()); + this.methods[owner][attribute.NiceName] = (values) => { var valueQueue = new Queue(values.AsEnumerable()); @@ -55,7 +56,7 @@ public void LoadDefinitions(object methodSet) { if (valueQueue.Any()) { - parameters[i] = this.translator.FromDynValue(methodParameters[i].ParameterType, valueQueue); + parameters[i] = this.translator.FromDynValue(methodParameters[i].ParameterType, valueQueue, owner); } else { if (!methodParameters[i].IsOptional) @@ -74,40 +75,43 @@ public void LoadDefinitions(object methodSet) } var result = method.Invoke(methodSet, parameters); - return this.translator.ToDynValues(result).ToArray(); + return this.translator.ToDynValues(result, owner).ToArray(); }; } } - public void LoadDefinitions() + public void LoadDefinitions(Script script) { - LoadDefinitions(this.server.Instantiate()!); + LoadDefinitions(this.server.Instantiate()!, script); } - public void LoadDefaultDefinitions() + public void LoadDefaultDefinitions(Script script) { foreach (var type in typeof(ScriptFunctionDefinitionAttribute).Assembly.DefinedTypes .Where(type => type.GetMethods() .Any(method => method.CustomAttributes .Any(attribute => attribute.AttributeType == typeof(ScriptFunctionDefinitionAttribute))))) { - LoadDefinitions(this.server.Instantiate(type)); + LoadDefinitions(this.server.Instantiate(type), script); } } - public void LoadScript(string identifier, string code) + public Script LoadScript(string identifier, string code) { var script = new Script(CoreModules.Preset_SoftSandbox); script.Options.DebugPrint = (value) => this.logger.LogInformation(value); this.scripts[identifier] = script; LoadGlobals(script); + LoadDefaultDefinitions(script); LoadDefinitions(script); script.DoString(code, codeFriendlyName: identifier); + + return script; } - public void LoadScript(string identifier, string[] codes) + public Script LoadScript(string identifier, string[] codes) { var script = new Script(CoreModules.Preset_SoftSandbox); script.Options.DebugPrint = (value) => @@ -118,18 +122,22 @@ public void LoadScript(string identifier, string[] codes) this.scripts[identifier] = script; LoadGlobals(script); + LoadDefaultDefinitions(script); LoadDefinitions(script); foreach (var code in codes) script.DoString(code, codeFriendlyName: identifier); + + return script; } - public async Task LoadScriptFromPath(string path) => LoadScript(path, await File.ReadAllTextAsync(path)); - public async Task LoadScriptFromPaths(string identifier, string[] paths) + public async Task