Add support for using a portable directory without special build flags. (#1956)

This commit is contained in:
Steveice10 2024-01-24 01:27:25 -08:00 committed by GitHub
parent 77274735d6
commit 4b576d066e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 49 deletions

View File

@ -166,11 +166,13 @@ target_link_libraries(melonDS PRIVATE ${QT_LINK_LIBS} ${CMAKE_DL_LIBS})
target_include_directories(melonDS PRIVATE "${Slirp_INCLUDE_DIRS}") target_include_directories(melonDS PRIVATE "${Slirp_INCLUDE_DIRS}")
target_link_libraries(melonDS PRIVATE "${Slirp_LINK_LIBRARIES}") target_link_libraries(melonDS PRIVATE "${Slirp_LINK_LIBRARIES}")
if (UNIX) if (WIN32)
option(PORTABLE "Make a portable build that looks for its configuration in the current directory" OFF)
elseif (WIN32)
option(PORTABLE "Make a portable build that looks for its configuration in the current directory" ON) option(PORTABLE "Make a portable build that looks for its configuration in the current directory" ON)
if (PORTABLE)
target_compile_definitions(melonDS PRIVATE WIN32_PORTABLE)
endif()
configure_file("${CMAKE_SOURCE_DIR}/res/melon.rc.in" "${CMAKE_BINARY_DIR}/res/melon.rc") configure_file("${CMAKE_SOURCE_DIR}/res/melon.rc.in" "${CMAKE_BINARY_DIR}/res/melon.rc")
target_sources(melonDS PUBLIC "${CMAKE_BINARY_DIR}/res/melon.rc") target_sources(melonDS PUBLIC "${CMAKE_BINARY_DIR}/res/melon.rc")
target_include_directories(melonDS PRIVATE "${CMAKE_BINARY_DIR}/res") target_include_directories(melonDS PRIVATE "${CMAKE_BINARY_DIR}/res")
@ -189,10 +191,6 @@ elseif (WIN32)
set_target_properties(melonDS PROPERTIES LINK_FLAGS_DEBUG "-mconsole") set_target_properties(melonDS PROPERTIES LINK_FLAGS_DEBUG "-mconsole")
endif() endif()
if (PORTABLE)
target_compile_definitions(melonDS PRIVATE PORTABLE)
endif()
if (APPLE) if (APPLE)
target_sources(melonDS PRIVATE sem_timedwait.cpp) target_sources(melonDS PRIVATE sem_timedwait.cpp)

View File

@ -21,6 +21,7 @@
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <QCoreApplication>
#include <QStandardPaths> #include <QStandardPaths>
#include <QString> #include <QString>
#include <QDateTime> #include <QDateTime>
@ -60,6 +61,42 @@ void emuStop();
namespace melonDS::Platform namespace melonDS::Platform
{ {
void PathInit(int argc, char** argv)
{
// First, check for the portable directory next to the executable.
QString appdirpath = QCoreApplication::applicationDirPath();
QString portablepath = appdirpath + QDir::separator() + "portable";
#if defined(__APPLE__)
// On Apple platforms we may need to navigate outside an app bundle.
// The executable directory would be "melonDS.app/Contents/MacOS", so we need to go a total of three steps up.
QDir bundledir(appdirpath);
if (bundledir.cd("..") && bundledir.cd("..") && bundledir.dirName().endsWith(".app") && bundledir.cd(".."))
{
portablepath = bundledir.absolutePath() + QDir::separator() + "portable";
}
#endif
QDir portabledir(portablepath);
if (portabledir.exists())
{
EmuDirectory = portabledir.absolutePath().toStdString();
}
else
{
// If no overrides are specified, use the default path.
#if defined(__WIN32__) && defined(WIN32_PORTABLE)
EmuDirectory = appdirpath.toStdString();
#else
QString confdir;
QDir config(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
config.mkdir("melonDS");
confdir = config.absolutePath() + QDir::separator() + "melonDS";
EmuDirectory = confdir.toStdString();
#endif
}
}
QSharedMemory* IPCBuffer = nullptr; QSharedMemory* IPCBuffer = nullptr;
int IPCInstanceID; int IPCInstanceID;
@ -133,38 +170,7 @@ void IPCDeInit()
void Init(int argc, char** argv) void Init(int argc, char** argv)
{ {
#if defined(__WIN32__) || defined(PORTABLE) PathInit(argc, argv);
if (argc > 0 && strlen(argv[0]) > 0)
{
int len = strlen(argv[0]);
while (len > 0)
{
if (argv[0][len] == '/') break;
if (argv[0][len] == '\\') break;
len--;
}
if (len > 0)
{
std::string emudir = argv[0];
EmuDirectory = emudir.substr(0, len);
}
else
{
EmuDirectory = ".";
}
}
else
{
EmuDirectory = ".";
}
#else
QString confdir;
QDir config(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
config.mkdir("melonDS");
confdir = config.absolutePath() + "/melonDS/";
EmuDirectory = confdir.toStdString();
#endif
IPCInit(); IPCInit();
} }
@ -284,15 +290,7 @@ FileHandle* OpenLocalFile(const std::string& path, FileMode mode)
} }
else else
{ {
#ifdef PORTABLE
fullpath = QString::fromStdString(EmuDirectory) + QDir::separator() + qpath; fullpath = QString::fromStdString(EmuDirectory) + QDir::separator() + qpath;
#else
// Check user configuration directory
QDir config(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation));
config.mkdir("melonDS");
fullpath = config.absolutePath() + "/melonDS/";
fullpath.append(qpath);
#endif
} }
return OpenFile(fullpath.toStdString(), mode); return OpenFile(fullpath.toStdString(), mode);

View File

@ -299,10 +299,10 @@ int main(int argc, char** argv)
if (argc != 0 && (!strcasecmp(argv[0], "derpDS") || !strcasecmp(argv[0], "./derpDS"))) if (argc != 0 && (!strcasecmp(argv[0], "derpDS") || !strcasecmp(argv[0], "./derpDS")))
printf("did you just call me a derp???\n"); printf("did you just call me a derp???\n");
Platform::Init(argc, argv);
MelonApplication melon(argc, argv); MelonApplication melon(argc, argv);
Platform::Init(argc, argv);
CLI::CommandLineOptions* options = CLI::ManageArgs(melon); CLI::CommandLineOptions* options = CLI::ManageArgs(melon);
// http://stackoverflow.com/questions/14543333/joystick-wont-work-using-sdl // http://stackoverflow.com/questions/14543333/joystick-wont-work-using-sdl