Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
            }
        }