Skip to content

Commit

Permalink
Updated framework to work without the need of separate json file. Add…
Browse files Browse the repository at this point in the history
…ed sample code. Finished release candidate 1.
  • Loading branch information
sebinside committed Oct 5, 2018
1 parent 3fb43c7 commit 9ea6105
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 210 deletions.
2 changes: 1 addition & 1 deletion CSXS/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</Lifecycle>
<UI>
<Type>Panel</Type>
<Menu>Premiere AHK Framework</Menu>
<Menu>AHK2CEP - Premiere</Menu>
<Geometry>
<Size>
<Height>50</Height>
Expand Down
10 changes: 9 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
<html>
<head>
<meta charset="utf-8">
<title>Premiere AHK Framework</title>
<title>AHK2CEP - Premiere</title>
<link rel="stylesheet" href="main.css">
<script type="text/javascript" src="CSInterface.js"></script>
<script type="text/javascript" src="index.js"></script>

<!-- Paradigm fail, but needed for endpoint construction -->
<script type="text/javascript" src="../host/index.jsx"></script>

<!-- Change this port, if there are conflicts with other software -->
<script type="text/javascript">
const SERVER_PORT = 8081;
</script>
</head>
<body ondblclick="openHostWindow()">
<div id="statusContainer" class="red">Loading...</div>
Expand Down
100 changes: 60 additions & 40 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,65 @@ const csInterface = new CSInterface();
const loc = window.location.pathname;
const dir = decodeURI(loc.substring(1, loc.lastIndexOf('/')));
const express = require(dir + "/node_modules/express/index.js");
const endpoints = require(dir + "/../host/commands.json");

function init() {

// Setup server
const app = express();
const port = process.env.PORT || 8081;
const port = SERVER_PORT;
const router = express.Router();

// Setup endpoints
Object.keys(endpoints).forEach(function(key) {
const value = endpoints[key];
router.get('/' + key, function(req, res) {

if (value.functionName === "killApp") {
res.json({ message: 'ok.' });
csInterface.closeExtension();
}

// Count request query parameters
let propertyCount = 0;
for (const propName in req.query) {
if (req.query.hasOwnProperty(propName)) {
propertyCount++;
for (const functionDeclaration in host) {
const key = functionDeclaration;
const signature = host[key].toString().split("{")[0];

if (signature === host[key].toString()) {
console.log("Unable to read function definition of '" + key + "'.");
} else {

const parameters = extractParameters(signature);

router.get('/' + key, function (req, res) {

// Special code for faster debugging
if (key === "kill") {
res.json({message: 'ok.'});
csInterface.closeExtension();
}
}

// Extract request query parameters
let params = [];
for (const id in value.parameters) {
const propName = value.parameters[id];
if (req.query.hasOwnProperty(propName)) {
params.push(req.query[propName]);
} else {
console.log("Param not found: '" + propName + "'");

// Count request query parameters
let propertyCount = 0;
for (const propName in req.query) {
if (req.query.hasOwnProperty(propName)) {
propertyCount++;
}
}
}

// Check query parameter count
if(value.parameters.length === params.length && params.length === propertyCount) {
res.json({ message: 'ok.' });
// Extract request query parameters
let params = [];
for (const id in parameters) {
const propName = parameters[id];
if (req.query.hasOwnProperty(propName)) {
params.push(req.query[propName]);
} else {
console.log("Param not found: '" + propName + "'");
}
}

// Execute function with given parameters
executeCommand(value.functionName, params);
} else {
res.json({ message: 'error. wrong parameters.' });
}
// Check query parameter count
if (parameters.length === params.length && params.length === propertyCount) {
res.json({message: 'ok.'});

});
});
// Execute function with given parameters
executeCommand(key, params);
} else {
res.json({message: 'error. wrong parameters.'});
}

});
}
}

// Start server
app.use('/', router);
Expand All @@ -62,22 +71,33 @@ function init() {
document.getElementById("statusContainer").className = "green";
}

const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
const ARGUMENT_NAMES = /([^\s,]+)/g;

function extractParameters(signature) {
const fnStr = signature.toString().replace(STRIP_COMMENTS, '');
let result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if (result === null)
result = [];
return Array.prototype.slice.call(result);
}

function executeCommand(command, params) {
console.log("Execute: " + command);
document.getElementById("lastCommandContainer").innerHTML = command;

command += "(";
for(let i = 0; i < params.length; i++) {
for (let i = 0; i < params.length; i++) {
command += '"' + params[i] + '"';

if(i < (params.length - 1)) {
if (i < (params.length - 1)) {
command += ", ";
}
}
command += ")";

console.log(command);
csInterface.evalScript(command);
csInterface.evalScript("host." + command);
}

function openHostWindow() {
Expand Down
Loading

0 comments on commit 9ea6105

Please sign in to comment.