public bool ToMValue(object obj, Type type, out MValueConst mValue) { if (adapters.TryGetValue(type, out var adapter)) { var writer = new MValueWriter2(this); adapter.ToMValue(obj, writer); writer.ToMValue(out mValue); return(true); } mValue = default; return(false); }
public static void CreateFromObjectLocked(object obj, out MValueConst mValue) { if (obj == null) { mValue = MValueConst.Nil; return; } int i; string[] dictKeys; MValueConst[] dictValues; MValueWriter2 writer; switch (obj) { case IPlayer player: CreateLocked(player, out mValue); return; case IVehicle vehicle: CreateLocked(vehicle, out mValue); return; case IBlip blip: CreateLocked(blip, out mValue); return; case ICheckpoint checkpoint: CreateLocked(checkpoint, out mValue); return; case bool value: Alt.Server.CreateMValueBool(out mValue, value); return; case int value: Alt.Server.CreateMValueInt(out mValue, value); return; case uint value: Alt.Server.CreateMValueUInt(out mValue, value); return; case long value: Alt.Server.CreateMValueInt(out mValue, value); return; case ulong value: Alt.Server.CreateMValueUInt(out mValue, value); return; case double value: Alt.Server.CreateMValueDouble(out mValue, value); return; case float value: Alt.Server.CreateMValueDouble(out mValue, value); return; case string value: Alt.Server.CreateMValueString(out mValue, value); return; case MValueConst value: mValue = value; return; case MValueConst[] value: Alt.Server.CreateMValueList(out mValue, value, (ulong)value.Length); return; case Invoker value: Alt.Server.CreateMValueFunction(out mValue, value.NativePointer); return; case MValueFunctionCallback value: Alt.Server.CreateMValueFunction(out mValue, Alt.Server.Resource.CSharpResourceImpl.CreateInvoker(value)); return; case Net.Function function: Alt.Server.CreateMValueFunction(out mValue, Alt.Server.Resource.CSharpResourceImpl.CreateInvoker(function.Call)); return; case byte[] byteArray: Alt.Server.CreateMValueByteArray(out mValue, byteArray); break; case IDictionary dictionary: dictKeys = new string[dictionary.Count]; dictValues = new MValueConst[dictionary.Count]; i = 0; foreach (var key in dictionary.Keys) { if (key is string stringKey) { dictKeys[i++] = stringKey; } else { mValue = MValueConst.Nil; return; } } i = 0; foreach (var value in dictionary.Values) { Alt.Server.CreateMValue(out var elementMValue, value); dictValues[i++] = elementMValue; } Alt.Server.CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count); for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++) { dictValues[j].Dispose(); } return; case ICollection collection: var length = (ulong)collection.Count; var listValues = new MValueConst[length]; i = 0; foreach (var value in collection) { Alt.Server.CreateMValue(out var elementMValue, value); listValues[i++] = elementMValue; } Alt.Server.CreateMValueList(out mValue, listValues, length); for (ulong j = 0; j < length; j++) { listValues[j].Dispose(); } return; case IDictionary <string, object> dictionary: dictKeys = new string[dictionary.Count]; dictValues = new MValueConst[dictionary.Count]; i = 0; foreach (var key in dictionary.Keys) { dictKeys[i++] = key; } i = 0; foreach (var value in dictionary.Values) { Alt.Server.CreateMValue(out var elementMValue, value); dictValues[i++] = elementMValue; } Alt.Server.CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count); for (int j = 0, dictLength = dictValues.Length; j < dictLength; j++) { dictValues[j].Dispose(); } return; case IWritable writable: writer = new MValueWriter2(); writable.OnWrite(writer); writer.ToMValue(out mValue); return; case IMValueConvertible convertible: writer = new MValueWriter2(); convertible.GetAdapter().ToMValue(obj, writer); writer.ToMValue(out mValue); return; case Position position: Alt.Server.CreateMValueVector3(out mValue, position); return; case Rotation rotation: Alt.Server.CreateMValueVector3(out mValue, rotation); return; case Rgba rgba: Alt.Server.CreateMValueRgba(out mValue, rgba); return; case short value: Alt.Server.CreateMValueInt(out mValue, value); return; case ushort value: Alt.Server.CreateMValueUInt(out mValue, value); return; case Vector3 position: Alt.Server.CreateMValueVector3(out mValue, position); return; default: Alt.Log("can't convert type:" + obj.GetType()); mValue = MValueConst.Nil; return; } }
public void CreateMValue(out MValueConst mValue, object obj) { if (obj == null) { mValue = MValueConst.Nil; return; } int i; string[] dictKeys; MValueConst[] dictValues; MValueWriter2 writer; switch (obj) { case ISharedBaseObject baseObject: CreateMValueBaseObject(out mValue, baseObject); return; case bool value: CreateMValueBool(out mValue, value); return; case int value: CreateMValueInt(out mValue, value); return; case uint value: CreateMValueUInt(out mValue, value); return; case long value: CreateMValueInt(out mValue, value); return; case ulong value: CreateMValueUInt(out mValue, value); return; case double value: CreateMValueDouble(out mValue, value); return; case float value: CreateMValueDouble(out mValue, value); return; case string value: CreateMValueString(out mValue, value); return; case MValueConst value: mValue = value; return; case MValueConst[] value: CreateMValueList(out mValue, value, (ulong)value.Length); return; case Invoker value: CreateMValueFunction(out mValue, value.NativePointer); return; case MValueFunctionCallback value: CreateMValueFunction(out mValue, Resource.CSharpResourceImpl.CreateInvoker(value)); return; case Function function: CreateMValueFunction(out mValue, Resource.CSharpResourceImpl.CreateInvoker(function.Call)); return; case byte[] byteArray: CreateMValueByteArray(out mValue, byteArray); return; case IDictionary dictionary: dictKeys = new string[dictionary.Count]; dictValues = new MValueConst[dictionary.Count]; i = 0; foreach (var key in dictionary.Keys) { if (key is string stringKey) { dictKeys[i++] = stringKey; } else { mValue = MValueConst.Nil; return; } } i = 0; foreach (var value in dictionary.Values) { CreateMValue(out var elementMValue, value); dictValues[i++] = elementMValue; } CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count); for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++) { dictValues[j].Dispose(); } return; case ICollection collection: var length = (ulong)collection.Count; var listValues = new MValueConst[length]; i = 0; foreach (var value in collection) { CreateMValue(out var elementMValue, value); listValues[i++] = elementMValue; } CreateMValueList(out mValue, listValues, length); for (ulong j = 0; j < length; j++) { listValues[j].Dispose(); } return; case IDictionary <string, object> dictionary: dictKeys = new string[dictionary.Count]; dictValues = new MValueConst[dictionary.Count]; i = 0; foreach (var key in dictionary.Keys) { dictKeys[i++] = key; } i = 0; foreach (var value in dictionary.Values) { CreateMValue(out var elementMValue, value); dictValues[i++] = elementMValue; } CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count); for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++) { dictValues[j].Dispose(); } return; case IWritable writable: writer = new MValueWriter2(this); writable.OnWrite(writer); writer.ToMValue(out mValue); return; case IMValueConvertible convertible: writer = new MValueWriter2(this); convertible.GetAdapter().ToMValue(obj, writer); writer.ToMValue(out mValue); return; case Position position: CreateMValueVector3(out mValue, position); return; case Rotation rotation: CreateMValueVector3(out mValue, rotation); return; case Rgba rgba: CreateMValueRgba(out mValue, rgba); return; case short value: CreateMValueInt(out mValue, value); return; case ushort value: CreateMValueUInt(out mValue, value); return; case Vector3 position: CreateMValueVector3(out mValue, position); return; case Vector2 value: CreateMValueVector2(out mValue, value); return; default: var type = obj?.GetType(); if (type != null && IsMValueConvertible(obj.GetType())) { ToMValue(obj, type, out mValue); return; } LogInfo("can't convert type:" + type); mValue = MValueConst.Nil; return; } }