Пример #1
0
 public void Value(IPlayer value)
 {
     if (currents.TryPeek(out currCurr))
     {
         currCurr.Values.Add(MValue.Create(value));
     }
 }
Пример #2
0
 public void Value(ICheckpoint value)
 {
     if (currents.TryPeek(out currCurr))
     {
         currCurr.Values.Add(MValue.Create(value));
     }
 }
Пример #3
0
        internal static        MValue[] CreateFromObjects(object[] objects)
        {
            var length  = objects.Length;
            var mValues = new MValue[length];

            for (var i = 0; i < length; i++)
            {
                mValues[i] = CreateFromObject(objects[i]);
            }

            return(mValues);
        }
Пример #4
0
        public static bool ToMValue(object obj, Type type, out MValue mValue)
        {
            if (Adapters.TryGetValue(type, out var adapter))
            {
                var writer = new MValueWriter();
                adapter.ToMValue(obj, writer);
                mValue = writer.ToMValue();
                return(true);
            }

            mValue = default;
            return(false);
        }
Пример #5
0
        public static bool FromMValue(ref MValue mValue, Type type, out object obj)
        {
            switch (mValue.type)
            {
            case MValue.Type.LIST when Adapters.TryGetValue(type, out var adapter):
            {
                obj = adapter.FromMValue(new MValueReader(ref mValue));
                return(true);
            }

            case MValue.Type.DICT when Adapters.TryGetValue(type, out var adapter):
                obj = adapter.FromMValue(new MValueReader(ref mValue));

                return(true);

            default:
                obj = null;
                return(false);
            }
        }
Пример #6
0
 public MValueReader(ref MValue mValue)
 {
     readableMValue = new MValueStartReader(ref mValue);
 }
Пример #7
0
 public MValueStartReader(ref MValue mValue)
 {
     this.mValue       = mValue;
     MValueArrayBuffer = new MValueArrayBuffer();
 }
Пример #8
0
        //TODO: create a map that holds function pointers for each object type, its probably faster then this switch now
        public static MValue CreateFromObject(object obj)
        {
            if (obj == null)
            {
                return(Nil);
            }

            int i;

            string[]     dictKeys;
            MValue[]     dictValues;
            MValueWriter writer;

            switch (obj)
            {
            case IPlayer player:
                return(Create(player));

            case IVehicle vehicle:
                return(Create(vehicle));

            case IBlip blip:
                return(Create(blip));

            case ICheckpoint checkpoint:
                return(Create(checkpoint));

            case bool value:
                return(Create(value));

            case int value:
                return(Create(value));

            case uint value:
                return(Create(value));

            case long value:
                return(Create(value));

            case ulong value:
                return(Create(value));

            case double value:
                return(Create(value));

            case float value:
                return(Create(value));

            case string value:
                return(Create(value));

            case MValue value:
                return(value);

            case MValue[] value:
                return(Create(value));

            case Invoker value:
                return(Create(value));

            case Function value:
                return(Create(value));

            case Net.Function function:
                return(Create(function.call));

            case IDictionary dictionary:
                dictKeys   = new string[dictionary.Count];
                dictValues = new MValue[dictionary.Count];
                i          = 0;
                foreach (var key in dictionary.Keys)
                {
                    if (key is string stringKey)
                    {
                        dictKeys[i++] = stringKey;
                    }
                    else
                    {
                        return(Nil);
                    }
                }

                i = 0;
                foreach (var value in dictionary.Values)
                {
                    dictValues[i++] = CreateFromObject(value);
                }

                return(Create(dictValues, dictKeys));

            case ICollection collection:
                var length     = collection.Count;
                var listValues = new MValue[length];
                i = 0;
                foreach (var value in collection)
                {
                    listValues[i++] = CreateFromObject(value);
                }

                return(Create(listValues));

            case IDictionary <string, object> dictionary:
                dictKeys   = new string[dictionary.Count];
                dictValues = new MValue[dictionary.Count];
                i          = 0;
                foreach (var key in dictionary.Keys)
                {
                    dictKeys[i++] = key;
                }

                i = 0;
                foreach (var value in dictionary.Values)
                {
                    dictValues[i++] = CreateFromObject(value);
                }

                return(Create(dictValues, dictKeys));

            case IWritable writable:
                writer = new MValueWriter();
                writable.OnWrite(writer);
                return(writer.ToMValue());

            case IMValueConvertible convertible:
                writer = new MValueWriter();
                convertible.GetAdapter().ToMValue(obj, writer);
                return(writer.ToMValue());

            case Position position:
                var posValues = new MValue[3];
                posValues[0] = Create(position.X);
                posValues[1] = Create(position.Y);
                posValues[2] = Create(position.Z);
                var posKeys = new string[3];
                posKeys[0] = "x";
                posKeys[1] = "y";
                posKeys[2] = "z";
                return(Create(posValues, posKeys));

            case Rotation rotation:
                var rotValues = new MValue[3];
                rotValues[0] = Create(rotation.Roll);
                rotValues[1] = Create(rotation.Pitch);
                rotValues[2] = Create(rotation.Yaw);
                var rotKeys = new string[3];
                rotKeys[0] = "roll";
                rotKeys[1] = "pitch";
                rotKeys[2] = "yaw";
                return(Create(rotValues, rotKeys));

            case short value:
                return(Create(value));

            case ushort value:
                return(Create(value));

            default:
                Alt.Log("can't convert type:" + obj.GetType());
                return(Nil);
            }
        }
Пример #9
0
 public MValue ToMValue()
 {
     return(MValue.Create(Values.ToArray()));
 }