From 9f44ee50688fe1fda3a3a17226d351da8da98405 Mon Sep 17 00:00:00 2001
From: Jonas Kulla <Nyocurio@gmail.com>
Date: Mon, 22 Jan 2018 10:54:21 +0100
Subject: [PATCH 1/3] FileSystem: Fix while termination condition

---
 src/filesystem.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/filesystem.cpp b/src/filesystem.cpp
index 8e50ee8..33d383d 100644
--- a/src/filesystem.cpp
+++ b/src/filesystem.cpp
@@ -508,7 +508,7 @@ findFontsFolderCB(void *data, const char *, const char *fname)
 	char buffer[512];
 	const char *s = fname;
 
-	while (s && i < sizeof(buffer))
+	while (*s && i < sizeof(buffer))
 		buffer[i++] = tolower(*s++);
 
 	buffer[i] = '\0';
-- 
2.43.0


From b02224206e6403fc0ed8e62c82ba8188260ce3ae Mon Sep 17 00:00:00 2001
From: ReinUsesLisp <reinuseslisp@airmail.cc>
Date: Thu, 22 Feb 2018 04:37:23 -0300
Subject: [PATCH 2/3] FileSystem: Fix while termination condition

---
 src/filesystem.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/filesystem.cpp b/src/filesystem.cpp
index 8e50ee8..33d383d 100644
--- a/src/filesystem.cpp
+++ b/src/filesystem.cpp
@@ -508,7 +508,7 @@ findFontsFolderCB(void *data, const char *, const char *fname)
 	char buffer[512];
 	const char *s = fname;
 
-	while (s && i < sizeof(buffer))
+	while (*s && i < sizeof(buffer))
 		buffer[i++] = tolower(*s++);
 
 	buffer[i] = '\0';
-- 
2.43.0


From eb917d722616116f16e36d1a33bb64f3dfcdf824 Mon Sep 17 00:00:00 2001
From: ReinUsesLisp <reinuseslisp@airmail.cc>
Date: Thu, 22 Feb 2018 04:37:47 -0300
Subject: [PATCH 3/3] Config: Set debug editor's debug variables into ruby

---
 binding-mri/binding-mri.cpp     |  9 +++++++++
 binding-mruby/binding-mruby.cpp |  9 +++++++++
 src/config.cpp                  | 21 +++++++++++++++++++++
 src/config.h                    |  6 ++++++
 4 files changed, 45 insertions(+)

diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp
index f0a4bb4..1c0057b 100644
--- a/binding-mri/binding-mri.cpp
+++ b/binding-mri/binding-mri.cpp
@@ -147,7 +147,16 @@ static void mriBindingInit()
 	_rb_define_module_function(mod, "raw_key_states", mkxpRawKeyStates);
 	_rb_define_module_function(mod, "mouse_in_window", mkxpMouseInWindow);
 
+	/* Load global constants */
 	rb_gv_set("MKXP", Qtrue);
+
+	VALUE debug = rb_bool_new(shState->config().editor.debug);
+	if (rgssVer == 1)
+		rb_gv_set("DEBUG", debug);
+	else if (rgssVer >= 2)
+		rb_gv_set("TEST", debug);
+
+	rb_gv_set("BTEST", rb_bool_new(shState->config().editor.battleTest));
 }
 
 static void
diff --git a/binding-mruby/binding-mruby.cpp b/binding-mruby/binding-mruby.cpp
index 867d2d6..a713d73 100644
--- a/binding-mruby/binding-mruby.cpp
+++ b/binding-mruby/binding-mruby.cpp
@@ -114,8 +114,17 @@ static void mrbBindingInit(mrb_state *mrb)
 	/* Load RPG module */
 	mrb_load_irep(mrb, mrbModuleRPG);
 
+	/* Load global constants */
 	mrb_define_global_const(mrb, "MKXP", mrb_true_value());
 
+	mrb_value debug = rb_bool_new(shState->config().editor.debug);
+	if (rgssVer == 1)
+		mrb_define_global_const(mrb, "DEBUG", debug);
+	else if (rgssVer >= 2)
+		mrb_define_global_const(mrb, "TEST", debug);
+
+	mrb_define_global_const(mrb, "BTEST", mrb_bool_value(shState->config().editor.battleTest));
+
 	mrb_gc_arena_restore(mrb, arena);
 }
 
diff --git a/src/config.cpp b/src/config.cpp
index 4d47152..fee3b5a 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -189,6 +189,27 @@ void Config::read(int argc, char *argv[])
 // Not gonna take your shit boost
 #define GUARD_ALL( exp ) try { exp } catch(...) {}
 
+	editor.debug = false;
+	editor.battleTest = false;
+
+	/* Read arguments sent from the editor */
+	if (argc > 1)
+	{
+		std::string argv1 = argv[1];
+		/* RGSS1 uses "debug", 2 and 3 use "test" */
+		if (argv1 == "debug" || argv1 == "test")
+			editor.debug = true;
+		else if (argv1 == "btest")
+			editor.battleTest = true;
+
+		/* Fix offset */
+		if (editor.debug || editor.battleTest)
+		{
+			argc--;
+			argv++;
+		}
+	}
+
 #define PO_DESC(key, type, def) (#key, po::value< type >()->default_value(def))
 
 	po::options_description podesc;
diff --git a/src/config.h b/src/config.h
index 5cc8bb7..e380250 100644
--- a/src/config.h
+++ b/src/config.h
@@ -88,6 +88,12 @@ struct Config
 
 	std::vector<std::string> rubyLoadpaths;
 
+	/* Editor flags */
+	struct {
+		bool debug;
+		bool battleTest;
+	} editor;
+
 	/* Game INI contents */
 	struct {
 		std::string scripts;
-- 
2.43.0