diff --git a/PorygonSharp/CoreSetup.cs b/PorygonSharp/CoreSetup.cs index 83297bc..a9a7a69 100644 --- a/PorygonSharp/CoreSetup.cs +++ b/PorygonSharp/CoreSetup.cs @@ -27,7 +27,7 @@ namespace PorygonSharp Print(message); } - [DllImport("libPorygonLang", EntryPoint = "SetDefaultPrintFunc", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "SetDefaultPrintFunc", CallingConvention = CallingConvention.Cdecl)] private static extern double InternalSetPrintFunc(IntPtr ptr); } } \ No newline at end of file diff --git a/PorygonSharp/DiagnosticHandling/Diagnostics.cs b/PorygonSharp/DiagnosticHandling/Diagnostics.cs index c138052..e361a4f 100644 --- a/PorygonSharp/DiagnosticHandling/Diagnostics.cs +++ b/PorygonSharp/DiagnosticHandling/Diagnostics.cs @@ -56,13 +56,13 @@ namespace PorygonSharp.DiagnosticHandling return GetFullDiagnostic(_handle, item.GetHandle()); } - [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetDiagnosticsCount", CallingConvention = CallingConvention.Cdecl)] private static extern int GetDiagnosticsCount(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "GetDiagnosticAt", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetDiagnosticAt", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetDiagnosticAt(IntPtr ptr, int position); - [DllImport("libPorygonLang", EntryPoint = "GetFullDiagnostic", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetFullDiagnostic", CallingConvention = CallingConvention.Cdecl)] private static extern string GetFullDiagnostic(IntPtr diagnosticsHolder, IntPtr diagnostic); } } \ No newline at end of file diff --git a/PorygonSharp/EvalValues/EvalValue.cs b/PorygonSharp/EvalValues/EvalValue.cs index e9289c6..4737771 100644 --- a/PorygonSharp/EvalValues/EvalValue.cs +++ b/PorygonSharp/EvalValues/EvalValue.cs @@ -65,6 +65,8 @@ namespace PorygonSharp.EvalValues public object GetObjectValue() { + if (_handle == IntPtr.Zero) + return null; switch (GetTypeClass()) { case TypeClass.Nil: @@ -87,22 +89,22 @@ namespace PorygonSharp.EvalValues return _handle; } - [DllImport("libPorygonLang", EntryPoint = "GetEvalValueTypeClass", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetEvalValueTypeClass", CallingConvention = CallingConvention.Cdecl)] private static extern int GetTypeClass(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "GetEvalValueType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetEvalValueType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetScriptType(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueInteger", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateEvalValueInteger", CallingConvention = CallingConvention.Cdecl)] private static extern long EvaluateInteger(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueFloat", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateEvalValueFloat", CallingConvention = CallingConvention.Cdecl)] private static extern double EvaluateFloat(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueBool", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateEvalValueBool", CallingConvention = CallingConvention.Cdecl)] private static extern bool EvaluateBool(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "GetEvalValueStringLength",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetEvalValueStringLength",CallingConvention = CallingConvention.Cdecl)] private static extern int GetStringLength(IntPtr ptr); - [DllImport("libPorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateEvalValueString",CallingConvention = CallingConvention.Cdecl)] private static extern int EvaluateString(IntPtr ptr, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] byte[] buffer, int capacity); - [DllImport("libPorygonLang", EntryPoint = "EvaluateUserDataObj",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateUserDataObj",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr EvaluateUserDataObj(IntPtr ptr); } diff --git a/PorygonSharp/EvalValues/EvalValueCreator.cs b/PorygonSharp/EvalValues/EvalValueCreator.cs index 8d8b900..8383e8f 100644 --- a/PorygonSharp/EvalValues/EvalValueCreator.cs +++ b/PorygonSharp/EvalValues/EvalValueCreator.cs @@ -66,23 +66,23 @@ namespace PorygonSharp.EvalValues return new EvalValue(ptr); } - [DllImport("libPorygonLang", EntryPoint = "CreateNilEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateNilEvalValue",CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr CreateNilEvalValue(); - [DllImport("libPorygonLang", EntryPoint = "CreateIntegerEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateIntegerEvalValue",CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr CreateIntegerEvalValue(long l); - [DllImport("libPorygonLang", EntryPoint = "CreateFloatEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateFloatEvalValue",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateFloatEvalValue(double d); - [DllImport("libPorygonLang", EntryPoint = "CreateBoolEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateBoolEvalValue",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateBoolEvalValue(bool b); - [DllImport("libPorygonLang", EntryPoint = "CreateStringEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateStringEvalValue",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateStringEvalValue(string s); - [DllImport("libPorygonLang", EntryPoint = "CreateUserDataEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateUserDataEvalValue",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateUserDataEvalValue(uint typeHash, IntPtr obj); - [DllImport("libPorygonLang", EntryPoint = "CreateFunctionEvalValue",CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateFunctionEvalValue",CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateFunctionEvalValue(IntPtr func, IntPtr parent); - [DllImport("libPorygonLang", EntryPoint = "CreateCollectionValue", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateCollectionValue", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateCollectionValue(IntPtr type, IntPtr parent, IntPtr getter, IntPtr setter, IntPtr iterator); } diff --git a/PorygonSharp/EvalValues/ListIterator.cs b/PorygonSharp/EvalValues/ListIterator.cs index 203b9e5..8444e1c 100644 --- a/PorygonSharp/EvalValues/ListIterator.cs +++ b/PorygonSharp/EvalValues/ListIterator.cs @@ -11,7 +11,7 @@ namespace PorygonSharp.EvalValues return CreateCollectionRangeIterator(0, list.Count); } - [DllImport("libPorygonLang", EntryPoint = "CreateCollectionRangeIterator", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateCollectionRangeIterator", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateCollectionRangeIterator(int start, int end); } } \ No newline at end of file diff --git a/PorygonSharp/EvaluateResult.cs b/PorygonSharp/EvaluateResult.cs index dc9e151..df7a518 100644 --- a/PorygonSharp/EvaluateResult.cs +++ b/PorygonSharp/EvaluateResult.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Text; using PorygonSharp.EvalValues; namespace PorygonSharp @@ -10,7 +11,9 @@ namespace PorygonSharp private struct EvaluateResultInternal { public readonly IntPtr Value; - public readonly bool Result; + public readonly byte Result; + public readonly IntPtr ErrorMessage; + public readonly long ErrorSize; } private readonly IntPtr _ptr; @@ -28,12 +31,15 @@ namespace PorygonSharp public bool IsSuccess() { - return !_internal.Result; + return _internal.Result == 0; } public string GetError() { - return GetErrorMessage(_ptr); + var nameArr = new byte[_internal.ErrorSize]; + for (var i = 0; i < _internal.ErrorSize; i++) + nameArr[i] = Marshal.ReadByte(_internal.ErrorMessage, i); + return $"Error length: {_internal.ErrorSize}" + Encoding.UTF8.GetString(nameArr); } public void Dispose() @@ -41,7 +47,7 @@ namespace PorygonSharp Marshal.FreeHGlobal(_ptr); } - [DllImport("libPorygonLang", EntryPoint = "GetResultError", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetResultError", CallingConvention = CallingConvention.Cdecl)] private static extern string GetErrorMessage(IntPtr ptr); } diff --git a/PorygonSharp/Script.cs b/PorygonSharp/Script.cs index dcefbcb..d9c4bd5 100644 --- a/PorygonSharp/Script.cs +++ b/PorygonSharp/Script.cs @@ -127,23 +127,23 @@ namespace PorygonSharp } } - [DllImport("libPorygonLang", EntryPoint = "CreateScript", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateScript", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr Create([MarshalAs(UnmanagedType.LPWStr)]string s, IntPtr options); - [DllImport("libPorygonLang", EntryPoint = "EvaluateScript", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "EvaluateScript", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr Evaluate(IntPtr script); - [DllImport("libPorygonLang", EntryPoint = "HasVariable", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "HasVariable", CallingConvention = CallingConvention.Cdecl)] private static extern bool HasVariable(IntPtr script, [MarshalAs(UnmanagedType.LPWStr)]string key); - [DllImport("libPorygonLang", EntryPoint = "GetVariable", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetVariable", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetVariable(IntPtr script, [MarshalAs(UnmanagedType.LPWStr)]string key); - [DllImport("libPorygonLang", EntryPoint = "HasFunction", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "HasFunction", CallingConvention = CallingConvention.Cdecl)] private static extern bool HasFunction(IntPtr script, [MarshalAs(UnmanagedType.LPWStr)]string key); - [DllImport("libPorygonLang", EntryPoint = "CallFunction", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CallFunction", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CallFunction(IntPtr script, [MarshalAs(UnmanagedType.LPWStr)]string key, IntPtr[] parameters, int parameterCount); - [DllImport("libPorygonLang", EntryPoint = "CloneScript", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CloneScript", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CloneScript(IntPtr script); diff --git a/PorygonSharp/ScriptOptions.cs b/PorygonSharp/ScriptOptions.cs index 280a0c8..83607b0 100644 --- a/PorygonSharp/ScriptOptions.cs +++ b/PorygonSharp/ScriptOptions.cs @@ -51,16 +51,16 @@ namespace PorygonSharp } - [DllImport("libPorygonLang", EntryPoint = "CreateOptions", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateOptions", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr Create(); - [DllImport("libPorygonLang", EntryPoint = "SetOptionPrintFunc", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "SetOptionPrintFunc", CallingConvention = CallingConvention.Cdecl)] private static extern void SetOptionPrintFunc(IntPtr opt, IntPtr func); - [DllImport("libPorygonLang", EntryPoint = "SetOptionModuleExistsFunc", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "SetOptionModuleExistsFunc", CallingConvention = CallingConvention.Cdecl)] private static extern void SetOptionModuleExistsFunc(IntPtr opt, IntPtr func); - [DllImport("libPorygonLang", EntryPoint = "SetOptionResolveModuleFunc", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "SetOptionResolveModuleFunc", CallingConvention = CallingConvention.Cdecl)] private static extern void SetOptionResolveModuleFunc(IntPtr opt, IntPtr func); } diff --git a/PorygonSharp/ScriptType/ScriptType.cs b/PorygonSharp/ScriptType/ScriptType.cs index f743e05..0a9d77e 100644 --- a/PorygonSharp/ScriptType/ScriptType.cs +++ b/PorygonSharp/ScriptType/ScriptType.cs @@ -115,19 +115,19 @@ namespace PorygonSharp.ScriptType return null; } - [DllImport("libPorygonLang", EntryPoint = "CreateScriptType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateScriptType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateScriptType(TypeClass t); - [DllImport("libPorygonLang", EntryPoint = "CreateNumericScriptType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateNumericScriptType", CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr CreateNumericScriptType(bool isAware, bool isFloat); - [DllImport("libPorygonLang", EntryPoint = "CreateStringScriptType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateStringScriptType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateStringScriptType(bool knownAtBind, uint hash); - [DllImport("libPorygonLang", EntryPoint = "CreateUserDataFunctionScriptType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateUserDataFunctionScriptType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateUserDataFunctionScriptType(IntPtr returnType, IntPtr[] parameters, int parameterCount); - [DllImport("libPorygonLang", EntryPoint = "CreateCollectionType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateCollectionType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateCollectionType(IntPtr keyType, IntPtr valueType); } } \ No newline at end of file diff --git a/PorygonSharp/StaticScope.cs b/PorygonSharp/StaticScope.cs index 46e3f02..09c5f0a 100644 --- a/PorygonSharp/StaticScope.cs +++ b/PorygonSharp/StaticScope.cs @@ -18,7 +18,7 @@ namespace PorygonSharp RegisterStaticVariable(hash, scriptType.Value, value.GetPointer()); } - [DllImport("libPorygonLang", EntryPoint = "RegisterStaticVariable", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "RegisterStaticVariable", CallingConvention = CallingConvention.Cdecl)] private static extern void RegisterStaticVariable(uint hashId, IntPtr scriptType, IntPtr value); } } \ No newline at end of file diff --git a/PorygonSharp/UserData/UserDataField.cs b/PorygonSharp/UserData/UserDataField.cs index cbb7716..c6ab92d 100644 --- a/PorygonSharp/UserData/UserDataField.cs +++ b/PorygonSharp/UserData/UserDataField.cs @@ -71,8 +71,16 @@ namespace PorygonSharp.UserData { var eval = new EvalValue(parameters[i]); var val = eval.GetObjectValue(); - var convertedType = Convert.ChangeType(val, parameterTypes[i]); - evaluatedParameters[i] = convertedType; + var desiredType = parameterTypes[i]; + if (val is IConvertible) + { + var convertedType = Convert.ChangeType(val, desiredType); + evaluatedParameters[i] = convertedType; + } + else + { + evaluatedParameters[i] = val; + } } var parentObj = GCHandle.FromIntPtr(parent).Target; diff --git a/PorygonSharp/UserData/UserDataHandler.cs b/PorygonSharp/UserData/UserDataHandler.cs index 6c87462..6af8379 100644 --- a/PorygonSharp/UserData/UserDataHandler.cs +++ b/PorygonSharp/UserData/UserDataHandler.cs @@ -90,7 +90,7 @@ namespace PorygonSharp.UserData var fieldName = valueName.ScriptHash(); var t = ScriptType.ScriptType.CreateNumericScriptType(true, false); RegisterUserDataField(hash, fieldName, - CreateUserDataField(t, Marshal.GetFunctionPointerForDelegate(getter), IntPtr.Zero)); + CreateUserDataField(t, fieldData.GetGetterPointer(), IntPtr.Zero)); ReverseLookup[hash].Fields[fieldName] = fieldData; } @@ -101,13 +101,17 @@ namespace PorygonSharp.UserData var scriptType = ScriptType.ScriptType.GetScriptType(field.FieldType); if (!scriptType.HasValue) return; + if (!ReverseLookup.TryGetValue(typeHash, out var userData)) + return; + var fieldName = field.Name.ScriptHash(); + if (userData.Fields.ContainsKey(fieldName)) + return; var fieldData = new UserDataField(field); var userDataField = CreateUserDataField(scriptType.Value, fieldData.GetGetterPointer(), fieldData.GetSetterPointer()); - var fieldName = field.Name.ScriptHash(); RegisterUserDataField(typeHash, fieldName, userDataField); - ReverseLookup[typeHash].Fields[fieldName] = fieldData; + userData.Fields.Add(fieldName, fieldData); } private static void RegisterProperty(PropertyInfo property, uint typeHash) @@ -115,13 +119,17 @@ namespace PorygonSharp.UserData var scriptType = ScriptType.ScriptType.GetScriptType(property.PropertyType); if (!scriptType.HasValue) return; + if (!ReverseLookup.TryGetValue(typeHash, out var userData)) + return; + var fieldName = property.Name.ScriptHash(); + if (userData.Fields.ContainsKey(fieldName)) + return; var fieldData = new UserDataField(property); var userDataField = CreateUserDataField(scriptType.Value, fieldData.GetGetterPointer(), fieldData.GetSetterPointer()); - var fieldName = property.Name.ScriptHash(); RegisterUserDataField(typeHash, fieldName, userDataField); - ReverseLookup[typeHash].Fields[fieldName] = fieldData; + userData.Fields.Add(fieldName, fieldData); } private static void RegisterFunction(MethodInfo method, uint typeHash) @@ -130,12 +138,16 @@ namespace PorygonSharp.UserData var type = ScriptType.ScriptType.GetFunctionScriptType(method); if (type == IntPtr.Zero || !type.HasValue) return; + if (!ReverseLookup.TryGetValue(typeHash, out var userData)) + return; + var fieldName = method.Name.ScriptHash(); + if (userData.Fields.ContainsKey(fieldName)) + return; var fieldData = new UserDataField(method); var userDataField = CreateUserDataField(type.Value, fieldData.GetGetterPointer(), IntPtr.Zero); - var fieldName = method.Name.ScriptHash(); RegisterUserDataField(typeHash, fieldName, userDataField); - ReverseLookup[typeHash].Fields[fieldName] = fieldData; + userData.Fields.Add(fieldName, fieldData); } public static uint GetTypeId(Type t) @@ -173,19 +185,19 @@ namespace PorygonSharp.UserData return CreateUserDataType(hash); } - [DllImport("libPorygonLang", EntryPoint = "RegisterUserDataType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "RegisterUserDataType", CallingConvention = CallingConvention.Cdecl)] private static extern void RegisterUserDataType(uint hashId); - [DllImport("libPorygonLang", EntryPoint = "RegisterUserDataField", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "RegisterUserDataField", CallingConvention = CallingConvention.Cdecl)] private static extern void RegisterUserDataField(uint hashId, uint fieldId, IntPtr field); - [DllImport("libPorygonLang", EntryPoint = "CreateUserDataField", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateUserDataField", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateUserDataField(IntPtr type, IntPtr getter, IntPtr setter); - [DllImport("libPorygonLang", EntryPoint = "GetUserDataFieldCount", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "GetUserDataFieldCount", CallingConvention = CallingConvention.Cdecl)] private static extern int GetUserDataFieldCount(uint hashId); - [DllImport("libPorygonLang", EntryPoint = "CreateUserDataType", CallingConvention = CallingConvention.Cdecl)] + [DllImport("PorygonLang", EntryPoint = "CreateUserDataType", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateUserDataType(uint hashId); }