push temp. shito

This commit is contained in:
Arisotura 2024-04-05 23:00:19 +02:00
parent 1abc1e9cff
commit adb5760604
3 changed files with 46 additions and 39 deletions

View File

@ -188,6 +188,9 @@ enum class FileSeekOrigin
*/ */
struct FileHandle; struct FileHandle;
// retrieves the path to a local file, without opening the file
std::string GetLocalFilePath(const std::string& filename);
// Simple fopen() wrapper that supports UTF8. // Simple fopen() wrapper that supports UTF8.
// Can be optionally restricted to only opening a file that already exists. // Can be optionally restricted to only opening a file that already exists.
FileHandle* OpenFile(const std::string& path, FileMode mode); FileHandle* OpenFile(const std::string& path, FileMode mode);

View File

@ -20,6 +20,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h> #include <inttypes.h>
#include <string_view>
#include "toml/toml.hpp"
#include "Platform.h" #include "Platform.h"
#include "Config.h" #include "Config.h"
@ -163,8 +165,10 @@ bool GdbARM9BreakOnStartup;
CameraConfig Camera[2]; CameraConfig Camera[2];
const char* kConfigFile = "melonDS.ini"; const char* kConfigFile = "melonDS.toml";
const char* kUniqueConfigFile = "melonDS.%d.ini";
const char* kLegacyConfigFile = "melonDS.ini";
const char* kLegacyUniqueConfigFile = "melonDS.%d.ini";
ConfigEntry ConfigFile[] = ConfigEntry ConfigFile[] =
{ {
@ -384,16 +388,16 @@ bool LoadFile(int inst, int actualinst)
if (inst > 0) if (inst > 0)
{ {
char name[100] = {0}; char name[100] = {0};
snprintf(name, 99, kUniqueConfigFile, inst+1); snprintf(name, 99, kLegacyUniqueConfigFile, inst+1);
f = Platform::OpenLocalFile(name, Platform::FileMode::ReadText); f = Platform::OpenLocalFile(name, Platform::FileMode::ReadText);
if (!Platform::CheckLocalFileWritable(name)) return false; if (!Platform::CheckLocalFileWritable(name)) return false;
} }
else else
{ {
f = Platform::OpenLocalFile(kConfigFile, Platform::FileMode::ReadText); f = Platform::OpenLocalFile(kLegacyConfigFile, Platform::FileMode::ReadText);
if (actualinst == 0 && !Platform::CheckLocalFileWritable(kConfigFile)) return false; if (actualinst == 0 && !Platform::CheckLocalFileWritable(kLegacyConfigFile)) return false;
} }
if (!f) return true; if (!f) return true;
@ -434,9 +438,8 @@ bool LoadFile(int inst, int actualinst)
return true; return true;
} }
bool Load() bool LoadLegacy()
{ {
for (ConfigEntry* entry = &ConfigFile[0]; entry->Value; entry++) for (ConfigEntry* entry = &ConfigFile[0]; entry->Value; entry++)
{ {
switch (entry->Type) switch (entry->Type)
@ -457,37 +460,33 @@ bool Load()
return ret; return ret;
} }
bool Load()
{
auto cfgpath = Platform::GetLocalFilePath(kConfigFile);
if (!Platform::CheckFileWritable(cfgpath))
return false;
if (!Platform::FileExists(cfgpath))
return LoadLegacy();
toml::table tbl;
try
{
tbl = toml::parse_file(cfgpath);
printf("toml worked\n");
}
catch (toml::parse_error& err)
{
printf("toml shat itself :(\n");
}
return true;
}
void Save() void Save()
{ {
int inst = Platform::InstanceID(); //
Platform::FileHandle* f;
if (inst > 0)
{
char name[100] = {0};
snprintf(name, 99, kUniqueConfigFile, inst+1);
f = Platform::OpenLocalFile(name, Platform::FileMode::WriteText);
}
else
f = Platform::OpenLocalFile(kConfigFile, Platform::FileMode::WriteText);
if (!f) return;
for (ConfigEntry* entry = &ConfigFile[0]; entry->Value; entry++)
{
if ((inst > 0) && (!entry->InstanceUnique))
continue;
switch (entry->Type)
{
case 0: Platform::FileWriteFormatted(f, "%s=%d\n", entry->Name, *(int*)entry->Value); break;
case 1: Platform::FileWriteFormatted(f, "%s=%d\n", entry->Name, *(bool*)entry->Value ? 1:0); break;
case 2: Platform::FileWriteFormatted(f, "%s=%s\n", entry->Name, (*(std::string*)entry->Value).c_str()); break;
case 3: Platform::FileWriteFormatted(f, "%s=%" PRId64 "\n", entry->Name, *(int64_t*)entry->Value); break;
}
}
CloseFile(f);
} }
} }

View File

@ -282,9 +282,9 @@ FileHandle* OpenFile(const std::string& path, FileMode mode)
} }
} }
FileHandle* OpenLocalFile(const std::string& path, FileMode mode) std::string GetLocalFilePath(const std::string& filename)
{ {
QString qpath = QString::fromStdString(path); QString qpath = QString::fromStdString(filename);
QDir dir(qpath); QDir dir(qpath);
QString fullpath; QString fullpath;
@ -298,7 +298,12 @@ FileHandle* OpenLocalFile(const std::string& path, FileMode mode)
fullpath = QString::fromStdString(EmuDirectory) + QDir::separator() + qpath; fullpath = QString::fromStdString(EmuDirectory) + QDir::separator() + qpath;
} }
return OpenFile(fullpath.toStdString(), mode); return fullpath.toStdString();
}
FileHandle* OpenLocalFile(const std::string& path, FileMode mode)
{
return OpenFile(GetLocalFilePath(path), mode);
} }
bool CloseFile(FileHandle* file) bool CloseFile(FileHandle* file)