protected static CMixArray ReadArray(Stream stm, CRefTable rt) { uint head = ReadInt(stm); Trace.Assert((head & 0x1) == 0x1); int count = (int)(head >> 1); CMixArray ary = new CMixArray(count); for (string key = ReadString(stm, rt); key != ""; key = ReadString(stm, rt)) { ary[key] = ReadAmf(stm, rt); } //读取子元素 for (int i = 0; i < count; i++) { ary[i] = ReadAmf(stm, rt); } return(ary); }
protected static void WriteAmf(Stream stm, object obj) { if (obj == null) { stm.WriteByte((byte)DataType.Null); } else if (obj is byte || (obj is int && (uint)(int)obj < MaxU29) || (obj is uint && (uint)obj < MaxU29)) // U29无符号整形 { stm.WriteByte((byte)DataType.Integer); WriteInt(stm, uint.Parse(obj.ToString())); } else if (obj is int || obj is uint || obj is float || obj is double) // 只列举了常用的 { stm.WriteByte((byte)DataType.Double); CDataHelper.BE_WriteDouble(stm, double.Parse(obj.ToString())); } else if (obj is bool) { if ((bool)obj) { stm.WriteByte((byte)DataType.True); } else { stm.WriteByte((byte)DataType.False); } } else if (obj is string) { stm.WriteByte((byte)DataType.String); WriteString(stm, obj as string); } else if (obj is CMixArray) { stm.WriteByte((byte)DataType.Array); CMixArray ary = obj as CMixArray; uint head = ((uint)ary.FixedLength << 1) | 1; WriteInt(stm, head); foreach (KeyValuePair <string, object> pair in ary.Dynamic) { WriteString(stm, pair.Key); WriteAmf(stm, pair.Value); } WriteString(stm, ""); foreach (object o in ary.Fixed) { WriteAmf(stm, o); } } else if (obj is Array) { stm.WriteByte((byte)DataType.Array); Array ary = obj as Array; uint head = ((uint)ary.Length << 1) | 1; WriteInt(stm, head); WriteString(stm, ""); foreach (object o in ary) { WriteAmf(stm, o); } } else if (obj is IDictionary) { stm.WriteByte((byte)DataType.Object); IDictionary dic = obj as IDictionary; uint head = 0x0B; WriteInt(stm, head); if (obj is CNameObjDict) { WriteString(stm, (obj as CNameObjDict).className); } else { WriteString(stm, ""); } foreach (DictionaryEntry e in obj as IDictionary) { if (e.Key.ToString() == "" && e.Value is string) //解析时为了好看放进去的ClassName { continue; } WriteString(stm, e.Key.ToString()); WriteAmf(stm, e.Value); } WriteString(stm, ""); } else { Trace.Assert(false, "暂未处理的数据类型"); stm.WriteByte((byte)DataType.Undefined); } }