2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Result.hpp"
|
2022-11-28 12:09:39 -05:00
|
|
|
#include "general.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-12-06 14:22:03 -05:00
|
|
|
#include "../external/json/json.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <Geode/DefaultInclude.hpp>
|
2022-10-05 08:41:05 -04:00
|
|
|
#include <fs/filesystem.hpp>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <string>
|
2022-11-28 12:09:39 -05:00
|
|
|
#include <unordered_set>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-09-26 06:53:40 -04:00
|
|
|
namespace geode::utils::file {
|
2022-10-30 14:59:20 -04:00
|
|
|
GEODE_DLL Result<std::string> readString(ghc::filesystem::path const& path);
|
2022-12-06 14:22:03 -05:00
|
|
|
GEODE_DLL Result<nlohmann::json> readJson(ghc::filesystem::path const& path);
|
2022-10-30 14:59:20 -04:00
|
|
|
GEODE_DLL Result<byte_array> readBinary(ghc::filesystem::path const& path);
|
2022-07-30 12:24:03 -04:00
|
|
|
|
|
|
|
GEODE_DLL Result<> writeString(ghc::filesystem::path const& path, std::string const& data);
|
2022-10-30 14:59:20 -04:00
|
|
|
GEODE_DLL Result<> writeBinary(ghc::filesystem::path const& path, byte_array const& data);
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-12-01 15:44:44 -05:00
|
|
|
GEODE_DLL Result<> createDirectory(ghc::filesystem::path const& path);
|
|
|
|
GEODE_DLL Result<> createDirectoryAll(ghc::filesystem::path const& path);
|
2022-12-08 06:35:02 -05:00
|
|
|
GEODE_DLL Result<std::vector<ghc::filesystem::path>> listFiles(
|
|
|
|
ghc::filesystem::path const& path, bool recursive = false
|
|
|
|
);
|
2022-10-05 08:41:05 -04:00
|
|
|
|
2022-11-29 17:48:06 -05:00
|
|
|
class UnzipImpl;
|
|
|
|
|
|
|
|
class GEODE_DLL Unzip final {
|
|
|
|
private:
|
|
|
|
UnzipImpl* m_impl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Unzip() = delete;
|
|
|
|
Unzip(Unzip const&) = delete;
|
|
|
|
Unzip(Unzip&& other);
|
|
|
|
Unzip(UnzipImpl* impl);
|
|
|
|
~Unzip();
|
|
|
|
|
|
|
|
using Path = ghc::filesystem::path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create unzipper for file
|
|
|
|
*/
|
|
|
|
static Result<Unzip> create(Path const& file);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to the opened zip
|
|
|
|
*/
|
|
|
|
Path getPath() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all entries in zip
|
|
|
|
*/
|
|
|
|
std::vector<Path> getEntries() const;
|
|
|
|
/**
|
|
|
|
* Check if zip has entry
|
|
|
|
* @param name Entry path in zip
|
|
|
|
*/
|
|
|
|
bool hasEntry(Path const& name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract entry to memory
|
|
|
|
* @param name Entry path in zip
|
|
|
|
*/
|
|
|
|
Result<byte_array> extract(Path const& name);
|
|
|
|
/**
|
|
|
|
* Extract entry to file
|
|
|
|
* @param name Entry path in zip
|
|
|
|
* @param path Target file path
|
|
|
|
*/
|
|
|
|
Result<> extractTo(Path const& name, Path const& path);
|
|
|
|
/**
|
|
|
|
* Extract all entries to directory
|
|
|
|
* @param dir Directory to unzip the contents to
|
|
|
|
*/
|
|
|
|
Result<> extractAllTo(Path const& dir);
|
|
|
|
|
2022-12-01 15:42:49 -05:00
|
|
|
/**
|
|
|
|
* Helper method for quickly unzipping a file
|
|
|
|
* @param from ZIP file to unzip
|
|
|
|
* @param to Directory to unzip to
|
|
|
|
* @param deleteZipAfter Whether to delete the zip after unzipping
|
|
|
|
* @returns Succesful result on success, errorful result on error
|
|
|
|
*/
|
|
|
|
static Result<> intoDir(
|
|
|
|
Path const& from,
|
|
|
|
Path const& to,
|
|
|
|
bool deleteZipAfter = false
|
|
|
|
);
|
|
|
|
};
|
2022-11-28 12:09:39 -05:00
|
|
|
|
|
|
|
GEODE_DLL bool openFolder(ghc::filesystem::path const& path);
|
|
|
|
|
|
|
|
enum class PickMode {
|
|
|
|
OpenFile,
|
|
|
|
SaveFile,
|
|
|
|
OpenFolder,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FilePickOptions {
|
|
|
|
struct Filter {
|
|
|
|
// Name of the filter
|
|
|
|
std::string description;
|
|
|
|
// Extensions (*.txt, *.doc, *.mp3, etc.)
|
|
|
|
std::unordered_set<std::string> files;
|
|
|
|
};
|
|
|
|
|
|
|
|
ghc::filesystem::path defaultPath;
|
|
|
|
std::vector<Filter> filters;
|
|
|
|
};
|
|
|
|
|
|
|
|
GEODE_DLL Result<ghc::filesystem::path> pickFile(PickMode mode, FilePickOptions const& options);
|
|
|
|
GEODE_DLL Result<std::vector<ghc::filesystem::path>> pickFiles(FilePickOptions const& options);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|