0%

cocos2d-x lua bridge

三个部分的代码模块,基于观察者模式,初始化代码添加观察者,等待lua消息。

lua:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
luabridge = {}

luabridge.callback = nil

function luaCallback(param)
local callback = luabridge.callback
if callback then
callback(param)
end
end

function luabridge.call(msg)
local platform = cc.Application:getInstance():getTargetPlatform()
if platform == cc.PLATFORM_OS_ANDROID then
local class = "org/vizee/android/jli"
local method = "callJava"
local args = {
msg,
function (data)
end
}
local sign = "(Ljava/lang/String;I)V"
local luaj = require("luaj")
local ok, ret = luaj.callStaticMethod(class, method, args, sign)
return ok
elseif platform == cc.PLATFORM_OS_IPHONE then
print("call objc: ", msg)
local class = "ocli"
local method = "callObjc"
local luaoc = require("luaoc")
local ok, ret = luaoc.callStaticMethod(class, method, {msg = msg})
return ok
end
return nil
end

if cc.Application:getInstance():getTargetPlatform() == cc.PLATFORM_OS_IPHONE then
require("luaoc").callStaticMethod("ocli", "callObjc", { callback = luaCallback })
end

android:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.vizee.android;

import java.util.HashMap;

import org.cocos2dx.lib.Cocos2dxLuaJavaBridge;

public class jli {

public interface Observer {
String onMessage(String msg);
}

private static HashMap<String, Observer> observers = new HashMap<String, Observer>();

private static String dispatchMessage(String msg) {
Observer observer = observers.get(msg);
return observer != null ? observer.onMessage(msg) : "";
}

public static void setObserver(String msg, Observer observer) {
if (handler == null) {
observers.remove(msg);
} else {
observers.put(msg, observer);
}
}

public static void callJava(String msg, int luaFunctionId) {
Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaFunctionId, dispatchMessage(msg));
Cocos2dxLuaJavaBridge.releaseLuaFunction(luaFunctionId);
}

public static void callLua(String param) {
Cocos2dxLuaJavaBridge.callLuaGlobalFunctionWithString("luaCallback", param);
}
}

ios:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// ocli.h
// luaocbridge
//
// Created by vizee on 15/7/29.
//
//

#ifndef luatest_ocli_h
#define luatest_ocli_h

#import <Foundation/Foundation.h>

@protocol ocliObserver <NSObject>

@required
- (void) onMessage:(NSString *)msg;
@end

@interface ocli : NSObject {
}

+ (void) setObserver:(id<ocliObserver>)observer forMsg:(NSString *) msg;
+ (NSDictionary *) callObjc:(NSDictionary *)dict;
+ (void) callLua:(const char *)param;
@end

#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// ocli.mm
// luaocbridge
//
// Created by vizee on 15/7/28.
//
//

#import "ocli.h"
#import "platform/ios/CCLuaObjcBridge.h"

static int _luaCallback = 0;
static NSMutableDictionary *_observers = [[NSMutableDictionary alloc] init];

@implementation ocli

+ (void) setObserver:(id<ocliObserver>)observer forMsg:(NSString *)msg {
if (observer == nil) {
[_observers removeObjectForKey:msg];
} else {
[_observers setValue:observer forKey:msg];
}
}

+ (NSDictionary *) callObjc:(NSDictionary *)dict {
NSString *msg = [dict objectForKey:@"msg"];
if (!msg || [msg length] == 0) {
NSNumber *callbackId = [dict objectForKey:@"callback"];
if (!callbackId) {
if (_luaCallback != 0) {
cocos2d::LuaObjcBridge::releaseLuaFunctionById(_luaCallback);
_luaCallback = 0;
}
return nil;
} else {
_luaCallback = [callbackId intValue];
cocos2d::LuaObjcBridge::retainLuaFunctionById(_luaCallback);
}
} else {
id<ocliObserver> observer = [_observers objectForKey:msg];
if (observer) {
[observer onMessage:msg];
}
}
return nil;
}

+ (void) callLua:(const char *)param {
cocos2d::LuaObjcBridge::pushLuaFunctionById(_luaCallback);
cocos2d::LuaStack *stack = cocos2d::LuaObjcBridge::getStack();
stack->pushString(param);
stack->executeFunction(1);
}

@end