public IEntry Load() { base.DeleteInner(); byte[] data = new byte[_sourceStream.Length]; _sourceStream.Read(data, 0, (int)_sourceStream.Length); _sourceStream.Close(); var entry = FileSystem.AddEntry(_targetPath, inner.Load(data)); if (entry != null) { return(entry); } throw new System.Exception("Failed to load lazy asset"); }
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))); }