Lift 'Disposable' concept from core into bindings
Instead of replicating the RGSS Disposable interface in C++ and merely binding it, redefine the 'disposed' state as the entire core object being deleted (and the binding object's private pointer being null). This makes the behavior more accurate in regard to RMXP. It is now for example possible to subclass disposable classes and access their 'dispose'/'disposed?' methods without initializing the base class first (because the internal pointer is simply null before initialization). Accessing any other base methods will still raise an exception. There are some quirks and irregular behavior in RMXP; eg. most nullable bitmap attributes of disposable classes (Sprite, Plane etc.) can still be queried afterwards, but some cannot (Tilemap#tileset), and disposing certain attributes crashes RMXP entirely (Tilemap#autotiles[n]). mkxp tries to behave as close possible, but will be more lenient some circumstances. To the core, disposed bitmap attributes will look identically to null, which slightly diverges from RMXP (where they're treated as still existing, but aren't drawn). The Disposable interface has been retained containing a single signal, for the binding to inform core when objects are disposed (so active attributes can be set to null).
This commit is contained in:
parent
b7af8cc92f
commit
e858bbdcf5
38 changed files with 341 additions and 386 deletions
|
@ -23,6 +23,8 @@
|
|||
#include "util.h"
|
||||
#include "exception.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define SYMD(symbol) { CS##symbol, #symbol }
|
||||
|
||||
struct
|
||||
|
@ -48,7 +50,9 @@ struct
|
|||
SYMD(cursor_rect),
|
||||
SYMD(path),
|
||||
SYMD(array),
|
||||
SYMD(default_color)
|
||||
SYMD(default_color),
|
||||
SYMD(children),
|
||||
SYMD(dispose)
|
||||
};
|
||||
|
||||
static elementsN(symData);
|
||||
|
@ -140,6 +144,19 @@ void raiseMrbExc(mrb_state *mrb, const Exception &exc)
|
|||
mrb_raise(mrb, excClass, exc.msg.c_str());
|
||||
}
|
||||
|
||||
void
|
||||
raiseDisposedAccess(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
const char *klassName = DATA_TYPE(self)->struct_name;
|
||||
char buf[32];
|
||||
|
||||
strncpy(buf, klassName, sizeof(buf));
|
||||
buf[0] = tolower(buf[0]);
|
||||
|
||||
mrb_raisef(mrb, getMrbData(mrb)->exc[RGSS],
|
||||
"disposed %S", mrb_str_new_cstr(mrb, buf));
|
||||
}
|
||||
|
||||
MRB_METHOD_PUB(inspectObject)
|
||||
{
|
||||
static char buffer[64];
|
||||
|
|
|
@ -50,6 +50,8 @@ enum CommonSymbol
|
|||
CSpath,
|
||||
CSarray,
|
||||
CSdefault_color,
|
||||
CSchildren,
|
||||
CSdispose,
|
||||
|
||||
CommonSymbolsMax
|
||||
};
|
||||
|
@ -143,16 +145,10 @@ defineClass(mrb_state *mrb, const char *name)
|
|||
|
||||
#define MRB_FUN_UNUSED_PARAM { (void) mrb; }
|
||||
|
||||
/* If we're not binding a disposable class,
|
||||
* we want to #undef DEF_PROP_CHK_DISP */
|
||||
#define DEF_PROP_CHK_DISP \
|
||||
checkDisposed(mrb, k, DISP_CLASS_NAME);
|
||||
|
||||
#define DEF_PROP_OBJ(Klass, PropKlass, PropName, prop_iv) \
|
||||
MRB_METHOD(Klass##Get##PropName) \
|
||||
{ \
|
||||
Klass *k = getPrivateData<Klass>(mrb, self); (void) k; \
|
||||
DEF_PROP_CHK_DISP \
|
||||
checkDisposed(mrb, self); \
|
||||
return getProperty(mrb, self, prop_iv); \
|
||||
} \
|
||||
MRB_METHOD(Klass##Set##PropName) \
|
||||
|
@ -171,8 +167,6 @@ defineClass(mrb_state *mrb, const char *name)
|
|||
#define DEF_PROP_OBJ_NIL(Klass, PropKlass, PropName, prop_iv) \
|
||||
MRB_METHOD(Klass##Get##PropName) \
|
||||
{ \
|
||||
Klass *k = getPrivateData<Klass>(mrb, self); (void) k; \
|
||||
DEF_PROP_CHK_DISP \
|
||||
return getProperty(mrb, self, prop_iv); \
|
||||
} \
|
||||
MRB_METHOD(Klass##Set##PropName) \
|
||||
|
@ -194,7 +188,6 @@ defineClass(mrb_state *mrb, const char *name)
|
|||
MRB_METHOD(Klass##Get##PropName) \
|
||||
{ \
|
||||
Klass *k = getPrivateData<Klass>(mrb, self); \
|
||||
DEF_PROP_CHK_DISP \
|
||||
return mrb_##conv_t##_value(k->get##PropName()); \
|
||||
} \
|
||||
MRB_METHOD(Klass##Set##PropName) \
|
||||
|
@ -215,6 +208,7 @@ defineClass(mrb_state *mrb, const char *name)
|
|||
#define DEF_PROP_B(Klass, PropName) \
|
||||
DEF_PROP(Klass, mrb_bool, PropName, "b", bool)
|
||||
|
||||
// FIXME: use initialize_copy
|
||||
#define CLONE_FUN(Klass) \
|
||||
MRB_METHOD(Klass##Clone) \
|
||||
{ \
|
||||
|
@ -252,19 +246,33 @@ getSym(mrb_state *mrb, CommonSymbol sym)
|
|||
return getMrbData(mrb)->symbols[sym];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T *
|
||||
void
|
||||
raiseDisposedAccess(mrb_state *mrb, mrb_value self);
|
||||
|
||||
inline void checkDisposed(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
if (!DATA_PTR(self))
|
||||
raiseDisposedAccess(mrb, self);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
inline C *
|
||||
getPrivateData(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
(void) mrb;
|
||||
return static_cast<T*>(DATA_PTR(self));
|
||||
C *c = static_cast<C*>(DATA_PTR(self));
|
||||
|
||||
if (!c)
|
||||
raiseDisposedAccess(mrb, self);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T *
|
||||
getPrivateDataCheck(mrb_state *mrb, mrb_value obj, const mrb_data_type &type)
|
||||
{
|
||||
return static_cast<T*>(mrb_check_datatype(mrb, obj, &type));
|
||||
void *ptr = mrb_check_datatype(mrb, obj, &type);
|
||||
return static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
inline void
|
||||
|
|
|
@ -26,8 +26,6 @@
|
|||
#include "binding-util.h"
|
||||
#include "binding-types.h"
|
||||
|
||||
#define DISP_CLASS_NAME "bitmap"
|
||||
|
||||
DEF_TYPE(Bitmap);
|
||||
|
||||
MRB_METHOD(bitmapInitialize)
|
||||
|
@ -111,7 +109,7 @@ MRB_METHOD(bitmapBlt)
|
|||
src = getPrivateDataCheck<Bitmap>(mrb, srcObj, BitmapType);
|
||||
srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType);
|
||||
|
||||
GUARD_EXC( b->blt(x, y, *src, srcRect->toIntRect(), opacity); )
|
||||
GUARD_EXC( b->blt(x, y, src, srcRect->toIntRect(), opacity); )
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
@ -134,7 +132,7 @@ MRB_METHOD(bitmapStretchBlt)
|
|||
destRect = getPrivateDataCheck<Rect>(mrb, destRectObj, RectType);
|
||||
srcRect = getPrivateDataCheck<Rect>(mrb, srcRectObj, RectType);
|
||||
|
||||
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), *src, srcRect->toIntRect(), opacity); )
|
||||
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), src, srcRect->toIntRect(), opacity); )
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
@ -277,8 +275,7 @@ MRB_METHOD(bitmapTextSize)
|
|||
|
||||
MRB_METHOD(bitmapGetFont)
|
||||
{
|
||||
Bitmap *b = getPrivateData<Bitmap>(mrb, self);
|
||||
checkDisposed(mrb, b, "bitmap");
|
||||
checkDisposed(mrb, self);
|
||||
|
||||
return getProperty(mrb, self, CSfont);
|
||||
}
|
||||
|
|
|
@ -25,14 +25,60 @@
|
|||
#include "disposable.h"
|
||||
#include "binding-util.h"
|
||||
|
||||
#include "mruby/array.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/* 'Children' are disposables that are disposed together
|
||||
* with their parent. Currently this is only used by Viewport
|
||||
* in RGSS1.
|
||||
* FIXME: Disable this behavior when RGSS2 or 3 */
|
||||
inline void
|
||||
disposableAddChild(mrb_state *mrb, mrb_value disp, mrb_value child)
|
||||
{
|
||||
mrb_sym sym = getMrbData(mrb)->symbols[CSchildren];
|
||||
mrb_value children = mrb_iv_get(mrb, disp, sym);
|
||||
|
||||
if (mrb_nil_p(children))
|
||||
{
|
||||
children = mrb_ary_new(mrb);
|
||||
mrb_iv_set(mrb, disp, sym, children);
|
||||
}
|
||||
|
||||
/* Assumes children are never removed until destruction */
|
||||
mrb_ary_push(mrb, children, child);
|
||||
}
|
||||
|
||||
inline void
|
||||
disposableDisposeChildren(mrb_state *mrb, mrb_value disp)
|
||||
{
|
||||
MrbData *mrbData = getMrbData(mrb);
|
||||
mrb_value children = mrb_iv_get(mrb, disp, mrbData->symbols[CSchildren]);
|
||||
|
||||
if (mrb_nil_p(children))
|
||||
return;
|
||||
|
||||
for (mrb_int i = 0; i < RARRAY_LEN(children); ++i)
|
||||
mrb_funcall_argv(mrb, mrb_ary_entry(children, i),
|
||||
mrbData->symbols[CSdispose], 0, 0);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
MRB_METHOD(disposableDispose)
|
||||
{
|
||||
Disposable *d = getPrivateData<C>(mrb, self);
|
||||
C *c = static_cast<C*>(DATA_PTR(self));
|
||||
|
||||
d->dispose();
|
||||
/* Nothing to do if already disposed */
|
||||
if (!c)
|
||||
return mrb_nil_value();
|
||||
|
||||
/* Inform core */
|
||||
c->wasDisposed();
|
||||
|
||||
disposableDisposeChildren(mrb, self);
|
||||
|
||||
delete c;
|
||||
DATA_PTR(self) = 0;
|
||||
|
||||
return mrb_nil_value();
|
||||
}
|
||||
|
@ -40,9 +86,9 @@ MRB_METHOD(disposableDispose)
|
|||
template<class C>
|
||||
MRB_METHOD(disposableDisposed)
|
||||
{
|
||||
Disposable *d = getPrivateData<C>(mrb, self);
|
||||
MRB_UNUSED_PARAM;
|
||||
|
||||
return mrb_bool_value(d->isDisposed());
|
||||
return mrb_bool_value(DATA_PTR(self) == 0);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
|
@ -52,13 +98,4 @@ static void disposableBindingInit(mrb_state *mrb, RClass *klass)
|
|||
mrb_define_method(mrb, klass, "disposed?", disposableDisposed<C>, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
inline void checkDisposed(mrb_state *mrb, Disposable *d, const char *klassName)
|
||||
{
|
||||
MrbData *data = getMrbData(mrb);
|
||||
|
||||
if (d->isDisposed())
|
||||
mrb_raisef(mrb, data->exc[RGSS], "disposed %S",
|
||||
mrb_str_new_static(mrb, klassName, strlen(klassName)));
|
||||
}
|
||||
|
||||
#endif // DISPOSABLEBINDING_H
|
||||
|
|
|
@ -78,9 +78,6 @@ MRB_METHOD(FontSetName)
|
|||
return name;
|
||||
}
|
||||
|
||||
#undef DEF_PROP_CHK_DISP
|
||||
#define DEF_PROP_CHK_DISP
|
||||
|
||||
DEF_PROP_I(Font, Size)
|
||||
DEF_PROP_B(Font, Bold)
|
||||
DEF_PROP_B(Font, Italic)
|
||||
|
|
|
@ -43,8 +43,6 @@ MRB_METHOD(planeInitialize)
|
|||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "plane"
|
||||
|
||||
DEF_PROP_OBJ(Plane, Bitmap, Bitmap, CSbitmap)
|
||||
DEF_PROP_OBJ(Plane, Color, Color, CScolor)
|
||||
DEF_PROP_OBJ(Plane, Tone, Tone, CStone)
|
||||
|
|
|
@ -47,8 +47,6 @@ MRB_METHOD(spriteInitialize)
|
|||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "sprite"
|
||||
|
||||
DEF_PROP_OBJ_NIL(Sprite, Bitmap, Bitmap, CSbitmap)
|
||||
DEF_PROP_OBJ(Sprite, Rect, SrcRect, CSsrc_rect)
|
||||
DEF_PROP_OBJ(Sprite, Color, Color, CScolor)
|
||||
|
|
|
@ -118,13 +118,9 @@ MRB_METHOD(tilemapUpdate)
|
|||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "tilemap"
|
||||
|
||||
MRB_METHOD(tilemapGetViewport)
|
||||
{
|
||||
Tilemap *t = getPrivateData<Tilemap>(mrb, self);
|
||||
|
||||
checkDisposed(mrb, t, DISP_CLASS_NAME);
|
||||
checkDisposed(mrb, self);
|
||||
|
||||
return getProperty(mrb, self, CSviewport);
|
||||
}
|
||||
|
|
|
@ -68,8 +68,6 @@ MRB_METHOD(viewportInitialize)
|
|||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "viewport"
|
||||
|
||||
DEF_PROP_OBJ(Viewport, Rect, Rect, CSrect)
|
||||
DEF_PROP_OBJ(Viewport, Color, Color, CScolor)
|
||||
DEF_PROP_OBJ(Viewport, Tone, Tone, CStone)
|
||||
|
|
|
@ -27,13 +27,12 @@
|
|||
#include "binding-types.h"
|
||||
|
||||
#include "sceneelement-binding.h"
|
||||
#include "disposable-binding.h"
|
||||
|
||||
template<class C>
|
||||
MRB_METHOD(viewportElementGetViewport)
|
||||
{
|
||||
ViewportElement *ve = getPrivateData<C>(mrb, self);
|
||||
|
||||
GUARD_EXC( ve->aboutToAccess(); )
|
||||
checkDisposed(mrb, self);
|
||||
|
||||
return getProperty(mrb, self, CSviewport);
|
||||
}
|
||||
|
@ -49,7 +48,10 @@ viewportElementInitialize(mrb_state *mrb, mrb_value self)
|
|||
mrb_get_args(mrb, "|o", &viewportObj);
|
||||
|
||||
if (!mrb_nil_p(viewportObj))
|
||||
{
|
||||
viewport = getPrivateDataCheck<Viewport>(mrb, viewportObj, ViewportType);
|
||||
disposableAddChild(mrb, viewportObj, self);
|
||||
}
|
||||
|
||||
/* Construct object */
|
||||
C *ve = new C(viewport);
|
||||
|
|
|
@ -49,8 +49,6 @@ MRB_METHOD(windowUpdate)
|
|||
return mrb_nil_value();
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "window"
|
||||
|
||||
DEF_PROP_OBJ_NIL(Window, Bitmap, Windowskin, CSwindowskin)
|
||||
DEF_PROP_OBJ_NIL(Window, Bitmap, Contents, CScontents)
|
||||
DEF_PROP_OBJ(Window, Rect, CursorRect, CScursor_rect)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue