2013-08-15 18:41:41 -04:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
using SDL2;
|
|
|
|
|
|
|
|
namespace Blarg.GameFramework.IO
|
|
|
|
{
|
|
|
|
public class SDLFileSystem : IFileSystem
|
|
|
|
{
|
2013-08-29 23:14:56 -04:00
|
|
|
ILogger _logger;
|
|
|
|
|
|
|
|
public string AssetsPath { get; private set; }
|
|
|
|
public string StoragePath { get; private set; }
|
2013-08-15 18:41:41 -04:00
|
|
|
|
2013-08-25 13:21:51 -04:00
|
|
|
public SDLFileSystem(ILogger logger)
|
2013-08-15 18:41:41 -04:00
|
|
|
{
|
2013-08-29 23:14:56 -04:00
|
|
|
_logger = logger;
|
2013-08-25 13:21:51 -04:00
|
|
|
|
2013-08-29 23:14:56 -04:00
|
|
|
AssetsPath = GetAssetsPath();
|
|
|
|
StoragePath = GetStoragePath();
|
2013-08-25 13:21:51 -04:00
|
|
|
|
2013-08-29 23:14:56 -04:00
|
|
|
if (!Directory.Exists(AssetsPath))
|
|
|
|
logger.Warn("SDL_FILESYSTEM", "Attempting to use assets directory {0} which doesn't exist.", AssetsPath);
|
2013-08-15 18:41:41 -04:00
|
|
|
}
|
|
|
|
|
2013-08-29 23:14:56 -04:00
|
|
|
public Stream Open(string filename, FileOpenMode mode)
|
2013-08-15 18:41:41 -04:00
|
|
|
{
|
|
|
|
string realPath = TranslateFilePath(filename);
|
2013-08-29 23:14:56 -04:00
|
|
|
return new FileStream(realPath, (FileMode)mode);
|
2013-08-15 18:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public string TranslateFilePath(string path)
|
|
|
|
{
|
|
|
|
if (path.StartsWith("assets://"))
|
2013-08-29 23:14:56 -04:00
|
|
|
return Path.Combine(AssetsPath, path.Substring(9));
|
|
|
|
else if (path.StartsWith("storage://"))
|
|
|
|
return Path.Combine(StoragePath, path.Substring(10));
|
2013-08-15 18:41:41 -04:00
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-08-29 23:14:56 -04:00
|
|
|
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.");
|
|
|
|
return envAssetsPath;
|
|
|
|
}
|
|
|
|
else if (Directory.Exists(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.");
|
|
|
|
return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "assets");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetStoragePath()
|
2013-08-15 18:41:41 -04:00
|
|
|
{
|
2013-08-29 23:14:56 -04:00
|
|
|
// TODO: set a proper path
|
|
|
|
return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
|
2013-08-15 18:41:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|