Merge pull request #224 from hcorion/alternate-loading

Load bios, firmware and melonDS.ini from exe, ~/.config/melonds or AppData
This commit is contained in:
StapleButter 2018-02-19 13:08:34 +01:00 committed by GitHub
commit aab0030137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 9 deletions

View File

@ -20,6 +20,10 @@
#include <string.h>
#include <stdlib.h>
#include "Config.h"
#include <string>
#ifndef _WIN32
#include <glib.h>
#endif
namespace Config
@ -100,6 +104,46 @@ ConfigEntry ConfigFile[] =
{"", -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()
{
@ -116,7 +160,7 @@ void Load()
entry++;
}
FILE* f = fopen("melonDS.ini", "r");
FILE* f = Config::GetConfigFile("melonDS.ini", "r");
if (!f) return;
char linebuf[1024];
@ -152,7 +196,7 @@ void Load()
void Save()
{
FILE* f = fopen("melonDS.ini", "w");
FILE* f = Config::GetConfigFile("melonDS.ini", "w");
if (!f) return;
ConfigEntry* entry = &ConfigFile[0];

View File

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

View File

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

View File

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

View File

@ -35,6 +35,7 @@
#include "../SPU.h"
#include "../Wifi.h"
#include "../Platform.h"
#include "../Config.h"
const int kScreenRot[4] = {0, 1, 2, 3};
@ -1047,7 +1048,7 @@ int main(int argc, char** argv)
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(
NULL,