Skip to content

Commit

Permalink
Replace filesystem/path.h
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Nov 3, 2018
1 parent 5b76965 commit e13d137
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ node_modules
/package.json
/.bsv
/*.mkv
*.d
5 changes: 1 addition & 4 deletions Makefile.common
Expand Up @@ -17,9 +17,6 @@ SOURCES_C := $(CORE_DIR)/vendor/semver/semver.c
# random
FLAGS += -I$(CORE_DIR)/vendor/random/include

# filesystem
FLAGS += -I$(CORE_DIR)/vendor/filesystem

# libretro-common
FLAGS += -I$(CORE_DIR)/vendor/libretro-common/include
# Only compile libretro-common when not STATIC_LINKING
Expand Down Expand Up @@ -49,7 +46,7 @@ ifneq ($(STATIC_LINKING), 1)
)
# Ensure the sinc_resampler_neon is available for ARM NEON devices.
OBJECTS += $(CORE_DIR)/vendor/libretro-common/audio/resampler/drivers/sinc_resampler_neon.o

# MD5
FLAGS += -I$(CORE_DIR)/vendor/libretro-common/include
SOURCES_C += $(CORE_DIR)/vendor/libretro-common/utils/md5.c
Expand Down
1 change: 1 addition & 0 deletions Makefile.libretro
Expand Up @@ -13,6 +13,7 @@ filter_out1 = $(filter-out $(firstword $1),$1)
filter_out2 = $(call filter_out1,$(call filter_out1,$1))
unixpath = $(subst \,/,$1)
unixcygpath = /$(subst :,,$(call unixpath,$1))
export DEPSDIR := $(CURDIR)/

ifeq ($(platform),)
platform = unix
Expand Down
6 changes: 2 additions & 4 deletions src/love/Types/FileSystem/FileData.cpp
Expand Up @@ -4,7 +4,6 @@

#include <stdio.h>
#include <stdlib.h>
#include "filesystem/path.h"

#include "../../../ChaiLove.h"

Expand Down Expand Up @@ -38,9 +37,8 @@ std::string FileData::getString() {
}

std::string FileData::getExtension() {
::filesystem::path p(m_filepath.c_str());
std::string extension(p.extension());
return extension;
ChaiLove* app = ChaiLove::getInstance();
return app->filesystem.getExtension(m_filepath);
}

} // namespace FileSystem
Expand Down
39 changes: 32 additions & 7 deletions src/love/filesystem.cpp
Expand Up @@ -5,7 +5,6 @@
#include "physfs.h"
#include "filesystem.h"
#include "physfsrwops.h"
#include "filesystem/path.h"
#include "../ChaiLove.h"
#include "Types/FileSystem/FileInfo.h"

Expand All @@ -32,17 +31,15 @@ bool filesystem::init(const std::string& file, const void* data) {
}

// Find the parent and extension of the given file.
::filesystem::path p(file.c_str());
std::string extension(p.extension());
std::string extension(getFileExtension(file));

// Allow loading from an Archive.
if (extension == "chaigame" || extension == "chailove" || extension == "zip") {
return mount(file.c_str(), "/", false);
}

// If we are just running the core, load the base path.
::filesystem::path parent(p.parent_path());
std::string parentPath(parent.str());
std::string parentPath(getParentDirectory(file));
if (parentPath.empty()) {
return mount(".", "/", false);
}
Expand All @@ -51,6 +48,34 @@ bool filesystem::init(const std::string& file, const void* data) {
return mount(parentPath.c_str(), "/", false);
}

std::string filesystem::getParentDirectory(const std::string& filepath) {
return filepath.substr(0, filepath.find_last_of("/\\"));
}

std::string filesystem::getFileExtension(const std::string& filepath) {
size_t i = filepath.rfind('.', filepath.length());
if (i != std::string::npos) {
return filepath.substr(i + 1, filepath.length() - i);
}
return "";
}

std::string filesystem::getBasename(const std::string& filepath) {
char sep = '/';
#ifdef _WIN32
if (filepath.find('\\') != std::string::npos) {
sep = '\\';
}
#endif

size_t i = filepath.rfind(sep, filepath.length());
if (i != std::string::npos) {
return filepath.substr(i + 1, filepath.length() - i);
}

return "";
}

void filesystem::mountlibretro() {
// Mount some of the libretro directories.
const char *system_dir = NULL;
Expand All @@ -60,8 +85,8 @@ void filesystem::mountlibretro() {

if (ChaiLove::environ_cb(RETRO_ENVIRONMENT_GET_LIBRETRO_PATH, &core_dir) && core_dir) {
// Make sure to get the directory of the core.
::filesystem::path p(core_dir);
mount(p.parent_path().str(), "/libretro/core", false);
std::string parentPath(getParentDirectory(core_dir));
mount(parentPath, "/libretro/core", false);
}
if (ChaiLove::environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir) {
mount(system_dir, "/libretro/system", false);
Expand Down
4 changes: 4 additions & 0 deletions src/love/filesystem.h
Expand Up @@ -78,6 +78,10 @@ class filesystem {
*/
int getSize(const std::string& file);

std::string getFileExtension(const std::string& filepath);
std::string getBasename(const std::string& filepath);
std::string getParentDirectory(const std::string& filepath);

/**
* Removes a file or empty directory.
*
Expand Down
10 changes: 5 additions & 5 deletions src/love/script.cpp
@@ -1,6 +1,5 @@
#include "script.h"
#include "../ChaiLove.h"
#include <filesystem/path.h>
#include <algorithm>

#ifdef __HAVE_CHAISCRIPT__
Expand Down Expand Up @@ -86,6 +85,7 @@ std::string script::evalString(const std::string& code, const std::string& filen

script::script(const std::string& file) {
#ifdef __HAVE_CHAISCRIPT__
ChaiLove* app = ChaiLove::getInstance();

// ChaiScript Standard Library Additions
// This adds some basic type definitions to ChaiScript.
Expand Down Expand Up @@ -388,18 +388,18 @@ script::script(const std::string& file) {
// Load the desired main.chai file.
if (file.empty()) {
// When no content is provided, display a No Game demo.
eval(ChaiLove::getInstance()->demo(), "demo.chai");
eval(app->demo(), "demo.chai");
mainLoaded = true;
} else {
// Load the main.chai file.
::filesystem::path p(file.c_str());
std::string extension(p.extension());
loadModuleRequire("conf");

std::string extension(app->filesystem.getExtension(file));
if (extension == "chailove" || extension == "chaigame") {
mainLoaded = loadModuleRequire("main");
} else {
// Otherwise, load the actual file.
std::string filename(p.filename());
std::string filename(app->filesystem.getBasename(file));
mainLoaded = loadModuleRequire(filename);
}
}
Expand Down

0 comments on commit e13d137

Please sign in to comment.