Skip to content

Commit

Permalink
Updated some code
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Nov 24, 2016
1 parent a183c9a commit 3a53b2c
Show file tree
Hide file tree
Showing 13 changed files with 549 additions and 238 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = (grunt) ->
watch:
scripting:
files: ['src/scripting/**/*', '!node_modules']
tasks: ['build-scripting']
tasks: ['build:scripting']
core:
files: ['src/**/*', '!node_modules', '!src/scripting/**/*']
tasks: ['build']
Expand Down
145 changes: 103 additions & 42 deletions dist/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -1106,51 +1106,112 @@ var CommentManager = (function() {
})();

/**
* Comment Filters Module Simplified (only supports modifiers & types)
* Comment Filters Module Simplified
* @license MIT
* @author Jim Chen
*/
function CommentFilter() {
this.modifiers = [];
this.runtime = null;
this.allowTypes = {
"1":true,
"4":true,
"5":true,
"6":true,
"7":true,
"8":true,
"17":true
};
this.doModify = function(cmt){
for(var k=0;k<this.modifiers.length;k++){
cmt = this.modifiers[k](cmt);
}
return cmt;
};
this.beforeSend = function(cmt){
return cmt;
}
this.doValidate = function(cmtData){
if(!this.allowTypes[cmtData.mode])
return false;
return true;
};
this.addRule = function(rule){

};
this.addModifier = function(f){
this.modifiers.push(f);
};
this.runtimeFilter = function(cmt){
if(this.runtime == null)
return cmt;
return this.runtime(cmt);
};
this.setRuntimeFilter = function(f){
this.runtime = f;
}
}
var CommentFilter = (function () {

function _match (rule, cmtData) {
var path = rule.subject.split('.');
var extracted = cmtData;
while (path.length > 0) {
var item = path.shift();
if (item === '') {
continue;
}
if (extracted.hasOwnProperty(item)) {
extracted = extracted[item];
}
if (extracted === null || typeof extracted === 'undefined') {
extracted = null;
break;
}
}
if (extracted === null) {
// Null precondition implies anything
return true;
}
switch (rule.op) {
case '~':
case 'regexp':
return (new RegExp(rule.value)).test(extracted.toString());
case '=':
case 'eq':
return rule.value === extracted.toString();
case 'NOT':
return !_match(rule.value, cmtData);
case 'AND':
if (Array.isArray(rule.value)) {
return rule.value.every(function (r) {
return _match(r, cmtData);
});
} else {
return false;
}
case 'OR':
if (Array.isArray(rule.value)) {
return rule.value.some(function (r) {
return _match(r, cmtData);
});
} else {
return false;
}
default:
return false;
}
}

function CommentFilter() {
this.rules = [];
this.modifiers = [];
this.allowUnknownTypes = true;
this.allowTypes = {
'1': true,
'2': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'17': true
};
}

CommentFilter.prototype.doModify = function (cmt) {
for (var k=0; k < this.modifiers.length; k++) {
cmt = this.modifiers[k](cmt);
}
return cmt;
};

CommentFilter.prototype.beforeSend = function (cmt) {
return cmt;
}

CommentFilter.prototype.doValidate = function (cmtData) {
if (cmtData.mode.toString() in this.allowTypes &&
!this.allowTypes[cmtData.mode.toString()]) {
return false;
}
return this.rules.every(function (rule) {
// Decide if matched
var matched = _match(rule, cmtData);
return rule.mode === 'accept' ? matched : !matched;
});
};

CommentFilter.prototype.addRule = function (rule) {
if (rule.mode !== 'accept' && rule.mode !== 'reject') {
throw new Error('Rule must be of accept type or reject type.');
}
this.rules.push(rule);
};

CommentFilter.prototype.addModifier = function (f) {
this.modifiers.push(f);
};
})();

