protected override bool OnLoad(Metadata.LoadContext context, TextBlock block, out string error) { if (!base.OnLoad(context, block, out error)) { return(false); } //!!!!Mask хранить в файле return(true); }
static bool LoadContext_IsAllChildrenLoaded(Metadata.LoadContext context, Metadata.LoadContext.ComponentItem item) { foreach (var child in item.children) { if (!child.loaded) { return(false); } } return(true); }
protected override bool OnLoad(Metadata.LoadContext context, TextBlock block, out string error) { if (!base.OnLoad(context, block, out error)) { return(false); } //old version compatibility if (block.AttributeExists("NodePosition")) { Position = Vector2I.Parse(block.GetAttribute("NodePosition")); } return(true); }
static bool LoadContext_InitComponentItemsRecursive(Metadata.LoadContext context, Metadata.LoadContext.ComponentItem componentItem, out string error) { context.allComponentItemsCreationOrder.Add(componentItem); var block = componentItem.textBlock; //load Name property if (block.AttributeExists("Name")) { componentItem.name = block.GetAttribute("Name"); } //components List <Metadata.LoadContext.ComponentItem> children = new List <Metadata.LoadContext.ComponentItem>(); foreach (TextBlock childBlock in block.Children) { if (childBlock.Name == ".component") { //!!!!!!что тут с пересечениями if (string.IsNullOrEmpty(childBlock.Data)) { error = "Invalid format. Component type is not specified."; return(false); } Metadata.LoadContext.ComponentItem child = new Metadata.LoadContext.ComponentItem(); child.parent = componentItem; child.textBlock = childBlock; if (!LoadContext_InitComponentItemsRecursive(context, child, out error)) { return(false); } children.Add(child); } } componentItem.children = children.ToArray(); context.allComponentItemsSerializationOrder.Add(componentItem); error = ""; return(true); }
static void LoadContext_ProcessComponentsCreation(Metadata.LoadContext context, out bool changed, out bool finished, ESet <string> unableToGetTypeName) { changed = false; finished = true; foreach (var item in context.allComponentItemsCreationOrder) { LoadContext_ProcessComponentCreation(context, item, out bool changed2, out bool finished2, unableToGetTypeName); if (changed2) { changed = true; } if (!finished2) { finished = false; } } }
static bool LoadContext_ProcessComponentSerialization(Metadata.LoadContext context, Metadata.LoadContext.ComponentItem item, out bool changed, out bool finished, out string error) { changed = false; if (item.component != null && !item.loaded && LoadContext_IsAllChildrenLoaded(context, item)) { if (!item.component._Load(context, item.textBlock, out var error2)) { error = error2 + $" Component \'{item.component.GetPathFromRoot( true )}\'."; finished = false; return(false); } item.component.providedTypeAllow = true; item.loaded = true; changed = true; } finished = item.loaded; error = ""; return(true); }
static bool LoadContext_ProcessComponentsSerialization(Metadata.LoadContext context, out bool changed, out bool finished, out string error) { changed = false; finished = true; foreach (var item in context.allComponentItemsSerializationOrder) { if (!LoadContext_ProcessComponentSerialization(context, item, out bool changed2, out bool finished2, out error)) { return(false); } if (changed2) { changed = true; } if (!finished2) { finished = false; } } error = ""; return(true); }
//!!!! //static void LoadContext_ProcessNotCompiledComponentItems( Metadata.LoadContext context, out bool changed ) //{ // changed = false; // var items = context.GetNotCompiledItems(); // if( items.Count != 0 ) // { // //creation stage // foreach( var item in items ) // { // //try create component // if( item.component == null && item.parent != null && item.parent.component != null ) // { // //!!!! // xx xx; // //if( componentBlock.Data == "_Dev\\Import3D\\ColladaTest2.dae:Originals\\Materials\\ColladaTestStoneStatic" ) // // Log.Warning( "dfgdfg" ); // xx xx; // bool readyOnly = xx; // Metadata.TypeInfo type = MetadataManager.GetType( item.textBlock.Data ); // xx xx;//загружен тип должен быть // if( type == null ) // { // xx xx; // //!!!! // Log.Fatal( "impl" ); // } // xx xx;//а также проверять на loaded. клонироваться же // item.component = (Component)type.InvokeInstance( null ); // item.component.providedTypeAllow = false; // //!!!!!!так? // item.parent.component.AddComponent( item.component ); // //!!!! // //string error3; // //if( !component._LoadHierarchy( context, componentBlock, out error3 ) ) // //{ // // //!!!! // // Log.Fatal( "impl" ); // // //!!!!!? // // //RemoveComponent // // //component = null; // //} // changed = true; // } // } // //if changed on creation stage then reply creation stage // if( changed ) // return; // //loading properties stage // { // xx xx; // } // xx xx; // xx xx; // //changed = true; // } //} //static void LoadContext_ProcessComponentItemsRecursive( Metadata.LoadContext.ComponentItem componentItem ) //{ //} //public static bool LoadComponentFromTextBlock( Component component, Metadata.LoadContext overrideContextObject, TextBlock block, string loadedFromFile, out string error ) //{ // xx xx; // xx xx; // loadedFromFile = VirtualPathUtils.NormalizePath( loadedFromFile ); // Metadata.LoadContext context = overrideContextObject; // if( context == null ) // context = new Metadata.LoadContext(); // context.virtualFileName = loadedFromFile; // error = ""; // if( block.Children.Count != 1 ) // { // //!!!! // Log.Fatal( "impl" ); // } // TextBlock componentBlock = block.Children[ 0 ]; // if( componentBlock.Name != ".component" ) // { // //!!!! // Log.Fatal( "impl" ); // } // if( string.IsNullOrEmpty( componentBlock.Data ) ) // { // //!!!! // Log.Fatal( "impl" ); // } // //get type // Metadata.TypeInfo type = MetadataManager.GetType( componentBlock.Data ); // if( type == null ) // { // //!!!! // Log.Fatal( "impl" ); // } // xx xx; // //create instance // var component = (Component)type.InvokeInstance( null ); // //create context, init tree structure of components // context.rootComponentItem = new Metadata.LoadContext.ComponentItem(); // context.rootComponentItem.textBlock = componentBlock; // LoadContext_InitComponentItemsRecursive( context, context.rootComponentItem ); // //processing // context.rootComponentItem.component = component; // LoadContext_ProcessComponentItems( context ); // if( !context.rootComponentItem.loaded ) // { // //!!!! // Log.Fatal( "impl" ); // } // return true; //} public static Component LoadComponentFromTextBlock(Metadata.LoadContext overrideContextObject, TextBlock block, string loadedFromFile, Resource.Instance createdByResource, bool?componentSetEnabled, bool componentCreateHierarchyController, out string error) { loadedFromFile = VirtualPathUtility.NormalizePath(loadedFromFile); Metadata.LoadContext context = overrideContextObject; if (context == null) { context = new Metadata.LoadContext(); } context.virtualFileName = loadedFromFile; error = ""; if (block.Children.Count != 1) { error = "Invalid format. block.Children.Count != 1"; return(null); } TextBlock componentBlock = block.Children[0]; if (componentBlock.Name != ".component") { error = "Invalid format. componentBlock.Name != \".component\""; return(null); } if (string.IsNullOrEmpty(componentBlock.Data)) { error = "Invalid format. string.IsNullOrEmpty( componentBlock.Data )"; return(null); } //get type Metadata.TypeInfo type = MetadataManager.GetType(componentBlock.Data); if (type == null) { error = $"Type with name \'{componentBlock.Data}\' is not exists."; return(null); } //create instance var component = (Component)type.InvokeInstance(null); //set ResultObject for resource if (createdByResource != null) { createdByResource.ResultObject = component; } //create hierarchy controller. disabled at creation. if (componentCreateHierarchyController) { ComponentUtility.CreateHierarchyControllerForRootComponent(component, createdByResource, false); //, true ); } //create context, init tree structure of components context.rootComponentItem = new Metadata.LoadContext.ComponentItem(); context.rootComponentItem.textBlock = componentBlock; if (!LoadContext_InitComponentItemsRecursive(context, context.rootComponentItem, out error)) { component?.Dispose(); return(null); } //processing context.rootComponentItem.component = component; if (!LoadContext_ProcessComponentItems(context, out error)) { component?.Dispose(); return(null); } if (!context.rootComponentItem.loaded) { error = "Root component is not loaded."; component?.Dispose(); return(null); } //set Enabled if (componentSetEnabled != null) { component.Enabled = componentSetEnabled.Value; } //enable hierarchy controller if (component.HierarchyController != null) { component.HierarchyController.HierarchyEnabled = true; } return(component); }
static bool LoadContext_ProcessComponentItems(Metadata.LoadContext context, out string error) { //базовое поведение: //сначала вглубь создаем компоненты //далее загружаем свойства //если нельзя создать компоненту, то помечаем "есть дыра" //если есть дыра, то после первого обхода смысла запускать еще раз первый нет //нужно второй запускать (сериализацию) //она для всех не пройдет, т.к. есть дыры //если были изменения, то всё сначала //!!!!сортировать компоненты int noChangesCounter = 0; again :; var unableToGetTypeName = new ESet <string>(); LoadContext_ProcessComponentsCreation(context, out var changed1, out var finished, unableToGetTypeName); if (!LoadContext_ProcessComponentsSerialization(context, out var changed2, out finished, out error)) { return(false); } //check can't load if (!changed1 && !changed2) { noChangesCounter++; if (noChangesCounter == 10) { //error if (unableToGetTypeName.Count != 0) { error = "Types with next names are not exists:"; //error = "Not all types are available. Types with next names are not available:"; foreach (var name in unableToGetTypeName) { error += "\r\n" + name; } } else { error = "Unknown error, no error info."; } context.rootComponentItem?.component?.Dispose(); return(false); } } else { noChangesCounter = 0; } if (!finished) { goto again; } if (!context.rootComponentItem.loaded) { Log.Fatal("never happen"); } error = ""; return(true); }
static void LoadContext_ProcessComponentCreation(Metadata.LoadContext context, Metadata.LoadContext.ComponentItem item, out bool changed, out bool finished, ESet <string> unableToGetTypeName) { changed = false; if (item.component == null && (item.parent != null && item.parent.component != null || item.parent == null)) { Metadata.TypeInfo type = MetadataManager.GetType(item.textBlock.Data); if (type != null) { //!!!!new //use component by base type if (item.parent != null && item.parent.component != null && item.textBlock.AttributeExists("CreatedByBaseType")) { bool createdByBaseType = bool.Parse(item.textBlock.GetAttribute("CreatedByBaseType")); if (createdByBaseType) { int hierarchyIndex = int.Parse(item.textBlock.GetAttribute("CreatedByBaseTypeHierarchyIndex", "0")); int nameIndex = int.Parse(item.textBlock.GetAttribute("CreatedByBaseTypeNameIndex", "0")); Component useComponent = null; foreach (var c in item.parent.component.Components) { if (c.Name == item.name) { //!!!!return true проверять может таки c.GetBaseTypeIndex(out int baseHierarchyIndex, out string baseName, out int baseNameIndex); if (baseHierarchyIndex == hierarchyIndex && baseNameIndex == nameIndex) { useComponent = c; break; } } } if (useComponent != null) { item.component = useComponent; } } } if (item.component == null) { //create item.component = (Component)type.InvokeInstance(null); //remove was deleted components created by base type { //!!!!slowly maybe //reverse order foreach (TextBlock childBlock in item.textBlock.Children.GetReverse()) { if (childBlock.Name == ".componentCreatedByBaseTypeWasDeleted") { int hierarchyIndex = int.Parse(childBlock.GetAttribute("CreatedByBaseTypeHierarchyIndex", "0")); string name = childBlock.GetAttribute("CreatedByBaseTypeName"); int nameIndex = int.Parse(childBlock.GetAttribute("CreatedByBaseTypeNameIndex", "0")); Component foundComponent = null; foreach (var c in item.component.Components) { if (c.Name == name) { //!!!!return true проверять может таки c.GetBaseTypeIndex(out int baseHierarchyIndex, out string baseName, out int baseNameIndex); if (baseHierarchyIndex == hierarchyIndex && baseNameIndex == nameIndex) { foundComponent = c; break; } } } if (foundComponent != null) { foundComponent.RemoveFromParent(false); foundComponent.Dispose(); } } } } item.component.providedTypeAllow = false; //!!!!new item.component.Name = item.name; //!!!!сортировать item.parent.component.AddComponent(item.component); } changed = true; } else { unableToGetTypeName.AddWithCheckAlreadyContained(item.textBlock.Data); } } finished = item.component != null; }