コード例 #1
0
            public void RegisterAsset(string type, string subtype, string[] fileExtensions, HandlerFunc handler)
            {
                if (AssetHandlers.ContainsKey(type) && AssetHandlers[type].ContainsKey(subtype))
                {
                    throw new Exception($"Registering duplicate handler for {type}/{subtype}");
                }

                if (!AssetHandlers.ContainsKey(type))
                {
                    AssetHandlers[type] = new Dictionary <string, HandlerFunc>();
                }

                foreach (var fileExtension in fileExtensions)
                {
                    if (FileExtensionAssetTypes.ContainsKey(fileExtension))
                    {
                        throw new Exception($"Registering duplicate handler for asset type with file extension {fileExtension}");
                    }
                    FileExtensionAssetTypes[fileExtension] = type + "/" + subtype;
                }

                AssetHandlers[type][subtype] = handler;
            }
コード例 #2
0
            public Asset?Import(string type, string subtype, bool createOnFail, bool lazyImport, Stream?sourceStream, string targetPath,
                                string name, Permissions perm, string sourcePath, params object[] args)
            {
                if (!AssetHandlers.ContainsKey(type) || !AssetHandlers[type].ContainsKey(subtype))
                {
                    throw new Exception($"Importing asset with unregistered type {type}/{subtype}");
                }

                string path = FileSystem.BasePath + sourcePath;

                if (sourceStream == null && sourcePath != "") // We need to read it
                {
                    if (!File.Exists(path))
                    {
                        if (createOnFail)
                        {
                            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));
                            sourceStream = File.Create(path);
                        }
                    }
                    else
                    {
                        sourceStream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
                    }

                    if (sourceStream == null)
                    {
                        throw new SynthesisException("Source stream for import is empty");
                    }
                }

                Asset?newAsset = AssetHandlers[type][subtype](name, perm, path, args);

                if (newAsset == null)
                {
                    return(null);
                }

                EventBus.EventBus.Push(AssetImportEvent.Tag, new AssetImportEvent(name, targetPath, type + "/" + subtype));

                if (lazyImport)
                {
                    LazyAsset lazyAsset = new LazyAsset(newAsset, sourceStream, targetPath);
                    return((Asset?)FileSystem.AddEntry(targetPath, lazyAsset.Load(new byte[0])));
                }

                byte[] data = new byte[0];
                if (sourceStream != null)
                {
                    int streamLength = (int)sourceStream.Length;

                    data = new byte[streamLength];
                    sourceStream.Read(data, 0, streamLength);

                    sourceStream.Close();
                }

                // TODO make it so we don't have to allocate twice the size of the asset every
                // time we import it (i.e. a 500 KB asset will result in 1000 KB of allocation)
                return((Asset?)FileSystem.AddEntry(targetPath, newAsset.Load(data)));
            }