/
beamngApi.js
149 lines (139 loc) · 3.93 KB
/
beamngApi.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
angular.module('beamng.stuff')
/**
* @ngdoc value
* @name beamng.stuff:apiCallbacks
*
* @description
* Storage for callbacks to be called from the various other game modules.
**/
.value({ 'apiCallbacks': {} })
/**
* @ngdoc service
* @name beamng.stuff:bngApi
* @requires beamng.stuff:apiCallbacks
*
* @description
* This is used for communication to the other module of the game,
* in order to avoid direct usage of the <emph>beamng</emph> object.
* Almost all functions can be paired with a callback that is stored
* in the {@link beamng.stuff:apiCallbacks apiCallbacks} value.
**/
.service('bngApi', ['apiCallbacks', function (apiCallbacks) {
var callbackId = Math.floor(Math.random() * 60000);
return {
/**
* @ngdoc method
* @name .#engineScript
* @propertyOf .
* @param {string} cmd
* @param {function} callback
*
* @description
* Sends a command to the game engine.
**/
engineScript: function (cmd, callback) {
if (!callback) {
beamng.sendGameEngine(cmd);
return;
}
apiCallbacks[++callbackId] = callback;
cmdStr = 'beamNGExecuteJS(bngApiCallback(' + callbackId + ',\'" @ strreplace(' + cmd + ',"\'","\\\\\'") @ "\')");';
beamng.sendGameEngine(cmdStr);
},
/**
* @ngdoc method
* @name .#activeObjectLua
* @propertyOf .
* @param {string} cmd
* @param {function} callback
*
* @description
* Sends a command to the active Lua object.
**/
activeObjectLua: function (cmd, callback) {
if (!callback) {
beamng.sendActiveObjectLua(cmd);
return;
}
apiCallbacks[++callbackId] = callback;
var cmdStr = 'obj:executeJS("bngApiCallback(' + callbackId + ',".. encodeJson(' + cmd + ') ..")");';
beamng.sendActiveObjectLua(cmdStr);
},
/**
* @ngdoc method
* @name .#engineLua
* @propertyOf .
* @param {string} cmd
* @param {function} callback
*
* @description
* Sends a command to the Lua engine.
**/
engineLua: function (cmd, callback) {
if (!callback) {
beamng.sendEngineLua(cmd);
return;
}
apiCallbacks[++callbackId] = callback;
var cmdStr = 'be:executeJS("bngApiCallback(' + callbackId + ',".. encodeJson(' + cmd + ') ..")");';
beamng.sendEngineLua(cmdStr);
},
/**
* @ngdoc method
* @name .#systemLua
* @propertyOf .
* @param {string} cmd
*
* @description
* Sends a command to Lua system. There is no callback option in this function.
**/
systemLua: function (cmd) {
beamng.sendSystemLua(cmd);
},
/**
* @ngdoc method
* @name .#serializeToLua
* @propertyOf .
* @param {object} obj
*
* @description
* Converts an object to a format compatible with Lua functions.
*
* @return
* A string representation of the object that can be directly used
* from Lua functions.
**/
serializeToLua: function (obj) {
var tmp;
if (obj === undefined) { return ''; } //nil';
switch (obj.constructor) {
case String:
return '"' + obj.replace(/\"/g, '\'') + '"';
case Number:
return isFinite(obj) ? obj.toString() : null;
case Boolean:
return obj ? 'true' : 'false';
case Array:
tmp = [];
for (var i = 0; i < obj.length; i++) {
if (obj[i] != null) {
tmp.push(this.serializeToLua(obj[i]));
}
}
return '{' + tmp.join(',') + '}';
default:
if (typeof obj == 'object') {
tmp = [];
for (var attr in obj) {
if (typeof obj[attr] != 'function') {
tmp.push('["' + attr + '"]=' + this.serializeToLua(obj[attr]));
}
}
return '{' + tmp.join(',') + '}';
} else {
return obj.toString();
}
}
}
};
}]);