Skip to content

Commit 3a53b2c

Browse files
committed
Updated some code
1 parent a183c9a commit 3a53b2c

13 files changed

Lines changed: 549 additions & 238 deletions

File tree

Gruntfile.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ module.exports = (grunt) ->
141141
watch:
142142
scripting:
143143
files: ['src/scripting/**/*', '!node_modules']
144-
tasks: ['build-scripting']
144+
tasks: ['build:scripting']
145145
core:
146146
files: ['src/**/*', '!node_modules', '!src/scripting/**/*']
147147
tasks: ['build']

dist/CommentCoreLibrary.js

Lines changed: 103 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,51 +1106,112 @@ var CommentManager = (function() {
11061106
})();
11071107

11081108
/**
1109-
* Comment Filters Module Simplified (only supports modifiers & types)
1109+
* Comment Filters Module Simplified
11101110
* @license MIT
11111111
* @author Jim Chen
11121112
*/
1113-
function CommentFilter() {
1114-
this.modifiers = [];
1115-
this.runtime = null;
1116-
this.allowTypes = {
1117-
"1":true,
1118-
"4":true,
1119-
"5":true,
1120-
"6":true,
1121-
"7":true,
1122-
"8":true,
1123-
"17":true
1124-
};
1125-
this.doModify = function(cmt){
1126-
for(var k=0;k<this.modifiers.length;k++){
1127-
cmt = this.modifiers[k](cmt);
1128-
}
1129-
return cmt;
1130-
};
1131-
this.beforeSend = function(cmt){
1132-
return cmt;
1133-
}
1134-
this.doValidate = function(cmtData){
1135-
if(!this.allowTypes[cmtData.mode])
1136-
return false;
1137-
return true;
1138-
};
1139-
this.addRule = function(rule){
1140-
1141-
};
1142-
this.addModifier = function(f){
1143-
this.modifiers.push(f);
1144-
};
1145-
this.runtimeFilter = function(cmt){
1146-
if(this.runtime == null)
1147-
return cmt;
1148-
return this.runtime(cmt);
1149-
};
1150-
this.setRuntimeFilter = function(f){
1151-
this.runtime = f;
1152-
}
1153-
}
1113+
var CommentFilter = (function () {
1114+
1115+
function _match (rule, cmtData) {
1116+
var path = rule.subject.split('.');
1117+
var extracted = cmtData;
1118+
while (path.length > 0) {
1119+
var item = path.shift();
1120+
if (item === '') {
1121+
continue;
1122+
}
1123+
if (extracted.hasOwnProperty(item)) {
1124+
extracted = extracted[item];
1125+
}
1126+
if (extracted === null || typeof extracted === 'undefined') {
1127+
extracted = null;
1128+
break;
1129+
}
1130+
}
1131+
if (extracted === null) {
1132+
// Null precondition implies anything
1133+
return true;
1134+
}
1135+
switch (rule.op) {
1136+
case '~':
1137+
case 'regexp':
1138+
return (new RegExp(rule.value)).test(extracted.toString());
1139+
case '=':
1140+
case 'eq':
1141+
return rule.value === extracted.toString();
1142+
case 'NOT':
1143+
return !_match(rule.value, cmtData);
1144+
case 'AND':
1145+
if (Array.isArray(rule.value)) {
1146+
return rule.value.every(function (r) {
1147+
return _match(r, cmtData);
1148+
});
1149+
} else {
1150+
return false;
1151+
}
1152+
case 'OR':
1153+
if (Array.isArray(rule.value)) {
1154+
return rule.value.some(function (r) {
1155+
return _match(r, cmtData);
1156+
});
1157+
} else {
1158+
return false;
1159+
}
1160+
default:
1161+
return false;
1162+
}
1163+
}
1164+
1165+
function CommentFilter() {
1166+
this.rules = [];
1167+
this.modifiers = [];
1168+
this.allowUnknownTypes = true;
1169+
this.allowTypes = {
1170+
'1': true,
1171+
'2': true,
1172+
'4': true,
1173+
'5': true,
1174+
'6': true,
1175+
'7': true,
1176+
'8': true,
1177+
'17': true
1178+
};
1179+
}
1180+
1181+
CommentFilter.prototype.doModify = function (cmt) {
1182+
for (var k=0; k < this.modifiers.length; k++) {
1183+
cmt = this.modifiers[k](cmt);
1184+
}
1185+
return cmt;
1186+
};
1187+
1188+
CommentFilter.prototype.beforeSend = function (cmt) {
1189+
return cmt;
1190+
}
1191+
1192+
CommentFilter.prototype.doValidate = function (cmtData) {
1193+
if (cmtData.mode.toString() in this.allowTypes &&
1194+
!this.allowTypes[cmtData.mode.toString()]) {
1195+
return false;
1196+
}
1197+
return this.rules.every(function (rule) {
1198+
// Decide if matched
1199+
var matched = _match(rule, cmtData);
1200+
return rule.mode === 'accept' ? matched : !matched;
1201+
});
1202+
};
1203+
1204+
CommentFilter.prototype.addRule = function (rule) {
1205+
if (rule.mode !== 'accept' && rule.mode !== 'reject') {
1206+
throw new Error('Rule must be of accept type or reject type.');
1207+
}
1208+
this.rules.push(rule);
1209+
};
1210+
1211+
CommentFilter.prototype.addModifier = function (f) {
1212+
this.modifiers.push(f);
1213+
};
1214+
})();
11541215

