private IMPObject DecodeMPObject(ByteArray buffer) { MPObject obj2 = MPObject.NewInstance(); byte num = buffer.ReadByte(); if (num != Convert.ToByte(MPDataType.MP_OBJECT)) { throw new Exception(string.Concat("Invalid MPDataType. Expected: ", MPDataType.MP_OBJECT, ", found: ", num)); } int num2 = buffer.ReadShort(); if (num2 < 0) { throw new Exception("Can't decode MPObject. Size is negative: " + num2); } for (int i = 0; i < num2; i++) { string key = buffer.ReadUTF(); MPDataWrapper val = DecodeObject(buffer); if (val == null) { throw new Exception("Could not decode value for MPObject with key: " + key); } obj2.Put(key, val); } return(obj2); }
private IMPArray DecodeMPArray(ByteArray buffer) { IMPArray array = MPArray.NewInstance(); MPDataType type = (MPDataType)Convert.ToInt32(buffer.ReadByte()); if (type != MPDataType.MP_ARRAY) { throw new Exception(string.Concat("Invalid MPDataType. Expected: ", MPDataType.MP_ARRAY, ", found: ", type)); } int num = buffer.ReadShort(); if (num < 0) { throw new Exception("Can't decode MPArray. Size is negative: " + num); } for (int i = 0; i < num; i++) { MPDataWrapper val = DecodeObject(buffer); if (val == null) { throw new Exception("Could not decode SFSArray item at index: " + i); } array.Add(val); } return(array); }
private void ConvertCsObj(object csObj, IMPObject mpObj) { Type type = csObj.GetType(); string fullName = type.FullName; if (!(mpObj is Serializable)) { throw new Exception(string.Concat("Cannot serialize object: ", csObj, ", type: ", fullName, " -- It doesn't implement the SerializableSFSType interface")); } IMPArray val = MPArray.NewInstance(); mpObj.PutUtfString(CLASS_MARKER_KEY, fullName); mpObj.PutMPArray(CLASS_FIELDS_KEY, val); foreach (FieldInfo info in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { string name = info.Name; object obj2 = info.GetValue(csObj); IMPObject obj3 = MPObject.NewInstance(); MPDataWrapper wrapper = WrapField(obj2); if (wrapper != null) { obj3.PutUtfString(FIELD_NAME_KEY, name); obj3.Put(FIELD_VALUE_KEY, wrapper); val.AddMPObject(obj3); } else { throw new Exception(string.Concat("Cannot serialize field of object: ", csObj, ", field: ", name, ", type: ", info.GetType().Name, " -- unsupported type!")); } } }
private IMPObject UnrollDictionary(IDictionary dict) { IMPObject obj2 = MPObject.NewInstance(); IEnumerator enumerator = dict.Keys.GetEnumerator(); try { while (enumerator.MoveNext()) { string current = (string)enumerator.Current; MPDataWrapper val = WrapField(dict[current]); if (val == null) { throw new Exception(string.Concat("Cannot serialize field of dictionary with key: ", current, ", ", dict[current], " -- unsupported type!")); } obj2.Put(current, val); } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } return(obj2); }
private object UnwrapField(MPDataWrapper wrapper) { object obj2 = null; int type = wrapper.Type; if (type <= (int)MPDataType.UTF_STRING) { return(wrapper.Data); } switch (type) { case (int)MPDataType.MP_ARRAY: return(RebuildArray(wrapper.Data as IMPArray)); case (int)MPDataType.MP_OBJECT: { IMPObject data = wrapper.Data as IMPObject; if (data.ContainsKey(CLASS_MARKER_KEY) && data.ContainsKey(CLASS_FIELDS_KEY)) { return(Cs2Mp(data)); } return(RebuildDict(wrapper.Data as IMPObject)); } case (int)MPDataType.CLASS: obj2 = wrapper.Data; break; } return(obj2); }
/// <summary> /// /// </summary> /// <returns></returns> public LuaTable ToLuaTable() { object[] tem = LuaScriptMgr.Instance.CallLuaFunction("Map.New"); LuaTable table = tem[0] as LuaTable; string[] keys = GetKeys(); foreach (string key in keys) { MPDataWrapper wrapp = GetData(key); switch (((MPDataType)wrapp.Type)) { case MPDataType.NULL: break; case MPDataType.BOOL: case MPDataType.BYTE: case MPDataType.SHORT: case MPDataType.INT: case MPDataType.LONG: case MPDataType.FLOAT: case MPDataType.DOUBLE: case MPDataType.UTF_STRING: table[key] = wrapp.Data; break; case MPDataType.MP_OBJECT: table[key] = ((IMPObject)wrapp.Data).ToLuaTable(); break; } } return(table); }
private ByteArray Arr2bin(IMPArray array, ByteArray buffer) { for (int i = 0; i < array.Size(); i++) { MPDataWrapper wrappedElementAt = array.GetWrappedElementAt(i); buffer = EncodeObject(buffer, wrappedElementAt.Type, wrappedElementAt.Data); } return(buffer); }
public T GetValue <T>(int index) { if (index >= dataHolder.Count) { return(default(T)); } MPDataWrapper wrapper = dataHolder[index]; return((T)wrapper.Data); }
static int Put(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 3); MPObject obj = LuaScriptMgr.GetNetObject <MPObject>(L, 1); string arg0 = LuaScriptMgr.GetLuaString(L, 2); com.gt.entities.MPDataWrapper arg1 = LuaScriptMgr.GetNetObject <com.gt.entities.MPDataWrapper>(L, 3); obj.Put(arg0, arg1); return(0); }
private ByteArray Obj2bin(IMPObject obj, ByteArray buffer) { foreach (string str in obj.GetKeys()) { MPDataWrapper data = obj.GetData(str); buffer = EncodeMPObjectKey(buffer, str); buffer = EncodeObject(buffer, data.Type, data.Data); } return(buffer); }
public bool IsNull(int index) { if (index >= dataHolder.Count) { return(true); } MPDataWrapper wrapper = dataHolder[index]; return(wrapper.Type == 0); }
static int GetData(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 2); MPObject obj = LuaScriptMgr.GetNetObject <MPObject>(L, 1); string arg0 = LuaScriptMgr.GetLuaString(L, 2); com.gt.entities.MPDataWrapper o = obj.GetData(arg0); LuaScriptMgr.PushObject(L, o); return(1); }
public object RemoveElementAt(int index) { if (index >= dataHolder.Count) { return(null); } MPDataWrapper wrapper = dataHolder[index]; dataHolder.RemoveAt(index); return(wrapper.Data); }
private MPDataWrapper WrapField(object val) { if (val == null) { return(new MPDataWrapper(MPDataType.NULL, null)); } MPDataWrapper wrapper = null; if (val is bool) { return(new MPDataWrapper(MPDataType.BOOL, val)); } if (val is int) { return(new MPDataWrapper(MPDataType.INT, val)); } if (val is double) { return(new MPDataWrapper(MPDataType.DOUBLE, val)); } if (val is string) { return(new MPDataWrapper(MPDataType.UTF_STRING, val)); } if (val is Serializable) { return(new MPDataWrapper(MPDataType.MP_OBJECT, Cs2Mp(val))); } if (val is IDictionary) { wrapper = new MPDataWrapper(MPDataType.MP_OBJECT, UnrollDictionary(val as IDictionary)); } if (val.GetType().IsArray) { wrapper = new MPDataWrapper(MPDataType.MP_ARRAY, UnrollArray((object[])val)); } if (val is ICollection) { wrapper = new MPDataWrapper(MPDataType.MP_ARRAY, UnrollCollection(val as ICollection)); } if (val is IMPObject) { wrapper = new MPDataWrapper(MPDataType.MP_OBJECT, val); } if (val is IMPArray) { wrapper = new MPDataWrapper(MPDataType.MP_ARRAY, val); } return(wrapper); }
private void ConvertMPObject(IMPArray fieldList, object csObj, Type objType) { for (int i = 0; i < fieldList.Size(); i++) { IMPObject sFSObject = fieldList.GetMPObject(i); string utfString = sFSObject.GetUtfString(FIELD_NAME_KEY); MPDataWrapper data = sFSObject.GetData(FIELD_VALUE_KEY); object obj3 = UnwrapField(data); FieldInfo field = objType.GetField(utfString, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (field == null) { throw new Exception(string.Format("The deserialized class ({0}) doesn't contain the field: {1}", objType.FullName, utfString)); } field.SetValue(csObj, obj3); } }
public void Put(string key, MPDataWrapper val) { dataHolder[key] = val; }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public object this[string key] { get { if (!dataHolder.ContainsKey(key)) { return(null); } return(dataHolder[key].Data); } set { if (value == null) { dataHolder[key] = new MPDataWrapper(MPDataType.NULL, value); } else if (value is bool) { dataHolder[key] = new MPDataWrapper(MPDataType.BOOL, value); } else if (value is int) { dataHolder[key] = new MPDataWrapper(MPDataType.INT, value); } else if (value is byte) { dataHolder[key] = new MPDataWrapper(MPDataType.BYTE, value); } else if (value is short) { dataHolder[key] = new MPDataWrapper(MPDataType.SHORT, value); } else if (value is string) { dataHolder[key] = new MPDataWrapper(MPDataType.UTF_STRING, value); } else if (value is long) { dataHolder[key] = new MPDataWrapper(MPDataType.LONG, value); } else if (value is float) { dataHolder[key] = new MPDataWrapper(MPDataType.FLOAT, value); } else if (value is double) { dataHolder[key] = new MPDataWrapper(MPDataType.DOUBLE, value); } else if (value is IMPObject) { dataHolder[key] = new MPDataWrapper(MPDataType.MP_OBJECT, value); } else if (value is IMPArray) { dataHolder[key] = new MPDataWrapper(MPDataType.MP_ARRAY, value); } else if (value is Serializable) { dataHolder[key] = new MPDataWrapper(MPDataType.CLASS, value); } else { throw new ArgumentException("Unknown data type : " + value.GetType()); } } }
public void PutBool(string key, bool val) { dataHolder[key] = new MPDataWrapper(MPDataType.BOOL, val); }
public void PutLong(string key, long val) { dataHolder[key] = new MPDataWrapper(MPDataType.LONG, val); }
public void PutByte(string key, byte val) { dataHolder[key] = new MPDataWrapper(MPDataType.BYTE, val); }
public void PutNull(string key) { dataHolder[key] = new MPDataWrapper(MPDataType.NULL, null); }
public void PutMPArray(string key, IMPArray val) { dataHolder[key] = new MPDataWrapper(MPDataType.MP_ARRAY, val); }
public void PutByteArray(string key, ByteArray val) { dataHolder[key] = new MPDataWrapper(MPDataType.BYTE_ARRAY, val); }
public void PutInt(string key, int val) { dataHolder[key] = new MPDataWrapper(MPDataType.INT, val); }
public void PutMPObject(string key, IMPObject val) { dataHolder[key] = new MPDataWrapper(MPDataType.MP_OBJECT, val); }
public void PutDouble(string key, double val) { dataHolder[key] = new MPDataWrapper(MPDataType.DOUBLE, val); }
public void PutClass(string key, Object val) { dataHolder[key] = new MPDataWrapper(MPDataType.CLASS, val); }
public void PutShort(string key, short val) { dataHolder[key] = new MPDataWrapper(MPDataType.SHORT, val); }
public void PutUtfString(string key, string val) { dataHolder[key] = new MPDataWrapper(MPDataType.UTF_STRING, val); }
public void PutFloat(string key, float val) { dataHolder[key] = new MPDataWrapper(MPDataType.FLOAT, val); }