Skip to content

Commit

Permalink
Migrate FileSystemInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 20, 2017
1 parent 07d8080 commit 6f6d719
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
14 changes: 7 additions & 7 deletions install/scripts/test.py
Expand Up @@ -136,14 +136,14 @@ def visit(self, key, value):
print(path)

# Test FileSystem (VFS)
#class TestFileVisitor(FileVisitor) :
# def visit(self, filename):
# print('Found file: ' + filename)
#
#filevisitor = TestFileVisitor()
#GlobalFileSystem.forEachFile('skins/', 'skin', filevisitor, 99)
class TestFileVisitor(FileVisitor) :
def visit(self, filename):
print('Found file: ' + filename)

filevisitor = TestFileVisitor()
GlobalFileSystem.forEachFile('skins/', 'skin', filevisitor, 99)

filecontents = GlobalFileSystem.readTextFile('skins/tdm_ai_elemental_fire.skin');
filecontents = GlobalFileSystem.readTextFile('skins/tdm_ai_guard_citywatch.skin');
print(filecontents)

# Test the Grid Interface
Expand Down
40 changes: 22 additions & 18 deletions plugins/script/interfaces/FileSystemInterface.cpp
Expand Up @@ -4,7 +4,8 @@
#include "iarchive.h"
#include "itextstream.h"

namespace script {
namespace script
{

void FileSystemInterface::forEachFile(const std::string& basedir,
const std::string& extension,
Expand All @@ -31,7 +32,8 @@ std::string FileSystemInterface::readTextFile(const std::string& filename)
char buffer[READSIZE];
std::size_t bytesRead = READSIZE;

do {
do
{
bytesRead = istream.read(buffer, READSIZE);

// Copy the stuff to the string
Expand All @@ -42,36 +44,38 @@ std::string FileSystemInterface::readTextFile(const std::string& filename)
return text;
}

int FileSystemInterface::getFileCount(const std::string& filename) {
int FileSystemInterface::getFileCount(const std::string& filename)
{
return GlobalFileSystem().getFileCount(filename);
}

std::string FileSystemInterface::findFile(const std::string& name) {
std::string FileSystemInterface::findFile(const std::string& name)
{
return GlobalFileSystem().findFile(name);
}

std::string FileSystemInterface::findRoot(const std::string& name) {
std::string FileSystemInterface::findRoot(const std::string& name)
{
return GlobalFileSystem().findRoot(name);
}

void FileSystemInterface::registerInterface(boost::python::object& nspace) {
void FileSystemInterface::registerInterface(py::module& scope, py::dict& globals)
{
// Expose the FileVisitor interface
nspace["FileVisitor"] =
boost::python::class_<FileVisitorWrapper, boost::noncopyable>("FileVisitor")
.def("visit", boost::python::pure_virtual(&FileVisitorWrapper::visit))
;
py::class_<VirtualFileSystemVisitor, FileVisitorWrapper> visitor(scope, "FileVisitor");
visitor.def(py::init<>());
visitor.def("visit", &VirtualFileSystemVisitor::visit);

// Add the VFS module declaration to the given python namespace
nspace["GlobalFileSystem"] = boost::python::class_<FileSystemInterface>("GlobalFileSystem")
.def("forEachFile", &FileSystemInterface::forEachFile)
.def("findFile", &FileSystemInterface::findFile)
.def("findRoot", &FileSystemInterface::findRoot)
.def("readTextFile", &FileSystemInterface::readTextFile)
.def("getFileCount", &FileSystemInterface::getFileCount)
;
py::class_<FileSystemInterface> filesystem(scope, "FileSystem");
filesystem.def("forEachFile", &FileSystemInterface::forEachFile);
filesystem.def("findFile", &FileSystemInterface::findFile);
filesystem.def("findRoot", &FileSystemInterface::findRoot);
filesystem.def("readTextFile", &FileSystemInterface::readTextFile);
filesystem.def("getFileCount", &FileSystemInterface::getFileCount);

// Now point the Python variable "GlobalFileSystem" to this instance
nspace["GlobalFileSystem"] = boost::python::ptr(this);
globals["GlobalFileSystem"] = this;
}

} // namespace script
20 changes: 12 additions & 8 deletions plugins/script/interfaces/FileSystemInterface.h
@@ -1,11 +1,12 @@
#pragma once

#include <boost/python.hpp>
#include "iscript.h"
#include <pybind11/pybind11.h>

#include "iscript.h"
#include "ifilesystem.h"

namespace script {
namespace script
{

/**
* Adaptor interface for a VFS traversor object.
Expand All @@ -24,14 +25,18 @@ class VirtualFileSystemVisitor

// Scripts will derive from this class
class FileVisitorWrapper :
public VirtualFileSystemVisitor,
public boost::python::wrapper<VirtualFileSystemVisitor>
public VirtualFileSystemVisitor
{
public:
void visit(const std::string& filename)
{
// Wrap this method to python
this->get_override("visit")(filename);
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
VirtualFileSystemVisitor, /* Parent class */
visit, /* Name of function in C++ (must match Python name) */
filename /* Argument(s) */
);
}
};

Expand All @@ -57,8 +62,7 @@ class FileSystemInterface :
std::string findRoot(const std::string& name);

// IScriptInterface implementation
void registerInterface(boost::python::object& nspace);
void registerInterface(py::module& scope, py::dict& globals) override;
};
typedef std::shared_ptr<FileSystemInterface> FileSystemInterfacePtr;

} // namespace script

0 comments on commit 6f6d719

Please sign in to comment.