Skip to content

Instantly share code, notes, and snippets.

@adamlwgriffiths
Created March 30, 2012 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamlwgriffiths/2250978 to your computer and use it in GitHub Desktop.
Save adamlwgriffiths/2250978 to your computer and use it in GitHub Desktop.
Hacks to get CocosBuilder working with Cocos1.x
<snip>
#import "Cocos+DefaultInits.h"
-(void) tryToRunFirstScene
{
CCDirector* director = [CCDirector sharedDirector];
// swizzle in our CCMenu fix for CCBuilder
{
MethodSwizzle( [CCLabelTTF class], @selector(init), @selector(initWithoutParams) );
MethodSwizzle( [CCMenu class], @selector(init), @selector(initWithoutParams) );
}
// try to run first scene
if (director.isSceneStackEmpty)
{
<snip>
//
// CCNode+KVC.h
//
// Created by Adam Griffiths on 30/03/12.
// Copyright (c) 2012 Twisted Pair Development. All rights reserved.
//
@interface CCNode (KVC)
- (void)setValue:(id)value forKey:(NSString *)key;
@end
//
// CCNode+KVC.m
//
// Created by Adam Griffiths on 30/03/12.
// Copyright (c) 2012 Twisted Pair Development. All rights reserved.
//
#import "CCNode+KVC.h"
@implementation CCNode (KVC)
- (void)setValue:(id)value forKey:(NSString *)key
{
@try
{
NSLog( @"[%@] INFO: Setting value for key %@", [self class], key );
[super setValue:value forKey:key];
}
@catch (NSException *e)//(NSUnknownKeyException *e)
{
@try
{
NSLog( @"[%@] WARNING: Key %@ not valid, trying %@_", [self class], key, key );
[super setValue:value forKey:[NSString stringWithFormat:@"%@_", key]];
NSLog( @"Success" );
}
@catch (NSException *e)
{
// we can't fix this
NSLog( @"[%@] ERROR: Key %@ failed!", [self class], key );
}
}
}
@end
//
// Cocos+DefaultInits.h
//
// Created by Adam Griffiths on 30/03/12.
// Copyright (c) 2012 Twisted Pair Development. All rights reserved.
//
@interface CCMenu (Constructor)
- (id)initWithoutParams;
@end
@interface CCLabelTTF (Constructor)
- (id)initWithoutParams;
@end
//
// Cocos+DefaultInits.m
//
// Created by Adam Griffiths on 30/03/12.
// Copyright (c) 2012 Twisted Pair Development. All rights reserved.
//
#import "Cocos+DefaultInits.h"
@implementation CCMenu (Constructor)
- (id)initWithoutParams
{
return [self initWithItems:nil vaList:nil];
}
@end
@implementation CCLabelTTF (Constructor)
- (id)initWithoutParams
{
self = [self initWithString:@"OH NO" fontName:@"arial" fontSize:12];
return self;
}
@end
//
// SelectorSwizzle.c
//
#import "SelectorSwizzle.h"
#import </usr/include/objc/objc-class.h>
void MethodSwizzle(Class aClass, SEL orig_sel, SEL alt_sel)
{
Method orig_method = nil, alt_method = nil;
// First, look for the methods
orig_method = class_getInstanceMethod(aClass, orig_sel);
alt_method = class_getInstanceMethod(aClass, alt_sel);
// If both are found, swizzle them
if ((orig_method != nil) && (alt_method != nil))
{
char *temp1;
IMP temp2;
temp1 = orig_method->method_types;
orig_method->method_types = alt_method->method_types;
alt_method->method_types = temp1;
temp2 = orig_method->method_imp;
orig_method->method_imp = alt_method->method_imp;
alt_method->method_imp = temp2;
}
}
//
// SelectorSwizzle.h
//
#import </usr/include/objc/objc-class.h>
void MethodSwizzle(Class aClass, SEL orig_sel, SEL alt_sel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment