예제 #1
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public static Resource.Instance CreateResource(ResourceType resourceType, Resource.InstanceType instanceType, string name, bool wait,
                                                       Metadata.TypeInfo createResultObjectWithType, bool componentCreateHierarchyController, bool?componentSetEnabled, out string error)
        {
            //!!!!
            if (!wait)
            {
                Log.Fatal("ResourceManager: CreateResource: wait == false. Background loading is not implemented.");
            }

            error = "";

            //!!!!!threading

            if (string.IsNullOrEmpty(name))
            {
                Log.Fatal("ResourceManager: CreateResource: The name can't be empty.");
                return(null);
            }

            Resource res;

            Resource.Instance ins;
            bool insCreated = false;

            lock ( lockObject )
            {
                //get or create resource
                res = GetByName(name);
                if (res == null)
                {
                    //!!!!override loader

                    ConstructorInfo constructor = resourceType.resourceClass.GetConstructor(new Type[0]);
                    res = (Resource)constructor.Invoke(new object[0]);
                    res.resourceType = resourceType;
                    res.name         = name;
                    res.loadFromFile = true;

                    //add to the list
                    resources.Add(GetKey(name), res);
                }

                //create instance
                if (instanceType == Resource.InstanceType.Resource)
                {
                    //as Resource
                    if (res.PrimaryInstance == null)
                    {
                        ins = res.CreateInstanceClassObject(instanceType, componentCreateHierarchyController, componentSetEnabled);
                        res.PrimaryInstance = ins;
                        insCreated          = true;
                    }
                    else
                    {
                        ins = res.PrimaryInstance;
                    }
                }
                else
                {
                    //as Separate instance
                    ins        = res.CreateInstanceClassObject(instanceType, componentCreateHierarchyController, componentSetEnabled);
                    insCreated = true;
                }
            }

            if (insCreated)
            {
                if (createResultObjectWithType != null)
                {
                    object obj = createResultObjectWithType.InvokeInstance(null);

                    var component = obj as Component;
                    if (component != null)
                    {
                        if (componentSetEnabled != null)
                        {
                            component.Enabled = componentSetEnabled.Value;
                        }
                        if (componentCreateHierarchyController)
                        {
                            ComponentUtility.CreateHierarchyControllerForRootComponent(component, ins, true);                              //, true );
                        }
                    }

                    ins.ResultObject = obj;
                }

                //resource is ready
                ins.Status = Resource.Instance.StatusEnum.Ready;
            }
            ////_Load function
            //if( insCreated )
            //{
            //	//load

            //	if( wait )
            //	{
            //		LoadTaskFunction( ins );
            //	}
            //	else
            //	{
            //		Task task = new Task( LoadTaskFunction, ins );
            //		task.Start();
            //	}
            //}

            //wait
            if (wait)
            {
                //wait end of creation
                while (ins.Status == Resource.Instance.StatusEnum.CreationProcess)
                {
                    //!!!!slow? maybe we can increase priority for this instance or something like this?

                    if (VirtualFileSystem.MainThread == Thread.CurrentThread)
                    {
                        //process main thread tasks
                        EngineThreading.ExecuteQueuedActionsFromMainThread();
                    }

                    //!!!!?
                    Thread.Sleep(0);
                }
            }

            //!!!!так?
            if (wait && ins.Status == Resource.Instance.StatusEnum.Error)
            {
                error = ins.StatusError;
                ins.Dispose();
                return(null);
            }

            return(ins);
        }
예제 #2
0
        //!!!!
        //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);
        }