From 1a489aafafe8d22b697dc7d2f2949ec3a706cfa6 Mon Sep 17 00:00:00 2001
From: Jonas Kulla <Nyocurio@gmail.com>
Date: Fri, 24 Oct 2014 18:29:08 +0200
Subject: [PATCH] Bindings: Fix inconsistency in Viewport dispose (RGSS1)

As noted, on Viewport dispose, RMXP always calls the core
dispose method for child objects regardless of whether
user scripts override it in sub classes.

Implement this behavior in mkxp to prevent infinite recursion.
---
 binding-mri/disposable-binding.h   | 7 +++++--
 binding-mruby/binding-util.cpp     | 2 +-
 binding-mruby/binding-util.h       | 2 +-
 binding-mruby/disposable-binding.h | 5 ++++-
 4 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/binding-mri/disposable-binding.h b/binding-mri/disposable-binding.h
index 7dfe928..fa057a4 100644
--- a/binding-mri/disposable-binding.h
+++ b/binding-mri/disposable-binding.h
@@ -52,9 +52,8 @@ disposableDisposeChildren(VALUE disp)
 	if (NIL_P(children))
 		return;
 
-	ID dispFun = rb_intern("dispose");
+	ID dispFun = rb_intern("_mkxp_dispose_alias");
 
-	/* Note: RMXP doesn't call overridden 'dispose' methods here */
 	for (long i = 0; i < RARRAY_LEN(children); ++i)
 		rb_funcall2(rb_ary_entry(children, i), dispFun, 0, 0);
 }
@@ -98,6 +97,10 @@ static void disposableBindingInit(VALUE klass)
 {
 	_rb_define_method(klass, "dispose", disposableDispose<C>);
 	_rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
+
+	/* Make sure we always have access to the original method, even
+	 * if it is overridden by user scripts */
+	rb_define_alias(klass, "_mkxp_dispose_alias", "dispose");
 }
 
 template<class C>
diff --git a/binding-mruby/binding-util.cpp b/binding-mruby/binding-util.cpp
index 49c2563..c062eec 100644
--- a/binding-mruby/binding-util.cpp
+++ b/binding-mruby/binding-util.cpp
@@ -55,7 +55,7 @@ struct
 	SYMD(default_color),
 	SYMD(default_out_color),
 	SYMD(children),
-	SYMD(dispose)
+	SYMD(_mkxp_dispose_alias)
 };
 
 static elementsN(symData);
diff --git a/binding-mruby/binding-util.h b/binding-mruby/binding-util.h
index 5dd5cd7..142a386 100644
--- a/binding-mruby/binding-util.h
+++ b/binding-mruby/binding-util.h
@@ -56,7 +56,7 @@ enum CommonSymbol
 	CSdefault_color,
 	CSdefault_out_color,
 	CSchildren,
-	CSdispose,
+	CS_mkxp_dispose_alias,
 
 	CommonSymbolsMax
 };
diff --git a/binding-mruby/disposable-binding.h b/binding-mruby/disposable-binding.h
index 9b1895c..7cf80cb 100644
--- a/binding-mruby/disposable-binding.h
+++ b/binding-mruby/disposable-binding.h
@@ -60,7 +60,7 @@ disposableDisposeChildren(mrb_state *mrb, mrb_value disp)
 
 	for (mrb_int i = 0; i < RARRAY_LEN(children); ++i)
 		mrb_funcall_argv(mrb, mrb_ary_entry(children, i),
-						 mrbData->symbols[CSdispose], 0, 0);
+		                 mrbData->symbols[CS_mkxp_dispose_alias], 0, 0);
 }
 
 template<class C>
@@ -99,6 +99,9 @@ static void disposableBindingInit(mrb_state *mrb, RClass *klass)
 {
 	mrb_define_method(mrb, klass, "dispose", disposableDispose<C>, MRB_ARGS_NONE());
 	mrb_define_method(mrb, klass, "disposed?", disposableIsDisposed<C>, MRB_ARGS_NONE());
+
+	mrb_alias_method(mrb, klass, getMrbData(mrb)->symbols[CS_mkxp_dispose_alias],
+	                 mrb_intern_lit(mrb, "dispose"));
 }
 
 template<class C>