From 9bd3a42744112d6977179fddff16ba0e168ea1c7 Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 29 Aug 2013 23:14:56 -0400 Subject: [PATCH] add extremely basic support for opening files in more modes then reading The "storage path" idea needs to be fleshed out quite a lot more still. Ideally it should be pointing to OS-specific application-writeable locations (e.g. ~/Library/Application Support/$APP_NAME/) --- Blarg.GameFramework.SDL2/IO/SDLFileSystem.cs | 66 +++++++++++-------- .../Blarg.GameFramework.csproj | 1 + Blarg.GameFramework/IO/FileOpenMode.cs | 14 ++++ Blarg.GameFramework/IO/IFileSystem.cs | 3 +- 4 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 Blarg.GameFramework/IO/FileOpenMode.cs diff --git a/Blarg.GameFramework.SDL2/IO/SDLFileSystem.cs b/Blarg.GameFramework.SDL2/IO/SDLFileSystem.cs index 1c608e1..19d296f 100644 --- a/Blarg.GameFramework.SDL2/IO/SDLFileSystem.cs +++ b/Blarg.GameFramework.SDL2/IO/SDLFileSystem.cs @@ -7,51 +7,65 @@ namespace Blarg.GameFramework.IO { public class SDLFileSystem : IFileSystem { - string _assetsPath; + ILogger _logger; + + public string AssetsPath { get; private set; } + public string StoragePath { get; private set; } public SDLFileSystem(ILogger logger) + { + _logger = logger; + + AssetsPath = GetAssetsPath(); + StoragePath = GetStoragePath(); + + if (!Directory.Exists(AssetsPath)) + logger.Warn("SDL_FILESYSTEM", "Attempting to use assets directory {0} which doesn't exist.", AssetsPath); + } + + public Stream Open(string filename, FileOpenMode mode) + { + string realPath = TranslateFilePath(filename); + return new FileStream(realPath, (FileMode)mode); + } + + public string TranslateFilePath(string path) + { + if (path.StartsWith("assets://")) + return Path.Combine(AssetsPath, path.Substring(9)); + else if (path.StartsWith("storage://")) + return Path.Combine(StoragePath, path.Substring(10)); + else + return path; + } + + private string GetAssetsPath() { string envAssetsPath = Environment.GetEnvironmentVariable("ASSETS_DIR"); string workingDirPath = Path.Combine(Environment.CurrentDirectory, "assets"); if (!String.IsNullOrEmpty(envAssetsPath)) { - logger.Info("SDL_FILESYSTEM", "Environment variable ASSETS_DIR value found."); - _assetsPath = envAssetsPath; + _logger.Info("SDL_FILESYSTEM", "Environment variable ASSETS_DIR value found."); + return envAssetsPath; } else if (Directory.Exists(workingDirPath)) { - logger.Info("SDL_FILESYSTEM", "'Assets' found under the current working directory."); - _assetsPath = workingDirPath; + _logger.Info("SDL_FILESYSTEM", "'Assets' found under the current working directory."); + return workingDirPath; } else { // fallback to the default otherwise - logger.Info("SDL_FILESYSTEM", "Assuming 'Assets' directory located next to application executable."); - _assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "assets"); + _logger.Info("SDL_FILESYSTEM", "Assuming 'Assets' directory located next to application executable."); + return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "assets"); } - - if (!Directory.Exists(_assetsPath)) - logger.Warn("SDL_FILESYSTEM", "Attempting to use assets directory {0} which doesn't exist.", _assetsPath); } - public Stream Open(string filename) + private string GetStoragePath() { - string realPath = TranslateFilePath(filename); - return new FileStream(realPath, FileMode.Open); - } - - public string TranslateFilePath(string path) - { - if (path.StartsWith("assets://")) - return Path.Combine(_assetsPath, path.Substring(9)); - else - return path; - } - - public string AssetsPath - { - get { return _assetsPath; } + // TODO: set a proper path + return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); } } } diff --git a/Blarg.GameFramework/Blarg.GameFramework.csproj b/Blarg.GameFramework/Blarg.GameFramework.csproj index 1d45602..6ebc3d8 100644 --- a/Blarg.GameFramework/Blarg.GameFramework.csproj +++ b/Blarg.GameFramework/Blarg.GameFramework.csproj @@ -221,6 +221,7 @@ + diff --git a/Blarg.GameFramework/IO/FileOpenMode.cs b/Blarg.GameFramework/IO/FileOpenMode.cs new file mode 100644 index 0000000..801953c --- /dev/null +++ b/Blarg.GameFramework/IO/FileOpenMode.cs @@ -0,0 +1,14 @@ +using System; + +namespace Blarg.GameFramework.IO +{ + public enum FileOpenMode + { + CreateNew = 1, + Create, + Open, + OpenOrCreate, + Truncate, + Append + } +} diff --git a/Blarg.GameFramework/IO/IFileSystem.cs b/Blarg.GameFramework/IO/IFileSystem.cs index 648afa2..ac2fd7a 100644 --- a/Blarg.GameFramework/IO/IFileSystem.cs +++ b/Blarg.GameFramework/IO/IFileSystem.cs @@ -5,9 +5,10 @@ namespace Blarg.GameFramework.IO { public interface IFileSystem { - Stream Open(string filename); + Stream Open(string filename, FileOpenMode mode = FileOpenMode.Open); string TranslateFilePath(string path); string AssetsPath { get; } + string StoragePath { get; } } }