finish android file utils

This commit is contained in:
Fleeym 2024-06-13 03:53:06 +03:00
parent 9d9c03f0f6
commit 37b1ae2ee6
3 changed files with 95 additions and 120 deletions

View file

@ -252,35 +252,17 @@ namespace geode::utils::file {
std::vector<Filter> filters; std::vector<Filter> filters;
}; };
// /** /**
// * Prompt the user to pick a file using the system's file system picker * Prompt the user to pick a file using the system's file system picker
// * @deprecated Will not work on Android, use the callback version instead * @param mode Type of file selection prompt to show
// * @param mode Type of file selection prompt to show * @param options Picker options
// * @param options Picker options */
// */
// [[deprecated("Use pick() instead, this will be removed in a later version.")]]
// GEODE_DLL Result<std::filesystem::path> pickFile(PickMode mode, FilePickOptions const& options);
// GEODE_DLL void pickFile(
// PickMode mode, FilePickOptions const& options,
// utils::MiniFunction<void(std::filesystem::path)> callback,
// utils::MiniFunction<void()> failed = {}
// );
GEODE_DLL Task<Result<std::filesystem::path>> pick(PickMode mode, FilePickOptions const& options); GEODE_DLL Task<Result<std::filesystem::path>> pick(PickMode mode, FilePickOptions const& options);
// /** /**
// * Prompt the user to pick a bunch of files for opening using the system's file system picker * Prompt the user to pick a bunch of files for opening using the system's file system picker
// * @deprecated Will not work on Android, use the callback version instead * @param options Picker options
// * @param options Picker options */
// */
// [[deprecated("Use pickMany() instead, this will be removed in a later version.")]]
// GEODE_DLL Result<std::vector<std::filesystem::path>> pickFiles(FilePickOptions const& options);
// GEODE_DLL void pickFiles(
// FilePickOptions const& options,
// utils::MiniFunction<void(std::vector<std::filesystem::path>)> callback,
// utils::MiniFunction<void()> failed = {}
// );
GEODE_DLL Task<Result<std::vector<std::filesystem::path>>> pickMany(FilePickOptions const& options); GEODE_DLL Task<Result<std::vector<std::filesystem::path>>> pickMany(FilePickOptions const& options);
class GEODE_DLL FileWatchEvent : public Event { class GEODE_DLL FileWatchEvent : public Event {

View file

@ -1,4 +1,4 @@
#include <Geode/DefaultInclude.hpp> #include <Geode/utils/file.hpp>
using namespace geode::prelude; using namespace geode::prelude;
@ -6,7 +6,6 @@ using namespace geode::prelude;
#include <Geode/loader/Dirs.hpp> #include <Geode/loader/Dirs.hpp>
#include <Geode/utils/web.hpp> #include <Geode/utils/web.hpp>
#include <filesystem> #include <filesystem>
#include <Geode/utils/file.hpp>
#include <Geode/utils/general.hpp> #include <Geode/utils/general.hpp>
#include <Geode/utils/MiniFunction.hpp> #include <Geode/utils/MiniFunction.hpp>
#include <Geode/utils/permission.hpp> #include <Geode/utils/permission.hpp>
@ -15,6 +14,9 @@ using namespace geode::prelude;
#include <Geode/binding/AppDelegate.hpp> #include <Geode/binding/AppDelegate.hpp>
#include <Geode/loader/Log.hpp> #include <Geode/loader/Log.hpp>
#include <Geode/binding/MenuLayer.hpp> #include <Geode/binding/MenuLayer.hpp>
#include <Geode/utils/Result.hpp>
#include <Geode/DefaultInclude.hpp>
#include <optional>
#include <jni.h> #include <jni.h>
#include <Geode/cocos/platform/android/jni/JniHelper.h> #include <Geode/cocos/platform/android/jni/JniHelper.h>
@ -137,9 +139,11 @@ bool utils::file::openFolder(std::filesystem::path const& path) {
} }
static utils::MiniFunction<void(std::filesystem::path)> s_fileCallback; // static utils::MiniFunction<void(std::filesystem::path)> s_fileCallback;
static utils::MiniFunction<void(std::vector<std::filesystem::path>)> s_filesCallback; // static utils::MiniFunction<void(std::vector<std::filesystem::path>)> s_filesCallback;
static utils::MiniFunction<void()> s_failedCallback; // static utils::MiniFunction<void()> s_failedCallback;
static std::optional<Result<std::filesystem::path>> s_fileResult = std::nullopt;
static std::optional<Result<std::vector<std::filesystem::path>>> s_filesResult = std::nullopt;
extern "C" extern "C"
JNIEXPORT void JNICALL Java_com_geode_launcher_utils_GeodeUtils_selectFileCallback( JNIEXPORT void JNICALL Java_com_geode_launcher_utils_GeodeUtils_selectFileCallback(
@ -151,10 +155,7 @@ JNIEXPORT void JNICALL Java_com_geode_launcher_utils_GeodeUtils_selectFileCallba
auto dataStr = env->GetStringUTFChars(data, &isCopy); auto dataStr = env->GetStringUTFChars(data, &isCopy);
log::debug("Selected file: {}", dataStr); log::debug("Selected file: {}", dataStr);
s_fileResult = Ok(std::filesystem::path(dataStr));
Loader::get()->queueInMainThread([dataStr]() {
s_fileCallback(dataStr);
});
} }
extern "C" extern "C"
@ -174,9 +175,7 @@ JNIEXPORT void JNICALL Java_com_geode_launcher_utils_GeodeUtils_selectFilesCallb
log::debug("Selected file {}: {}", i, dataStr); log::debug("Selected file {}: {}", i, dataStr);
} }
Loader::get()->queueInMainThread([result]() { s_filesResult = Ok(std::move(result));
s_filesCallback(result);
});
} }
extern "C" extern "C"
@ -184,32 +183,14 @@ JNIEXPORT void JNICALL Java_com_geode_launcher_utils_GeodeUtils_failedCallback(
JNIEnv *env, JNIEnv *env,
jobject jobject
) { ) {
if (s_failedCallback) { s_fileResult = Err("Permission error");
Loader::get()->queueInMainThread([]() { s_filesResult = Err("Permission error");
s_failedCallback();
});
}
} }
Result<std::filesystem::path> file::pickFile(file::PickMode mode, file::FilePickOptions const& options) { Task<Result<std::filesystem::path>> file::pick(file::PickMode mode, file::FilePickOptions const& options) {
return Err("Use the callback version"); using RetTask = Task<Result<std::filesystem::path>>;
} s_fileResult = std::nullopt;
return RetTask::run([mode, options](auto progress, auto cancelled) -> RetTask::Result {
GEODE_DLL Task<Result<std::filesystem::path>> pick(PickMode mode, FilePickOptions const& options) {
using RetTask = Task<Result<std::filesystem::path>>
return RetTask::runWithCallback([] (auto resultCallback, auto progressCallback, auto cancelCallback) {
});
}
void file::pickFile(
PickMode mode, FilePickOptions const& options,
MiniFunction<void(std::filesystem::path)> callback,
MiniFunction<void()> failed
) {
s_fileCallback = callback;
s_failedCallback = failed;
std::string method; std::string method;
switch (mode) { switch (mode) {
case file::PickMode::OpenFile: case file::PickMode::OpenFile:
@ -232,28 +213,28 @@ void file::pickFile(
t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID); t.env->DeleteLocalRef(t.classID);
if (result) { if (result) {
return; return Err("Failed to open file picker");
} }
} }
if (s_failedCallback) {
Loader::get()->queueInMainThread([]() { // Time for while loop :D
s_failedCallback(); while (!s_fileResult.has_value()) {
if (cancelled()) {
return RetTask::Cancel();
}
}
Result<std::filesystem::path> result = s_fileResult.value();
s_fileResult = std::nullopt;
return result;
}); });
} }
}
Result<std::vector<std::filesystem::path>> file::pickFiles(file::FilePickOptions const& options) { Task<Result<std::vector<std::filesystem::path>>> file::pickMany(FilePickOptions const& options) {
return Err("Use the callback version"); using RetTask = Task<Result<std::vector<std::filesystem::path>>>;
} s_filesResult = std::nullopt;
void file::pickFiles(
FilePickOptions const& options,
MiniFunction<void(std::vector<std::filesystem::path>)> callback,
MiniFunction<void()> failed
) {
s_filesCallback = callback;
s_failedCallback = failed;
return RetTask::run([options](auto progress, auto cancelled) -> RetTask::Result {
JniMethodInfo t; JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", "selectFiles", "(Ljava/lang/String;)Z")) { if (JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", "selectFiles", "(Ljava/lang/String;)Z")) {
jstring stringArg1 = t.env->NewStringUTF(options.defaultPath.value_or(std::filesystem::path()).string().c_str()); jstring stringArg1 = t.env->NewStringUTF(options.defaultPath.value_or(std::filesystem::path()).string().c_str());
@ -263,15 +244,22 @@ void file::pickFiles(
t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID); t.env->DeleteLocalRef(t.classID);
if (result) { if (result) {
return; return Err("Failed to open file dialog");
} }
} }
if (s_failedCallback) {
Loader::get()->queueInMainThread([]() { // Time for while loop :D
s_failedCallback(); while (!s_fileResult.has_value()) {
if (cancelled()) {
return RetTask::Cancel();
}
}
Result<std::vector<std::filesystem::path>> result = s_filesResult.value();
s_filesResult = std::nullopt;
return result;
}); });
} }
}
void geode::utils::game::launchLoaderUninstaller(bool deleteSaveData) { void geode::utils::game::launchLoaderUninstaller(bool deleteSaveData) {
log::error("Launching Geode uninstaller is not supported on android"); log::error("Launching Geode uninstaller is not supported on android");

View file

@ -9,6 +9,7 @@
#include <Geode/loader/Dirs.hpp> #include <Geode/loader/Dirs.hpp>
#include <charconv> #include <charconv>
#include <clocale> #include <clocale>
#include <filesystem>
// Helpers // Helpers
@ -364,16 +365,20 @@ void FileSettingNode::valueChanged(bool updateText) {
} }
void FileSettingNode::onPickFile(CCObject*) { void FileSettingNode::onPickFile(CCObject*) {
file::pickFile( file::pick(
file::PickMode::OpenFile, file::PickMode::OpenFile,
{ {
dirs::getGameDir(), dirs::getGameDir(),
setting()->castDefinition().controls.filters setting()->castDefinition().controls.filters
}, }).then(
[&](auto path) { [this](Result<std::filesystem::path>* path) {
m_uncommittedValue = path; if (path->isOk()) {
m_uncommittedValue = path->unwrap();
this->valueChanged(true); this->valueChanged(true);
} }
},
[] (auto progress) {},
[] () {}
); );
} }