/// <summary> /// Checks the existence of the identified asset as an async method. /// </summary> /// <param name="id">The asset identifier.</param> /// <returns>Implementors should return true if a stream can be created.</returns> protected override async Task <bool> CheckExistsAsync(string id) { return(await Task.Factory.StartNew(() => { if (id == null) { throw new ArgumentNullException(nameof(id)); } // If it is an absolute path (e.g. C:\SomeDir\AnAssetFile.ext) directly check its presence if (Path.IsPathRooted(id)) { return File.Exists(id); } // Path seems relative. First see if the file exists at the current working directory if (File.Exists(id)) { return true; } foreach (var baseDir in _baseDirs) { string path = Path.Combine(baseDir, id); if (File.Exists(path)) { return true; } } return false; })); }
/// <summary> /// Create an async stream for the asset identified by id. /// </summary> /// <param name="id">The asset identifier.</param> /// <returns>Implementors should return null if the asset cannot be retrieved. Otherwise returns a file stream to the asset.</returns> protected override async Task <Stream> GetStreamAsync(string id) { return(await Task <Stream> .Factory.StartNew(() => { if (id == null) { throw new ArgumentNullException(nameof(id)); } // If it is an absolute path (e.g. C:\SomeDir\AnAssetFile.ext) open it directly if (Path.IsPathRooted(id)) { return new FileStream(id, FileMode.Open); } // Path seems relative. First see if the file exists at the current working directory if (File.Exists(id)) { return new FileStream(id, FileMode.Open); } // At last, look at the specified base directories foreach (var baseDir in _baseDirs) { string path = Path.Combine(baseDir, id); if (File.Exists(path)) { return new FileStream(path, FileMode.Open); } } return null; })); }
/// <summary> /// Creates a stream for the asset identified by id using <see cref="FileStream"/> /// </summary> /// <param name="id">The asset identifier.</param> /// <returns> /// A valid stream for reading if the asset ca be retrieved. null otherwise. /// </returns> /// <exception cref="System.ArgumentNullException"></exception> protected override Stream GetStream(string id) { if (id == null) { throw new ArgumentNullException(nameof(id)); } string path = Path.Combine(_baseDir, id); return(new FileStream(path, FileMode.Open)); }
/// <summary> /// Checks the existance of the identified asset using <see cref="File.Exists"/> /// </summary> /// <param name="id">The asset identifier.</param> /// <returns> /// true if a stream can be created. /// </returns> /// <exception cref="System.ArgumentNullException"></exception> protected override bool CheckExists(string id) { if (id == null) { throw new ArgumentNullException(nameof(id)); } string path = Path.Combine(_baseDir, id); if (File.Exists(path)) { return(true); } return(false); }
public static void Main(string[] args) { // Inject Fusee.Engine.Base InjectMe dependencies IO.IOImp = new Fusee.Base.Imp.Desktop.IOImp(); Type tApp = null; string modelFile = null; List <string> assetDirs = new List <string>(); TryAddDir(assetDirs, "Assets"); string ExeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string Cwd = Directory.GetCurrentDirectory(); if (Cwd != ExeDir) { TryAddDir(assetDirs, Path.Combine(ExeDir, "Assets")); } if (args.Length >= 1) { if (File.Exists(args[0])) { TryAddDir(assetDirs, Path.GetDirectoryName(args[0])); if (Path.GetExtension(args[0]).ToLower().Contains("fus")) { // A .fus file - open it. modelFile = Path.GetFileName(args[0]); } else { // See if the passed argument is an entire Fusee App DLL try { Assembly asm = Assembly.LoadFrom(args[0]); tApp = asm.GetTypes().FirstOrDefault(t => typeof(RenderCanvas).IsAssignableFrom(t)); TryAddDir(assetDirs, Path.Combine(Path.GetDirectoryName(args[0]), "Assets")); } catch (Exception e) { Diagnostics.Log(e.ToString()); } } } else { Diagnostics.Log($"Cannot open {args[0]}."); } } if (tApp == null) { // See if we are in "Deployed mode". That is: A Fusee.App.dll is lying next to us. try { Assembly asm = Assembly.LoadFrom(Path.Combine(ExeDir, "Fusee.App.dll")); tApp = asm.GetTypes().FirstOrDefault(t => typeof(RenderCanvas).IsAssignableFrom(t)); } catch (Exception e) { Diagnostics.Log(e.ToString()); } // No App was specified and we're not in Deplyed mode. Simply use the default App (== Viewer) if (tApp == null) { tApp = typeof(Fusee.Engine.Player.Core.Player); } } var fap = new Fusee.Base.Imp.Desktop.FileAssetProvider(assetDirs); fap.RegisterTypeHandler( new AssetHandler { ReturnedType = typeof(Font), Decoder = delegate(string id, object storage) { if (!Path.GetExtension(id).ToLower().Contains("ttf")) { return(null); } return(new Font { _fontImp = new FontImp((Stream)storage) }); }, Checker = id => Path.GetExtension(id).ToLower().Contains("ttf") }); fap.RegisterTypeHandler( new AssetHandler { ReturnedType = typeof(SceneContainer), Decoder = delegate(string id, object storage) { if (!Path.GetExtension(id).ToLower().Contains("fus")) { return(null); } var ser = new Serializer(); var scene = ser.Deserialize((Stream)storage, null, typeof(SceneContainer)); var container = scene as SceneContainer; return(new ConvertSceneGraph().Convert(container)); }, Checker = id => Path.GetExtension(id).ToLower().Contains("fus") }); AssetStorage.RegisterProvider(fap); // Dynamically instantiate the app because it might live in some external (.NET core) DLL. var ctor = tApp.GetConstructor(Type.EmptyTypes); if (ctor == null) { Diagnostics.Log($"Cannot instantiate FUSEE App. {tApp.Name} contains no default constructor"); } else { // invoke the first public constructor with no parameters. RenderCanvas app = (RenderCanvas)ctor.Invoke(new object[] { }); if (!string.IsNullOrEmpty(modelFile) && app is Fusee.Engine.Player.Core.Player) { ((Fusee.Engine.Player.Core.Player)app).ModelFile = modelFile; } // Inject Fusee.Engine InjectMe dependencies (hard coded) System.Drawing.Icon appIcon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); app.CanvasImplementor = new Fusee.Engine.Imp.Graphics.Desktop.RenderCanvasImp(appIcon); app.ContextImplementor = new Fusee.Engine.Imp.Graphics.Desktop.RenderContextImp(app.CanvasImplementor); Input.AddDriverImp(new Fusee.Engine.Imp.Graphics.Desktop.RenderCanvasInputDriverImp(app.CanvasImplementor)); Input.AddDriverImp(new Fusee.Engine.Imp.Graphics.Desktop.WindowsSpaceMouseDriverImp(app.CanvasImplementor)); Input.AddDriverImp(new Fusee.Engine.Imp.Graphics.Desktop.WindowsTouchInputDriverImp(app.CanvasImplementor)); // app.InputImplementor = new Fusee.Engine.Imp.Graphics.Desktop.InputImp(app.CanvasImplementor); // app.AudioImplementor = new Fusee.Engine.Imp.Sound.Desktop.AudioImp(); // app.NetworkImplementor = new Fusee.Engine.Imp.Network.Desktop.NetworkImp(); // app.InputDriverImplementor = new Fusee.Engine.Imp.Input.Desktop.InputDriverImp(); // app.VideoManagerImplementor = ImpFactory.CreateIVideoManagerImp(); // Start the app app.Run(); } }