Skip to content

Commit

Permalink
Replace FileSystem singleton with free functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 committed Jul 25, 2013
1 parent 1961ec1 commit a0d2352
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 58 deletions.
5 changes: 3 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ Refactoring
* Put (at least) core components in namespace jd to allow ADL: avoids ugly
forward declarations of operator<< before #include <luabind/operator.hpp>
and is good style anyway.
* Replace the FileSystem singleton with free functions in a namespace.
* StateManager / State needs to be completely rewritten: It's way to
complicated without providing any benefits and, worse, lowers flexibility.
* Maybe replace jd.connect with a Lua function <-> luabind::object() converter
so that jd.connect() is not necessary anymore and
fooComponent:connect_bar(f) can be used instead.
* Get rid of LuaComponent::bindLuaPart(): Maybe luabind::adopt() is now
capable of replacing it.
capable of replacing it.
* Exceptions: Use boost::exception/error_info system or at least a proper
hierarchy.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ set(LUAEXPORT_SOURCES
luaexport/ServiceLocator.cpp
luaexport/Collisions.cpp
luaexport/Geometry.cpp
luaexport/FileSystemMeta.cpp
luaexport/DrawServiceMeta.cpp
luaexport/MainloopMeta.cpp
luaexport/StateManagerMeta.cpp
Expand Down
10 changes: 0 additions & 10 deletions src/luaexport/FileSystemMeta.cpp

This file was deleted.

17 changes: 7 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,7 @@ int main(int argc, char* argv[])
auto const regSvc = ServiceLocator::registerService;

LOG_D("Initializing virtual filesystem...");
FileSystem::Init fsinit;

FileSystem& fs = FileSystem::get();
regSvc(fs);
vfs::Init fsinit;

fs::path programpath = PHYSFS_getBaseDir();
std::vector<std::string> baselibpaths;
Expand All @@ -294,17 +291,17 @@ int main(int argc, char* argv[])
baselibpaths.push_back((programpath / "../share/base.jd").string());
baselibpaths.push_back(basepath + "/base.jd");
baselibpaths.push_back("./base.jd");
fs.mountFirstWorking(baselibpaths, "/", FileSystem::logWarnings|FileSystem::mountOptional);
vfs::mountFirstWorking(baselibpaths, "/", vfs::logWarnings|vfs::mountOptional);

if (!fs.mount(game, "/", FileSystem::logWarnings|(gameSpecified ?
FileSystem::prependPath : FileSystem::mountOptional))
if (!vfs::mount(game, "/", vfs::logWarnings|(gameSpecified ?
vfs::prependPath : vfs::mountOptional))
) {
LOG_W("Mounting working directory instead.");
fs.mount(".");
vfs::mount(".");
}

fs.mount(basepath + "data/", "/",
FileSystem::writeDirectory|FileSystem::prependPath);
vfs::mount(basepath + "data/", "/",
vfs::writeDirectory|vfs::prependPath);


LOG_D("Finished initializing virtual filesystem.");
Expand Down
31 changes: 11 additions & 20 deletions src/svc/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ static std::string lastPhysFsError()
}


FileSystem::Error::Error(std::string const& msg, bool getLastError):
vfs::Error::Error(std::string const& msg, bool getLastError):
std::runtime_error(msg + (
getLastError ? std::string(": ") + lastPhysFsError() : std::string())) { }



#define CALL_PHYSFS(fn, ...) \
if (!fn(__VA_ARGS__)) \
throw ::FileSystem::Error(#fn " failed"); \
throw ::vfs::Error(#fn " failed"); \
(void)0

VFile::VFile():
Expand All @@ -36,7 +36,7 @@ VFile::VFile():
}

VFile::VFile(std::string const& filename, OpenMode mode):
m_f(FileSystem::get().openRaw(filename, mode))
m_f(vfs::openRaw(filename, mode))
{
}

Expand All @@ -58,7 +58,7 @@ VFile::VFile(VFile&& rhs)
void VFile::open(std::string const& filename, OpenMode mode)
{
close();
m_f = FileSystem::get().openRaw(filename, mode);
m_f = vfs::openRaw(filename, mode);
m_err.clear();
}

Expand All @@ -77,6 +77,7 @@ VFile::~VFile()
try {
close();
} catch(std::exception const& e) {
LOG_E("Error closing VFile in destructor:");
LOG_EX(e);
}
}
Expand Down Expand Up @@ -140,7 +141,7 @@ sf::Int64 VFile::check(sf::Int64 r, sf::Int64 rq, std::string const& op)
void VFile::throwError() const
{
if (!m_err.empty())
throw FileSystem::Error(m_err, false);
throw vfs::Error(m_err, false);
}


Expand Down Expand Up @@ -200,13 +201,13 @@ void VFileDevice::throwErr()
//////////////////////////////////////////////////////////


FileSystem::Init::Init()
vfs::Init::Init()
{
// Use original encoding here.
CALL_PHYSFS(PHYSFS_init, argv()[0]);
}

FileSystem::Init::~Init()
vfs::Init::~Init()
{
if (!PHYSFS_isInit()) {
LOG_W("PhysFS already shut down.");
Expand All @@ -219,17 +220,7 @@ FileSystem::Init::~Init()
//////////////////////////////////////////////////////////


FileSystem::FileSystem()
{
if (!PHYSFS_isInit())
throw Error("FileSystem not initialized", false);
}

FileSystem::~FileSystem()
{
}

PHYSFS_File* FileSystem::openRaw(std::string const& name, VFile::OpenMode mode)
PHYSFS_File* vfs::openRaw(std::string const& name, VFile::OpenMode mode)
{
PHYSFS_File* f;
switch (mode) {
Expand All @@ -243,7 +234,7 @@ PHYSFS_File* FileSystem::openRaw(std::string const& name, VFile::OpenMode mode)
return f;
}

bool FileSystem::mount(
bool vfs::mount(
std::string const& path,
std::string const& mountPoint,
int flags)
Expand All @@ -268,7 +259,7 @@ bool FileSystem::mount(
}


bool FileSystem::mountFirstWorking(
bool vfs::mountFirstWorking(
std::vector<std::string> const& paths,
std::string const& mountPoint,
int flags)
Expand Down
18 changes: 3 additions & 15 deletions src/svc/FileSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,14 @@ class VFileDevice
VFile& m_f;
};

class FileSystem: public Component {
JD_COMPONENT
public:
namespace vfs { // Virtual File System
struct Init { Init(); ~Init(); };

class Error: public std::runtime_error {
public:
Error(std::string const& msg, bool getLastError = true);
};

virtual ~FileSystem();

static FileSystem& get()
{
static FileSystem instance;
return instance;
}

PHYSFS_File* openRaw(std::string const& name, VFile::OpenMode mode);

enum MountFlags {
Expand All @@ -113,9 +104,6 @@ class FileSystem: public Component {
std::vector<std::string> const& paths,
std::string const& mountPoint = std::string(),
int flags = prependPath);

private:
FileSystem();
};
} // namespace vfs

#endif

0 comments on commit a0d2352

Please sign in to comment.