Load bios, firmware and melonDS.ini from executable or ~/.config/melonds or AppData

This commit is contained in:
Zion Nimchuk 2018-01-11 14:38:57 -08:00
parent 981661a36d
commit 52df6be5f8
5 changed files with 57 additions and 9 deletions

View File

@ -20,6 +20,10 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "Config.h" #include "Config.h"
#include <string>
#ifndef _WIN32
#include <glib.h>
#endif
namespace Config namespace Config
@ -100,6 +104,46 @@ ConfigEntry ConfigFile[] =
{"", -1, NULL, 0, NULL, 0} {"", -1, NULL, 0, NULL, 0}
}; };
FILE* GetConfigFile(const char* fileName, const char* permissions)
{
// Locations are application directory, and XDG_CONFIG_HOME/melonds or AppData/MelonDS on windows
FILE* f;
// First check application directory
f = fopen(fileName, permissions);
if (f) return f;
#ifdef _WIN32
// Now check AppData
PWSTR appDataPath = NULL;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath);
if (!appDataPath)
return NULL;
std::string path = std::string(appDataPath) + "\\melonds\\" + fileName;
f = fopen(path, permissions);
if (f) return f;
#else
// Now check XDG_CONFIG_HOME
std::string path = std::string(g_get_user_config_dir()) + "/melonds/" + fileName;
f = fopen(path.c_str(), permissions);
if (f) return f;
#endif
return NULL;
}
bool HasConfigFile(const char* fileName)
{
FILE* f = GetConfigFile(fileName, "rb");
if (f)
{
fclose(f);
return true;
}
else
return false;
}
void Load() void Load()
{ {
@ -116,7 +160,7 @@ void Load()
entry++; entry++;
} }
FILE* f = fopen("melonDS.ini", "r"); FILE* f = Config::GetConfigFile("melonDS.ini", "r");
if (!f) return; if (!f) return;
char linebuf[1024]; char linebuf[1024];
@ -152,7 +196,7 @@ void Load()
void Save() void Save()
{ {
FILE* f = fopen("melonDS.ini", "w"); FILE* f = Config::GetConfigFile("melonDS.ini", "w");
if (!f) return; if (!f) return;
ConfigEntry* entry = &ConfigFile[0]; ConfigEntry* entry = &ConfigFile[0];

View File

@ -23,7 +23,8 @@
namespace Config namespace Config
{ {
FILE* GetConfigFile(const char* fileName, const char* permissions);
bool HasConfigFile(const char* fileName);
void Load(); void Load();
void Save(); void Save();

View File

@ -18,6 +18,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "Config.h"
#include "NDS.h" #include "NDS.h"
#include "ARM.h" #include "ARM.h"
#include "CP15.h" #include "CP15.h"
@ -244,7 +245,7 @@ void Reset()
FILE* f; FILE* f;
u32 i; u32 i;
f = fopen("bios9.bin", "rb"); f = Config::GetConfigFile("bios9.bin", "rb");
if (!f) if (!f)
{ {
printf("ARM9 BIOS not found\n"); printf("ARM9 BIOS not found\n");
@ -261,7 +262,7 @@ void Reset()
fclose(f); fclose(f);
} }
f = fopen("bios7.bin", "rb"); f = Config::GetConfigFile("bios7.bin", "rb");
if (!f) if (!f)
{ {
printf("ARM7 BIOS not found\n"); printf("ARM7 BIOS not found\n");

View File

@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "Config.h"
#include "NDS.h" #include "NDS.h"
#include "SPI.h" #include "SPI.h"
@ -88,7 +89,7 @@ void Reset()
if (Firmware) delete[] Firmware; if (Firmware) delete[] Firmware;
Firmware = NULL; Firmware = NULL;
FILE* f = fopen("firmware.bin", "rb"); FILE* f = Config::GetConfigFile("firmware.bin", "rb");
if (!f) if (!f)
{ {
printf("firmware.bin not found\n"); printf("firmware.bin not found\n");
@ -128,7 +129,7 @@ void Reset()
// take a backup // take a backup
char* firmbkp = "firmware.bin.bak"; char* firmbkp = "firmware.bin.bak";
f = fopen(firmbkp, "rb"); f = Config::GetConfigFile(firmbkp, "rb");
if (f) fclose(f); if (f) fclose(f);
else else
{ {
@ -307,7 +308,7 @@ void Write(u8 val, u32 hold)
if (!hold && (CurCmd == 0x02 || CurCmd == 0x0A)) if (!hold && (CurCmd == 0x02 || CurCmd == 0x0A))
{ {
FILE* f = fopen("firmware.bin", "r+b"); FILE* f = Config::GetConfigFile("firmware.bin", "r+b");
if (f) if (f)
{ {
u32 cutoff = 0x7FA00 & FirmwareMask; u32 cutoff = 0x7FA00 & FirmwareMask;

View File

@ -35,6 +35,7 @@
#include "../SPU.h" #include "../SPU.h"
#include "../Wifi.h" #include "../Wifi.h"
#include "../Platform.h" #include "../Platform.h"
#include "../Config.h"
const int kScreenRot[4] = {0, 1, 2, 3}; const int kScreenRot[4] = {0, 1, 2, 3};
@ -1047,7 +1048,7 @@ int main(int argc, char** argv)
Config::Load(); Config::Load();
if (!_fileexists("bios7.bin") || !_fileexists("bios9.bin") || !_fileexists("firmware.bin")) if (!Config::HasConfigFile("bios7.bin") || !Config::HasConfigFile("bios9.bin") || !Config::HasConfigFile("firmware.bin"))
{ {
uiMsgBoxError( uiMsgBoxError(
NULL, NULL,