public static String ResolveContentPath(String _Asset, params string[] AlternateExtensions) { string Asset = FileUtils.NormalizePath(_Asset); var extensionList = new List <String>(AlternateExtensions); if (extensionList.Count != 0) { extensionList.Add(".xnb"); } else { extensionList.Add(""); } foreach (var mod in DirectorySearchList) { foreach (var extension in extensionList) { if (File.Exists(mod.Directory + Path.DirectorySeparatorChar + Asset + extension)) { return(mod.Directory + Path.DirectorySeparatorChar + Asset + extension); } } } return("Content" + Path.DirectorySeparatorChar + Asset); }
public static Texture2D LoadUnbuiltTextureFromAbsolutePath(string _file) { try { string file = FileUtils.NormalizePath(_file); using (var stream = new FileStream(file, FileMode.Open)) { if (!stream.CanRead) { Console.Out.WriteLine("Failed to read {0}, stream cannot be read.", file); return(null); } try { return(Texture2D.FromStream(GameState.Game.GraphicsDevice, stream)); } catch (Exception exception) { Console.Out.Write("Failed to load texture {0}: {1}", file, exception.ToString()); return(null); } } } catch (Exception exception) { Console.Out.Write("Failed to load texture {0}", exception.ToString()); return(null); } }
/// <summary> /// Enumerates the relative paths of all mods (including base content) that include the content. /// </summary> /// <param name="_AssetPath"></param> /// <returns></returns> public static IEnumerable <String> EnumerateMatchingPaths(String _AssetPath) { string AssetPath = FileUtils.NormalizePath(_AssetPath); foreach (var mod in DirectorySearchList) { var resolvedAssetPath = mod.Directory + Path.DirectorySeparatorChar + AssetPath; if (File.Exists(resolvedAssetPath)) { yield return(resolvedAssetPath); } } }
public static RawPrimitive GetContentMesh(string _asset) { if (String.IsNullOrEmpty(_asset)) { DwarfGame.LogSentryBreadcrumb("AssetManager", "Attempt to load mesh asset from empty string.", SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } string asset = FileUtils.NormalizePath(_asset); if (asset == null) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was null.", _asset), SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } try { var filename = ResolveContentPath(asset, ".obj"); if (Path.GetExtension(filename) == ".xnb") { //var toReturn = Content.Load<ModelMesh>(filename.Substring(0, filename.Length - 4)); //TextureCache[asset] = toReturn; //return toReturn; // Todo: Convert model mesh to raw prim? return(null); } else if (Path.GetExtension(filename) == ".obj") { return(ObjLoader.LoadObject(File.ReadAllLines(FileUtils.NormalizePath(filename)))); } else { return(null); } } catch (Exception exception) { Console.Error.WriteLine(exception.ToString()); try { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Failed to load asset {0} : {1}", asset, exception.ToString()), SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } catch (Exception innerException) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Everything is broken! {0}", innerException.ToString()), SharpRaven.Data.BreadcrumbLevel.Error); return(null); } } }
public static Texture2D GetContentTexture(string _asset) { string asset = FileUtils.NormalizePath(_asset); if (asset == null) { var r = Content.Load <Texture2D>(ContentPaths.Error); return(r); } if (TextureCache.ContainsKey(asset)) { var existing = TextureCache[asset]; if (!existing.IsDisposed && !existing.GraphicsDevice.IsDisposed) { return(existing); } } try { var filename = ResolveContentPath(asset, ".png"); if (Path.GetExtension(filename) == ".xnb") { var toReturn = Content.Load <Texture2D>(filename.Substring(0, filename.Length - 4)); TextureCache[asset] = toReturn; return(toReturn); } else { var toReturn = LoadUnbuiltTextureFromAbsolutePath(filename); TextureCache[asset] = toReturn; return(toReturn); } } catch (Exception exception) { Console.Error.WriteLine(exception.ToString()); try { var r = Content.Load <Texture2D>(ContentPaths.Error); TextureCache[asset] = r; return(r); } catch (Exception innerException) { return(null); } } }
public static IEnumerable <String> EnumerateAllFiles(String BasePath) { string basePath = FileUtils.NormalizePath(BasePath); foreach (var mod in DirectorySearchList) { var directoryPath = mod.Directory + Path.DirectorySeparatorChar + basePath; if (!Directory.Exists(directoryPath)) { continue; } foreach (var file in EnumerateDirectory(directoryPath)) { yield return(file); } } }
public static bool DoesTextureExist(string _asset) { string asset = FileUtils.NormalizePath(_asset); if (asset == null) { return(false); } if (TextureCache.ContainsKey(asset)) { return(true); } try { var filename = ResolveContentPath(asset, ".png"); return(File.Exists(filename)); } catch (Exception exception) { return(false); } }
public static Texture2D GetContentTexture(string _asset) { if (String.IsNullOrEmpty(_asset)) { DwarfGame.LogSentryBreadcrumb("AssetManager", "Attempt to load texture asset from empty string.", SharpRaven.Data.BreadcrumbLevel.Warning); return(Content.Load <Texture2D>("Content/newgui/error")); } #if DEBUG if (!DwarfGame.IsMainThread) { // This completely breaks loading... //throw new InvalidOperationException("Can't load an asset outside of the main thread."); } #endif string asset = FileUtils.NormalizePath(_asset); if (asset == null) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was null.", _asset), SharpRaven.Data.BreadcrumbLevel.Warning); var r = Content.Load <Texture2D>(ContentPaths.Error); return(r); } if (TextureCache.ContainsKey(asset)) { var existing = TextureCache[asset]; if (existing != null && !existing.IsDisposed && existing.GraphicsDevice != null && !existing.GraphicsDevice.IsDisposed) { return(existing); } else { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was invalid.", asset), SharpRaven.Data.BreadcrumbLevel.Warning); TextureCache.Remove(asset); } } try { var filename = ResolveContentPath(asset, ".png", ".bmp"); if (Path.GetExtension(filename) == ".xnb") { var toReturn = Content.Load <Texture2D>(filename.Substring(0, filename.Length - 4)); TextureCache[asset] = toReturn; return(toReturn); } else { var toReturn = LoadUnbuiltTextureFromAbsolutePath(filename); TextureCache[asset] = toReturn; return(toReturn); } } catch (Exception exception) { Console.Error.WriteLine(exception.ToString()); try { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Failed to load asset {0} : {1}", asset, exception.ToString()), SharpRaven.Data.BreadcrumbLevel.Warning); var r = Content.Load <Texture2D>(ContentPaths.Error); TextureCache[asset] = r; return(r); } catch (Exception innerException) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Everything is broken! {0}", innerException.ToString()), SharpRaven.Data.BreadcrumbLevel.Error); return(null); } } }