Skip to content

Commit bc27cb3

Browse files
Improve internal meta data for entities and base objects
1 parent a2a0768 commit bc27cb3

File tree

12 files changed

+282
-151
lines changed

12 files changed

+282
-151
lines changed

api/AltV.Net.Mock/MockEntity.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ public bool GetSyncedMetaData<T>(string key, out T result)
111111
return true;
112112
}
113113

114+
public void SetMetaData(string key, ref MValue value)
115+
{
116+
throw new NotImplementedException();
117+
}
118+
119+
public void GetMetaData(string key, ref MValue value)
120+
{
121+
throw new NotImplementedException();
122+
}
123+
124+
public void SetSyncedMetaData(string key, ref MValue value)
125+
{
126+
throw new NotImplementedException();
127+
}
128+
129+
public void GetSyncedMetaData(string key, ref MValue value)
130+
{
131+
throw new NotImplementedException();
132+
}
133+
114134
public abstract void Remove();
115135

116136
public void CheckIfEntityExists()
@@ -119,6 +139,7 @@ public void CheckIfEntityExists()
119139
{
120140
return;
121141
}
142+
122143
throw new EntityRemovedException(this);
123144
}
124145
}

api/AltV.Net.Mock/MockWorldObject.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using AltV.Net.Data;
3+
using AltV.Net.Elements.Args;
34
using AltV.Net.Elements.Entities;
45

56
namespace AltV.Net.Mock
@@ -12,12 +13,13 @@ public MockWorldObject(IntPtr nativePointer, BaseObjectType type) : base(nativeP
1213

1314
public override Position Position { get; set; }
1415
public override short Dimension { get; set; }
15-
public override void SetMetaData(string key, object value)
16+
17+
public override void SetMetaData(string key, ref MValue value)
1618
{
1719
throw new NotImplementedException();
1820
}
1921

20-
public override bool GetMetaData<T>(string key, out T result)
22+
public override void GetMetaData(string key, ref MValue value)
2123
{
2224
throw new NotImplementedException();
2325
}

api/AltV.Net/Data/ReadOnlyPlayer.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using AltV.Net.Elements.Args;
34
using AltV.Net.Elements.Entities;
45
using AltV.Net.Native;
56

@@ -57,6 +58,26 @@ public bool GetData<T>(string key, out T result)
5758
throw new NotImplementedException();
5859
}
5960

