From 1388e9ebb1c78fe946913125bbc6fd3b972d7612 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 21 Sep 2019 00:37:40 -0400 Subject: [PATCH] Add network::init(). Add asset::bundlePath. --- include/asset.hpp | 2 ++ include/network.hpp | 1 + src/asset.cpp | 13 ++++++++++++- src/main.cpp | 3 +++ src/network.cpp | 7 +++++++ src/updater.cpp | 15 +++++++++++++-- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/asset.hpp b/include/asset.hpp index b1ba8760..9ccdb2bd 100644 --- a/include/asset.hpp +++ b/include/asset.hpp @@ -31,6 +31,8 @@ extern std::string pluginsPath; extern std::string settingsPath; extern std::string autosavePath; extern std::string templatePath; +// Only defined on Mac +extern std::string bundlePath; } // namespace asset diff --git a/include/network.hpp b/include/network.hpp index 1843bb8d..3d815e2b 100644 --- a/include/network.hpp +++ b/include/network.hpp @@ -18,6 +18,7 @@ enum Method { METHOD_DELETE, }; +void init(); /** Requests a JSON API URL over HTTP(S), using the data as the query (GET) or the body (POST, etc) Caller must json_decref(). */ diff --git a/src/asset.cpp b/src/asset.cpp index 522623cc..4b1d2915 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -37,9 +37,19 @@ void init() { #if defined ARCH_MAC CFBundleRef bundle = CFBundleGetMainBundle(); assert(bundle); + + CFURLRef bundleUrl = CFBundleCopyBundleURL(bundle); + char bundleBuf[PATH_MAX]; + Boolean success = CFURLGetFileSystemRepresentation(bundleUrl, TRUE, (UInt8*) bundleBuf, sizeof(bundleBuf)); + assert(success); + bundlePath = bundleBuf; + // If the bundle path doesn't end with ".app", assume it's a fake app bundle run from the command line. + if (string::filenameExtension(string::filename(bundlePath)) != "app") + bundlePath = ""; + CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL(bundle); char resourcesBuf[PATH_MAX]; - Boolean success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); + success = CFURLGetFileSystemRepresentation(resourcesUrl, TRUE, (UInt8*) resourcesBuf, sizeof(resourcesBuf)); assert(success); CFRelease(resourcesUrl); systemDir = resourcesBuf; @@ -145,6 +155,7 @@ std::string pluginsPath; std::string settingsPath; std::string autosavePath; std::string templatePath; +std::string bundlePath; } // namespace asset diff --git a/src/main.cpp b/src/main.cpp index 6ee1bb2a..47acb830 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -97,6 +98,7 @@ int main(int argc, char* argv[]) { patchPath = argv[optind]; } + // Initialize environment asset::init(); logger::init(); @@ -145,6 +147,7 @@ int main(int argc, char* argv[]) { INFO("Initializing environment"); random::init(); + network::init(); midi::init(); rtmidiInit(); bridgeInit(); diff --git a/src/network.cpp b/src/network.cpp index 2fe28477..694a4952 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -30,6 +30,13 @@ static size_t writeStringCallback(char* ptr, size_t size, size_t nmemb, void* us } +void init() { + // curl_easy_init() calls this automatically, but it's good to make sure this is done on the main thread before other threads are spawned. + // https://curl.haxx.se/libcurl/c/curl_easy_init.html + curl_global_init(CURL_GLOBAL_ALL); +} + + json_t* requestJson(Method method, std::string url, json_t* dataJ) { CURL* curl = createCurl(); char* reqStr = NULL; diff --git a/src/updater.cpp b/src/updater.cpp index 15a93fcc..42e6a10e 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -74,8 +74,19 @@ void update() { INFO("Launching update %s", path.c_str()); system::runProcessDetached(path); #elif defined ARCH_MAC - // Unzip app using Apple's unzipper, since Rack's unzipper doesn't handle the metadata stuff correctly. - std::string cmd = "open \"" + path + "\""; + std::string cmd; + // std::string appPath = asset::userDir + "/Rack.app"; + // cmd = "rm -rf '" + appPath + "'"; + // std::system(cmd.c_str()); + // // Unzip app using Apple's unzipper, since Rack's unzipper doesn't handle the metadata stuff correctly. + // cmd = "unzip -q '" + path + "' -d '" + asset::userDir + "'"; + // std::system(cmd.c_str()); + // // Open app in Finder + // cmd = "open -R '" + appPath + "'"; + // std::system(cmd.c_str()); + + // Open Archive Utility + cmd = "open '" + path + "'"; std::system(cmd.c_str()); #else system::openBrowser(downloadUrl);