2019-04-13 12:21:56 -04:00
|
|
|
/* Copyright (C) 2019 Mr Goldberg
|
|
|
|
This file is part of the Goldberg Emulator
|
|
|
|
|
|
|
|
The Goldberg Emulator is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Goldberg Emulator is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the Goldberg Emulator; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
2023-12-27 09:21:59 +02:00
|
|
|
#include "dll/local_storage.h"
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2024-09-01 08:17:23 +02:00
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
// NOTE: stb_image_write
|
|
|
|
#define STBIW_WINDOWS_UTF8
|
|
|
|
// NOTE: stb_image
|
|
|
|
#define STBI_WINDOWS_UTF8
|
|
|
|
#endif
|
|
|
|
|
2020-10-09 13:48:09 +02:00
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#define STB_IMAGE_STATIC
|
|
|
|
#define STBI_ONLY_PNG
|
|
|
|
#define STBI_ONLY_JPEG
|
2023-12-27 09:21:59 +02:00
|
|
|
#include "stb/stb_image.h"
|
2020-10-09 13:48:09 +02:00
|
|
|
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#define STB_IMAGE_WRITE_STATIC
|
2023-12-27 09:21:59 +02:00
|
|
|
#include "stb/stb_image_write.h"
|
2020-10-09 13:48:09 +02:00
|
|
|
|
2023-08-21 06:58:55 +03:00
|
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
2024-09-01 08:17:23 +02:00
|
|
|
#define STB_IMAGE_RESIZE_STATIC
|
2024-08-18 11:05:49 +02:00
|
|
|
#include "stb/stb_image_resize2.h"
|
2023-08-21 06:58:55 +03:00
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
struct File_Data {
|
2024-04-16 22:58:05 +02:00
|
|
|
std::string name{};
|
2019-05-23 09:35:12 -04:00
|
|
|
};
|
|
|
|
|
2024-04-16 22:58:05 +02:00
|
|
|
|
2024-04-20 01:10:40 +02:00
|
|
|
std::string Local_Storage::saves_folder_name = "GSE Saves";
|
2024-04-16 22:58:05 +02:00
|
|
|
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
#ifdef NO_DISK_WRITES
|
2024-04-23 02:02:36 +02:00
|
|
|
|
|
|
|
static const std::string empty_str{};
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
std::string Local_Storage::get_program_path()
|
|
|
|
{
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:58:05 +02:00
|
|
|
std::string Local_Storage::get_game_settings_path()
|
|
|
|
{
|
|
|
|
return " ";
|
|
|
|
}
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
std::string Local_Storage::get_user_appdata_path()
|
|
|
|
{
|
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:58:05 +02:00
|
|
|
int Local_Storage::get_file_data(const std::string &full_path, char *data, unsigned int max_length, unsigned int offset)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2024-04-16 22:58:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Local_Storage::store_file_data(std::string folder, std::string file, const char *data, unsigned int length)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Local_Storage::get_filenames_path(std::string path)
|
|
|
|
{
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Local_Storage::get_folders_path(std::string path)
|
|
|
|
{
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:13:43 +03:00
|
|
|
void Local_Storage::set_saves_folder_name(std::string_view str)
|
2024-04-16 22:58:05 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Local_Storage::get_saves_folder_name()
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::get_path(std::string folder)
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-03 09:09:06 -04:00
|
|
|
std::string Local_Storage::get_global_settings_path()
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2019-05-03 09:09:06 -04:00
|
|
|
}
|
|
|
|
|
2024-03-03 03:32:17 +02:00
|
|
|
Local_Storage::Local_Storage(const std::string &save_directory)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-23 02:02:36 +02:00
|
|
|
const std::string& Local_Storage::get_current_save_directory() const
|
|
|
|
{
|
|
|
|
return empty_str;
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
void Local_Storage::setAppId(uint32 appid)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int Local_Storage::store_data(std::string folder, std::string file, char *data, unsigned int length)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:32:17 +02:00
|
|
|
int Local_Storage::store_data_settings(std::string file, const char *data, unsigned int length)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-07-31 10:25:05 -04:00
|
|
|
int Local_Storage::get_data(std::string folder, std::string file, char *data, unsigned int max_length, unsigned int offset)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-04-23 02:02:36 +02:00
|
|
|
unsigned int Local_Storage::data_settings_size(std::string file)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
int Local_Storage::get_data_settings(std::string file, char *data, unsigned int max_length)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Local_Storage::count_files(std::string folder)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::file_exists(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Local_Storage::file_size(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::file_delete(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Local_Storage::file_timestamp(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::iterate_file(std::string folder, int index, char *output_filename, int32 *output_size)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::update_save_filenames(std::string folder)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-03 01:22:00 +03:00
|
|
|
bool Local_Storage::load_json(const std::string &full_path, nlohmann::json& json)
|
2019-10-19 12:02:30 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:19:18 +00:00
|
|
|
bool Local_Storage::load_json_file(std::string folder, std::string const&file, nlohmann::json& json)
|
2019-08-24 17:42:39 +02:00
|
|
|
{
|
2019-10-19 12:02:30 -04:00
|
|
|
return false;
|
2019-08-24 17:42:39 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:19:18 +00:00
|
|
|
bool Local_Storage::write_json_file(std::string folder, std::string const&file, nlohmann::json const& json)
|
2019-08-24 17:42:39 +02:00
|
|
|
{
|
2019-10-19 12:02:30 -04:00
|
|
|
return false;
|
2019-08-24 17:42:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 13:48:09 +02:00
|
|
|
std::vector<image_pixel_t> Local_Storage::load_image(std::string const& image_path)
|
|
|
|
{
|
|
|
|
return std::vector<image_pixel_t>();
|
|
|
|
}
|
|
|
|
|
2023-08-21 06:58:55 +03:00
|
|
|
std::string Local_Storage::load_image_resized(std::string const& image_path, std::string const& image_data, int resolution)
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2023-08-21 06:58:55 +03:00
|
|
|
}
|
|
|
|
|
2020-10-09 13:48:09 +02:00
|
|
|
bool Local_Storage::save_screenshot(std::string const& image_path, uint8_t* img_ptr, int32_t width, int32_t height, int32_t channels)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-29 23:48:51 +02:00
|
|
|
std::string Local_Storage::sanitize_string(std::string name)
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2023-10-29 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::desanitize_string(std::string name)
|
|
|
|
{
|
2024-04-23 02:02:36 +02:00
|
|
|
return empty_str;
|
2023-10-29 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
#else
|
2024-01-20 17:50:16 +02:00
|
|
|
|
2020-07-16 11:56:24 +02:00
|
|
|
#if defined(__WINDOWS__)
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static BOOL DirectoryExists(LPCWSTR szPath)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
DWORD dwAttrib = GetFileAttributesW(szPath);
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static void createDirectoryRecursively(std::wstring path)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2024-06-04 14:58:16 +03:00
|
|
|
size_t pos = 0;
|
2019-04-13 12:21:56 -04:00
|
|
|
do
|
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
pos = path.find_first_of(L"\\/", pos + 1);
|
|
|
|
CreateDirectoryW(path.substr(0, pos).c_str(), NULL);
|
2019-04-13 12:21:56 -04:00
|
|
|
} while (pos != std::string::npos);
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static void create_directory(std::string in_path)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
std::wstring strPath = utf8_decode(in_path);
|
2019-04-13 12:21:56 -04:00
|
|
|
if (DirectoryExists(strPath.c_str()) == FALSE)
|
|
|
|
createDirectoryRecursively(strPath);
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static std::vector<struct File_Data> get_filenames(std::string in_path)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> output;
|
2021-04-25 12:44:41 -04:00
|
|
|
in_path = in_path.append("\\*");
|
|
|
|
WIN32_FIND_DATAW ffd;
|
2019-04-13 12:21:56 -04:00
|
|
|
HANDLE hFind = INVALID_HANDLE_VALUE;
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
std::wstring strPath = utf8_decode(in_path);
|
2019-04-13 12:21:56 -04:00
|
|
|
// Start iterating over the files in the path directory.
|
2021-04-25 12:44:41 -04:00
|
|
|
hFind = ::FindFirstFileW (strPath.c_str(), &ffd);
|
2019-04-13 12:21:56 -04:00
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
do // Managed to locate and create an handle to that folder.
|
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
if (wcscmp(L".", ffd.cFileName) == 0) continue;
|
|
|
|
if (wcscmp(L"..", ffd.cFileName) == 0) continue;
|
2019-05-23 09:35:12 -04:00
|
|
|
struct File_Data f_data;
|
2021-04-25 12:44:41 -04:00
|
|
|
f_data.name = utf8_encode(ffd.cFileName);
|
2019-05-23 09:35:12 -04:00
|
|
|
output.push_back(f_data);
|
2021-04-25 12:44:41 -04:00
|
|
|
} while (::FindNextFileW(hFind, &ffd) == TRUE);
|
2019-04-13 12:21:56 -04:00
|
|
|
::FindClose(hFind);
|
|
|
|
} else {
|
2019-05-23 09:35:12 -04:00
|
|
|
//printf("Failed to find path: %s", strPath.c_str());
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
return output;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static std::vector<struct File_Data> get_filenames_recursive_w(std::wstring base_path)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
if (base_path.back() == *L"\\")
|
2019-06-17 23:08:23 +02:00
|
|
|
base_path.pop_back();
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> output;
|
2021-04-25 12:44:41 -04:00
|
|
|
std::wstring strPath = base_path;
|
|
|
|
strPath = strPath.append(L"\\*");
|
|
|
|
WIN32_FIND_DATAW ffd;
|
2019-04-13 12:21:56 -04:00
|
|
|
HANDLE hFind = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
// Start iterating over the files in the path directory.
|
2021-04-25 12:44:41 -04:00
|
|
|
hFind = ::FindFirstFileW (strPath.c_str(), &ffd);
|
2019-04-13 12:21:56 -04:00
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
do // Managed to locate and create an handle to that folder.
|
|
|
|
{
|
2021-04-25 12:44:41 -04:00
|
|
|
if (wcscmp(L".", ffd.cFileName) == 0) continue;
|
|
|
|
if (wcscmp(L"..", ffd.cFileName) == 0) continue;
|
2019-05-23 09:35:12 -04:00
|
|
|
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
// Construct new path from our base path
|
2021-04-25 12:44:41 -04:00
|
|
|
std::wstring dir_name = ffd.cFileName;
|
2019-05-23 09:35:12 -04:00
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
std::wstring path = base_path;
|
|
|
|
path += L"\\";
|
2019-05-23 09:35:12 -04:00
|
|
|
path += dir_name;
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
std::vector<struct File_Data> lower = get_filenames_recursive_w(path);
|
|
|
|
std::transform(lower.begin(), lower.end(), std::back_inserter(output), [&dir_name](File_Data f) {f.name = utf8_encode(dir_name) + "\\" + f.name; return f;});
|
2019-05-23 09:35:12 -04:00
|
|
|
} else {
|
|
|
|
File_Data f;
|
2021-04-25 12:44:41 -04:00
|
|
|
f.name = utf8_encode(ffd.cFileName);
|
2019-05-23 09:35:12 -04:00
|
|
|
output.push_back(f);
|
|
|
|
}
|
2021-04-25 12:44:41 -04:00
|
|
|
} while (::FindNextFileW(hFind, &ffd) == TRUE);
|
2019-04-13 12:21:56 -04:00
|
|
|
::FindClose(hFind);
|
|
|
|
} else {
|
2019-05-23 09:35:12 -04:00
|
|
|
//printf("Failed to find path: %s", strPath.c_str());
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2022-08-11 20:52:02 -04:00
|
|
|
reset_LastError();
|
2019-04-13 12:21:56 -04:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
static std::vector<struct File_Data> get_filenames_recursive(std::string base_path)
|
|
|
|
{
|
|
|
|
return get_filenames_recursive_w(utf8_decode(base_path));
|
|
|
|
}
|
|
|
|
|
2020-07-16 11:56:24 +02:00
|
|
|
#else
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
/* recursive mkdir */
|
|
|
|
static int mkdir_p(const char *dir, const mode_t mode) {
|
|
|
|
char tmp[PATH_MAX_STRING_SIZE];
|
|
|
|
char *p = NULL;
|
|
|
|
struct stat sb;
|
|
|
|
size_t len;
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
/* copy path */
|
|
|
|
len = strnlen (dir, PATH_MAX_STRING_SIZE);
|
|
|
|
if (len == 0 || len == PATH_MAX_STRING_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy (tmp, dir, len);
|
|
|
|
tmp[len] = '\0';
|
|
|
|
|
|
|
|
/* remove trailing slash */
|
|
|
|
if(tmp[len - 1] == '/') {
|
|
|
|
tmp[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if path exists and is a directory */
|
|
|
|
if (stat (tmp, &sb) == 0) {
|
|
|
|
if (S_ISDIR (sb.st_mode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
/* recursive mkdir */
|
|
|
|
for(p = tmp + 1; *p; p++) {
|
|
|
|
if(*p == '/') {
|
|
|
|
*p = 0;
|
|
|
|
/* test path */
|
|
|
|
if (stat(tmp, &sb) != 0) {
|
|
|
|
/* path does not exist - create directory */
|
|
|
|
if (mkdir(tmp, mode) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
/* not a directory */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* test path */
|
|
|
|
if (stat(tmp, &sb) != 0) {
|
|
|
|
/* path does not exist - create directory */
|
|
|
|
if (mkdir(tmp, mode) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
/* not a directory */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void create_directory(std::string strPath)
|
|
|
|
{
|
|
|
|
mkdir_p(strPath.c_str(), 0777);
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
static std::vector<struct File_Data> get_filenames(std::string strPath)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
int i = 0;
|
2019-05-23 09:35:12 -04:00
|
|
|
struct dirent *ep;
|
|
|
|
std::vector<struct File_Data> output;
|
2019-04-13 12:21:56 -04:00
|
|
|
dp = opendir (strPath.c_str());
|
|
|
|
|
|
|
|
if (dp != NULL)
|
|
|
|
{
|
|
|
|
while ((ep = readdir (dp))) {
|
|
|
|
if (memcmp(ep->d_name, ".", 2) != 0 && memcmp(ep->d_name, "..", 3) != 0) {
|
2019-05-23 09:35:12 -04:00
|
|
|
struct File_Data f_data;
|
|
|
|
f_data.name = ep->d_name;
|
|
|
|
output.push_back(f_data);
|
2019-04-13 12:21:56 -04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) closedir (dp);
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
return output;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
static std::vector<struct File_Data> get_filenames_recursive(std::string base_path)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> output;
|
|
|
|
std::string path;
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dir = opendir(base_path.c_str());
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
// Unable to open directory stream
|
|
|
|
if (!dir)
|
|
|
|
return output;
|
|
|
|
|
|
|
|
while ((dp = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
|
|
|
|
{
|
|
|
|
if (dp->d_type == DT_REG) {
|
|
|
|
File_Data f;
|
|
|
|
f.name = dp->d_name;
|
|
|
|
output.push_back(f);
|
|
|
|
} else if (dp->d_type == DT_DIR) {
|
|
|
|
// Construct new path from our base path
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string dir_name(dp->d_name);
|
2019-05-23 09:35:12 -04:00
|
|
|
|
|
|
|
path = base_path;
|
|
|
|
path += "/";
|
|
|
|
path += dir_name;
|
|
|
|
|
|
|
|
std::vector<struct File_Data> lower = get_filenames_recursive(path);
|
2019-06-19 18:39:14 +02:00
|
|
|
std::transform(lower.begin(), lower.end(), std::back_inserter(output), [&dir_name](File_Data f) {f.name = dir_name + "/" + f.name; return f;});
|
2019-05-23 09:35:12 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
closedir(dir);
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
return output;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
|
2024-08-18 11:05:49 +02:00
|
|
|
#endif
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
std::string Local_Storage::get_program_path()
|
|
|
|
{
|
|
|
|
return get_full_program_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::get_game_settings_path()
|
|
|
|
{
|
2019-08-24 17:42:39 +02:00
|
|
|
return get_program_path().append(game_settings_folder).append(PATH_SEPARATOR);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::get_user_appdata_path()
|
|
|
|
{
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string user_appdata_path("SAVE");
|
2019-04-13 12:21:56 -04:00
|
|
|
#if defined(STEAM_WIN32)
|
2024-11-21 20:09:55 +01:00
|
|
|
WCHAR szPath[MAX_PATH] = {};
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
HRESULT hr = SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, szPath);
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
2021-04-25 12:44:41 -04:00
|
|
|
user_appdata_path = utf8_encode(szPath);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2024-08-18 11:05:49 +02:00
|
|
|
/* $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored.
|
2019-04-15 19:25:54 +03:00
|
|
|
If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used. */
|
|
|
|
char *datadir = getenv("XDG_DATA_HOME");
|
|
|
|
if (datadir) {
|
|
|
|
user_appdata_path = datadir;
|
|
|
|
} else {
|
|
|
|
char *homedir = getenv("HOME");
|
|
|
|
if (homedir) {
|
2019-04-16 07:46:40 +03:00
|
|
|
user_appdata_path = std::string(homedir) + "/.local/share";
|
2019-04-15 19:25:54 +03:00
|
|
|
}
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
#endif
|
2024-04-16 22:58:05 +02:00
|
|
|
return user_appdata_path.append(PATH_SEPARATOR).append(get_saves_folder_name()).append(PATH_SEPARATOR);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 08:45:09 +00:00
|
|
|
static std::string replace_with(std::string s, std::string const &old, const char *new_str)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2024-06-07 19:55:30 +03:00
|
|
|
size_t pos{};
|
2019-04-13 12:21:56 -04:00
|
|
|
while ((pos = s.find(old)) != std::string::npos)
|
|
|
|
s.replace(pos, old.length(), new_str);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string sanitize_file_name(std::string name)
|
|
|
|
{
|
2023-10-29 23:48:51 +02:00
|
|
|
if (name.empty()) return name;
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
//I'm not sure all of these are necessary but just to be sure
|
|
|
|
if (name[0] == '.' && name.size() > 2 && (name[1] == '\\' || name[1] == '/')) name.erase(0, 2);
|
2019-05-23 09:35:12 -04:00
|
|
|
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
name = replace_with(name, "/", PATH_SEPARATOR);
|
|
|
|
#else
|
|
|
|
//On linux does using "\\" in a remote storage file name create a directory?
|
|
|
|
//I didn't test but I'm going to say yes
|
|
|
|
name = replace_with(name, "\\", PATH_SEPARATOR);
|
|
|
|
#endif
|
2019-04-13 12:21:56 -04:00
|
|
|
name = replace_with(name, "|", ".V_SLASH.");
|
|
|
|
name = replace_with(name, ":", ".COLON.");
|
|
|
|
name = replace_with(name, "*", ".ASTERISK.");
|
|
|
|
name = replace_with(name, "\"", ".QUOTE.");
|
|
|
|
name = replace_with(name, "?", ".Q_MARK.");
|
2024-11-21 20:09:55 +01:00
|
|
|
name = replace_with(name, "%", ".PERCENT.");
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string desanitize_file_name(std::string name)
|
|
|
|
{
|
2023-10-29 23:48:51 +02:00
|
|
|
if (name.empty()) return name;
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
//I'm not sure all of these are necessary but just to be sure
|
|
|
|
name = replace_with(name, ".SLASH.", "/");
|
|
|
|
name = replace_with(name, ".B_SLASH.", "\\");
|
|
|
|
name = replace_with(name, ".F_SLASH.", "/");
|
|
|
|
name = replace_with(name, ".V_SLASH.", "|");
|
|
|
|
name = replace_with(name, ".COLON.", ":");
|
|
|
|
name = replace_with(name, ".ASTERISK.", "*");
|
|
|
|
name = replace_with(name, ".QUOTE.", "\"");
|
|
|
|
name = replace_with(name, ".Q_MARK.", "?");
|
2024-11-21 20:09:55 +01:00
|
|
|
name = replace_with(name, ".PERCENT.", "%");
|
2019-04-13 12:21:56 -04:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:32:17 +02:00
|
|
|
Local_Storage::Local_Storage(const std::string &save_directory)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
this->save_directory = save_directory;
|
|
|
|
|
|
|
|
if (this->save_directory.back() != *PATH_SEPARATOR) {
|
|
|
|
this->save_directory.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 02:02:36 +02:00
|
|
|
const std::string& Local_Storage::get_current_save_directory() const
|
|
|
|
{
|
|
|
|
return this->save_directory;
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
void Local_Storage::setAppId(uint32 appid)
|
|
|
|
{
|
|
|
|
this->appid = std::to_string(appid) + PATH_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:32:17 +02:00
|
|
|
int Local_Storage::store_file_data(std::string folder, std::string file, const char *data, unsigned int length)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
if (folder.back() != *PATH_SEPARATOR) {
|
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
file = sanitize_file_name(file);
|
2019-05-23 09:35:12 -04:00
|
|
|
std::string::size_type pos = file.rfind(PATH_SEPARATOR);
|
|
|
|
|
|
|
|
std::string file_folder;
|
|
|
|
if (pos == 0 || pos == std::string::npos) {
|
|
|
|
file_folder = "";
|
|
|
|
} else {
|
|
|
|
file_folder = file.substr(0,pos);
|
|
|
|
}
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
create_directory(folder + file_folder);
|
2019-04-13 12:21:56 -04:00
|
|
|
std::ofstream myfile;
|
2024-05-22 05:29:20 +03:00
|
|
|
myfile.open(std::filesystem::u8path(folder + file), std::ios::binary | std::ios::out);
|
2019-04-13 12:21:56 -04:00
|
|
|
if (!myfile.is_open()) return -1;
|
|
|
|
myfile.write(data, length);
|
2024-08-24 19:57:23 +02:00
|
|
|
int position = static_cast<int>(myfile.tellp());
|
2019-04-13 12:21:56 -04:00
|
|
|
myfile.close();
|
2019-05-23 09:35:12 -04:00
|
|
|
return position;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::get_path(std::string folder)
|
|
|
|
{
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string path(save_directory + appid + folder);
|
2019-04-13 12:21:56 -04:00
|
|
|
create_directory(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2019-05-03 08:50:10 -04:00
|
|
|
std::string Local_Storage::get_global_settings_path()
|
|
|
|
{
|
2019-08-24 17:42:39 +02:00
|
|
|
return save_directory + settings_storage_folder + PATH_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
std::vector<std::string> Local_Storage::get_filenames_path(std::string path)
|
|
|
|
{
|
2024-01-20 17:50:16 +02:00
|
|
|
if (path.empty()) return {};
|
2019-04-13 12:21:56 -04:00
|
|
|
if (path.back() != *PATH_SEPARATOR) {
|
|
|
|
path.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> filenames = get_filenames(path);
|
|
|
|
std::vector<std::string> output;
|
|
|
|
std::transform(filenames.begin(), filenames.end(), std::back_inserter(output), [](struct File_Data d) { return d.name;});
|
|
|
|
return output;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2024-01-20 17:50:16 +02:00
|
|
|
std::vector<std::string> Local_Storage::get_folders_path(std::string path)
|
|
|
|
{
|
|
|
|
if (path.empty()) return {};
|
|
|
|
if (path.back() != *PATH_SEPARATOR) {
|
|
|
|
path.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> output{};
|
|
|
|
try
|
|
|
|
{
|
2024-06-29 06:50:46 +03:00
|
|
|
const auto path_p(std::filesystem::u8path(path));
|
2024-04-28 00:45:11 +03:00
|
|
|
if (!common_helpers::dir_exist(path_p)) return output;
|
2024-01-20 17:50:16 +02:00
|
|
|
|
|
|
|
for (const auto &dir_entry :
|
|
|
|
std::filesystem::directory_iterator(path_p, std::filesystem::directory_options::follow_directory_symlink)) {
|
|
|
|
if (std::filesystem::is_directory(dir_entry)) {
|
|
|
|
output.push_back(dir_entry.path().filename().u8string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(...) { }
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2024-01-20 17:50:16 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:13:43 +03:00
|
|
|
void Local_Storage::set_saves_folder_name(std::string_view str)
|
2024-04-16 22:58:05 +02:00
|
|
|
{
|
|
|
|
Local_Storage::saves_folder_name = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Local_Storage::get_saves_folder_name()
|
|
|
|
{
|
|
|
|
return Local_Storage::saves_folder_name;
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
int Local_Storage::store_data(std::string folder, std::string file, char *data, unsigned int length)
|
|
|
|
{
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return store_file_data(save_directory + appid + folder, file, data, length);
|
|
|
|
}
|
|
|
|
|
2024-03-03 03:32:17 +02:00
|
|
|
int Local_Storage::store_data_settings(std::string file, const char *data, unsigned int length)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2019-05-03 08:50:10 -04:00
|
|
|
return store_file_data(get_global_settings_path(), file, data, length);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
int Local_Storage::get_file_data(const std::string &full_path, char *data, unsigned int max_length, unsigned int offset)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
2024-04-10 08:10:50 +02:00
|
|
|
std::ifstream myfile{};
|
2024-05-22 05:29:20 +03:00
|
|
|
myfile.open(std::filesystem::u8path(full_path), std::ios::binary | std::ios::in);
|
2019-04-13 12:21:56 -04:00
|
|
|
if (!myfile.is_open()) return -1;
|
|
|
|
|
2019-07-31 10:01:55 -04:00
|
|
|
myfile.seekg (offset, std::ios::beg);
|
2019-04-13 12:21:56 -04:00
|
|
|
myfile.read (data, max_length);
|
|
|
|
myfile.close();
|
2022-08-11 20:52:02 -04:00
|
|
|
reset_LastError();
|
2024-08-24 19:57:23 +02:00
|
|
|
|
|
|
|
return static_cast<int>(myfile.gcount());
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2019-07-31 10:01:55 -04:00
|
|
|
int Local_Storage::get_data(std::string folder, std::string file, char *data, unsigned int max_length, unsigned int offset)
|
2019-04-13 12:21:56 -04:00
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(save_directory + appid + folder + file);
|
2019-07-31 10:01:55 -04:00
|
|
|
return get_file_data(full_path, data, max_length, offset);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
2024-04-23 02:02:36 +02:00
|
|
|
unsigned int Local_Storage::data_settings_size(std::string file)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
|
|
|
std::string full_path(get_global_settings_path() + file);
|
|
|
|
return file_size_(full_path);
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
int Local_Storage::get_data_settings(std::string file, char *data, unsigned int max_length)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(get_global_settings_path() + file);
|
2019-04-13 12:21:56 -04:00
|
|
|
return get_file_data(full_path, data, max_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Local_Storage::count_files(std::string folder)
|
|
|
|
{
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-08-24 19:57:23 +02:00
|
|
|
return static_cast<int>(get_filenames_recursive(save_directory + appid + folder).size());
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::file_exists(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(save_directory + appid + folder + file);
|
2021-09-19 01:07:19 -04:00
|
|
|
return file_exists_(full_path);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Local_Storage::file_size(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(save_directory + appid + folder + file);
|
2021-09-19 01:07:19 -04:00
|
|
|
return file_size_(full_path);
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::file_delete(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(save_directory + appid + folder + file);
|
2021-04-25 12:44:41 -04:00
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
return _wremove(utf8_decode(full_path).c_str()) == 0;
|
|
|
|
#else
|
2019-04-13 12:21:56 -04:00
|
|
|
return remove(full_path.c_str()) == 0;
|
2021-04-25 12:44:41 -04:00
|
|
|
#endif
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Local_Storage::file_timestamp(std::string folder, std::string file)
|
|
|
|
{
|
|
|
|
file = sanitize_file_name(file);
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string full_path(save_directory + appid + folder + file);
|
2021-04-25 12:44:41 -04:00
|
|
|
|
|
|
|
#if defined(STEAM_WIN32)
|
|
|
|
struct _stat buffer = {};
|
|
|
|
if (_wstat(utf8_decode(full_path).c_str(), &buffer) != 0) return 0;
|
|
|
|
#else
|
2019-04-13 12:21:56 -04:00
|
|
|
struct stat buffer = {};
|
|
|
|
if (stat (full_path.c_str(), &buffer) != 0) return 0;
|
2021-04-25 12:44:41 -04:00
|
|
|
#endif
|
2019-04-13 12:21:56 -04:00
|
|
|
return buffer.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::iterate_file(std::string folder, int index, char *output_filename, int32 *output_size)
|
|
|
|
{
|
2024-01-19 21:45:32 +02:00
|
|
|
if (folder.size() && folder.back() != *PATH_SEPARATOR) {
|
2019-04-13 12:21:56 -04:00
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> files = get_filenames_recursive(save_directory + appid + folder);
|
2024-06-04 14:58:16 +03:00
|
|
|
if (index < 0 || static_cast<size_t>(index) >= files.size()) return false;
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string name(desanitize_file_name(files[index].name));
|
2019-05-23 09:35:12 -04:00
|
|
|
if (output_size) *output_size = file_size(folder, name);
|
|
|
|
#if defined(STEAM_WIN32)
|
2019-06-17 23:08:23 +02:00
|
|
|
name = replace_with(name, PATH_SEPARATOR, "/");
|
2019-05-23 09:35:12 -04:00
|
|
|
#endif
|
|
|
|
strcpy(output_filename, name.c_str());
|
|
|
|
return true;
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Local_Storage::update_save_filenames(std::string folder)
|
|
|
|
{
|
2019-05-23 09:35:12 -04:00
|
|
|
std::vector<struct File_Data> files = get_filenames_recursive(save_directory + appid + folder);
|
2019-04-13 12:21:56 -04:00
|
|
|
|
2019-05-23 09:35:12 -04:00
|
|
|
for (auto &f : files) {
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string path(f.name);
|
2024-04-06 07:49:07 +02:00
|
|
|
PRINT_DEBUG("remote file '%s'", path.c_str());
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string to(sanitize_file_name(desanitize_file_name(path)));
|
2019-05-23 09:35:12 -04:00
|
|
|
if (path != to && !file_exists(folder, to)) {
|
|
|
|
//create the folder
|
|
|
|
store_data(folder, to, (char *)"", 0);
|
|
|
|
file_delete(folder, to);
|
|
|
|
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string from(save_directory + appid + folder + PATH_SEPARATOR + path);
|
|
|
|
to = save_directory + appid + folder + PATH_SEPARATOR + to;
|
2024-04-06 07:49:07 +02:00
|
|
|
PRINT_DEBUG("renaming '%s' to '%s'", from.c_str(), to.c_str());
|
2019-04-13 12:21:56 -04:00
|
|
|
if (std::rename(from.c_str(), to.c_str()) < 0) {
|
2024-04-06 07:49:07 +02:00
|
|
|
PRINT_DEBUG("ERROR RENAMING");
|
2019-04-13 12:21:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-07-03 01:22:00 +03:00
|
|
|
bool Local_Storage::load_json(const std::string &full_path, nlohmann::json& json)
|
2019-08-24 17:42:39 +02:00
|
|
|
{
|
2024-07-03 01:22:00 +03:00
|
|
|
std::ifstream inventory_file(std::filesystem::u8path(full_path), std::ios::in | std::ios::binary);
|
2019-08-24 17:42:39 +02:00
|
|
|
// If there is a file and we opened it
|
2024-04-06 07:49:07 +02:00
|
|
|
if (inventory_file) {
|
2019-08-24 17:42:39 +02:00
|
|
|
try {
|
2024-07-03 01:22:00 +03:00
|
|
|
json = nlohmann::json::parse(inventory_file);
|
2024-04-06 07:49:07 +02:00
|
|
|
PRINT_DEBUG("Loaded json '%s' (%zu items)", full_path.c_str(), json.size());
|
2019-08-24 17:42:39 +02:00
|
|
|
return true;
|
2024-07-03 01:22:00 +03:00
|
|
|
} catch (const std::exception& e) {
|
2024-09-01 07:24:16 +02:00
|
|
|
const char *errorMessage = e.what();
|
|
|
|
PRINT_DEBUG("Error while parsing '%s' json error: %s", full_path.c_str(), errorMessage);
|
2019-08-24 17:42:39 +02:00
|
|
|
}
|
2024-04-06 07:49:07 +02:00
|
|
|
} else {
|
|
|
|
PRINT_DEBUG("Couldn't open file '%s' to read json", full_path.c_str());
|
2019-08-24 17:42:39 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 20:52:02 -04:00
|
|
|
reset_LastError();
|
2019-08-24 17:42:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-19 12:02:30 -04:00
|
|
|
bool Local_Storage::load_json_file(std::string folder, std::string const&file, nlohmann::json& json)
|
|
|
|
{
|
|
|
|
if (!folder.empty() && folder.back() != *PATH_SEPARATOR) {
|
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string inv_path(save_directory + appid + folder);
|
|
|
|
std::string full_path(inv_path + file);
|
2019-10-19 12:02:30 -04:00
|
|
|
|
|
|
|
return load_json(full_path, json);
|
|
|
|
}
|
|
|
|
|
2019-10-13 12:26:22 +02:00
|
|
|
bool Local_Storage::write_json_file(std::string folder, std::string const&file, nlohmann::json const& json)
|
2019-08-24 17:42:39 +02:00
|
|
|
{
|
2019-10-13 12:26:22 +02:00
|
|
|
if (!folder.empty() && folder.back() != *PATH_SEPARATOR) {
|
|
|
|
folder.append(PATH_SEPARATOR);
|
|
|
|
}
|
2024-04-09 19:10:04 +02:00
|
|
|
std::string inv_path(save_directory + appid + folder);
|
|
|
|
std::string full_path(inv_path + file);
|
2019-08-24 17:42:39 +02:00
|
|
|
|
|
|
|
create_directory(inv_path);
|
|
|
|
|
2024-06-08 18:03:02 +03:00
|
|
|
std::ofstream inventory_file(std::filesystem::u8path(full_path), std::ios::trunc | std::ios::out | std::ios::binary);
|
2024-04-06 07:49:07 +02:00
|
|
|
if (inventory_file) {
|
2019-08-24 17:42:39 +02:00
|
|
|
inventory_file << std::setw(2) << json;
|
|
|
|
return true;
|
|
|
|
}
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2024-04-06 07:49:07 +02:00
|
|
|
PRINT_DEBUG("Couldn't open file '%s' to write json", full_path.c_str());
|
2019-08-24 17:42:39 +02:00
|
|
|
|
2022-08-11 20:52:02 -04:00
|
|
|
reset_LastError();
|
2019-08-24 17:42:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:48:09 +02:00
|
|
|
std::vector<image_pixel_t> Local_Storage::load_image(std::string const& image_path)
|
|
|
|
{
|
2024-06-08 18:03:02 +03:00
|
|
|
std::vector<image_pixel_t> res{};
|
|
|
|
int width{}, height{};
|
2021-04-25 12:44:41 -04:00
|
|
|
image_pixel_t* img = (image_pixel_t*)stbi_load(image_path.c_str(), &width, &height, nullptr, 4);
|
2024-08-17 18:26:48 +03:00
|
|
|
PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason()));
|
|
|
|
if (img) {
|
|
|
|
res.resize(width * height);
|
2021-04-25 12:44:41 -04:00
|
|
|
std::copy(img, img + width * height, res.begin());
|
2020-10-09 13:48:09 +02:00
|
|
|
|
2021-04-25 12:44:41 -04:00
|
|
|
stbi_image_free(img);
|
2020-10-09 13:48:09 +02:00
|
|
|
}
|
2022-08-11 20:52:02 -04:00
|
|
|
|
|
|
|
reset_LastError();
|
2020-10-09 13:48:09 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-08-21 06:58:55 +03:00
|
|
|
std::string Local_Storage::load_image_resized(std::string const& image_path, std::string const& image_data, int resolution)
|
|
|
|
{
|
2024-02-08 02:26:06 +02:00
|
|
|
std::string resized_image{};
|
2024-03-12 21:21:09 +02:00
|
|
|
const size_t resized_img_size = resolution * resolution * 4;
|
2024-08-17 18:26:48 +03:00
|
|
|
if (image_path.size()) {
|
2024-03-12 21:21:09 +02:00
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
unsigned char *img = stbi_load(image_path.c_str(), &width, &height, nullptr, 4);
|
2024-08-17 18:26:48 +03:00
|
|
|
PRINT_DEBUG("stbi_load('%s') -> %s", image_path.c_str(), (img ? "loaded" : stbi_failure_reason()));
|
|
|
|
if (img) {
|
2024-03-12 21:21:09 +02:00
|
|
|
std::vector<char> out_resized(resized_img_size);
|
2024-08-18 11:05:49 +02:00
|
|
|
stbir_resize_uint8_linear(img, width, height, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, STBIR_RGBA);
|
2024-03-12 21:21:09 +02:00
|
|
|
resized_image = std::string((char*)&out_resized[0], out_resized.size());
|
|
|
|
stbi_image_free(img);
|
2023-08-24 21:03:31 +03:00
|
|
|
}
|
2024-08-17 18:26:48 +03:00
|
|
|
} else if (image_data.size()) {
|
2024-03-12 21:21:09 +02:00
|
|
|
std::vector<char> out_resized(resized_img_size);
|
2024-08-18 11:05:49 +02:00
|
|
|
stbir_resize_uint8_linear((unsigned char*)image_data.c_str(), 184, 184, 0, (unsigned char*)&out_resized[0], resolution, resolution, 0, STBIR_RGBA);
|
2024-03-12 21:21:09 +02:00
|
|
|
resized_image = std::string((char*)&out_resized[0], out_resized.size());
|
2023-08-21 06:58:55 +03:00
|
|
|
}
|
2024-08-18 11:05:49 +02:00
|
|
|
|
2023-08-21 06:58:55 +03:00
|
|
|
reset_LastError();
|
|
|
|
return resized_image;
|
|
|
|
}
|
|
|
|
|
2020-10-09 13:48:09 +02:00
|
|
|
bool Local_Storage::save_screenshot(std::string const& image_path, uint8_t* img_ptr, int32_t width, int32_t height, int32_t channels)
|
|
|
|
{
|
2024-08-18 11:05:49 +02:00
|
|
|
std::string screenshot_path(save_directory + appid + screenshots_folder + PATH_SEPARATOR);
|
2020-10-09 13:48:09 +02:00
|
|
|
create_directory(screenshot_path);
|
|
|
|
screenshot_path += image_path;
|
|
|
|
return stbi_write_png(screenshot_path.c_str(), width, height, channels, img_ptr, 0) == 1;
|
|
|
|
}
|
|
|
|
|
2023-10-29 23:48:51 +02:00
|
|
|
std::string Local_Storage::sanitize_string(std::string name)
|
|
|
|
{
|
|
|
|
return sanitize_file_name(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Local_Storage::desanitize_string(std::string name)
|
|
|
|
{
|
|
|
|
return desanitize_file_name(name);
|
|
|
|
}
|
|
|
|
|
2019-04-13 12:21:56 -04:00
|
|
|
#endif
|