altmp / coreclr-module

CoreClr (.NET Core Common Language Runtime) community made module
MIT License
15 stars 7 forks source link

Enums and/or MValue adapters not working properly for VirtualEntities #1

Open Rodhin opened 6 months ago

Rodhin commented 6 months ago

Hi, I was trying to set some enum values to a virtual entity's dataDict dictionary on server side (both a custom one and one from AltV.Net.Shared.Enums) and the server was complaining that it cannot convert those enums so I've created MValueAdapters for both but now on client side the value will always be the default value for the enum, should MValue adapters even work for VirtualEntity dataDict?

Server version: 15.48 (release) Nuget version: 15.0.5

Server side code:

Dictionary<string, object> data = new()
{
    { "type", VirtualEntityType.Marker },
    { "markerType", MarkerType.MarkerCylinder },
    { "color", color },
    { "scale", (Position)scale },
    { "faceCamera", faceCamera },
};
VirtualEntity = Alt.CreateVirtualEntity(_virtualEntityGroup, Position - new Position(0, 0, 1), 20, data);

Client side code:

var hasType = virtualEntity.HasStreamSyncedMetaData("type");
var hasType2 = virtualEntity.GetStreamSyncedMetaData("type", out VirtualEntityType type);
var hasMarkerType = virtualEntity.HasStreamSyncedMetaData("markerType");
var hasMarkerType2 = virtualEntity.GetStreamSyncedMetaData("markerType", out MarkerType markerType);

Alt.Log($"[DATA CHECK] hasType {hasType} {hasType2} {type}");
Alt.Log($"[DATA CHECK] hasMarkerType {hasMarkerType} {hasMarkerType2} {markerType}");

Client side log:

[DATA CHECK] hasType True False None 
[DATA CHECK] hasMarkerType True False MarkerCone 

Custom enum:

namespace Shared.Enums;

public enum VirtualEntityType
{
    None,
    Marker,
}

MValueAdapter (both looks the same):

using AltV.Net;
using AltV.Net.Shared.Enums;

namespace Shared.Adapters;

public class MarkerTypeAdapter : IMValueAdapter<MarkerType>
{
    object IMValueBaseAdapter.FromMValue(IMValueReader reader)
    {
        return FromMValue(reader);
    }

    public void ToMValue(MarkerType value, IMValueWriter writer)
    {
        writer.BeginObject();
        writer.Name("markerType");
        writer.Value(Convert.ToUInt32(value));
        writer.EndObject();
    }

    public MarkerType FromMValue(IMValueReader reader)
    {
        MarkerType data = default;

        reader.BeginObject();
        while (reader.HasNext())
        {
            switch (reader.NextName())
            {
                case "markerType":
                    data = (MarkerType)reader.NextUInt();
                    break;
            }
        }

        reader.EndObject();

        return data;
    }

    public void ToMValue(object obj, IMValueWriter writer)
    {
        if (obj is MarkerType value)
        {
            ToMValue(value, writer);
        }
    }
}
Rodhin commented 6 months ago

Original issue URL: https://github.com/FabianTerhorst/coreclr-module/issues/824

Since that I noticed something similar with LocalMeta, I'll do new tests on newer versions in the coming days and report my findings.