11551216
/**
11561217
* Comment Provider

dist/CommentCoreLibrary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/scripting/Host.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,12 @@ var CCLScripting = function(workerUrl){
171171
var WorkerHook = function(event){
172172
try{
173173
var resp = JSON.parse(event.data);
174-
}catch(e){
175-
console.log(e);
174+
} catch(e) {
175+
if (e.stack) {
176+
scripter.logger.error(e.stack);
177+
} else {
178+
scripter.logger.error(e);
179+
}
176180
return;
177181
}
178182
if(resp.channel === ""){
@@ -209,8 +213,12 @@ var CCLScripting = function(workerUrl){
209213
}
210214
break;
211215
}
216+
case ':debug':{
217+
scripter.logger.log(JSON.stringify(resp.payload));
218+
break;
219+
}
212220
default:{
213-
console.log(resp);
221+
scripter.logger.log(JSON.stringify(resp));
214222
break;
215223
}
216224
}

dist/scripting/OOAPI.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var __OOAPI = new function () {
88
var channels = {};
99

1010
function dispatchMessage (msg) {
11-
if (channels[msg.channel]) {
11+
if (channels.hasOwnProperty(msg.channel)) {
1212
for(var i = 0; i < channels[msg.channel].listeners.length; i++) {
1313
try {
1414
channels[msg.channel].listeners[i](msg.payload);
@@ -20,6 +20,9 @@ var __OOAPI = new function () {
2020
}
2121
}
2222
}
23+
} else {
24+
__trace('Got message on channel "' + msg.channel +
25+
'" but channel does not exist.', 'warn');
2326
}
2427
};
2528

@@ -33,10 +36,11 @@ var __OOAPI = new function () {
3336
__trace(e, 'err');
3437
return;
3538
}
36-
if (msg && msg.channel) {
39+
if (msg !== null && msg.hasOwnProperty('channel') &&
40+
typeof msg.channel === 'string') {
3741
dispatchMessage(msg);
3842
} else {
39-
_trace(msg, 'warn');
43+
__trace(msg, 'warn');
4044
}
4145
});
4246

@@ -111,7 +115,7 @@ function __channel (id, payload, callback) {
111115
'payload': payload,
112116
'callback': true
113117
}));
114-
__OOAPI.addListenerChannel(id, callback, true);
118+
__OOAPI.addListenerChannel(id, callback);
115119
};
116120

117121
function __schannel (id, callback) {

dist/scripting/Worker.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,55 @@ var __OOAPI;
22

33
importScripts("OOAPI.js");
44

5-
if(!__OOAPI){
6-
console.log("Error: OOAPI Not Loaded");
7-
self.close();
5+
if (!__OOAPI) {
6+
console.log("Error: OOAPI Not Loaded");
7+
self.close();
88
};
99

10-
/** Hook independant channels, channel will not be deletable **/
10+
// Hook independent channels that cannot be removed
1111
__OOAPI.createChannel("::eval", 1, Math.round(Math.random() * 100000));
1212
__OOAPI.createChannel("::debug", 1, Math.round(Math.random() * 100000));
1313

14-
/** Load the BSE Abstraction Runtime **/
15-
importScripts('api/Runtime.js', 'api/ScriptManager.js', 'api/Player.js', 'api/Display.js', 'api/Tween.js', 'api/Utils.js','api/Global.js', 'api/Function.js');
14+
// Load the BSE Abstraction Runtime
15+
importScripts('api/Runtime.js',
16+
'api/ScriptManager.js',
17+
'api/Player.js',
18+
'api/Display.js',
19+
'api/Tween.js',
20+
'api/Utils.js',
21+
'api/Global.js',
22+
'api/Function.js');
1623

17-
/** Immediately Hook into the eval channel, blocking future hooks **/
18-
__schannel("::eval", function(msg){
19-
if(Tween && Tween.extendWithEasingFunctions){
20-
Tween.extendWithEasingFunctions(this);
21-
}
22-
var clearTimeout = Utils.clearTimeout;
23-
var clearInterval = Utils.clearInterval;
24-
eval(msg);
24+
// Immediately Hook into the eval channel, blocking future hooks
25+
__schannel("::eval", function (msg) {
26+
// Prevent some things from being accessed in eval easily
27+
(function (__code, importScripts, postMessage, addEventListener, self) {
28+
if (Tween && Tween.extendWithEasingFunctions) {
29+
Tween.extendWithEasingFunctions(this);
30+
}
31+
var clearTimeout = Utils.clearTimeout;
32+
var clearInterval = Utils.clearInterval;
33+
eval(__code);
34+
})(msg);
2535
});
26-
__schannel("::debug", function(msg){
27-
if(msg.action === "list_channels"){
28-
__achannel("::worker:debug", "worker", __OOAPI.listChannels());
29-
}
36+
37+
__schannel("::debug", function (msg) {
38+
if (typeof msg === 'undefined' || msg === null ||
39+
!msg.hasOwnProperty('action')) {
40+
__achannel('::worker:debug', 'worker', 'Malformed request');
41+
return;
42+
}
43+
if (msg.action === 'list-channels') {
44+
__achannel('::worker:debug', 'worker', __OOAPI.listChannels());
45+
} else if (msg.action === 'raw-eval') {
46+
try {
47+
__achannel('::worker:debug', 'worker', eval(msg.code));
48+
} catch (e) {
49+
__achannel('::worker:debug', 'worker', 'Error: ' + e);
50+
}
51+
} else {
52+
__achannel('::worker:debug', 'worker', 'Unrecognized action');
53+
}
3054
});
3155

3256
__achannel("::worker:state", "worker", "running");

0 commit comments

Comments
 (0)