private Dictionary <string, object> ReadDictionary(string dictName, ByteArray ba) { int fieldType = 0; int repeatType = 0; string fieldName = null; string customType = null; try { int st_len = ba.ReadUInt(); Dictionary <string, object> tbl = new Dictionary <string, object>(); if (st_len == 0) { return(tbl); } List <Dictionary <string, object> > tableInfo = m_protocolInfo[dictName]; for (int i = 0; i < tableInfo.Count; i++) { fieldType = (int)tableInfo[i]["type"]; repeatType = (int)tableInfo[i]["spl"]; fieldName = (string)tableInfo[i]["name"]; if (fieldType == TYPE_string) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadStringList(ba); } else { tbl[fieldName] = ReadString(ba); } } else if (fieldType == TYPE_bool) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadBoolList(ba); } else { tbl[fieldName] = ReadBool(ba); } } else if (fieldType == TYPE_double) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadDoubleList(ba); } else { tbl[fieldName] = ReadDouble(ba); } } else if (fieldType == TYPE_int32) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadIntList(ba); } else { tbl[fieldName] = ReadInt32(ba); } } else if (fieldType == TYPE_int16) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadShortList(ba); } else { tbl[fieldName] = ReadInt16(ba); } } else if (fieldType == TYPE_int8) { if (repeatType == RT_repeated) { tbl[fieldName] = ReadInt8List(ba); } else { tbl[fieldName] = ReadInt8(ba); } } else { customType = (string)tableInfo[i]["vp"]; if (repeatType == RT_repeated) { tbl[fieldName] = ReadDictionaryList(customType, ba); } else { tbl[fieldName] = ReadDictionary(customType, ba); } } } return(tbl); } catch (Exception e) { throw new Exception(@"ReadDictionary Excepiton DictName is ->" + dictName + "<-\nFieldName:->" + fieldName + "<-\nFieldType:->" + GetFieldType(fieldType) + "<-\nRepeatType:->" + GetRepeatType(repeatType) + "<-\nCustomType:->" + customType + "<-\n" + e.ToString()); } }
private List <byte> GetCustomTypeByte(string customType, Dictionary <string, object> data) { string fieldName = null; int fieldType = 0; int repeatType = 0; try { ByteArray Bytes = new ByteArray(); //ByteArray Bytes = new ByteArray(); Bytes.clear(); if (!m_protocolInfo.ContainsKey(customType)) { throw new Exception("ProtocolInfo NOT Exist ->" + customType + "<-"); } List <Dictionary <string, object> > tableInfo = m_protocolInfo[customType]; for (int i = 0; i < tableInfo.Count; i++) { Dictionary <string, object> currentField = tableInfo[i]; fieldType = (int)currentField["type"]; fieldName = (string)currentField["name"]; repeatType = (int)currentField["spl"]; if (fieldType == TYPE_string) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteString((string)data[fieldName]); } else { List <object> list = (List <object>)data[fieldName]; Bytes.WriteShort(list.Count); Bytes.WriteInt(GetStringListLength(list)); for (int i2 = 0; i2 < list.Count; i2++) { Bytes.WriteString((string)list[i2]); } } } else { Bytes.WriteShort(0); } } else if (fieldType == TYPE_bool) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteBoolean((bool)data[fieldName]); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); Bytes.WriteInt(tb.Count); for (int i2 = 0; i2 < tb.Count; i2++) { Bytes.WriteBoolean((bool)tb[i2]); } } } } else if (fieldType == TYPE_double) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteDouble((float)data[fieldName]); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); Bytes.WriteInt(tb.Count * 8); for (int j = 0; j < tb.Count; j++) { Bytes.WriteDouble((float)tb[j]); } } } } else if (fieldType == TYPE_int32) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteInt(int.Parse(data[fieldName].ToString())); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); Bytes.WriteInt(tb.Count * 4); for (int i2 = 0; i2 < tb.Count; i2++) { Bytes.WriteInt(int.Parse(tb[i2].ToString())); } } } } else if (fieldType == TYPE_int16) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteShort(int.Parse(data[fieldName].ToString())); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); Bytes.WriteInt(tb.Count * 2); for (int i2 = 0; i2 < tb.Count; i2++) { Bytes.WriteShort(int.Parse(tb[i2].ToString())); } } } } else if (fieldType == TYPE_int8) { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { Bytes.WriteInt8(int.Parse(data[fieldName].ToString())); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); Bytes.WriteInt(tb.Count); for (int i2 = 0; i2 < tb.Count; i2++) { Bytes.WriteInt8(int.Parse(tb[i2].ToString())); } } } } else { if (data.ContainsKey(fieldName)) { if (repeatType == RT_equired) { customType = (string)currentField["vp"]; Bytes.bytes.AddRange(GetSendByte(customType, (Dictionary <string, object>)data[fieldName])); } else { List <object> tb = (List <object>)data[fieldName]; Bytes.WriteShort(tb.Count); //这里会修改m_arrayCatch的值,下面就可以直接使用 Bytes.WriteInt(GetCustomListLength(customType, tb)); for (int j = 0; j < m_arrayCache.Count; j++) { List <byte> tempb = m_arrayCache[j]; Bytes.WriteInt(tempb.Count); Bytes.bytes.AddRange(tempb); } } } } } return(Bytes.bytes); } catch (Exception e) { throw new Exception(@"GetCustomTypeByte Excepiton CustomType is ->" + customType + "<-\nFieldName:->" + fieldName + "<-\nFieldType:->" + GetFieldType(fieldType) + "<-\nRepeatType:->" + GetRepeatType(repeatType) + "<-\nCustomType:->" + customType + "<-\n" + e.ToString()); } }
private double ReadDouble(ByteArray ba) { double tem_double = ba.ReadDouble(); return(Math.Floor(tem_double * 1000) / 1000); }
private int ReadInt8(ByteArray ba) { return(ba.ReadInt8()); }
private bool ReadBool(ByteArray ba) { return(ba.ReadBoolean()); }
private string ReadString(ByteArray ba) { uint len = (uint)ba.ReadUShort(); return(ba.ReadUTFBytes(len)); }
Dictionary <string, object> AnalysisData(string MessageType, byte[] bytes) { //Debug.Log("MessageType:" + MessageType + "AnalysisData: " + BitConverter.ToString(bytes)); string fieldName = ""; string customType = ""; int fieldType = 0; int repeatType = 0; try { Dictionary <string, object> data = new Dictionary <string, object>(); ByteArray ba = new ByteArray(); ba.clear(); ba.Add(bytes); string messageTypeTemp = "m_" + MessageType + "_s"; if (!m_protocolInfo.ContainsKey(messageTypeTemp)) { throw new Exception("ProtocolInfo NOT Exist ->" + messageTypeTemp + "<-"); } List <Dictionary <string, object> > tableInfo = m_protocolInfo["m_" + MessageType + "_s"]; for (int i = 0; i < tableInfo.Count; i++) { fieldType = (int)tableInfo[i]["type"]; repeatType = (int)tableInfo[i]["spl"]; fieldName = (string)tableInfo[i]["name"]; if (fieldType == TYPE_string) { if (repeatType == RT_repeated) { data[fieldName] = ReadStringList(ba); } else { data[fieldName] = ReadString(ba); } } else if (fieldType == TYPE_bool) { if (repeatType == RT_repeated) { data[fieldName] = ReadBoolList(ba); } else { data[fieldName] = ReadBool(ba); } } else if (fieldType == TYPE_double) { if (repeatType == RT_repeated) { data[fieldName] = ReadDoubleList(ba); } else { data[fieldName] = ReadDouble(ba); } } else if (fieldType == TYPE_int32) { if (repeatType == RT_repeated) { data[fieldName] = ReadIntList(ba); } else { data[fieldName] = ReadInt32(ba); } } else if (fieldType == TYPE_int16) { if (repeatType == RT_repeated) { data[fieldName] = ReadShortList(ba); } else { data[fieldName] = ReadInt16(ba); } } else if (fieldType == TYPE_int8) { if (repeatType == RT_repeated) { data[fieldName] = ReadInt8List(ba); } else { data[fieldName] = ReadInt8(ba); } } else { customType = (string)tableInfo[i]["vp"]; if (repeatType == RT_repeated) { data[fieldName] = ReadDictionaryList(customType, ba); } else { data[fieldName] = ReadDictionary(customType, ba); } } } return(data); } catch (Exception e) { throw new Exception(@"AnalysisData Excepiton Data is ->" + MessageType + "<-\nFieldName:->" + fieldName + "<-\nFieldType:->" + GetFieldType(fieldType) + "<-\nRepeatType:->" + GetRepeatType(repeatType) + "<-\nCustomType:->" + customType + "<-\n" + e.ToString()); } }