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
|
@ -26,6 +26,7 @@
|
|||
#include "util.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
void initType(rb_data_type_struct &type,
|
||||
|
@ -97,6 +98,18 @@ void raiseRbExc(const Exception &exc)
|
|||
rb_raise(excClass, "%s", exc.msg.c_str());
|
||||
}
|
||||
|
||||
void
|
||||
raiseDisposedAccess(VALUE self)
|
||||
{
|
||||
const char *klassName = RTYPEDDATA_TYPE(self)->wrap_struct_name;
|
||||
char buf[32];
|
||||
|
||||
strncpy(buf, klassName, sizeof(buf));
|
||||
buf[0] = tolower(buf[0]);
|
||||
|
||||
rb_raise(getRbData()->exc[RGSS], "disposed %s", buf);
|
||||
}
|
||||
|
||||
int
|
||||
rb_get_args(int argc, VALUE *argv, const char *format, ...)
|
||||
{
|
||||
|
|
|
@ -87,17 +87,34 @@ static void freeInstance(void *inst)
|
|||
|
||||
#define INIT_TYPE(Klass) initType(Klass##Type, #Klass, freeInstance<Klass>)
|
||||
|
||||
void
|
||||
raiseDisposedAccess(VALUE self);
|
||||
|
||||
inline void
|
||||
checkDisposed(VALUE self)
|
||||
{
|
||||
if (!RTYPEDDATA_DATA(self))
|
||||
raiseDisposedAccess(self);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
static inline C *
|
||||
inline C *
|
||||
getPrivateData(VALUE self)
|
||||
{
|
||||
return static_cast<C*>(RTYPEDDATA_DATA(self));
|
||||
C *c = static_cast<C*>(RTYPEDDATA_DATA(self));
|
||||
|
||||
if (!c)
|
||||
raiseDisposedAccess(self);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
template<class C>
|
||||
static inline C *
|
||||
getPrivateDataCheck(VALUE self, const rb_data_type_struct &type)
|
||||
{
|
||||
/* We don't check for disposed here because any disposable
|
||||
* property is always also nullable */
|
||||
void *obj = Check_TypedStruct(self, &type);
|
||||
return static_cast<C*>(obj);
|
||||
}
|
||||
|
@ -286,17 +303,11 @@ rb_check_argc(int actual, int expected)
|
|||
return self; \
|
||||
}
|
||||
|
||||
/* If we're not binding a disposable class,
|
||||
* we want to #undef DEF_PROP_CHK_DISP */
|
||||
#define DEF_PROP_CHK_DISP \
|
||||
checkDisposed(k, DISP_CLASS_NAME);
|
||||
|
||||
#define DEF_PROP_OBJ(Klass, PropKlass, PropName, prop_iv) \
|
||||
RB_METHOD(Klass##Get##PropName) \
|
||||
{ \
|
||||
RB_UNUSED_PARAM; \
|
||||
Klass *k = getPrivateData<Klass>(self); (void) k; \
|
||||
DEF_PROP_CHK_DISP \
|
||||
checkDisposed(self); \
|
||||
return rb_iv_get(self, prop_iv); \
|
||||
} \
|
||||
RB_METHOD(Klass##Set##PropName) \
|
||||
|
@ -311,13 +322,14 @@ rb_check_argc(int actual, int expected)
|
|||
return propObj; \
|
||||
}
|
||||
|
||||
/* Object property with allowed NIL */
|
||||
/* Object property with allowed NIL
|
||||
* FIXME: Getter assumes prop is disposable,
|
||||
* because self.disposed? is not checked in this case.
|
||||
* Should make this more clear */
|
||||
#define DEF_PROP_OBJ_NIL(Klass, PropKlass, PropName, prop_iv) \
|
||||
RB_METHOD(Klass##Get##PropName) \
|
||||
{ \
|
||||
RB_UNUSED_PARAM; \
|
||||
Klass *k = getPrivateData<Klass>(self); (void) k; \
|
||||
DEF_PROP_CHK_DISP \
|
||||
return rb_iv_get(self, prop_iv); \
|
||||
} \
|
||||
RB_METHOD(Klass##Set##PropName) \
|
||||
|
@ -341,7 +353,6 @@ rb_check_argc(int actual, int expected)
|
|||
{ \
|
||||
RB_UNUSED_PARAM; \
|
||||
Klass *k = getPrivateData<Klass>(self); \
|
||||
DEF_PROP_CHK_DISP \
|
||||
return value_fun(k->get##PropName()); \
|
||||
} \
|
||||
RB_METHOD(Klass##Set##PropName) \
|
||||
|
|
|
@ -26,8 +26,6 @@
|
|||
#include "binding-util.h"
|
||||
#include "binding-types.h"
|
||||
|
||||
#define DISP_CLASS_NAME "bitmap"
|
||||
|
||||
DEF_TYPE(Bitmap);
|
||||
|
||||
void bitmapInitProps(Bitmap *b, VALUE self)
|
||||
|
@ -123,7 +121,7 @@ RB_METHOD(bitmapBlt)
|
|||
src = getPrivateDataCheck<Bitmap>(srcObj, BitmapType);
|
||||
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
|
||||
|
||||
GUARD_EXC( b->blt(x, y, *src, srcRect->toIntRect(), opacity); );
|
||||
GUARD_EXC( b->blt(x, y, src, srcRect->toIntRect(), opacity); );
|
||||
|
||||
return self;
|
||||
}
|
||||
|
@ -146,7 +144,7 @@ RB_METHOD(bitmapStretchBlt)
|
|||
destRect = getPrivateDataCheck<Rect>(destRectObj, RectType);
|
||||
srcRect = getPrivateDataCheck<Rect>(srcRectObj, RectType);
|
||||
|
||||
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), *src, srcRect->toIntRect(), opacity); );
|
||||
GUARD_EXC( b->stretchBlt(destRect->toIntRect(), src, srcRect->toIntRect(), opacity); );
|
||||
|
||||
return self;
|
||||
}
|
||||
|
|
|
@ -25,41 +25,74 @@
|
|||
#include "disposable.h"
|
||||
#include "binding-util.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(VALUE disp, VALUE child)
|
||||
{
|
||||
VALUE children = rb_iv_get(disp, "children");
|
||||
|
||||
if (NIL_P(children))
|
||||
{
|
||||
children = rb_ary_new();
|
||||
rb_iv_set(disp, "children", children);
|
||||
}
|
||||
|
||||
/* Assumes children are never removed until destruction */
|
||||
rb_ary_push(children, child);
|
||||
}
|
||||
|
||||
inline void
|
||||
disposableDisposeChildren(VALUE disp)
|
||||
{
|
||||
VALUE children = rb_iv_get(disp, "children");
|
||||
|
||||
if (NIL_P(children))
|
||||
return;
|
||||
|
||||
ID dispFun = rb_intern("dispose");
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
RB_METHOD(disposableDispose)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
Disposable *d = getPrivateData<C>(self);
|
||||
C *c = static_cast<C*>(RTYPEDDATA_DATA(self));
|
||||
|
||||
d->dispose();
|
||||
/* Nothing to do if already disposed */
|
||||
if (!c)
|
||||
return Qnil;
|
||||
|
||||
/* Inform core */
|
||||
c->wasDisposed();
|
||||
|
||||
disposableDisposeChildren(self);
|
||||
|
||||
delete c;
|
||||
setPrivateData(self, 0);
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
template<class C>
|
||||
RB_METHOD(disposableIsDisposed)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
Disposable *d = getPrivateData<C>(self);
|
||||
|
||||
return rb_bool_new(d->isDisposed());
|
||||
return rb_bool_new(RTYPEDDATA_DATA(self) == 0);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
static void disposableBindingInit(VALUE klass)
|
||||
{
|
||||
_rb_define_method(klass, "dispose", disposableDispose<C>);
|
||||
_rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
|
||||
}
|
||||
|
||||
inline void checkDisposed(Disposable *d, const char *klassName)
|
||||
{
|
||||
RbData *data = getRbData(); (void) data;
|
||||
|
||||
if (d->isDisposed())
|
||||
rb_raise(getRbData()->exc[RGSS], "disposed %s", klassName);
|
||||
_rb_define_method(klass, "disposed?", disposableIsDisposed);
|
||||
}
|
||||
|
||||
#endif // DISPOSABLEBINDING_H
|
||||
|
|
|
@ -153,9 +153,6 @@ RB_METHOD(FontSetName)
|
|||
return argv[0];
|
||||
}
|
||||
|
||||
#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 @@ RB_METHOD(planeInitialize)
|
|||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "plane"
|
||||
|
||||
DEF_PROP_OBJ_NIL(Plane, Bitmap, Bitmap, "bitmap")
|
||||
DEF_PROP_OBJ(Plane, Color, Color, "color")
|
||||
DEF_PROP_OBJ(Plane, Tone, Tone, "tone")
|
||||
|
|
|
@ -48,8 +48,6 @@ RB_METHOD(spriteInitialize)
|
|||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "sprite"
|
||||
|
||||
DEF_PROP_OBJ_NIL(Sprite, Bitmap, Bitmap, "bitmap")
|
||||
DEF_PROP_OBJ(Sprite, Rect, SrcRect, "src_rect")
|
||||
DEF_PROP_OBJ(Sprite, Color, Color, "color")
|
||||
|
|
|
@ -115,15 +115,11 @@ RB_METHOD(tilemapUpdate)
|
|||
return Qnil;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "tilemap"
|
||||
|
||||
RB_METHOD(tilemapGetViewport)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
Tilemap *t = getPrivateData<Tilemap>(self);
|
||||
|
||||
checkDisposed(t, DISP_CLASS_NAME);
|
||||
checkDisposed(self);
|
||||
|
||||
return rb_iv_get(self, "viewport");
|
||||
}
|
||||
|
@ -170,4 +166,3 @@ tilemapBindingInit()
|
|||
INIT_PROP_BIND( Tilemap, OX, "ox" );
|
||||
INIT_PROP_BIND( Tilemap, OY, "oy" );
|
||||
}
|
||||
|
||||
|
|
|
@ -65,11 +65,14 @@ RB_METHOD(viewportInitialize)
|
|||
wrapProperty(self, v->getColor(), "color", ColorType);
|
||||
wrapProperty(self, v->getTone(), "tone", ToneType);
|
||||
|
||||
/* 'elements' holds all SceneElements that become children
|
||||
* of this viewport, so we can dispose them when the viewport
|
||||
* is disposed */
|
||||
rb_iv_set(self, "elements", rb_ary_new());
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "viewport"
|
||||
|
||||
DEF_PROP_OBJ(Viewport, Rect, Rect, "rect")
|
||||
DEF_PROP_OBJ(Viewport, Color, Color, "color")
|
||||
DEF_PROP_OBJ(Viewport, Tone, Tone, "tone")
|
||||
|
|
|
@ -27,15 +27,14 @@
|
|||
#include "binding-types.h"
|
||||
|
||||
#include "sceneelement-binding.h"
|
||||
#include "disposable-binding.h"
|
||||
|
||||
template<class C>
|
||||
RB_METHOD(viewportElementGetViewport)
|
||||
{
|
||||
RB_UNUSED_PARAM;
|
||||
|
||||
ViewportElement *ve = getPrivateData<C>(self);
|
||||
|
||||
GUARD_EXC( ve->aboutToAccess(); );
|
||||
checkDisposed(self);
|
||||
|
||||
return rb_iv_get(self, "viewport");
|
||||
}
|
||||
|
@ -77,7 +76,10 @@ viewportElementInitialize(int argc, VALUE *argv, VALUE self)
|
|||
rb_get_args(argc, argv, "|o", &viewportObj RB_ARG_END);
|
||||
|
||||
if (!NIL_P(viewportObj))
|
||||
{
|
||||
viewport = getPrivateDataCheck<Viewport>(viewportObj, ViewportType);
|
||||
disposableAddChild(viewportObj, self);
|
||||
}
|
||||
|
||||
/* Construct object */
|
||||
C *ve = new C(viewport);
|
||||
|
|
|
@ -51,8 +51,6 @@ RB_METHOD(windowUpdate)
|
|||
return Qnil;
|
||||
}
|
||||
|
||||
#define DISP_CLASS_NAME "window"
|
||||
|
||||
DEF_PROP_OBJ_NIL(Window, Bitmap, Windowskin, "windowskin")
|
||||
DEF_PROP_OBJ_NIL(Window, Bitmap, Contents, "contents")
|
||||
DEF_PROP_OBJ(Window, Rect, CursorRect, "cursor_rect")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue