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/)
This commit is contained in:
parent
da23265904
commit
9bd3a42744
|
@ -7,51 +7,65 @@ namespace Blarg.GameFramework.IO
|
||||||
{
|
{
|
||||||
public class SDLFileSystem : IFileSystem
|
public class SDLFileSystem : IFileSystem
|
||||||
{
|
{
|
||||||
string _assetsPath;
|
ILogger _logger;
|
||||||
|
|
||||||
|
public string AssetsPath { get; private set; }
|
||||||
|
public string StoragePath { get; private set; }
|
||||||
|
|
||||||
public SDLFileSystem(ILogger logger)
|
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 envAssetsPath = Environment.GetEnvironmentVariable("ASSETS_DIR");
|
||||||
string workingDirPath = Path.Combine(Environment.CurrentDirectory, "assets");
|
string workingDirPath = Path.Combine(Environment.CurrentDirectory, "assets");
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(envAssetsPath))
|
if (!String.IsNullOrEmpty(envAssetsPath))
|
||||||
{
|
{
|
||||||
logger.Info("SDL_FILESYSTEM", "Environment variable ASSETS_DIR value found.");
|
_logger.Info("SDL_FILESYSTEM", "Environment variable ASSETS_DIR value found.");
|
||||||
_assetsPath = envAssetsPath;
|
return envAssetsPath;
|
||||||
}
|
}
|
||||||
else if (Directory.Exists(workingDirPath))
|
else if (Directory.Exists(workingDirPath))
|
||||||
{
|
{
|
||||||
logger.Info("SDL_FILESYSTEM", "'Assets' found under the current working directory.");
|
_logger.Info("SDL_FILESYSTEM", "'Assets' found under the current working directory.");
|
||||||
_assetsPath = workingDirPath;
|
return workingDirPath;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// fallback to the default otherwise
|
// fallback to the default otherwise
|
||||||
logger.Info("SDL_FILESYSTEM", "Assuming 'Assets' directory located next to application executable.");
|
_logger.Info("SDL_FILESYSTEM", "Assuming 'Assets' directory located next to application executable.");
|
||||||
_assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "assets");
|
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);
|
// TODO: set a proper path
|
||||||
return new FileStream(realPath, FileMode.Open);
|
return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
||||||
}
|
|
||||||
|
|
||||||
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; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,6 +221,7 @@
|
||||||
<Compile Include="TileMap\TileDataSerializer.cs" />
|
<Compile Include="TileMap\TileDataSerializer.cs" />
|
||||||
<Compile Include="TileMap\Json\JsonTileMap.cs" />
|
<Compile Include="TileMap\Json\JsonTileMap.cs" />
|
||||||
<Compile Include="TileMap\Json\TileMapLoader.cs" />
|
<Compile Include="TileMap\Json\TileMapLoader.cs" />
|
||||||
|
<Compile Include="IO\FileOpenMode.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
14
Blarg.GameFramework/IO/FileOpenMode.cs
Normal file
14
Blarg.GameFramework/IO/FileOpenMode.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Blarg.GameFramework.IO
|
||||||
|
{
|
||||||
|
public enum FileOpenMode
|
||||||
|
{
|
||||||
|
CreateNew = 1,
|
||||||
|
Create,
|
||||||
|
Open,
|
||||||
|
OpenOrCreate,
|
||||||
|
Truncate,
|
||||||
|
Append
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,9 +5,10 @@ namespace Blarg.GameFramework.IO
|
||||||
{
|
{
|
||||||
public interface IFileSystem
|
public interface IFileSystem
|
||||||
{
|
{
|
||||||
Stream Open(string filename);
|
Stream Open(string filename, FileOpenMode mode = FileOpenMode.Open);
|
||||||
string TranslateFilePath(string path);
|
string TranslateFilePath(string path);
|
||||||
|
|
||||||
string AssetsPath { get; }
|
string AssetsPath { get; }
|
||||||
|
string StoragePath { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue