consolidate file related functions, add nativefiledialog-extended to build

This commit is contained in:
scawful
2025-01-06 13:21:28 -05:00
parent fdce133e39
commit b3bc05e620
17 changed files with 156 additions and 177 deletions

View File

@@ -34,6 +34,8 @@ foreach (FILE ${YAZE_RESOURCE_FILES})
)
endforeach()
add_subdirectory(lib/nativefiledialog-extended)
if (YAZE_BUILD_APP)
include(app/app.cmake)
endif()

View File

@@ -48,6 +48,11 @@ target_include_directories(
${PROJECT_BINARY_DIR}
)
target_link_libraries(
yaze PRIVATE
nfd
)
target_link_libraries(
yaze PUBLIC
asar-static

View File

@@ -10,7 +10,7 @@
#include "app/core/platform/font_loader.h"
#include "app/editor/editor_manager.h"
#include "app/gui/style.h"
#include "core/utils/file_util.h"
#include "app/core/platform/file_dialog.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h"

View File

@@ -7,7 +7,7 @@
#include "absl/status/status.h"
#include "app/core/platform/renderer.h"
#include "app/core/utils/file_util.h"
#include "app/core/platform/file_dialog.h"
#include "app/editor/editor.h"
#include "app/editor/editor_manager.h"
#include "imgui/backends/imgui_impl_sdl2.h"

View File

@@ -4,7 +4,6 @@ set(
app/core/controller.cc
app/emu/emulator.cc
app/core/project.cc
app/core/utils/file_util.cc
)
if (WIN32 OR MINGW OR UNIX AND NOT APPLE)
@@ -17,12 +16,12 @@ endif()
if(APPLE)
list(APPEND YAZE_APP_CORE_SRC
app/core/platform/file_dialog.cc
app/core/platform/file_dialog.mm
app/core/platform/app_delegate.mm
app/core/platform/font_loader.cc
app/core/platform/font_loader.mm
app/core/platform/clipboard.mm
app/core/platform/file_path.mm
)
find_library(COCOA_LIBRARY Cocoa)

View File

@@ -4,11 +4,95 @@
// Include Windows-specific headers
#include <shobjidl.h>
#include <windows.h>
#endif // _WIN32
#else // Linux and MacOS
#include <dirent.h>
#include <sys/stat.h>
#endif
#include <fstream>
#include <sstream>
namespace yaze {
namespace core {
std::string GetFileExtension(const std::string &filename) {
size_t dot = filename.find_last_of(".");
if (dot == std::string::npos) {
return "";
}
return filename.substr(dot + 1);
}
std::string GetFileName(const std::string &filename) {
size_t slash = filename.find_last_of("/");
if (slash == std::string::npos) {
return filename;
}
return filename.substr(slash + 1);
}
std::string LoadFile(const std::string &filename) {
std::string contents;
std::ifstream file(filename);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
contents = buffer.str();
file.close();
} else {
// Throw an exception
throw std::runtime_error("Could not open file: " + filename);
}
return contents;
}
std::string LoadConfigFile(const std::string &filename) {
std::string contents;
Platform platform;
#if defined(_WIN32)
platform = Platform::kWindows;
#elif defined(__APPLE__)
platform = Platform::kMacOS;
#else
platform = Platform::kLinux;
#endif
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ifstream file(filepath);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
contents = buffer.str();
file.close();
}
return contents;
}
void SaveFile(const std::string &filename, const std::string &contents,
Platform platform) {
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ofstream file(filepath);
if (file.is_open()) {
file << contents;
file.close();
}
}
std::string GetConfigDirectory(Platform platform) {
std::string config_directory = ".yaze";
switch (platform) {
case Platform::kWindows:
config_directory = "~/AppData/Roaming/yaze";
break;
case Platform::kMacOS:
case Platform::kLinux:
config_directory = "~/.config/yaze";
break;
default:
break;
}
return config_directory;
}
#ifdef _WIN32
std::string FileDialogWrapper::ShowOpenFileDialog() {
@@ -119,8 +203,27 @@ std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
#elif defined(__linux__)
#include <nfd.h>
std::string FileDialogWrapper::ShowOpenFileDialog() {
return "Linux: Open file dialog";
NFD_Init();
nfdu8char_t *out_path = NULL;
nfdu8filter_item_t filters[1] = {{ "Rom File", "sfc,smc" } };
nfdopendialogu8args_t args = { 0 };
args.filterList = filters;
args.filterCount = 1;
nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args);
if (result == NFD_OKAY) {
std::string file_path_linux(out_path);
NFD_Free(out_path);
NFD_Quit();
return file_path_linux;
} else if (result == NFD_CANCEL) {
NFD_Quit();
return "";
}
NFD_Quit();
return "Error: NFD_OpenDialog";
}
std::string FileDialogWrapper::ShowOpenFolderDialog() {

View File

@@ -21,11 +21,28 @@ class FileDialogWrapper {
*/
static std::string ShowOpenFolderDialog();
static std::vector<std::string> GetSubdirectoriesInFolder(
const std::string& folder_path);
const std::string &folder_path);
static std::vector<std::string> GetFilesInFolder(
const std::string& folder_path);
const std::string &folder_path);
};
/**
* @brief GetBundleResourcePath returns the path to the bundle resource
* directory. Specific to MacOS.
*/
std::string GetBundleResourcePath();
enum class Platform { kUnknown, kMacOS, kiOS, kWindows, kLinux };
std::string GetFileExtension(const std::string &filename);
std::string GetFileName(const std::string &filename);
std::string LoadFile(const std::string &filename);
std::string LoadConfigFile(const std::string &filename);
std::string GetConfigDirectory(Platform platform);
void SaveFile(const std::string &filename, const std::string &data,
Platform platform);
} // namespace core
} // namespace yaze

View File

@@ -1,11 +1,12 @@
#include "app/core/platform/file_dialog.h"
#include <iostream>
#include <string>
#include <vector>
#include "imgui/imgui.h"
#if defined(__APPLE__) && defined(__MACH__)
/* Apple OSX and iOS (Darwin). */
#include <Foundation/Foundation.h>
#include <TargetConditionals.h>
#import <CoreText/CoreText.h>
@@ -53,6 +54,13 @@ std::vector<std::string> yaze::core::FileDialogWrapper::GetSubdirectoriesInFolde
return {};
}
std::string yaze::core::GetBundleResourcePath() {
NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
return [path UTF8String];
}
#elif TARGET_OS_MAC == 1
/* macOS */
@@ -125,6 +133,14 @@ std::vector<std::string> yaze::core::FileDialogWrapper::GetSubdirectoriesInFolde
}
return subdirectories;
}
std::string yaze::core::GetBundleResourcePath() {
NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
return [path UTF8String];
}
#else
// Unsupported platform
#endif // TARGET_OS_MAC

View File

@@ -1,18 +0,0 @@
#ifndef YAZE_APP_CORE_PLATFORM_FILE_PATH_H
#define YAZE_APP_CORE_PLATFORM_FILE_PATH_H
#include <string>
namespace yaze {
namespace core {
/**
* @brief GetBundleResourcePath returns the path to the bundle resource
* directory. Specific to MacOS.
*/
std::string GetBundleResourcePath();
} // namespace core
} // namespace yaze
#endif // YAZE_APP_CORE_PLATFORM_FILE_PATH_H

View File

@@ -1,26 +0,0 @@
#include "file_path.h"
#include <iostream>
#include <string>
#if defined(__APPLE__) && defined(__MACH__)
#include <Foundation/Foundation.h>
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
std::string yaze::core::GetBundleResourcePath() {
NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
return [path UTF8String];
}
#elif TARGET_OS_MAC == 1
std::string yaze::core::GetBundleResourcePath() {
NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString:@"/"];
return [path UTF8String];
}
#endif
#endif

View File

@@ -9,7 +9,7 @@
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "app/core/platform/file_path.h"
#include "app/core/platform/file_dialog.h"
#include "app/gui/icons.h"
#include "imgui/imgui.h"

View File

@@ -8,7 +8,7 @@
#include "absl/status/status.h"
#include "app/core/common.h"
#include "app/core/utils/file_util.h"
#include "app/core/platform/file_dialog.h"
namespace yaze {

View File

@@ -1,95 +0,0 @@
#include "file_util.h"
#if defined(_WIN32)
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#endif
#include <fstream>
#include <sstream>
namespace yaze {
namespace core {
std::string GetFileExtension(const std::string &filename) {
size_t dot = filename.find_last_of(".");
if (dot == std::string::npos) {
return "";
}
return filename.substr(dot + 1);
}
std::string GetFileName(const std::string &filename) {
size_t slash = filename.find_last_of("/");
if (slash == std::string::npos) {
return filename;
}
return filename.substr(slash + 1);
}
std::string LoadFile(const std::string &filename) {
std::string contents;
std::ifstream file(filename);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
contents = buffer.str();
file.close();
} else {
// Throw an exception
throw std::runtime_error("Could not open file: " + filename);
}
return contents;
}
std::string LoadConfigFile(const std::string &filename) {
std::string contents;
Platform platform;
#if defined(_WIN32)
platform = Platform::kWindows;
#elif defined(__APPLE__)
platform = Platform::kMacOS;
#else
platform = Platform::kLinux;
#endif
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ifstream file(filepath);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
contents = buffer.str();
file.close();
}
return contents;
}
void SaveFile(const std::string &filename, const std::string &contents,
Platform platform) {
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ofstream file(filepath);
if (file.is_open()) {
file << contents;
file.close();
}
}
std::string GetConfigDirectory(Platform platform) {
std::string config_directory = ".yaze";
switch (platform) {
case Platform::kWindows:
config_directory = "~/AppData/Roaming/yaze";
break;
case Platform::kMacOS:
case Platform::kLinux:
config_directory = "~/.config/yaze";
break;
default:
break;
}
return config_directory;
}
} // namespace core
} // namespace yaze

View File

@@ -1,23 +0,0 @@
#ifndef YAZE_APP_CORE_UTILS_FILE_UTIL_H
#define YAZE_APP_CORE_UTILS_FILE_UTIL_H
#include <string>
namespace yaze {
namespace core {
enum class Platform { kUnknown, kMacOS, kiOS, kWindows, kLinux };
std::string GetFileExtension(const std::string &filename);
std::string GetFileName(const std::string &filename);
std::string LoadFile(const std::string &filename);
std::string LoadConfigFile(const std::string &filename);
std::string GetConfigDirectory(Platform platform);
void SaveFile(const std::string &filename, const std::string &data,
Platform platform);
} // namespace core
} // namespace yaze
#endif // YAZE_APP_CORE_UTILS_FILE_UTIL_H

View File

@@ -1,6 +1,6 @@
#include "style.h"
#include "app/core/utils/file_util.h"
#include "app/core/platform/file_dialog.h"
#include "gui/color.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"

View File

@@ -9,7 +9,7 @@
#include <string>
#include <vector>
#include "app/core/platform/file_path.h"
#include "app/core/platform/file_dialog.h"
#include "app/gui/canvas.h"
#include "app/gui/input.h"
#include "imgui/imgui.h"

View File

@@ -21,8 +21,7 @@ add_executable(
app/rom.cc
app/core/common.cc
app/core/project.cc
app/core/platform/file_path.mm
app/core/utils/file_util.cc
app/core/platform/file_dialog.mm
${YAZE_APP_EMU_SRC}
${YAZE_APP_GFX_SRC}
${YAZE_APP_ZELDA3_SRC}