-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdreemnodejs.js
430 lines (381 loc) · 15.6 KB
/
dreemnodejs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* Copyright 2015-2016 Teem2 LLC. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions and limitations under the License.*/
// dreemnode.js Dreem client for headless and dali.
// Supported arguments:
//
// -server Path to Dreem server. default: http://localhost:8080
// -composition Composition to load. default: editor_test
// -screen Name of composition screen to use. default: default
// -reload If specified, reload the running application when server
// changes are found. default: disabled
// -dali Use dali sprite. default: Use headless sprite
// -preview If specified, enable dynamic editing support to view live
// editor changes as they are made. default: disabled
// -loading If specified, display a dali loading screen when this
// application launches. It is removed when the first actor
// is added to the screen. default: disabled
//
// Example: (dreem server running at 192.168.1.19)
// node dreemnodejs.js -server http://192.168.1.19:8080 -composition editor_test -dali -reload -preview -loading
var require = require('./define.js');
var NodeWebSocket = require('./core/nodewebsocket');
global.WebSocket = NodeWebSocket;
// Create a colorization function (ANSI output) use ~rb~ to set color in string
// and ~~ to end colorization.
console.color = (function colorize() {
var colors = {
bl: "30", // black
bo: "1", // bold current color
r: "0;31", // red
g: "0;32", // green
y: "0;33", // yellow
b: "0;34", // blue
m: "0;35", // magenta
c: "0;36", // cyan
w: "0;37", // white
br: "1;31", // bold red
bg: "1;32", // bold green
by: "1;33", // bold yellow
bb: "1;34", // bold blue
bm: "1;35", // bold magenta
bc: "1;36", // bold cyan
bw: "1;37" // bold white
};
return function() {
for (var v = Array.prototype.slice.call(arguments), i = 0; i < v.length; i++) {
v[i] = String(v[i]).replace(/~(\w*)~/g, function(m, a) {
return "\033[" + (colors[a] || 0) + "m";
}) + "\033[0m";
process.stdout.write(v[i] + "\n");
}
}
})();
var argv = process.argv,
args = {};
for (var lastkey = '', arg, i = 0; i < argv.length; i++) {
arg = argv[i];
if (arg.charAt(0) == '-') {
lastkey = arg;
args[lastkey] = true;
} else {
args[lastkey] = arg;
}
}
var server = "http://localhost:8080";
var composition = "example_spirallayout";
var screen = "default";
var reload = false;
if (args["-server"]) server = args["-server"];
// If preview exists, add preview to urls when communicating to the server.
var preview = args["-preview"];
// If loading exists, show a loading screen immediately, and remove when
// the application ui starts.
var loading = args["-loading"];
if (args["-composition"]) {
composition = args["-composition"];
} else {
console.log("not starting without composition! use \"-composition composition\" to load composition.dre from the server!");
process.exit();
}
if (args["-screen"]) screen = args["-screen"];
if (args["-reload"]) reload = process.argv.join(' ');
if (args["-dali"]) {
define.SPRITE = "$ROOT/lib/dr/sprite_daliruntime";
console.color("*** ~bw~D~bb~r~bg~e~by~e~br~m ~bw~Dali Runtime~~ ***");
} else {
define.SPRITE = "$ROOT/lib/dr/sprite_headless";
console.color("*** ~bw~D~bb~r~bg~e~by~e~br~m ~bw~Headless Runtime~~ ***");
}
console.color("** using server: ~by~" + server + "~~");
console.color("** using composition: ~by~" + composition + "~~");
console.color("** using screen: ~by~" + screen + "~~");
define.env = "v8";
var http = require('http');
var url = require('url');
var fs = require('fs');
// Some directories must be manually created
// (ex: /dalicache is removed)
var dirs = ["./dalicache", "./dalicache/classes", "./dalicache/classes/behavior", "./dalicache/compositions", "./dalicache/resources"];
for (var i in dirs) {
var dir = dirs[i];
console.log('dir', dir, fs.existsSync(dir));
if (fs.existsSync(dir) == false) {
fs.mkdir(dir);
}
}
define.MAIN = '';
var loadedscripts = [];
// the main dependency download queue counter
var downloads = 0;
var script_tags = {};
var loaded = false;
function startMain() {
Unload();
//the next line is a hack... but we need it for now..
var JP = require("json-path");
global.JsonPath = JP;
define.ROOTSERVER = server;
define.COMPOSITIONROOT = "./compositions/" +define.filePath( composition )+ "/";
define.ROOTURL = "compositions/"+ composition + ".dre";
define.ROOT = define.filePath(module.filename.replace(/\\/g, '/'));
define.BUILD = "$ROOT/dalicache";
define.MAIN = "$BUILD/compositions/" + composition + ".dre.screens." + screen + ".js";
if (preview) {
define.COMPOSITIONROOT = UpdatePreview(define.COMPOSITIONROOT);
define.ROOTURL = UpdatePreview(define.ROOTURL);
define.BUILD = UpdatePreview(define.BUILD);
define.MAIN = UpdatePreview(define.MAIN);
}
var F = require(define.MAIN)();
define.startMain();
loaded = true;
}
function UpdatePreview(str) {
return str.replace(/compositions/g, 'preview/compositions');
}
function Unload() {
if (loaded == false) return;
console.color("~~** ~rb~Unloading!~~");
}
function Reload() {
// Reload the application (by replacing the running program with another
// instance of ourself) if reload is set.
if (reload) {
console.log('Reloading application: ' + reload);
var kexec = require('kexec');
kexec(reload);
}
}
// Retrieve the main application file from the server to detect if the file
// is in edit mode. A reload is invoked ONLY if the file is not in edit
// mode. No changes to the server are required because this is only needed
// in the short term.
function selectiveReload() {
script_url = server + "/build/compositions/" + composition + ".dre.screens." + screen + ".js";
if (preview) script_url = UpdatePreview(script_url);
var script = {}
var scripturl = url.parse(script_url);
var base_path = define.filePath(script_url);
script.src = script_url;
script_tags[script_url] = script;
http.get({
host: scripturl.hostname,
port: scripturl.port,
path: scripturl.path
}, function(res) {
res.src = script_url;
res.data = "";
res.on('data', function(buf) {
this.data += buf;
});
res.on('end', function() {
var editable = this.data.indexOf('editable');
//console.log('*****onend. File loaded', editable);
if (editable == -1) {
// Reload because the application is not in edit mode
Reload();
}
});
}.bind(this));
}
function CreateNeededFoldersForFilePath(path) {
//console.log(path);
var S = path.split('/');
var pathsofar = '';
if (path[0] == '/') pathsofar = '/';
for (i = 1; i < S.length - 1; i++) {
pathsofar += S[i] + '/';
//console.log(pathsofar);
if (fs.existsSync(pathsofar) == false) {
fs.mkdirSync(pathsofar);
}
}
}
function localExpand(path) {
var originalroot = define.ROOT;
var originalbuild = define.BUILD;
define.BUILD = "$ROOT/dalicache";
define.ROOT = define.filePath(module.filename.replace(/\\/g, '/'));
var ret = define.expandVariables(path)
define.ROOT = originalroot;
define.BUILD = originalbuild;
return ret;
}
function remoteExpand(path) {
var originalroot = define.ROOT;
var originalbuild = define.BUILD;
define.BUILD = "$ROOT/build";
define.ROOT = server; // + "/" + composition + "/default";
var ret = define.expandVariables(path)
define.ROOT = originalroot;
define.BUILD = originalbuild;
return ret;
}
function requireWalker(script_url, from_file, save_path) {
var script = {}
var scripturl = url.parse(script_url);
var base_path = define.filePath(script_url);
script.src = script_url;
script_tags[script_url] = script;
downloads++;
http.get({
host: scripturl.hostname,
port: scripturl.port,
path: scripturl.path
}, function(res) {
res.src = script_url;
res.data = "";
res.on('data', function(buf) {
this.data += buf;
});
res.on('end', function() {
if (save_path.indexOf("/dalicache") > -1) {
CreateNeededFoldersForFilePath(save_path);
fs.writeFileSync(save_path, this.data);
}
define.findRequires(this.data).forEach(function(path) {
// Make path absolute and process variables
var dep_path = define.joinPath(base_path, remoteExpand(path));
var local_dep_path = define.joinPath(base_path, localExpand(path));
// automatic .js appending if not given
if (dep_path.indexOf(".js") != dep_path.length - 3) dep_path += '.js';
// load it
if (!script_tags[dep_path]) requireWalker(dep_path, script_url, local_dep_path);
});
if (!--downloads) {
console.color('~~** ~bg~download complete~~.');
startMain(); // no more deps
}
});
}.bind(this));
loadedscripts.push(script);
}
var main_file = "./dalicache/compositions/" + composition + ".dre.screens." + screen + ".js";
if (preview) main_file = UpdatePreview(main_file);
console.log('main_file', main_file);
function LoadAll() {
var originalroot = define.ROOT;
define.ROOT = server + "/" + composition + "/default";
define.MAIN = server + "/build/compositions/" + composition + ".dre.screens." + screen + ".js";
if (preview) {
define.ROOT = UpdatePreview(define.ROOT);
define.MAIN = UpdatePreview(define.MAIN);
}
requireWalker(define.MAIN, define.ROOT, main_file);
}
var sock;
var sockethost = server + "/compositions/" + composition + ".dre"
if (preview) sockethost = UpdatePreview(sockethost);
var reconnect = function() {
// put up websocket.
console.color("~~** reconnect started");
if (sock) sock.close();
sock = new NodeWebSocket(sockethost);
sock.onError = function(msg) {
console.log("error in socket!");
setTimeout(function() {
reconnect();
}.bind(this), 500);
}.bind(this);
sock.onMessage = function(msg) {
try {
msg = JSON.parse(msg);
} catch (e) {}
// Dynamic editing support messages
switch (msg.type) {
case 'undostack_do':
case 'undostack_undo':
case 'undostack_redo':
case 'undostack_reset':
console.log('*** undostack message', msg.type);
break;
}
if (msg.type == "sessionCheck") {
console.color('~~** ~by~Files updated on server: downloading~~.');
if (loaded) {
// selectiveReload only loads the file when not in edit mode
selectiveReload();
//Reload();
} else {
LoadAll();
}
}
}.bind(this);
sock.onConnect = function() {
console.color('~~** ~bg~Connected to server~~.');
}
sock.onClose = function() {
setTimeout(function() {
reconnect();
}.bind(this), 500);
}.bind(this);
};
// dali setup code
var dalicontainer;
var dalihost;
var RootActor;
if (args["-dali"]) {
var window = {
x: 0,
y: 0,
// NOTE: I'm using a smaller size while debugging dynamic editing
width: 1920, //1067,
height: 1080, //600,
transparent: false,
name: 'Dreem Dali Runtime: ' + composition
};
var viewMode = {
'stereoscopic-mode': 'mono', // stereo-horizontal, stereo-vertical, stereo-interlaced,
'stereo-base': 65 // Distance in millimeters between left/right cameras typically between (50-70mm)
};
var options = {
'window': window,
'view-mode': viewMode,
}
console.log("** loading Dali")
try {
var exists = fs.statSync('/etc/tizen-release').isFile();
console.log('Running Tizen, requiring dali from: /usr/lib/node_modules/npm/node_modules/dali/dali')
global.dali = require('/usr/lib/node_modules/npm/node_modules/dali/dali')(options)
} catch (err) {
console.log('Running Ubuntu, requiring dali from ./dalinode/dali')
global.dali = require('./dalinode/dali')(options)
}
// Show/hide a loading page
global.show_loading_page = function() {
var sz = dali.stage.getSize();
dali.stage.setBackgroundColor([0.5, 0.5, 0.5, 1]);
global.loading_page = new dali.Control('TextField');
loading_page.text = "Loading...";
// Compute scale to fit the text to the screen size
var lpsz = loading_page.getNaturalSize();
var scale = Math.min (sz.x / lpsz.x, sz.y / lpsz.y);
var ptsize = loading_page.pointSize * scale / (dali.stage.getDpi().x / 72);
loading_page.pointSize = ptsize;
loading_page.textColor = [0, 0, 0, .25];
loading_page.parentOrigin = dali.CENTER;
loading_page.anchorPoint = dali.CENTER;
loading_page.horizontalAlignment = 'CENTER';
loading_page.verticalAlignment = 'CENTER';
dali.stage.add(loading_page);
};
global.remove_loading_page = function() {
if (global.loading_page) {
//console.log('Removing loading_page');
dali.stage.remove(global.loading_page);
global.loading_page = null;
}
};
// Show a loading page
if (loading)
global.show_loading_page();
// global.dali.stage.add(new global.dali.ImageActor(new global.dali.ResourceImage({url:"./img/shoarma.jpg"})));
console.color("~~** Dali loaded");
global.dalihost = require('./lib/dr/sprite_daliruntime/dalihost.js');
global.dalihost.init();
}
// attempt to connect to the notifier service, download initial version and download again if changes have been made.
reconnect();
//LoadAll();