aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZion Nimchuk <zionnimchuk@gmail.com>2018-01-11 14:38:57 -0800
committerZion Nimchuk <zionnimchuk@gmail.com>2018-01-11 14:38:57 -0800
commit52df6be5f8a912aeef050fa4c563cca3ef80ad5a (patch)
tree5b1a9e98287daa8c170a810a263c786a0ac1ceeb
parent981661a36d691c00f3499d6cf5a3a4db6736a626 (diff)
Load bios, firmware and melonDS.ini from executable or ~/.config/melonds or AppData
-rw-r--r--src/Config.cpp48
-rw-r--r--src/Config.h3
-rw-r--r--src/NDS.cpp5
-rw-r--r--src/SPI.cpp7
-rw-r--r--src/libui_sdl/main.cpp3
5 files changed, 57 insertions, 9 deletions
diff --git a/src/Config.cpp b/src/Config.cpp
index dfa4416..660ff8c 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -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];
diff --git a/src/Config.h b/src/Config.h
index b3d9d64..0f5ca9e 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -23,7 +23,8 @@
namespace Config
{
-
+FILE* GetConfigFile(const char* fileName, const char* permissions);
+bool HasConfigFile(const char* fileName);
void Load();
void Save();
diff --git a/src/NDS.cpp b/src/NDS.cpp
index 8e5700f..34eccf7 100644
--- a/src/NDS.cpp
+++ b/src/NDS.cpp
@@ -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");
diff --git a/src/SPI.cpp b/src/SPI.cpp
index 2c62a6a..12e9291 100644
--- a/src/SPI.cpp
+++ b/src/SPI.cpp
@@ -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;
diff --git a/src/libui_sdl/main.cpp b/src/libui_sdl/main.cpp
index 814d92c..6736fbc 100644
--- a/src/libui_sdl/main.cpp
+++ b/src/libui_sdl/main.cpp
@@ -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,