Cleanup and improvements

This commit is contained in:
Deukhoofd 2019-08-25 19:30:36 +02:00
parent cb1273b92e
commit c1ed9b1eb6
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
12 changed files with 84 additions and 56 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -71,9 +71,17 @@ namespace PorygonSharp.UserData
{
var eval = new EvalValue(parameters[i]);
var val = eval.GetObjectValue();
var convertedType = Convert.ChangeType(val, parameterTypes[i]);
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;
var result = method.Invoke(parentObj, evaluatedParameters);

View File

@ -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);
}