made alias names posix compilant and added global aliases

This commit is contained in:
MrNuggelz 2017-06-30 18:44:03 +02:00
parent 1eb3bffe09
commit 6bfe6a549d
3 changed files with 82 additions and 15 deletions

@ -1,5 +1,6 @@
/* Alias.js */ /* Alias.js */
Aliases = {}; Aliases = {};
GlobalAliases = {};
//Print all aliases to terminal //Print all aliases to terminal
function printAliases() { function printAliases() {
@ -8,21 +9,40 @@ function printAliases() {
post("alias " + name + "=" + Aliases[name]); post("alias " + name + "=" + Aliases[name]);
} }
} }
for (var name in GlobalAliases) {
if (GlobalAliases.hasOwnProperty(name)) {
post("global alias " + name + "=" + GlobalAliases[name]);
}
}
} }
//True if successful, false otherwise //True if successful, false otherwise
function parseAliasDeclaration(dec) { function parseAliasDeclaration(dec,global=false) {
var re = /([^=]+)="(.+)"/; var re = /^([_|\w|!|%|,|@]+)="(.+)"$/;
var matches = dec.match(re); var matches = dec.match(re);
if (matches == null || matches.length != 3) {return false;} if (matches == null || matches.length != 3) {return false;}
if (global){
addGlobalAlias(matches[1],matches[2]);
} else {
addAlias(matches[1], matches[2]); addAlias(matches[1], matches[2]);
}
return true; return true;
} }
function addAlias(name, value) { function addAlias(name, value) {
if (name in GlobalAliases){
delete GlobalAliases[name];
}
Aliases[name] = value; Aliases[name] = value;
} }
function addGlobalAlias(name, value) {
if (name in Aliases){
delete Aliases[name];
}
GlobalAliases[name] = value;
}
function getAlias(name) { function getAlias(name) {
if (Aliases.hasOwnProperty(name)) { if (Aliases.hasOwnProperty(name)) {
return Aliases[name]; return Aliases[name];
@ -30,6 +50,13 @@ function getAlias(name) {
return null; return null;
} }
function getGlobalAlias(name) {
if (GlobalAliases.hasOwnProperty(name)) {
return GlobalAliases[name];
}
return null;
}
function removeAlias(name) { function removeAlias(name) {
if (Aliases.hasOwnProperty(name)) { if (Aliases.hasOwnProperty(name)) {
delete Aliases[name]; delete Aliases[name];
@ -42,11 +69,22 @@ function removeAlias(name) {
//Aliases only applied to "whole words", one level deep //Aliases only applied to "whole words", one level deep
function substituteAliases(origCommand) { function substituteAliases(origCommand) {
var commandArray = origCommand.split(" "); var commandArray = origCommand.split(" ");
if (commandArray.length>0){
var alias = getAlias(commandArray[0]);
if (alias != null) {
commandArray[0] = alias;
} else {
var alias = getGlobalAlias(commandArray[0]);
if (alias != null) {
commandArray[0] = alias;
}
}
for (var i = 0; i < commandArray.length; ++i) { for (var i = 0; i < commandArray.length; ++i) {
var alias = getAlias(commandArray[i]); var alias = getGlobalAlias(commandArray[i]);
if (alias != null) { if (alias != null) {
commandArray[i] = alias; commandArray[i] = alias;
} }
} }
}
return commandArray.join(" "); return commandArray.join(" ");
} }

@ -11,6 +11,7 @@ function BitburnerSaveObject() {
this.SpecialServerIpsSave = ""; this.SpecialServerIpsSave = "";
this.AugmentationsSave = ""; this.AugmentationsSave = "";
this.AliasesSave = ""; this.AliasesSave = "";
this.GlobalAliasesSave = "";
this.MessagesSave = ""; this.MessagesSave = "";
this.VersionSave = ""; this.VersionSave = "";
} }
@ -37,6 +38,7 @@ BitburnerSaveObject.prototype.saveGame = function() {
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps); this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
this.AugmentationsSave = JSON.stringify(Augmentations); this.AugmentationsSave = JSON.stringify(Augmentations);
this.AliasesSave = JSON.stringify(Aliases); this.AliasesSave = JSON.stringify(Aliases);
this.GlobalAliasesSave = JSON.stringify(GlobalAliases);
this.MessagesSave = JSON.stringify(Messages); this.MessagesSave = JSON.stringify(Messages);
this.VersionSave = JSON.stringify(CONSTANTS.Version); this.VersionSave = JSON.stringify(CONSTANTS.Version);
@ -70,6 +72,15 @@ loadGame = function(saveObj) {
} else { } else {
Aliases = {}; Aliases = {};
} }
if (saveObj.hasOwnProperty("GlobalAliasesSave")) {
try {
GlobalAliases = JSON.parse(saveObj.GlobalAliasesSave, Reviver);
} catch(e) {
GlobalAliases = {};
}
} else {
GlobalAliases = {};
}
if (saveObj.hasOwnProperty("MessagesSave")) { if (saveObj.hasOwnProperty("MessagesSave")) {
try { try {
Messages = JSON.parse(saveObj.MessagesSave, Reviver); Messages = JSON.parse(saveObj.MessagesSave, Reviver);
@ -118,6 +129,7 @@ loadImportedGame = function(saveObj, saveString) {
var tempSpecialServerIps = null; var tempSpecialServerIps = null;
var tempAugmentations = null; var tempAugmentations = null;
var tempAliases = null; var tempAliases = null;
var tempGlobalAliases = null;
var tempMessages = null; var tempMessages = null;
try { try {
saveString = decodeURIComponent(escape(atob(saveString))); saveString = decodeURIComponent(escape(atob(saveString)));
@ -139,6 +151,15 @@ loadImportedGame = function(saveObj, saveString) {
} else { } else {
tempAliases = {}; tempAliases = {};
} }
if (tempSaveObj.hasOwnProperty("GlobalAliases")) {
try {
tempGlobalAliases = JSON.parse(tempSaveObj.AliasesSave, Reviver);
} catch(e) {
tempGlobalAliases = {};
}
} else {
tempGlobalAliases = {};
}
if (tempSaveObj.hasOwnProperty("MessagesSave")) { if (tempSaveObj.hasOwnProperty("MessagesSave")) {
try { try {
tempMessages = JSON.parse(tempSaveObj.MessagesSave, Reviver); tempMessages = JSON.parse(tempSaveObj.MessagesSave, Reviver);
@ -191,6 +212,10 @@ loadImportedGame = function(saveObj, saveString) {
Aliases = tempAliases; Aliases = tempAliases;
} }
if (tempGlobalAliases) {
GlobalAliases = tempGlobalAliases;
}
if (tempMessages) { if (tempMessages) {
Messages = tempMessages; Messages = tempMessages;
} }
@ -263,6 +288,7 @@ BitburnerSaveObject.prototype.exportGame = function() {
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps); this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
this.AugmentationsSave = JSON.stringify(Augmentations); this.AugmentationsSave = JSON.stringify(Augmentations);
this.AliasesSave = JSON.stringify(Aliases); this.AliasesSave = JSON.stringify(Aliases);
this.GlobalAliasesSave = JSON.stringify(GlobalAliasesSave);
this.MessagesSave = JSON.stringify(Messages); this.MessagesSave = JSON.stringify(Messages);
this.VersionSave = JSON.stringify(CONSTANTS.Version); this.VersionSave = JSON.stringify(CONSTANTS.Version);

@ -243,7 +243,7 @@ function determineAllPossibilitiesForTabCompletion(input, index=0) {
return ["alias", "analyze", "cat", "check", "clear", "cls", "connect", "free", return ["alias", "analyze", "cat", "check", "clear", "cls", "connect", "free",
"hack", "help", "home", "hostname", "ifconfig", "kill", "killall", "hack", "help", "home", "hostname", "ifconfig", "kill", "killall",
"ls", "mem", "nano", "ps", "rm", "run", "scan", "scan-analyze", "ls", "mem", "nano", "ps", "rm", "run", "scan", "scan-analyze",
"scp", "sudov", "tail", "theme", "top"].concat(Object.keys(Aliases)); "scp", "sudov", "tail", "theme", "top"].concat(Object.keys(Aliases)).concat(Object.keys(GlobalAliases));
} }
if (input.startsWith ("buy ")) { if (input.startsWith ("buy ")) {
@ -581,15 +581,18 @@ var Terminal = {
case "alias": case "alias":
if (commandArray.length == 1) { if (commandArray.length == 1) {
printAliases(); printAliases();
} else if (commandArray.length == 2) { return;
if (parseAliasDeclaration(commandArray[1])) {
} else {
post('Incorrect usage of alias command. Usage: alias [aliasname="value"]'); return;
} }
} else { if (commandArray.length == 2 ) {
post('Incorrect usage of alias command. Usage: alias [aliasname="value"]'); return; var args = commandArray[1].split(" ");
if (args.length == 1 && parseAliasDeclaration(args[0])){
return;
} }
if (args.length == 2 && args[0] == "-g" && parseAliasDeclaration(args[1],true)){
return;
}
}
post('Incorrect usage of alias command. Usage: alias [aliasname="value"]');
break; break;
case "analyze": case "analyze":
if (commandArray.length != 1) { if (commandArray.length != 1) {