61+
public void SetMetaData(string key, ref MValue value)
62+
{
63+
throw new NotImplementedException();
64+
}
65+
66+
public void GetMetaData(string key, ref MValue value)
67+
{
68+
throw new NotImplementedException();
69+
}
70+
71+
public void SetSyncedMetaData(string key, ref MValue value)
72+
{
73+
throw new NotImplementedException();
74+
}
75+
76+
public void GetSyncedMetaData(string key, ref MValue value)
77+
{
78+
throw new NotImplementedException();
79+
}
80+
6081
public Position Position
6182
{
6283
get => position;

api/AltV.Net/Elements/Entities/BaseObject.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Concurrent;
3+
using AltV.Net.Elements.Args;
34

45
namespace AltV.Net.Elements.Entities
56
{
@@ -12,20 +13,88 @@ public abstract class BaseObject : IBaseObject, IInternalBaseObject
1213

1314
public BaseObjectType Type { get; }
1415

15-
public abstract void SetMetaData(string key, object value);
16-
public abstract bool GetMetaData<T>(string key, out T result);
16+
public abstract void SetMetaData(string key, ref MValue value);
17+
public abstract void GetMetaData(string key, ref MValue value);
1718

1819
protected BaseObject(IntPtr nativePointer, BaseObjectType type)
1920
{
2021
if (nativePointer == IntPtr.Zero)
2122
{
2223
throw new BaseObjectRemovedException(this);
2324
}
25+
2426
NativePointer = nativePointer;
2527
Type = type;
2628
Exists = true;
2729
}
2830

31+
public void SetMetaData(string key, object value)
32+
{
33+
CheckIfEntityExists();
34+
var mValue = MValue.CreateFromObject(value);
35+
SetMetaData(key, ref mValue);
36+
}
37+
38+
public bool GetMetaData(string key, out int result)
39+
{
40+
CheckIfEntityExists();
41+
var mValue = MValue.Nil;
42+
GetMetaData(key, ref mValue);
43+
if (mValue.type != MValue.Type.INT)
44+
{
45+
result = default;
46+
return false;
47+
}
48+
49+
result = (int) mValue.GetInt();
50+
return true;
51+
}
52+
53+
public bool GetMetaData(string key, out uint result)
54+
{
55+
CheckIfEntityExists();
56+
var mValue = MValue.Nil;
57+
GetMetaData(key, ref mValue);
58+
if (mValue.type != MValue.Type.UINT)
59+
{
60+
result = default;
61+
return false;
62+
}
63+
64+
result = (uint) mValue.GetUint();
65+
return true;
66+
}
67+
68+
public bool GetMetaData(string key, out float result)
69+
{
70+
CheckIfEntityExists();
71+
var mValue = MValue.Nil;
72+
GetMetaData(key, ref mValue);
73+
if (mValue.type != MValue.Type.DOUBLE)
74+
{
75+
result = default;
76+
return false;
77+
}
78+
79+
result = (float) mValue.GetDouble();
80+
return true;
81+
}
82+
83+
public bool GetMetaData<T>(string key, out T result)
84+
{
85+
CheckIfEntityExists();
86+
var mValue = MValue.Nil;
87+
GetMetaData(key, ref mValue);
88+
if (!(mValue.ToObject() is T cast))
89+
{
90+
result = default;
91+
return false;
92+
}
93+
94+
result = cast;
95+
return true;
96+
}
97+
2998
public void SetData(string key, object value)
3099
{
31100
data[key] = value;

api/AltV.Net/Elements/Entities/Blip.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,11 @@ public override short Dimension
3737
}
3838
}
3939

40-
public override void SetMetaData(string key, object value)
41-
{
42-
CheckIfEntityExists();
43-
var mValue = MValue.CreateFromObject(value);
44-
AltNative.Blip.Blip_SetMetaData(NativePointer, key, ref mValue);
45-
}
40+
public override void GetMetaData(string key, ref MValue value) =>
41+
AltNative.Blip.Blip_GetMetaData(NativePointer, key, ref value);
4642

47-
public override bool GetMetaData<T>(string key, out T result)
48-
{
49-
CheckIfEntityExists();
50-
var mValue = MValue.Nil;
51-
AltNative.Blip.Blip_GetMetaData(NativePointer, key, ref mValue);
52-
if (!(mValue.ToObject() is T cast))
53-
{
54-
result = default;
55-
return false;
56-
}
57-
58-
result = cast;
59-
return true;
60-
}
43+
public override void SetMetaData(string key, ref MValue value) =>
44+
AltNative.Blip.Blip_SetMetaData(NativePointer, key, ref value);
6145

6246
public bool IsGlobal
6347
{

api/AltV.Net/Elements/Entities/Checkpoint.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,11 @@ public IPlayer Target
4848
}
4949
}
5050

51-
public override void SetMetaData(string key, object value)
52-
{
53-
CheckIfEntityExists();
54-
var mValue = MValue.CreateFromObject(value);
55-
AltNative.Checkpoint.Checkpoint_SetMetaData(NativePointer, key, ref mValue);
56-
}
51+
public override void GetMetaData(string key, ref MValue value) =>
52+
AltNative.Checkpoint.Checkpoint_SetMetaData(NativePointer, key, ref value);
5753

58-
public override bool GetMetaData<T>(string key, out T result)
59-
{
60-
CheckIfEntityExists();
61-
var mValue = MValue.Nil;
62-
AltNative.Checkpoint.Checkpoint_GetMetaData(NativePointer, key, ref mValue);
63-
if (!(mValue.ToObject() is T cast))
64-
{
65-
result = default;
66-
return false;
67-
}
68-
69-
result = cast;
70-
return true;
71-
}
54+
public override void SetMetaData(string key, ref MValue value) =>
55+
AltNative.Checkpoint.Checkpoint_SetMetaData(NativePointer, key, ref value);
7256

