public void LoadMetaClasses() { var t1 = Support.Time.HighPrecision_GetTickCount(); var dirs = CEngine.Instance.FileManager.GetDirectories(MetaDirectory.Address); if (dirs == null) { Profiler.Log.WriteLine(Profiler.ELogTag.Error, "MetaData", $"MetaDirectory.Address is null:{MetaDirectory.Address}"); return; } foreach (var i in dirs) { if (false == CEngine.Instance.FileManager.FileExists(i + "/typename.txt")) { continue; } //System.IO.StreamReader sr = new System.IO.StreamReader(i + "/typename.txt", System.Text.Encoding.ASCII); //string className = sr.ReadLine(); //sr.Close(); byte[] bytes = IO.FileManager.ReadFile(i + "/typename.txt"); string className = System.Text.Encoding.ASCII.GetString(bytes); className = className.Replace("\r\n", ""); /////////////////////////////////////////////////////////////////// //if (className.Contains("enginecore")) //{ // using (var sw = new System.IO.StreamWriter(i + "/typename.txt", false, System.Text.Encoding.ASCII)) // { // className = className.Replace("enginecore", "EngineCore"); // sw.WriteLine(className); // } //} //if (className.Contains("Common|EngineCore")) //{ // using (var sw = new System.IO.StreamWriter(i + "/typename.txt", false, System.Text.Encoding.ASCII)) // { // className = className.Replace("Common|EngineCore", "Client|EngineCore"); // sw.WriteLine(className); // } //} /////////////////////////////////////////////////////////////////// bool isRedirection; var type = RttiHelper.GetTypeFromSaveString(className, out isRedirection); if (type == null) { continue; } if (CIPlatform.Instance.PlayMode != CIPlatform.enPlayMode.Game) { var noUsedFile = i + "/" + type.FullName + ".noused"; if (CEngine.Instance.FileManager.FileExists(noUsedFile) == false) { CEngine.Instance.FileManager.CreateFile(noUsedFile); } } MetaClass klass = new MetaClass(); klass.MetaType = type; var csIdx = className.IndexOf('|'); var fileStr = className; if (csIdx >= 0) { fileStr = className.Substring(csIdx + 1); } klass.ClassName = RName.GetRName(MetaDirectory.Name + "/" + fileStr); bool hasRedirection = false; klass.LoadXnd(out hasRedirection); #if PWindow //if (hasRedirection) // klass.Save2Xnd(false); #endif // 重定向后由于原来的类型已不存在,不再进行当前版本的处理 if (!isRedirection) { MetaData curVer = new MetaData(); curVer.BuildMetaData(type); MetaData data = klass.FindMetaData(curVer.MetaHash); if (data == null) { klass.RegMetaData(curVer.MetaHash, curVer); #if PWindow var xnd = IO.XndHolder.NewXNDHolder(); curVer.Save2Xnd(xnd.Node); var hash = klass.GetFolderHash(); var file = klass.ClassName.GetRootFolder() + "MetaClasses/" + hash.ToString() + "/" + curVer.MetaHash + ".MetaData"; IO.XndHolder.SaveXND(file, xnd); klass.CurrentVersion = curVer; #endif } else { klass.CurrentVersion = data; } } Klasses.Add(className, klass); } var t2 = Support.Time.HighPrecision_GetTickCount(); Profiler.Log.WriteLine(Profiler.ELogTag.Info, "MetaData", $"LoadMetaClasses Time:{t2-t1}"); //MetaDatas.Clear(); //foreach (var i in Klasses) //{ // foreach (var j in i.Value.Metas) // { // MetaDatas.Add(j.Key, j.Value); // } //} }
private bool LoadXnd(IO.XndNode node, out bool isRedirection, out bool needSave) { needSave = false; isRedirection = false; var attr = node.FindAttrib("Properties"); if (attr == null) { return(false); } try { attr.BeginRead(); string metaStr; attr.Read(out metaStr); MetaType = RttiHelper.GetTypeFromSaveString(metaStr, out isRedirection); if (MetaType == null) { return(false); } var method = MetaType.GetMethod("WhenMetaDataBeginLoad"); if (method != null && method.IsStatic) { method.Invoke(null, null); } UInt32 hash; attr.Read(out hash); MetaHash = hash; Members.Clear(); int count = 0; attr.Read(out count); bool hasRedirectionType = false; for (int i = 0; i < count; i++) { var item = new FieldDesc(); string typeStr; attr.Read(out typeStr); attr.Read(out item.Name); var saveType = RttiHelper.GetTypeFromSaveString(typeStr, out hasRedirectionType); if (hasRedirectionType) {//如果有成员变量被重定向成别的类型,需要用到的metadata存盘成新的定向后的数据 needSave = true; } isRedirection = isRedirection || hasRedirectionType; if (saveType == null) { Profiler.Log.WriteLine(Profiler.ELogTag.Warning, "MetaData", $"RttiHelper.GetTypeFromSaveString == null: {typeStr}"); } var pinfo = MetaType.GetProperty(item.Name); if (pinfo != null) { if (saveType == pinfo.PropertyType) { item.PropInfo = new Rtti.PropMemberDesc(pinfo); } else { Profiler.Log.WriteLine(Profiler.ELogTag.Warning, "MetaData", $"MetaData Property {item.Name} SaveType({typeStr}) != Type({pinfo.PropertyType.ToString()})"); item.PropInfo = new Rtti.LostedMemberDesc(saveType, MetaType, item.Name); } } var finfo = MetaType.GetField(item.Name); if (finfo != null) { if (saveType == finfo.FieldType) { System.Diagnostics.Debug.Assert(item.PropInfo == null); item.PropInfo = new Rtti.FieldMemberDesc(finfo); } else { Profiler.Log.WriteLine(Profiler.ELogTag.Warning, "MetaData", $"MetaData Field {item.Name} SaveType({typeStr}) != Type({finfo.FieldType.ToString()})"); //item.PropInfo = new Rtti.FieldMemberDesc(finfo); item.PropInfo = new Rtti.LostedMemberDesc(saveType, MetaType, item.Name); } } IO.Serializer.CustomSerializer custSerializer = null; if (item.PropInfo == null) { Profiler.Log.WriteLine(Profiler.ELogTag.Warning, "MetaData", $"MetaData can't find PropInfo: {item.Name}"); item.PropInfo = new Rtti.LostedMemberDesc(saveType, MetaType, item.Name); } else { var attrs = item.PropInfo.GetCustomAttributes(typeof(IO.Serializer.CustomFieldSerializerAttribute), true); if (attrs != null && attrs.Length != 0) { var fsdesc = attrs[0] as IO.Serializer.CustomFieldSerializerAttribute; if (fsdesc != null && fsdesc.SerializerType != null) { custSerializer = System.Activator.CreateInstance(fsdesc.SerializerType) as IO.Serializer.CustomSerializer; } } } if (custSerializer != null) { item.Serializer = custSerializer; item.IsList = false; } else if (saveType != null) { item.Serializer = IO.Serializer.TypeDescGenerator.Instance.GetSerializer(saveType); if (saveType.IsGenericType && (saveType.GetInterface(typeof(System.Collections.IList).FullName) != null)) { var argType = saveType.GenericTypeArguments[0]; item.Serializer = IO.Serializer.TypeDescGenerator.Instance.GetSerializer(argType); item.IsList = true; } else { item.IsList = false; } } if (item.Serializer == null) { throw new TypeInitializationException(typeStr, null); } Members.Add(item); } } finally { attr.EndRead(); } return(true); }