/**
* Comment Provider
Expand Down
2 changes: 1 addition & 1 deletion dist/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions dist/scripting/Host.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ var CCLScripting = function(workerUrl){
var WorkerHook = function(event){
try{
var resp = JSON.parse(event.data);
}catch(e){
console.log(e);
} catch(e) {
if (e.stack) {
scripter.logger.error(e.stack);
} else {
scripter.logger.error(e);
}
return;
}
if(resp.channel === ""){
Expand Down Expand Up @@ -209,8 +213,12 @@ var CCLScripting = function(workerUrl){
}
break;
}
case ':debug':{
scripter.logger.log(JSON.stringify(resp.payload));
break;
}
default:{
console.log(resp);
scripter.logger.log(JSON.stringify(resp));
break;
}
}
Expand Down
12 changes: 8 additions & 4 deletions dist/scripting/OOAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var __OOAPI = new function () {
var channels = {};

function dispatchMessage (msg) {
if (channels[msg.channel]) {
if (channels.hasOwnProperty(msg.channel)) {
for(var i = 0; i < channels[msg.channel].listeners.length; i++) {
try {
channels[msg.channel].listeners[i](msg.payload);
Expand All @@ -20,6 +20,9 @@ var __OOAPI = new function () {
}
}
}
} else {
__trace('Got message on channel "' + msg.channel +
'" but channel does not exist.', 'warn');
}
};

Expand All @@ -33,10 +36,11 @@ var __OOAPI = new function () {
__trace(e, 'err');
return;
}
if (msg && msg.channel) {
if (msg !== null && msg.hasOwnProperty('channel') &&
typeof msg.channel === 'string') {
dispatchMessage(msg);
} else {
_trace(msg, 'warn');
__trace(msg, 'warn');
}
});

Expand Down Expand Up @@ -111,7 +115,7 @@ function __channel (id, payload, callback) {
'payload': payload,
'callback': true
}));
__OOAPI.addListenerChannel(id, callback, true);
__OOAPI.addListenerChannel(id, callback);
};

function __schannel (id, callback) {
Expand Down
60 changes: 42 additions & 18 deletions dist/scripting/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,55 @@ var __OOAPI;

importScripts("OOAPI.js");

if(!__OOAPI){
console.log("Error: OOAPI Not Loaded");
self.close();
if (!__OOAPI) {
console.log("Error: OOAPI Not Loaded");
self.close();
};

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

/** Load the BSE Abstraction Runtime **/
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');
// Load the BSE Abstraction Runtime
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');

/** Immediately Hook into the eval channel, blocking future hooks **/
__schannel("::eval", function(msg){
if(Tween && Tween.extendWithEasingFunctions){
Tween.extendWithEasingFunctions(this);
}
var clearTimeout = Utils.clearTimeout;
var clearInterval = Utils.clearInterval;
eval(msg);
// Immediately Hook into the eval channel, blocking future hooks
__schannel("::eval", function (msg) {
// Prevent some things from being accessed in eval easily
(function (__code, importScripts, postMessage, addEventListener, self) {
if (Tween && Tween.extendWithEasingFunctions) {
Tween.extendWithEasingFunctions(this);
}
var clearTimeout = Utils.clearTimeout;
var clearInterval = Utils.clearInterval;
eval(__code);
})(msg);
});
__schannel("::debug", function(msg){
if(msg.action === "list_channels"){
__achannel("::worker:debug", "worker", __OOAPI.listChannels());
}

__schannel("::debug", function (msg) {
if (typeof msg === 'undefined' || msg === null ||
!msg.hasOwnProperty('action')) {
__achannel('::worker:debug', 'worker', 'Malformed request');
return;
}
if (msg.action === 'list-channels') {
__achannel('::worker:debug', 'worker', __OOAPI.listChannels());
} else if (msg.action === 'raw-eval') {
try {
__achannel('::worker:debug', 'worker', eval(msg.code));
} catch (e) {
__achannel('::worker:debug', 'worker', 'Error: ' + e);
}
} else {
__achannel('::worker:debug', 'worker', 'Unrecognized action');
}
});

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

0 comments on commit 3a53b2c

Please sign in to comment.