changeset 859:46bb673eed4e

Load config file and rom.db from appropriate locations on Android
author Michael Pavone <pavone@retrodev.com>
date Wed, 04 Nov 2015 22:48:27 -0800
parents ba19bcc00483
children 213c3b5160d0
files config.c config.h romdb.c ym2612.c
diffstat 4 files changed, 80 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/config.c	Wed Nov 04 22:48:09 2015 -0800
+++ b/config.c	Wed Nov 04 22:48:27 2015 -0800
@@ -114,34 +114,84 @@
 	return ret;
 }
 
+#ifdef __ANDROID__
+#include <SDL.h>
+
+tern_node * parse_config_file_assets(char *config_path)
+{
+	tern_node * ret = NULL;
+	SDL_RWops *rw = SDL_RWFromFile(config_path, "rb");
+	if (!rw) {
+		goto open_fail;
+	}
+	size_t config_size = rw->size(rw);
+	if (!config_size) {
+		goto config_empty;
+	}
+	
+	char * config_data = malloc(config_size+1);
+	if (SDL_RWread(rw, config_data, 1, config_size) != config_size) {
+		goto config_read_fail;
+	}
+	config_data[config_size] = '\0';
+
+	ret = parse_config(config_data);
+config_read_fail:
+	free(config_data);
+config_empty:
+	SDL_RWclose(rw);
+open_fail:
+	return ret;
+}
+
+tern_node * load_config()
+{
+	char *path = alloc_concat(SDL_AndroidGetInternalStoragePath(), "/blastem.cfg");
+	tern_node * ret = parse_config_file(path);
+	free(path);
+	if (ret) {
+		return ret;
+	}
+	
+	ret = parse_config_file_assets("default.cfg");
+	if (ret) {
+		return ret;
+	}
+	
+	fatal_error("Failed to find a config file in internal storage or in the blastem APK\n");
+	//this will never get reached, but the compiler doesn't know that. Let's make it happy
+	return NULL;
+}
+
+#else
+
 tern_node * load_config()
 {
 	char * exe_dir;
 	char * home = get_home_dir();
-	if (!home) {
-		goto load_in_app_dir;
+	if (home) {		
+		char * path = alloc_concat(home, "/.config/blastem/blastem.cfg");
+		tern_node * ret = parse_config_file(path);
+		free(path);
+		if (ret) {
+			return ret;
+		}
 	}
-	char * path = alloc_concat(home, "/.config/blastem/blastem.cfg");
-	tern_node * ret = parse_config_file(path);
-	if (ret) {
-		goto success;
-	}
-	free(path);
-load_in_app_dir:
+	
 	exe_dir = get_exe_dir();
-	if (!exe_dir) {
-		goto no_config;
+	if (exe_dir) {		
+		path = alloc_concat(exe_dir, "/default.cfg");
+		ret = parse_config_file(path);
+		free(path);
+		if (ret) {
+			return ret;
+		}
 	}
-	path = alloc_concat(exe_dir, "/default.cfg");
-	ret = parse_config_file(path);
-	free(path);
-success:
-	if (ret) {
-		return ret;
-	}
-no_config:
+	
 	fatal_error("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n");
 	//this will never get reached, but the compiler doesn't know that. Let's make it happy
 	return NULL;
 }
 
+#endif
+
--- a/config.h	Wed Nov 04 22:48:09 2015 -0800
+++ b/config.h	Wed Nov 04 22:48:27 2015 -0800
@@ -9,6 +9,9 @@
 
 tern_node * parse_config_file(char * config_path);
 tern_node * load_config();
+#ifdef __ANDROID__
+tern_node * parse_config_file_assets(char *config_path);
+#endif
 
 #endif //CONFIG_H_
 
--- a/romdb.c	Wed Nov 04 22:48:09 2015 -0800
+++ b/romdb.c	Wed Nov 04 22:48:27 2015 -0800
@@ -384,6 +384,9 @@
 
 tern_node *load_rom_db()
 {
+#ifdef __ANDROID__
+	tern_node *db = parse_config_file_assets("rom.db");
+#else
 	char *exe_dir = get_exe_dir();
 	if (!exe_dir) {
 		fatal_error("Failed to find executable path\n");
@@ -391,6 +394,7 @@
 	char *path = alloc_concat(exe_dir, "/rom.db");
 	tern_node *db = parse_config_file(path);
 	free(path);
+#endif
 	if (!db) {
 		fatal_error("Failed to load ROM DB\n");
 	}
--- a/ym2612.c	Wed Nov 04 22:48:09 2015 -0800
+++ b/ym2612.c	Wed Nov 04 22:48:27 2015 -0800
@@ -118,6 +118,10 @@
 	context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc;
 }
 
+#ifdef __ANDROID__
+#define log2(x) (log(x)/log(2))
+#endif
+
 void ym_init(ym2612_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t sample_limit, uint32_t options)
 {
 	dfopen(debug_file, "ym_debug.txt", "w");