-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathNative.ts
120 lines (102 loc) · 2.94 KB
/
Native.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
函数定义:
andrid -> org.cocos2dx.javascript.Native.SayHello(String helloString, String cbName);
ios -> Native.SayHello : (NSString*) helloString
arg1 : (NSString*) cbName;
js调用写法
native.call("SayHello", "hello world", (ok) => { })
native.callClass("Native", "SayHello", "hello world", (ok) => { })
注意:
Android 这边 接受数字的函数都要用float。
double 在 cocos c++ 代码中未实现
*/
export enum AndrodSign {
Void = "V",
String = "Ljava/lang/String;",
Boolean = "Z",
Float = "F",
Double = "D",
Int = "I",
}
export class Native {
static cbIdx : number = 0
static cbs = {}
static defaultClass = "Native"
static defaultPackage = "com/cocos2dx/javascript/"
private static callback(cbID: string, ...args: any[]) {
let func = this.cbs[cbID]
if (func) {
delete this.cbs[cbID]
func(...args)
} else {
cc.log("no func ", cbID)
}
}
private static _newCB(f) {
this.cbIdx++
let cbID = "" + this.cbIdx
this.cbs[cbID] = f
return cbID
}
static getStr(clazz: string, method: string, ...args: any[]) {
return this.callWithPackage(this.defaultPackage, clazz, method, AndrodSign.String, ...args)
}
static call(method: string, ...args: any[]) {
this.callWithPackage(this.defaultPackage, this.defaultClass, method, AndrodSign.Void, ...args)
}
static callClass(clazz: string, method: string, ...args: any[]) {
this.callWithPackage(this.defaultPackage, clazz, method, AndrodSign.Void, ...args)
}
static callWithReturnType(clazz: string, method: string, returnTypeAndroid: AndrodSign, ...args: any[]) {
return this.callWithPackage(this.defaultPackage, clazz, method, returnTypeAndroid, ...args)
}
static callWithPackage(pkg: string, clazz: string, method: string, returnTypeAndroid: AndrodSign, ...args: any[]) {
let real_args = []
cc.log("clazz:", clazz)
cc.log("method:", method)
if (cc.sys.os == cc.sys.OS_ANDROID) {
var sig : string = ""
for (let i = 0; i< args.length; i++) {
let v = args[i]
switch (typeof v) {
case 'boolean':
sig += AndrodSign.Boolean;
real_args.push(v + "");
break;
case 'string':
sig += AndrodSign.String;
real_args.push(v);
break;
case 'number':
sig += AndrodSign.Float;
real_args.push(v);
break;
case 'function':
sig += AndrodSign.String;
real_args.push(this._newCB(v));
break;
}
}
return jsb.reflection.callStaticMethod(pkg + clazz, method, "("+sig+")" + returnTypeAndroid, ...real_args)
}
if (cc.sys.os == cc.sys.OS_IOS) {
for (let i = 0; i < args.length; i++) {
let v = args[i]
if (typeof v == "function") {
real_args.push(this._newCB(v))
} else {
real_args.push(v)
}
if (i == 0) {
method += ":"
} else {
method += "arg" + i + ":"
}
}
//@ts-ignore
return jsb.reflection.callStaticMethod(clazz, method, ...real_args)
}
}
}
//@ts-ignore
window.native = Native