Exemplo n.º 1
0
        /////////////////////////////////////////

        public static Component_Mesh GetMesh(RectangleF uv)
        {
            //try to get from the cache
            if (items.TryGetValue(uv, out var item))
            {
                item.EngineTime = Time.Current;

                items.Remove(uv);
                items.Add(uv, item);

                return(item.Mesh);
            }

            //remove old item from the cache
            if (items.Count >= MaxCacheSize)
            {
                foreach (var pair in items)
                {
                    if (pair.Value.EngineTime != Time.Current)
                    {
                        pair.Value.Dispose();
                        items.Remove(pair.Key);
                        break;
                    }
                }
            }

            //create item and add to the cache
            item = new Item(uv);
            items.Add(uv, item);

            return(item.Mesh);
        }
Exemplo n.º 2
0
        public static ResourceType RegisterType(string name, IEnumerable <string> fileExtensions, Type resourceClass)
        {
            if (GetTypeByName(name) != null)
            {
                Log.Fatal($"ResourceManager: RegisterResourceType: The resource with name {name} is already registered.");
            }

            ResourceType type = new ResourceType();

            type.name          = name;
            type.resourceClass = resourceClass;
            //type.loadResourceFunction = loadFunction;
            types.Add(name, type);
            foreach (var v in fileExtensions)
            {
                type.AddExtension(v);
            }

            return(type);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static RegisteredAssemblyItem RegisterAssembly(Assembly assembly, string registerTypeNamesWithIncludedAssemblyName)
        {
            lock ( registeredAssemblies )
            {
                RegisteredAssemblyItem item2 = GetRegisteredAssembly(assembly);
                if (item2 != null)
                {
                    return(item2);
                }

                if (disableAssemblyRegistration == null)
                {
                    ParseDisableAssemblyNamespaceRegistration();
                }

                RegisteredAssemblyItem item = new RegisteredAssemblyItem();
                item.assembly = assembly;
                item.registerTypeNamesWithIncludedAssemblyName = registerTypeNamesWithIncludedAssemblyName;
                registeredAssemblies.Add(assembly, item);

                string name = assembly.GetName().Name;
                if (name.EndsWith(".STUB"))
                {
                    name = name.Replace(".STUB", "");
                }

                bool enabled = !disableAssemblyRegistration.Contains(name);
                Log.InvisibleInfo("Register assembly: {0}{1}", (enabled ? "" : "[DISABLED] "), assembly.FullName);
                //Log.InvisibleInfo( "AssemblyUtils: RegisterAssembly: {0} {1}", ( enabled ? "" : "[DISABLED]" ), assembly.FullName );
                //Log.InvisibleInfo( "AssemblyUtils: RegisterAssembly: [{0}] {1}", ( enabled ? "ENABLED" : "DISABLED" ), assembly.FullName );

                if (enabled)
                {
                    StartupTiming.CounterStart("Register assembly \'" + name + "\'");
                    try
                    {
                        var types = assembly.GetExportedTypes();

                        //optimization
                        var isBaseNetAssembly = assembly == typeof(string).Assembly || assembly == typeof(Uri).Assembly;

                        MetadataManager.RegisterTypesOfAssembly(assembly, types);

                        if (!isBaseNetAssembly)
                        {
                            //Android: 8 seconds.
                            MetadataManager.RegisterAutoConvertItemsForAssembly(assembly, types);

                            MetadataManager.RegisterExtensions(assembly, types);

                            //invoke AssemblyRegistration classes
                            foreach (Type type in types)
                            {
                                if (!type.IsAbstract && typeof(AssemblyRegistration).IsAssignableFrom(type))
                                {
                                    ConstructorInfo constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                                                                                      null, new Type[0] {
                                    }, null);
                                    AssemblyRegistration ins = (AssemblyRegistration)constructor.Invoke(new object[0]);

                                    item.registrationObjects.Add(ins);

                                    ins.OnRegister();
                                }
                            }
                        }

                        if (EngineApp.ApplicationType == EngineApp.ApplicationTypeEnum.Editor)
                        {
                            EditorUtility.RegisterEditorExtensions(assembly);
                            ResourcesWindowItems.RegisterAssembly(assembly);
                        }

                        RegisterAssemblyEvent?.Invoke(assembly);
                    }
                    catch (Exception e)
                    {
                        Log.Error("Unable to register assembly \'" + name + "\'. " + e.Message);
                    }
                    finally
                    {
                        StartupTiming.CounterEnd("Register assembly \'" + name + "\'");
                    }
                }

                return(item);
            }
        }