///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 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); }
public static Resource.Instance _Load(Resource.InstanceType instanceType, string name, bool wait, bool componentCreateHierarchyController, bool?componentSetEnabled, out string error) { error = ""; //!!!!!threading if (string.IsNullOrEmpty(name)) { error = ""; return(null); } //!!!!!на неналичие файла проверять. но не в локе? Resource res; Resource.Instance ins; bool insCreated = false; lock ( lockObject ) { //get or create resource res = GetByName(name); if (res == null) { //!!!!override loader var extension = Path.GetExtension(name); var resourceType = GetTypeByFileExtension(extension); if (resourceType == null) { //!!!!всё же как-то можно грузить? error = $"Loading of file type \'{extension}\' is not supported."; //error = ""; return(null); } 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; } } //begin loading if (insCreated) { if (wait) { LoadTaskFunction(ins); } else { Task task = new Task(LoadTaskFunction, ins); task.Start(); } } //!!!!new //!!!!только InstanceType.Resource? //if wait == true and inside call ResourceManager._Load recursively with wait == true, then return null. if (wait && instanceType == Resource.InstanceType.Resource && !insCreated && ins.Status == Resource.Instance.StatusEnum.CreationProcess) { //так норм импорт работает, хотя это не совсем логично. return(ins); //return null; //xx; //ESet<Resource.Instance> waitLoadingResources = new ESet<Resource.Instance>(); //if( waitLoadingResources.Contains( ins ) ) // return null; //waitLoadingResources.Add( ins ); } //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); }