Пример #1
0
        public ResourceBase CreateResource(Type t, string resourceName, byte[] initData = null, bool canStore = false)
        {
            if (!resCreatorMaps.ContainsKey(t))
            {
                Log.WriteError("Cannot find creator resource of type " + t.Name + ", no creator delegate exists.");
                return(null);
            }
            if (headers.ContainsKey(resourceName))
            {
                Log.WriteError("Cannot create resource, name is already in use: " + resourceName);
                return(null);
            }

            ResourceHeader createdHeader = new ResourceHeader(resourceName, string.Empty, 0, 0, false, canStore, t.Name, new Dictionary <string, string>());

            headers.Add(createdHeader.resourceName, createdHeader);

            ResourceBase resource = resCreatorMaps[t].Invoke(this);

            if (initData == null)
            {
                initData = new byte[0];
            }
            resource.Load(initData);
            resources.Add(resource.rid, resource);
            createdHeader.loaded   = true;
            createdHeader.loadedId = resource.rid;

            return(resource);
        }
Пример #2
0
        public T Get <T>() where T : ResourceBase
        {
            ResourceBase rbase = Get();

            if (rbase is T)
            {
                return((T)rbase);
            }

            Log.WriteError("Could not convert resource to type: " + typeof(T));
            return(default(T));
        }
Пример #3
0
        public ResourceRef GetRef(string resourceName)
        {
            ResourceBase resource = GetResource(resourceName);

            if (resource != null)
            {
                return(new ResourceRef(resource.rid, this));
            }
            else
            {
                return(null);
            }
        }
Пример #4
0
        private void LoadResource(string resourceName)
        {
            if (!headers.ContainsKey(resourceName))
            {
                Log.WriteError("Cannot get resource with name=" + resourceName + ", does not exist.");
            }

            ResourceHeader header = headers[resourceName];

            if (!File.Exists(header.filename))
            {
                Log.WriteError("Cannot get resource with name=" + resourceName + ", specified file does not exist.");
            }

            try
            {
                byte[] rawData;
                using (FileStream fstream = File.OpenRead(header.filename))
                    using (BinaryReader reader = new BinaryReader(fstream))
                    {
                        if (header.fileStart > 0)
                        {
                            reader.BaseStream.Seek((long)header.fileStart, SeekOrigin.Begin);
                        }

                        int readLen = header.fileLength == 0 ? (int)reader.BaseStream.Length - (int)reader.BaseStream.Position : (int)header.fileLength;
                        rawData = reader.ReadBytes(readLen);
                    }

                if (!resLoaderMaps.ContainsKey(header.loaderExt))
                {
                    throw new Exception("Loader not found for extension: " + header.loaderExt + ". Unable to load resource: " + resourceName);
                }

                Type         resourceType = resLoaderMaps[header.loaderExt];
                ResourceBase resource     = resCreatorMaps[resourceType].Invoke(this);
                resource.Load(rawData);
                resources.Add(resource.rid, resource);
                ridToHeaderMap.Add(resource.rid, header.resourceName);

                header.loaded   = true;
                header.loadedId = resource.rid;
                Log.WriteLine("Loaded resource: " + resourceName);
            }
            catch (Exception e)
            {
                Log.WriteError("Error occured during resource load: " + e.ToString());
            }
        }
Пример #5
0
        }                                        //TODO: have this run on a background thread, and return a task-like object that can querry the loading status

        public void UnloadResource(string resourceName, bool force = false)
        {
            if (!headers.ContainsKey(resourceName))
            {
                Log.WriteError("Cannot unload resource, no resource exists with name=" + resourceName);
                return;
            }
            ResourceHeader header = headers[resourceName];

            if (!header.loaded)
            {
                Log.WriteError("Cannot unload resource, it's currently not loaded. Name=" + resourceName);
                return;
            }

            if (!header.canUnload)
            {
                if (!force)
                {
                    Log.WriteError("Resource marked as never-unload, cannot unload. Name=" + resourceName);
                    return;
                }
                else
                {
                    Log.WriteLine("Resource marked as never-unload, but force was specified. This may lead to cascading errors. Name=" + resourceName);
                }
            }

            ResourceBase res = GetResource(header.loadedId);

            resources.Remove(res.rid);
            ridGenerator.FreeId(res.rid);
            res.Unload();
            res = null;

            header.loadedId = 0;
            header.loaded   = false;
            Log.WriteLine("Unloaded resource: " + header.resourceName);
        }