From ace30d208576c01d04eeb8d61a8006ce23fc3963 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 4 Jul 2022 10:54:39 +0100 Subject: [PATCH] Xbox NXDK: Handle drives 1. Automount D drive 2. Mount E drive 3. Set preference and config paths to `E:\UDATA\devilutionx` Co-authored-by: Ryzee119 --- CMakeLists.txt | 3 ++ Source/main.cpp | 6 ++++ Source/utils/paths.cpp | 63 +++++++++++++++++++++++++++++++----------- 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6906dae3..dfe54468f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -468,6 +468,9 @@ if(UWP_LIB) endif() if(NXDK) + target_link_libraries(${BIN_TARGET} PRIVATE "${NXDK_DIR}/lib/libnxdk_automount_d.lib") + target_link_options(${BIN_TARGET} PRIVATE "-include:_automount_d_drive") + set(_nxdk_pkg_dir "${CMAKE_BINARY_DIR}/pkg") set(_xbe_path "${_nxdk_pkg_dir}/default.xbe") add_custom_command( diff --git a/Source/main.cpp b/Source/main.cpp index 130ca0492..af8960a83 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -13,6 +13,9 @@ #include "platform/vita/network.h" #include "platform/vita/random.hpp" #endif +#ifdef NXDK +#include +#endif #ifdef GPERF_HEAP_MAIN #include #endif @@ -40,6 +43,9 @@ extern "C" int main(int argc, char **argv) vita_enable_network(); randombytes_vitarandom_init(); #endif +#ifdef NXDK + nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\"); +#endif #ifdef GPERF_HEAP_MAIN HeapProfilerStart("main"); #endif diff --git a/Source/utils/paths.cpp b/Source/utils/paths.cpp index 4a869b9fb..7c3085b39 100644 --- a/Source/utils/paths.cpp +++ b/Source/utils/paths.cpp @@ -2,6 +2,7 @@ #include +#include "appfat.h" #include "utils/file_util.h" #include "utils/log.hpp" #include "utils/sdl_ptrs.h" @@ -10,6 +11,12 @@ #include "platform/ios/ios_paths.h" #endif +#ifdef NXDK +#define NOMINMAX 1 +#define WIN32_LEAN_AND_MEAN +#include +#endif + #ifdef USE_SDL1 #include "utils/sdl2_to_1_2_backports.h" #endif @@ -25,15 +32,18 @@ std::optional prefPath; std::optional configPath; std::optional assetsPath; -void AddTrailingSlash(std::string &path) -{ #ifdef _WIN32 - if (!path.empty() && path.back() != '\\') - path += '\\'; +constexpr char DirectorySeparator = '\\'; +#define DIRECTORY_SEPARATOR_STR "\\" #else - if (!path.empty() && path.back() != '/') - path += '/'; +constexpr char DirectorySeparator = '/'; +#define DIRECTORY_SEPARATOR_STR "/" #endif + +void AddTrailingSlash(std::string &path) +{ + if (!path.empty() && path.back() != DirectorySeparator) + path += DirectorySeparator; } std::string FromSDL(char *s) @@ -47,6 +57,20 @@ std::string FromSDL(char *s) return result; } +#ifdef NXDK +const std::string &NxdkGetPrefPath() +{ + static const std::string Path = []() { + const char *path = "E:\\UDATA\\devilutionx\\"; + if (CreateDirectoryA(path, nullptr) == FALSE && ::GetLastError() != ERROR_ALREADY_EXISTS) { + DirErrorDlg(path); + } + return path; + }(); + return Path; +} +#endif + } // namespace const std::string &BasePath() @@ -60,13 +84,15 @@ const std::string &BasePath() const std::string &PrefPath() { if (!prefPath) { -#ifndef __IPHONEOS__ +#if defined(__IPHONEOS__) + prefPath = FromSDL(IOSGetPrefPath()); +#elif defined(NXDK) + prefPath = NxdkGetPrefPath(); +#else prefPath = FromSDL(SDL_GetPrefPath("diasurgical", "devilution")); if (FileExistsAndIsWriteable("diablo.ini")) { - prefPath = std::string("./"); + prefPath = std::string("." DIRECTORY_SEPARATOR_STR); } -#else - prefPath = FromSDL(IOSGetPrefPath()); #endif } return *prefPath; @@ -75,13 +101,15 @@ const std::string &PrefPath() const std::string &ConfigPath() { if (!configPath) { -#ifndef __IPHONEOS__ +#if defined(__IPHONEOS__) + configPath = FromSDL(IOSGetPrefPath()); +#elif defined(NXDK) + configPath = NxdkGetPrefPath(); +#else configPath = FromSDL(SDL_GetPrefPath("diasurgical", "devilution")); if (FileExistsAndIsWriteable("diablo.ini")) { - configPath = std::string("./"); + configPath = std::string("." DIRECTORY_SEPARATOR_STR); } -#else - configPath = FromSDL(IOSGetPrefPath()); #endif } return *configPath; @@ -89,12 +117,15 @@ const std::string &ConfigPath() const std::string &AssetsPath() { - if (!assetsPath) + if (!assetsPath) { #if __EMSCRIPTEN__ assetsPath.emplace("assets/"); +#elif defined(NXDK) + assetsPath.emplace("D:\\assets\\"); #else - assetsPath.emplace(FromSDL(SDL_GetBasePath()) + "assets/"); + assetsPath.emplace(FromSDL(SDL_GetBasePath()) + ("assets" DIRECTORY_SEPARATOR_STR)); #endif + } return *assetsPath; }