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,93 +183,82 @@ 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 {
std::string method;
switch (mode) {
case file::PickMode::OpenFile:
method = "selectFile";
break;
case file::PickMode::SaveFile:
method = "createFile";
break;
case file::PickMode::OpenFolder:
method = "selectFolder";
break;
}
GEODE_DLL Task<Result<std::filesystem::path>> pick(PickMode mode, FilePickOptions const& options) { JniMethodInfo t;
using RetTask = Task<Result<std::filesystem::path>> if (JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", method.c_str(), "(Ljava/lang/String;)Z")) {
return RetTask::runWithCallback([] (auto resultCallback, auto progressCallback, auto cancelCallback) { jstring stringArg1 = t.env->NewStringUTF(options.defaultPath.value_or(std::filesystem::path()).filename().string().c_str());
jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
if (result) {
return Err("Failed to open file picker");
}
}
// Time for while loop :D
while (!s_fileResult.has_value()) {
if (cancelled()) {
return RetTask::Cancel();
}
}
Result<std::filesystem::path> result = s_fileResult.value();
s_fileResult = std::nullopt;
return result;
}); });
} }
void file::pickFile( Task<Result<std::vector<std::filesystem::path>>> file::pickMany(FilePickOptions const& options) {
PickMode mode, FilePickOptions const& options, using RetTask = Task<Result<std::vector<std::filesystem::path>>>;
MiniFunction<void(std::filesystem::path)> callback, s_filesResult = std::nullopt;
MiniFunction<void()> failed
) {
s_fileCallback = callback;
s_failedCallback = failed;
std::string method; return RetTask::run([options](auto progress, auto cancelled) -> RetTask::Result {
switch (mode) { JniMethodInfo t;
case file::PickMode::OpenFile: if (JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", "selectFiles", "(Ljava/lang/String;)Z")) {
method = "selectFile"; jstring stringArg1 = t.env->NewStringUTF(options.defaultPath.value_or(std::filesystem::path()).string().c_str());
break;
case file::PickMode::SaveFile:
method = "createFile";
break;
case file::PickMode::OpenFolder:
method = "selectFolder";
break;
}
JniMethodInfo t; jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1);
if (JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", method.c_str(), "(Ljava/lang/String;)Z")) {
jstring stringArg1 = t.env->NewStringUTF(options.defaultPath.value_or(std::filesystem::path()).filename().string().c_str());
jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1); t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1); if (result) {
t.env->DeleteLocalRef(t.classID); return Err("Failed to open file dialog");
if (result) { }
return;
} }
}
if (s_failedCallback) {
Loader::get()->queueInMainThread([]() {
s_failedCallback();
});
}
}
Result<std::vector<std::filesystem::path>> file::pickFiles(file::FilePickOptions const& options) { // Time for while loop :D
return Err("Use the callback version"); while (!s_fileResult.has_value()) {
} if (cancelled()) {
return RetTask::Cancel();
void file::pickFiles( }
FilePickOptions const& options,
MiniFunction<void(std::vector<std::filesystem::path>)> callback,
MiniFunction<void()> failed
) {
s_filesCallback = callback;
s_failedCallback = failed;
JniMethodInfo t;
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());
jboolean result = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
if (result) {
return;
} }
}
if (s_failedCallback) { Result<std::vector<std::filesystem::path>> result = s_filesResult.value();
Loader::get()->queueInMainThread([]() { s_filesResult = std::nullopt;
s_failedCallback(); return result;
}); });
}
} }
void geode::utils::game::launchLoaderUninstaller(bool deleteSaveData) { void geode::utils::game::launchLoaderUninstaller(bool deleteSaveData) {

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(
[this](Result<std::filesystem::path>* path) {
if (path->isOk()) {
m_uncommittedValue = path->unwrap();
this->valueChanged(true);
}
}, },
[&](auto path) { [] (auto progress) {},
m_uncommittedValue = path; [] () {}
this->valueChanged(true);
}
); );
} }