public Type GetType(ushort index) { Type type = mTypes[index]; if (type != null) { return(type); } type = Type.GetType(string.Format("{0},{1}", mTypeNames[index], mNameSpace)); mTypes[index] = type; ProtoBufInheritSupport.AddType(type); return(type); }
static void SerializeXml(string name, Stream streamData, string xmlPath, string typeName, Dictionary <string, int> allTypes) { Type type = Type.GetType(string.Format("{0},Assembly-CSharp", typeName)); if (type == null) { type = Type.GetType(string.Format("{0},Assembly-CSharp-Editor", typeName)); if (type == null) { throw new Exception(string.Format("Type '{0}' not found !", typeName)); } } FileStream fs = File.OpenRead(xmlPath); XmlReader reader = XmlReader.Create(fs); try { int typeIndex; if (!allTypes.TryGetValue(typeName, out typeIndex)) { ProtoBufInheritSupport.AddType(type); //UnityEngine.Debug.LogError(typeName); typeIndex = allTypes.Count; allTypes.Add(typeName, typeIndex); } byte[] bytesType = BitConverter.GetBytes((ushort)typeIndex); streamData.Write(bytesType, 0, bytesType.Length); XSerializer serializer = new XSerializer(type); object obj = serializer.Deserialize(reader); if (OnPreSerialize != null) { OnPreSerialize(name, type, ref obj); } RuntimeTypeModel.Default.SerializeWithLengthPrefix(streamData, obj, type, ProtoBuf.PrefixStyle.Fixed32, 0); fs.Close(); } catch (Exception e) { if (reader is XmlTextReader) { XmlTextReader xr = reader as XmlTextReader; string err = string.Format("Parse Xml Failed at '{0}':{1},{2}", xmlPath, xr.LineNumber, xr.LinePosition); e = new Exception(string.Concat(err, "\n\n", e.Message), e); } reader.Close(); fs.Close(); throw e; } FunPlus.Util.CheckXML.GetInstance().Check(type, xmlPath, false, true); }
private static void SerializeXmlSheet(string[] xmlPaths, string[] typeNames, IList <SheetDataIndex> indices, Stream stream, Dictionary <string, int> allTypes, out bool strKey) { strKey = false; List <Type> itemTypes = new List <Type>(); List <int> typeIndices = new List <int>();; List <PropertyInfo> propIds = new List <PropertyInfo>(); SortedList <uint, List <TypeAndObj> > objsInt = null; SortedList <string, List <TypeAndObj> > objsStr = null; for (int i = 0, imax = typeNames.Length; i < imax; i++) { string typeName = typeNames[i]; Type itemType = Type.GetType(string.Format("{0},Assembly-CSharp", typeName)); if (itemType == null) { itemType = Type.GetType(string.Format("{0},Assembly-CSharp-Editor", typeName)); if (itemType == null) { throw new Exception(string.Format("Cannot find type '{0}' !", typeName)); } } int typeIndex; if (!allTypes.TryGetValue(typeName, out typeIndex)) { ProtoBufInheritSupport.AddType(itemType); //UnityEngine.Debug.LogError(typeName); typeIndex = allTypes.Count; allTypes.Add(typeName, typeIndex); } PropertyInfo[] props = itemType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); PropertyInfo propId = null; Type protoMember = typeof(ProtoBuf.ProtoMemberAttribute); for (int j = 0, jmax = props.Length; j < jmax; j++) { PropertyInfo prop = props[j]; ProtoBuf.ProtoMemberAttribute attr = Attribute.GetCustomAttribute(prop, protoMember) as ProtoBuf.ProtoMemberAttribute; if (attr == null || attr.Tag != 1) { continue; } propId = prop; break; } if (propId == null) { throw new Exception("'ProtoMember 1' is required as Id or Key property !"); } if (propId.PropertyType.Equals(typeof(uint)) || propId.PropertyType.Equals(typeof(int))) { if (objsStr != null) { throw new Exception("Id or Key should only be the same type in one sheet !: " + propId.Name + " " + typeName); } strKey = false; if (objsInt == null) { objsInt = new SortedList <uint, List <TypeAndObj> >(); } } else if (propId.PropertyType.Equals(typeof(string))) { if (objsInt != null) { throw new Exception("Id or Key should only be the same type in one sheet !: " + propId.Name + " " + typeName); } strKey = true; if (objsStr == null) { objsStr = new SortedList <string, List <TypeAndObj> >(); } } else { throw new Exception("Id or Key with 'ProtoMember=1' should only be int or string type !"); } itemTypes.Add(itemType); propIds.Add(propId); typeIndices.Add(typeIndex); } Exception ex = null; for (int i = 0, imax = xmlPaths.Length; i < imax; i++) { if (ex != null) { break; } string xmlPath = xmlPaths[i]; Type itemType = itemTypes[i]; byte[] typeIndex = BitConverter.GetBytes((ushort)typeIndices[i]); FileStream fs = File.OpenRead(xmlPath); XmlReader reader = XmlReader.Create(fs); XSerializer x = new XSerializer(itemType); PropertyInfo propId = propIds[i]; try { while (reader.Read()) { if (reader.Name == "Root" || reader.Name == "ConfLangs" || reader.Name == "ConfMsgCodeList") { continue; } else if (reader.Depth < 1 && reader.NodeType != XmlNodeType.Element) { continue; } object obj; try { obj = x.Deserialize(reader); } catch (Exception e) { string err = xmlPath + " (" + itemType + ") : Failed to parse \"" + reader.Name + "\""; System.Xml.XmlTextReader xr = reader as System.Xml.XmlTextReader; if (xr != null) { err += " [" + xr.LineNumber + " row, " + xr.LinePosition + " column]"; } UnityEngine.Debug.LogError(err + "\n" + e.ToString()); continue; } //UnityEngine.Debug.LogWarning(obj); List <TypeAndObj> list; if (objsInt != null) { uint ik = 0; if (propId.PropertyType.Equals(typeof(uint))) { ik = (uint)propId.GetValue(obj, null); } else if (propId.PropertyType.Equals(typeof(int))) { int temp = (int)propId.GetValue(obj, null); ik = (uint)temp; } if (objsInt.TryGetValue(ik, out list)) { list.Add(new TypeAndObj(itemType, obj, typeIndex)); } else { list = new List <TypeAndObj>(); list.Add(new TypeAndObj(itemType, obj, typeIndex)); objsInt.Add(ik, list); } } if (objsStr != null) { string sk = (string)propId.GetValue(obj, null); if (objsStr.TryGetValue(sk, out list)) { list.Add(new TypeAndObj(itemType, obj, typeIndex)); } else { list = new List <TypeAndObj>(); list.Add(new TypeAndObj(itemType, obj, typeIndex)); objsStr.Add(sk, list); } } } } catch (Exception e) { objsInt = null; objsStr = null; ex = e; if (reader is XmlTextReader) { XmlTextReader xr = reader as XmlTextReader; string err = string.Format("Parse Xml Failed at '{0}':{1},{2}", xmlPath, xr.LineNumber, xr.LinePosition); ex = new Exception(string.Concat(err, "\n\n", e.Message), e); } } reader.Close(); fs.Close(); FunPlus.Util.CheckXML.GetInstance().Check(itemType, xmlPath, true, true); if (ex != null) { break; } } if (objsInt != null) { foreach (KeyValuePair <uint, List <TypeAndObj> > kv in objsInt) { foreach (TypeAndObj to in kv.Value) { object obj = to.obj; if (OnIdPreSerialize != null) { OnIdPreSerialize(kv.Key, to.type, ref obj); } SheetDataIntIndex index = new SheetDataIntIndex(kv.Key, (int)stream.Position); stream.Write(to.typeIndex, 0, to.typeIndex.Length); RuntimeTypeModel.Default.SerializeWithLengthPrefix(stream, obj, to.type, ProtoBuf.PrefixStyle.Fixed32, 0); indices.Add(index); } } } if (objsStr != null) { foreach (KeyValuePair <string, List <TypeAndObj> > kv in objsStr) { foreach (TypeAndObj to in kv.Value) { object obj = to.obj; if (OnKeyPreSerialize != null) { OnKeyPreSerialize(kv.Key, to.type, ref obj); } SheetDataStringIndex index = new SheetDataStringIndex(kv.Key, (int)stream.Position); stream.Write(to.typeIndex, 0, to.typeIndex.Length); RuntimeTypeModel.Default.SerializeWithLengthPrefix(stream, obj, to.type, ProtoBuf.PrefixStyle.Fixed32, 0); indices.Add(index); } } } if (ex != null) { throw ex; } }