Skip to content

Commit

Permalink
feat(shared): fix getMetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Mar 31, 2024
1 parent 8457a8d commit a5f6c60
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 38 deletions.
7 changes: 4 additions & 3 deletions api/AltV.Net.Client/Alt.GlobalMeta.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AltV.Net.Elements.Args;
using AltV.Net.Shared.Utils;

namespace AltV.Net.Client
{
Expand Down Expand Up @@ -71,7 +72,7 @@ public static bool GetMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand All @@ -90,7 +91,7 @@ public static bool GetSyncedMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand All @@ -109,7 +110,7 @@ public static bool GetLocalMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
2 changes: 1 addition & 1 deletion api/AltV.Net.Client/Elements/Data/LocalStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public bool Get<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
2 changes: 1 addition & 1 deletion api/AltV.Net.Client/Elements/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public bool GetStreamSyncedMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
2 changes: 1 addition & 1 deletion api/AltV.Net.Client/Elements/Entities/VirtualEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public bool GetStreamSyncedMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
5 changes: 3 additions & 2 deletions api/AltV.Net.Shared/Elements/Args/MValueConst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -252,6 +252,7 @@ public byte[] GetByteArray()

public object ToObject()
{
Console.WriteLine($"To Object: {type}");
switch (type)
{
case Type.None:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions api/AltV.Net.Shared/Elements/Entities/SharedBaseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public bool GetMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down Expand Up @@ -302,7 +302,7 @@ public bool GetSyncedMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
118 changes: 118 additions & 0 deletions api/AltV.Net.Shared/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using AltV.Net.Elements.Args;

namespace AltV.Net.Shared.Utils
{
Expand All @@ -25,5 +26,122 @@ public static uint Hash(string stringToHash)

return hash;
}

public static T GetCastedMValue<T>(MValueConst mValue)
{
object @object;

if (mValue.type == MValueConst.Type.List)
{
if (mValue.ToObject() is IEnumerable<object> sourceEnumerable)
{

if (typeof(T).IsArray)
{
var resultList = new List<object>();
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<T>();

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<object> sein.");
}

/*
T resultList = default(T)!;
if (typeof(T).IsGenericType && (typeof(T).GetGenericTypeDefinition() == typeof(List<>)))
{
resultList = Activator.CreateInstance<T>();
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<string, object> sourceDictionary)
{
resultDictionary = Activator.CreateInstance<T>();

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<object, object> sein.");
}
}
return resultDictionary;
}

@object = mValue.ToObject();

return (T)Convert.ChangeType(@object, typeof(T));
}
}
}
8 changes: 5 additions & 3 deletions api/AltV.Net/Alt.GlobalMeta.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using AltV.Net.Elements.Args;
using AltV.Net.Shared.Utils;

namespace AltV.Net
{
Expand All @@ -14,12 +16,12 @@ public static partial class Alt
public static bool GetMetaData<T>(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<T>(mValue);
return true;
}
catch
Expand All @@ -44,7 +46,7 @@ public static bool GetSyncedMetaData<T>(string key, out T result)

try
{
result = (T)Convert.ChangeType(mValue.ToObject(), typeof(T));
result = Utils.GetCastedMValue<T>(mValue);
return true;
}
catch
Expand Down
Loading

0 comments on commit a5f6c60

Please sign in to comment.