Do FilesystemImpl for meson builds

This commit is contained in:
Struma 2020-11-22 04:51:36 -05:00 committed by ゾロアーク
parent 704d1622ef
commit 4ef6b3c337
13 changed files with 71 additions and 110 deletions

2
.gitignore vendored
View file

@ -50,7 +50,7 @@ Temporary Items
# Xcode
## User settings
xcuserdata/
xcuserdata
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint

View file

@ -636,7 +636,7 @@ static void runRMXPScripts(BacktraceData &btData) {
#define EVALFILE(name) if (!evalScript(SCRIPT(name))) break;
#else
#define EVALFILE(name) { \
std::string s = Cocoa::getFile("BindingScripts/" #name, "rb"); \
std::string s = mkxp_fs::contentsOfAssetAsString("BindingScripts/" #name, "rb"); \
if (!evalScript(rb_str_new_cstr(s.c_str()), #name)) break; \
}
#endif

View file

@ -1,20 +0,0 @@
//
// CocoaHelpers.hpp
// PlayerLegacy
//
// Created by ゾロアーク on 11/18/20.
//
#ifndef CocoaHelpers_hpp
#define CocoaHelpers_hpp
#include <string>
#include <vector>
class Cocoa {
public:
static std::string getFile(const char *baseName, const char *ext);
static std::string getFilePath(const char *baseName, const char *ext);
};
#endif /* CocoaHelpers_hpp */

View file

@ -1,34 +0,0 @@
//
// CocoaHelpers.cpp
// PlayerLegacy
//
// Created by ゾロアーク on 11/18/20.
//
#import "CocoaHelpers.hpp"
#import <Foundation/Foundation.h>
#import <ObjFW/ObjFW.h>
// ObjFW is a bit of a dick to strings when Cocoa gets involved
// Can't use literals when using both Apple and ObjFW headers, meh
// This is a pretty lazy header, but it'll do for the moment while
// I work on Apple stuff, lots to do
#define NSSTR(ptr) [NSString stringWithUTF8String: ptr]
std::string Cocoa::getFile(const char* baseName, const char* ext) {
return std::string([[NSString stringWithContentsOfFile: NSSTR(getFilePath(baseName, ext).c_str())] UTF8String]);
}
std::string Cocoa::getFilePath(const char *baseName, const char *ext) {
NSBundle *assetBundle = [NSBundle bundleWithPath:
[NSString stringWithFormat:
NSSTR("%@/%s"),
NSBundle.mainBundle.resourcePath,
"Assets.bundle"
]
];
return std::string([assetBundle pathForResource: NSSTR(baseName) ofType: NSSTR(ext)].UTF8String);
}

View file

@ -118,13 +118,11 @@
3B10EE092568E96A00372D13 /* binding-mri.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10EDF02568E96A00372D13 /* binding-mri.cpp */; };
3B10EE0B2568E96A00372D13 /* module_rpg.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10EDF32568E96A00372D13 /* module_rpg.cpp */; };
3B10EE0C2568E96A00372D13 /* viewport-binding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10EDF42568E96A00372D13 /* viewport-binding.cpp */; };
3B10EE142568EA0900372D13 /* CocoaHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B10EE122568EA0900372D13 /* CocoaHelpers.mm */; };
3B5A84062569B56F00BAF2E5 /* config.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A84052569B56F00BAF2E5 /* config.cpp */; };
3B5A840D2569BE7C00BAF2E5 /* filesystemImplApple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */; };
3B5A84342569F82C00BAF2E5 /* mkxp.json in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3BD2B7282565B35D003DAD8A /* mkxp.json */; };
3B5A8445256A0F6300BAF2E5 /* libopenal.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B5A8444256A0F6300BAF2E5 /* libopenal.a */; };
3B5A8464256A46B200BAF2E5 /* systemImplApple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B5A8463256A46B200BAF2E5 /* systemImplApple.mm */; };
3B83DC26256A51B9001991CB /* CocoaHelpers.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3B10EE122568EA0900372D13 /* CocoaHelpers.mm */; };
3B83DC27256A51B9001991CB /* tilemapvx.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED7D2568E95D00372D13 /* tilemapvx.cpp */; };
3B83DC28256A51B9001991CB /* SDL_sound.c in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED112568E8F100372D13 /* SDL_sound.c */; };
3B83DC29256A51B9001991CB /* rgssad.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3B10ED382568E95D00372D13 /* rgssad.cpp */; };
@ -544,8 +542,6 @@
3B10EDF22568E96A00372D13 /* module_rpg3.rb.xxd */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = module_rpg3.rb.xxd; sourceTree = "<group>"; };
3B10EDF32568E96A00372D13 /* module_rpg.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = module_rpg.cpp; sourceTree = "<group>"; };
3B10EDF42568E96A00372D13 /* viewport-binding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "viewport-binding.cpp"; sourceTree = "<group>"; };
3B10EE122568EA0900372D13 /* CocoaHelpers.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CocoaHelpers.mm; sourceTree = SOURCE_ROOT; };
3B10EE132568EA0900372D13 /* CocoaHelpers.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CocoaHelpers.hpp; sourceTree = SOURCE_ROOT; };
3B10EE1F2569348E00372D13 /* json5pp.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = json5pp.hpp; sourceTree = "<group>"; };
3B5A84052569B56F00BAF2E5 /* config.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = config.cpp; sourceTree = "<group>"; };
3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = filesystemImplApple.mm; sourceTree = "<group>"; };
@ -1001,8 +997,6 @@
3B10EE112568E9FF00372D13 /* Temp */ = {
isa = PBXGroup;
children = (
3B10EE132568EA0900372D13 /* CocoaHelpers.hpp */,
3B10EE122568EA0900372D13 /* CocoaHelpers.mm */,
);
path = Temp;
sourceTree = "<group>";
@ -1252,7 +1246,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3B83DC26256A51B9001991CB /* CocoaHelpers.mm in Sources */,
3B83DC27256A51B9001991CB /* tilemapvx.cpp in Sources */,
3B83DC28256A51B9001991CB /* SDL_sound.c in Sources */,
3B83DC29256A51B9001991CB /* rgssad.cpp in Sources */,
@ -1344,7 +1337,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3B10EE142568EA0900372D13 /* CocoaHelpers.mm in Sources */,
3B10EDC22568E95E00372D13 /* tilemapvx.cpp in Sources */,
3B10ED212568E8F100372D13 /* SDL_sound.c in Sources */,
3B10EDA72568E95E00372D13 /* rgssad.cpp in Sources */,

View file

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Assets.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>Player copy.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
<key>Player.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View file

@ -32,7 +32,7 @@
#include <utility>
#ifdef MKXPZ_BUILD_XCODE
#include "CocoaHelpers.hpp"
#include "filesystem/filesystem.h"
#endif
#include <SDL_ttf.h>
@ -71,7 +71,7 @@ static SDL_RWops *openBundledFont()
#ifndef MKXPZ_BUILD_XCODE
return SDL_RWFromConstMem(BNDL_F_D(BUNDLED_FONT), BNDL_F_L(BUNDLED_FONT));
#else
return SDL_RWFromFile(Cocoa::getFilePath("Fonts/liberation", "ttf").c_str(), "rb");
return SDL_RWFromFile(mkxp_fs::getPathForAsset("Fonts/liberation", "ttf").c_str(), "rb");
#endif
}

