diff --git a/.gitignore b/.gitignore index 872c005c..6233b13c 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,7 @@ Temporary Items # Xcode ## User settings -xcuserdata/ +xcuserdata ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) *.xcscmblueprint diff --git a/binding/binding-mri.cpp b/binding/binding-mri.cpp index 49bc9e82..bf90ca55 100644 --- a/binding/binding-mri.cpp +++ b/binding/binding-mri.cpp @@ -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 diff --git a/macos/CocoaHelpers.hpp b/macos/CocoaHelpers.hpp deleted file mode 100644 index 80390e60..00000000 --- a/macos/CocoaHelpers.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// CocoaHelpers.hpp -// PlayerLegacy -// -// Created by ゾロアーク on 11/18/20. -// - -#ifndef CocoaHelpers_hpp -#define CocoaHelpers_hpp - -#include -#include - -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 */ diff --git a/macos/CocoaHelpers.mm b/macos/CocoaHelpers.mm deleted file mode 100644 index 72adc93b..00000000 --- a/macos/CocoaHelpers.mm +++ /dev/null @@ -1,34 +0,0 @@ -// -// CocoaHelpers.cpp -// PlayerLegacy -// -// Created by ゾロアーク on 11/18/20. -// - -#import "CocoaHelpers.hpp" -#import -#import - -// 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); -} diff --git a/macos/mkxp-z.xcodeproj/project.pbxproj b/macos/mkxp-z.xcodeproj/project.pbxproj index 9258073c..024112ab 100644 --- a/macos/mkxp-z.xcodeproj/project.pbxproj +++ b/macos/mkxp-z.xcodeproj/project.pbxproj @@ -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 = ""; }; 3B10EDF32568E96A00372D13 /* module_rpg.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = module_rpg.cpp; sourceTree = ""; }; 3B10EDF42568E96A00372D13 /* viewport-binding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "viewport-binding.cpp"; sourceTree = ""; }; - 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 = ""; }; 3B5A84052569B56F00BAF2E5 /* config.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = config.cpp; sourceTree = ""; }; 3B5A840C2569BE7C00BAF2E5 /* filesystemImplApple.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = filesystemImplApple.mm; sourceTree = ""; }; @@ -1001,8 +997,6 @@ 3B10EE112568E9FF00372D13 /* Temp */ = { isa = PBXGroup; children = ( - 3B10EE132568EA0900372D13 /* CocoaHelpers.hpp */, - 3B10EE122568EA0900372D13 /* CocoaHelpers.mm */, ); path = Temp; sourceTree = ""; @@ -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 */, diff --git a/macos/mkxp-z.xcodeproj/project.xcworkspace/xcuserdata/zoroark.xcuserdatad/UserInterfaceState.xcuserstate b/macos/mkxp-z.xcodeproj/project.xcworkspace/xcuserdata/zoroark.xcuserdatad/UserInterfaceState.xcuserstate index 184cedea..6d1c399b 100644 Binary files a/macos/mkxp-z.xcodeproj/project.xcworkspace/xcuserdata/zoroark.xcuserdatad/UserInterfaceState.xcuserstate and b/macos/mkxp-z.xcodeproj/project.xcworkspace/xcuserdata/zoroark.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/macos/mkxp-z.xcodeproj/xcuserdata/zoroark.xcuserdatad/xcschemes/xcschememanagement.plist b/macos/mkxp-z.xcodeproj/xcuserdata/zoroark.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index 127e2d49..00000000 --- a/macos/mkxp-z.xcodeproj/xcuserdata/zoroark.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,24 +0,0 @@ - - - - - SchemeUserState - - Assets.xcscheme_^#shared#^_ - - orderHint - 1 - - Player copy.xcscheme_^#shared#^_ - - orderHint - 2 - - Player.xcscheme_^#shared#^_ - - orderHint - 0 - - - - diff --git a/src/display/font.cpp b/src/display/font.cpp index 449bfcd0..cde763e9 100644 --- a/src/display/font.cpp +++ b/src/display/font.cpp @@ -32,7 +32,7 @@ #include #ifdef MKXPZ_BUILD_XCODE -#include "CocoaHelpers.hpp" +#include "filesystem/filesystem.h" #endif #include @@ -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 } diff --git a/src/display/gl/shader.cpp b/src/display/gl/shader.cpp index a558109c..995aa200 100644 --- a/src/display/gl/shader.cpp +++ b/src/display/gl/shader.cpp @@ -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); diff --git a/src/filesystem/filesystemImpl.cpp b/src/filesystem/filesystemImpl.cpp index ae1fb538..b4e0f300 100644 --- a/src/filesystem/filesystemImpl.cpp +++ b/src/filesystem/filesystemImpl.cpp @@ -6,34 +6,43 @@ // #include "filesystemImpl.h" -#include +#include "util/exception.h" + +#include #include +#include -// 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(ifs) ), + (std::istreambuf_iterator() ) ); + } 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); } diff --git a/src/filesystem/filesystemImpl.h b/src/filesystem/filesystemImpl.h index 097af91c..f870aeea 100644 --- a/src/filesystem/filesystemImpl.h +++ b/src/filesystem/filesystemImpl.h @@ -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 */ diff --git a/src/filesystem/filesystemImplApple.mm b/src/filesystem/filesystemImplApple.mm index 917826aa..eacefe1a 100644 --- a/src/filesystem/filesystemImplApple.mm +++ b/src/filesystem/filesystemImplApple.mm @@ -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); + +} diff --git a/src/main.cpp b/src/main.cpp index 4748db43..89b927d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,9 +19,7 @@ ** along with mkxp. If not, see . */ -#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");