diff --git a/api/AltV.Net.Client/Alt.GlobalMeta.cs b/api/AltV.Net.Client/Alt.GlobalMeta.cs index cdf0e40c3..3f943eb88 100644 --- a/api/AltV.Net.Client/Alt.GlobalMeta.cs +++ b/api/AltV.Net.Client/Alt.GlobalMeta.cs @@ -1,4 +1,5 @@ using AltV.Net.Elements.Args; +using AltV.Net.Shared.Utils; namespace AltV.Net.Client { @@ -71,7 +72,7 @@ public static bool GetMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch @@ -90,7 +91,7 @@ public static bool GetSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch @@ -109,7 +110,7 @@ public static bool GetLocalMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net.Client/Elements/Data/LocalStorage.cs b/api/AltV.Net.Client/Elements/Data/LocalStorage.cs index cc3a0bbf7..73bf5a4c9 100644 --- a/api/AltV.Net.Client/Elements/Data/LocalStorage.cs +++ b/api/AltV.Net.Client/Elements/Data/LocalStorage.cs @@ -165,7 +165,7 @@ public bool Get(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net.Client/Elements/Entities/Entity.cs b/api/AltV.Net.Client/Elements/Entities/Entity.cs index 1914158d7..5c741f012 100644 --- a/api/AltV.Net.Client/Elements/Entities/Entity.cs +++ b/api/AltV.Net.Client/Elements/Entities/Entity.cs @@ -172,7 +172,7 @@ public bool GetStreamSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net.Client/Elements/Entities/VirtualEntity.cs b/api/AltV.Net.Client/Elements/Entities/VirtualEntity.cs index ecc28df66..5f83aa217 100644 --- a/api/AltV.Net.Client/Elements/Entities/VirtualEntity.cs +++ b/api/AltV.Net.Client/Elements/Entities/VirtualEntity.cs @@ -73,7 +73,7 @@ public bool GetStreamSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net.Shared/Elements/Args/MValueConst.cs b/api/AltV.Net.Shared/Elements/Args/MValueConst.cs index 8720c8867..912e82e45 100644 --- a/api/AltV.Net.Shared/Elements/Args/MValueConst.cs +++ b/api/AltV.Net.Shared/Elements/Args/MValueConst.cs @@ -188,7 +188,7 @@ public void CallFunction(MValueConst[] args, out MValueConst result) argsPointers[i] = args[i].nativePointer; } - result = new MValueConst(core, + result = new MValueConst(core, core.Library.Shared.MValueConst_CallFunction(core.NativePointer, nativePointer, argsPointers, length)); } @@ -252,6 +252,7 @@ public byte[] GetByteArray() public object ToObject() { + Console.WriteLine($"To Object: {type}"); switch (type) { case Type.None: @@ -316,7 +317,7 @@ public object ToObject() if (entityPointer == IntPtr.Zero) return null; // TODO get or create return core.PoolManager.Get(entityPointer, entityType); - + case Type.Function: return null; case Type.Vector3: diff --git a/api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs b/api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs index 17e305321..917e0f1d3 100644 --- a/api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs +++ b/api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs @@ -223,7 +223,7 @@ public bool GetMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.Utils.GetCastedMValue(mValue); return true; } catch @@ -302,7 +302,7 @@ public bool GetSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net.Shared/Utils/Utils.cs b/api/AltV.Net.Shared/Utils/Utils.cs index 5e0d3e3c7..246a70966 100644 --- a/api/AltV.Net.Shared/Utils/Utils.cs +++ b/api/AltV.Net.Shared/Utils/Utils.cs @@ -1,4 +1,5 @@ using System.Text; +using AltV.Net.Elements.Args; namespace AltV.Net.Shared.Utils { @@ -25,5 +26,122 @@ public static uint Hash(string stringToHash) return hash; } + + public static T GetCastedMValue(MValueConst mValue) + { + object @object; + + if (mValue.type == MValueConst.Type.List) + { + if (mValue.ToObject() is IEnumerable sourceEnumerable) + { + + if (typeof(T).IsArray) + { + var resultList = new List(); + var elementType = typeof(T).IsArray ? typeof(T).GetElementType() : typeof(T).GetGenericArguments()[0]; + + foreach (var item in sourceEnumerable) + { + if (item == null) + { + resultList.Add(null); + } + else + { + resultList.Add(Convert.ChangeType(item, elementType)); + } + } + + var resultArray = Array.CreateInstance(elementType, resultList.Count); + for (int i = 0; i < resultList.Count; i++) + { + resultArray.SetValue(resultList[i], i); + } + return (T)(object)resultArray; + } + else + { + T resultList = default(T)!; + if (typeof(T).IsGenericType && (typeof(T).GetGenericTypeDefinition() == typeof(List<>))) + { + resultList = Activator.CreateInstance(); + + var type = typeof(T).GetGenericArguments()[0]; + foreach (var item in (object[])mValue.ToObject()) + { + if (item == null) + { + resultList.GetType().GetMethod("Add").Invoke(resultList, [null]); + } + else + { + resultList.GetType().GetMethod("Add").Invoke(resultList, [Convert.ChangeType(item, type)]); + } + } + } + return resultList; + } + } + else + { + throw new ArgumentException("Quellobjekt muss eine IEnumerable sein."); + } + + /* + T resultList = default(T)!; + if (typeof(T).IsGenericType && (typeof(T).GetGenericTypeDefinition() == typeof(List<>))) + { + resultList = Activator.CreateInstance(); + + var type = typeof(T).GetGenericArguments()[0]; + foreach (var item in (object[])mValue.ToObject()) + { + if (item == null) + { + resultList.GetType().GetMethod("Add").Invoke(resultList, [null]); + } + else + { + resultList.GetType().GetMethod("Add").Invoke(resultList, [Convert.ChangeType(item, type)]); + } + } + } + + return resultList;*/ + } + + if (mValue.type == MValueConst.Type.Dict) + { + T resultDictionary = default(T); + if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Dictionary<,>)) + { + var dictionaryType = typeof(T); + var valueType = dictionaryType.GetGenericArguments()[1]; + + if (mValue.ToObject() is Dictionary sourceDictionary) + { + resultDictionary = Activator.CreateInstance(); + + foreach (var kvp in sourceDictionary) + { + object value = Convert.ChangeType(kvp.Value, valueType); + resultDictionary.GetType().GetMethod("Add").Invoke(resultDictionary, new object[] { kvp.Key, value }); + } + + return resultDictionary; + } + else + { + throw new ArgumentException("Quellobjekt muss ein Dictionary sein."); + } + } + return resultDictionary; + } + + @object = mValue.ToObject(); + + return (T)Convert.ChangeType(@object, typeof(T)); + } } } \ No newline at end of file diff --git a/api/AltV.Net/Alt.GlobalMeta.cs b/api/AltV.Net/Alt.GlobalMeta.cs index b485b6a70..de87c4325 100644 --- a/api/AltV.Net/Alt.GlobalMeta.cs +++ b/api/AltV.Net/Alt.GlobalMeta.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using AltV.Net.Elements.Args; +using AltV.Net.Shared.Utils; namespace AltV.Net { @@ -14,12 +16,12 @@ public static partial class Alt public static bool GetMetaData(string key, out T result) { Core.GetMetaData(key, out var mValue); + using (mValue) { - try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch @@ -44,7 +46,7 @@ public static bool GetSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net/Alt.Import.cs b/api/AltV.Net/Alt.Import.cs index ed1495870..4428ddf98 100644 --- a/api/AltV.Net/Alt.Import.cs +++ b/api/AltV.Net/Alt.Import.cs @@ -1,6 +1,7 @@ using System; using AltV.Net.Elements.Args; using AltV.Net.Native; +using AltV.Net.Shared.Utils; namespace AltV.Net { @@ -141,7 +142,7 @@ public static bool Import(string resourceName, string key, out object value) value = mValue.ToObject(); return true; } - + public static bool Import(string resourceName, string key, out string value) { if (HostImport(resourceName, key, out value)) @@ -168,7 +169,7 @@ public static bool Import(string resourceName, string key, out MValueConst mValu throw new InvalidImportException( $"Resource: '{resourceName}' not found."); } - + if (!resource.GetExport(key, out mValue)) { throw new InvalidImportException( @@ -198,14 +199,14 @@ public static bool Import(string resourceName, string key, MValueConst.Type type Alt.Core.CreateMValue(out var mValueElement, args[i]); mValueArgs[i] = mValueElement.nativePointer; } - + result = new MValueConst(Alt.Core.Library.MValue_CallFunction(mValue.nativePointer, mValueArgs, length)); for (ulong i = 0;i < length;i++) { Alt.Core.Library.MValueConst_Delete(mValueArgs[i]); } }*/ - + private static object ImportCall(in MValueConst mValue, object[] args) { unsafe @@ -230,6 +231,30 @@ private static object ImportCall(in MValueConst mValue, object[] args) } } + private static object ImportCall(in MValueConst mValue, object[] args) + { + unsafe + { + var length = (ulong) args.Length; + var mValueArgs = new IntPtr[length]; + for (uint i = 0; i < length; i++) + { + Alt.Core.CreateMValue(out var mValueElement, args[i]); + mValueArgs[i] = mValueElement.nativePointer; + } + + var result = new MValueConst(Alt.Core, Alt.Core.Library.Shared.MValueConst_CallFunction(Alt.Core.NativePointer, mValue.nativePointer, mValueArgs, length)); + var resultObj = Utils.GetCastedMValue(result); + result.Dispose(); + for (ulong i = 0;i < length;i++) + { + Alt.Core.Library.Shared.MValueConst_Delete(mValueArgs[i]); + } + + return resultObj; + } + } + public static bool Import(string resourceName, string key, out Action value) { if (HostImport(resourceName, key, out value)) @@ -578,7 +603,7 @@ public static bool Import(string resourceName, string key, out Func { - if (ImportCall(mValue, new object[] { }) is TResult result) + if (ImportCall(mValue, new object[] { }) is TResult result) { return result; } @@ -603,7 +628,7 @@ public static bool Import(string resourceName, string key, out Func value = (p1) => { - if (ImportCall(mValue, new object[] {p1}) is TResult result) + if (ImportCall(mValue, new object[] {p1}) is TResult result) { return result; } @@ -628,7 +653,7 @@ public static bool Import(string resourceName, string key, out value = (p1, p2) => { - if (ImportCall(mValue, new object[] {p1, p2}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2}) is TResult result) { return result; } @@ -654,7 +679,7 @@ public static bool Import(string resourceName, string key, value = (p1, p2, p3) => { - if (ImportCall(mValue, new object[] {p1, p2, p3}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3}) is TResult result) { return result; } @@ -680,7 +705,7 @@ public static bool Import(string resourceName, string k value = (p1, p2, p3, p4) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4}) is TResult result) { return result; } @@ -706,7 +731,7 @@ public static bool Import(string resourceName, stri value = (p1, p2, p3, p4, p5) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5}) is TResult result) { return result; } @@ -732,7 +757,7 @@ public static bool Import(string resourceName, value = (p1, p2, p3, p4, p5, p6) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6}) is TResult result) { return result; } @@ -758,7 +783,7 @@ public static bool Import(string resourceNa value = (p1, p2, p3, p4, p5, p6, p7) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7}) is TResult result) { return result; } @@ -784,7 +809,7 @@ public static bool Import(string resour value = (p1, p2, p3, p4, p5, p6, p7, p8) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8}) is TResult result) { return result; } @@ -810,7 +835,7 @@ public static bool Import(string re value = (p1, p2, p3, p4, p5, p6, p7, p8, p9) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9}) is TResult result) { return result; } @@ -836,7 +861,7 @@ public static bool Import(stri value = (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10}) is TResult result) { return result; } @@ -863,7 +888,7 @@ public static bool Import value = (p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) => { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11}) is TResult result) + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11}) is TResult result) { return result; } @@ -890,7 +915,7 @@ public static bool Import { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12}) is TResult + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12}) is TResult result) { return result; @@ -918,7 +943,7 @@ public static bool Import { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13}) is TResult + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13}) is TResult result) { return result; @@ -946,7 +971,7 @@ public static bool Import { - if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14}) is + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14}) is TResult result) { return result; @@ -974,7 +999,7 @@ public static bool Import { - if (ImportCall(mValue, + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}) is TResult result) { return result; @@ -1002,7 +1027,7 @@ public static bool Import { - if (ImportCall(mValue, + if (ImportCall(mValue, new object[] {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16}) is TResult result) { diff --git a/api/AltV.Net/Elements/Entities/Checkpoint.cs b/api/AltV.Net/Elements/Entities/Checkpoint.cs index 72462469e..6a3eb4faa 100644 --- a/api/AltV.Net/Elements/Entities/Checkpoint.cs +++ b/api/AltV.Net/Elements/Entities/Checkpoint.cs @@ -251,7 +251,7 @@ public bool GetStreamSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net/Elements/Entities/Entity.cs b/api/AltV.Net/Elements/Entities/Entity.cs index db06c7f0f..50e911794 100644 --- a/api/AltV.Net/Elements/Entities/Entity.cs +++ b/api/AltV.Net/Elements/Entities/Entity.cs @@ -206,7 +206,7 @@ public bool GetStreamSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net/Elements/Entities/Player.cs b/api/AltV.Net/Elements/Entities/Player.cs index dee69418d..14b6549b2 100644 --- a/api/AltV.Net/Elements/Entities/Player.cs +++ b/api/AltV.Net/Elements/Entities/Player.cs @@ -85,7 +85,7 @@ public bool GetLocalMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch diff --git a/api/AltV.Net/Elements/Entities/VirtualEntity.cs b/api/AltV.Net/Elements/Entities/VirtualEntity.cs index 816f2c6c3..5756f71ff 100644 --- a/api/AltV.Net/Elements/Entities/VirtualEntity.cs +++ b/api/AltV.Net/Elements/Entities/VirtualEntity.cs @@ -71,7 +71,7 @@ public bool GetStreamSyncedMetaData(string key, out T result) try { - result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T)); + result = Utils.GetCastedMValue(mValue); return true; } catch