View file

@ -57,11 +57,11 @@
#endif
#ifdef MKXPZ_BUILD_XCODE
#include "CocoaHelpers.hpp"
#include "filesystem/filesystem.h"
#define INIT_SHADER(vert, frag, name) \
{ \
std::string v = Cocoa::getFile("Shaders/" #vert, "vert"); \
std::string f = Cocoa::getFile("Shaders/" #frag, "frag"); \
std::string v = mkxp_fs::contentsOfAssetAsString("Shaders/" #vert, "vert"); \
std::string f = mkxp_fs::contentsOfAssetAsString("Shaders/" #frag, "frag"); \
Shader::init((const unsigned char*)v.c_str(), v.length(), (const unsigned char*)f.c_str(), f.length(), #vert, #frag, #name); \
}
#else
@ -104,7 +104,7 @@ Shader::Shader()
{
#ifdef MKXPZ_BUILD_XCODE
if (Shader::shaderCommon.empty())
Shader::shaderCommon = Cocoa::getFile("Shaders/common", "h");
Shader::shaderCommon = mkxp_fs::contentsOfAssetAsString("Shaders/common", "h");
#endif
vertShader = gl.CreateShader(GL_VERTEX_SHADER);
fragShader = gl.CreateShader(GL_FRAGMENT_SHADER);

View file

@ -6,34 +6,43 @@
//
#include "filesystemImpl.h"
#include <filesystem>
#include "util/exception.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
// TODO
// https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c
bool filesystemImpl::fileExists(const char *path) {
assert(!"Not implemented");
return false;
struct stat buffer;
return (stat (path, &buffer) == 0);
}
// TODO
// https://stackoverflow.com/questions/2912520/read-file-contents-into-a-string-in-c
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
assert(!"Not implemented");
return std::string("");
std::string ret;
try {
std::ifstream ifs(path);
ret = std::string ( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
} catch (...) {
throw new Exception(Exception::NoFileError, "Failed to read file at %s", path);
}
return ret;
}
// TODO
// chdir and getcwd do not support unicode on Windows
bool filesystemImpl::setCurrentDirectory(const char *path) {
return chdir(path);
}
// TODO
std::string filesystemImpl::getCurrentDirectory() {
return std::string(getcwd(0,0));
}
std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool absolute) {
assert(!"Not implemented");
return std::string("");
return std::string(path);
}

View file

@ -21,5 +21,10 @@ std::string getCurrentDirectory();
std::string normalizePath(const char *path, bool preferred, bool absolute);
#ifdef MKXPZ_BUILD_XCODE
std::string getPathForAsset(const char *baseName, const char *ext);
std::string contentsOfAssetAsString(const char *baseName, const char *ext);
#endif
};
#endif /* filesystemImpl_h */

View file

@ -24,7 +24,7 @@ bool filesystemImpl::fileExists(const char *path) {
std::string filesystemImpl::contentsOfFileAsString(const char *path) {
NSString *fileContents = [NSString stringWithContentsOfFile: PATHTONS(path)];
if (fileContents == nil)
throw new Exception(Exception::NoFileError, "Failed to locate file at %s", path);
throw new Exception(Exception::NoFileError, "Failed to read file at %s", path);
return std::string(fileContents.UTF8String);
@ -49,3 +49,38 @@ std::string filesystemImpl::normalizePath(const char *path, bool preferred, bool
}
return std::string(NSTOPATH(nspath));
}
NSString *getPathForAsset_internal(const char *baseName, const char *ext) {
NSBundle *assetBundle = [NSBundle bundleWithPath:
[NSString stringWithFormat:
@"%@/%s",
NSBundle.mainBundle.resourcePath,
"Assets.bundle"
]
];
if (assetBundle == nil)
return nil;
return [assetBundle pathForResource: @(baseName) ofType: @(ext)];
}
std::string filesystemImpl::getPathForAsset(const char *baseName, const char *ext) {
NSString *assetPath = getPathForAsset_internal(baseName, ext);
if (assetPath == nil)
throw new Exception(Exception::NoFileError, "Failed to find the asset named %s.%s", baseName, ext);
return std::string(getPathForAsset_internal(baseName, ext).UTF8String);
}
std::string filesystemImpl::contentsOfAssetAsString(const char *baseName, const char *ext) {
NSString *path = getPathForAsset_internal(baseName, ext);
NSString *fileContents = [NSString stringWithContentsOfFile: path];
// This should never fail
if (fileContents == nil)
throw new Exception(Exception::MKXPError, "Failed to read file at %s", path.UTF8String);
return std::string(fileContents.UTF8String);
}

View file

@ -19,9 +19,7 @@
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef MKXPZ_BUILD_XCODE
#include "CocoaHelpers.hpp"
#else
#ifndef MKXPZ_BUILD_XCODE
#include "icon.png.xxd"
#endif
@ -151,7 +149,7 @@ static void setupWindowIcon(const Config &conf, SDL_Window *win) {
#ifndef MKXPZ_BUILD_XCODE
iconSrc = SDL_RWFromConstMem(___assets_icon_png, ___assets_icon_png_len);
#else
iconSrc = SDL_RWFromFile(Cocoa::getFilePath("icon", "png").c_str(), "rb");
iconSrc = SDL_RWFromFile(mkxp_fs::getPathForAsset("icon", "png").c_str(), "rb");
#endif
else
iconSrc = SDL_RWFromFile(conf.iconPath.c_str(), "rb");