7357
public bool IsGlobal
7458
{

api/AltV.Net/Elements/Entities/Entity.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using AltV.Net.Data;
3+
using AltV.Net.Elements.Args;
34

45
namespace AltV.Net.Elements.Entities
56
{
7+
//TODO: implement all meta data implementation here and require a AltNative.Player.Player_SetSyncedMetaData(NativePointer, key, ref mValue); implementation for each entity
68
public abstract class Entity : WorldObject, IEntity
79
{
810
public ushort Id { get; }
@@ -11,8 +13,30 @@ public abstract class Entity : WorldObject, IEntity
1113

1214
public abstract uint Model { get; set; }
1315

14-
public abstract void SetSyncedMetaData(string key, object value);
15-
public abstract bool GetSyncedMetaData<T>(string key, out T result);
16+
public abstract void SetSyncedMetaData(string key, ref MValue value);
17+
public abstract void GetSyncedMetaData(string key, ref MValue value);
18+
19+
public void SetSyncedMetaData(string key, object value)
20+
{
21+
CheckIfEntityExists();
22+
var mValue = MValue.CreateFromObject(value);
23+
SetSyncedMetaData(key, ref mValue);
24+
}
25+
26+
public bool GetSyncedMetaData<T>(string key, out T result)
27+
{
28+
CheckIfEntityExists();
29+
var mValue = MValue.Nil;
30+
GetSyncedMetaData(key, ref mValue);
31+
if (!(mValue.ToObject() is T cast))
32+
{
33+
result = default;
34+
return false;
35+
}
36+
37+
result = cast;
38+
return true;
39+
}
1640

1741
protected Entity(IntPtr nativePointer, BaseObjectType type, ushort id) : base(nativePointer, type)
1842
{

api/AltV.Net/Elements/Entities/IBaseObject.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using AltV.Net.Elements.Args;
23

34
namespace AltV.Net.Elements.Entities
45
{
@@ -17,9 +18,9 @@ public interface IBaseObject
1718
/// WARNING: Do NOT use this.
1819
/// </summary>
1920
bool Exists { get; }
20-
21+
2122
BaseObjectType Type { get; }
22-
23+
2324
/// <summary>
2425
/// Sets the given object into the meta data with the given key
2526
/// </summary>
@@ -30,10 +31,62 @@ public interface IBaseObject
3031

3132
bool GetMetaData<T>(string key, out T result);
3233

34+
void SetMetaData(string key, ref MValue value);
35+
36+
void GetMetaData(string key, ref MValue value);
37+
3338
void SetData(string key, object value);
3439

3540
bool GetData<T>(string key, out T result);
3641

3742
void CheckIfEntityExists();
3843
}
44+
45+
public static class BaseObjectExtensions
46+
{
47+
public static bool GetMetaData(this IBaseObject baseObject, string key, out int result)
48+
{
49+
baseObject.CheckIfEntityExists();
50+
var mValue = MValue.Nil;
51+
baseObject.GetMetaData(key, ref mValue);
52+
if (mValue.type != MValue.Type.INT)
53+
{
54+
result = default;
55+
return false;
56+
}
57+
58+
result = (int) mValue.GetInt();
59+
return true;
60+
}
61+
62+
public static bool GetMetaData(this IBaseObject baseObject, string key, out uint result)
63+
{
64+
baseObject.CheckIfEntityExists();
65+
var mValue = MValue.Nil;
66+
baseObject.GetMetaData(key, ref mValue);
67+
if (mValue.type != MValue.Type.UINT)
68+
{
69+
result = default;
70+
return false;
71+
}
72+
73+
result = (uint) mValue.GetUint();
74+
return true;
75+
}
76+
77+
public static bool GetMetaData(this IBaseObject baseObject, string key, out float result)
78+
{
79+
baseObject.CheckIfEntityExists();
80+
var mValue = MValue.Nil;
81+
baseObject.GetMetaData(key, ref mValue);
82+
if (mValue.type != MValue.Type.DOUBLE)
83+
{
84+
result = default;
85+
return false;
86+
}
87+
88+
result = (float) mValue.GetDouble();
89+
return true;
90+
}
91+
}
3992
}

0 commit comments

Comments
 (0)