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

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

@ -243,7 +243,7 @@ function determineAllPossibilitiesForTabCompletion(input, index=0) {
return ["alias", "analyze", "cat", "check", "clear", "cls", "connect", "free",
"hack", "help", "home", "hostname", "ifconfig", "kill", "killall",
"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 ")) {
@ -581,15 +581,18 @@ var Terminal = {
case "alias":
if (commandArray.length == 1) {
printAliases();
} else if (commandArray.length == 2) {
if (parseAliasDeclaration(commandArray[1])) {
} else {
post('Incorrect usage of alias command. Usage: alias [aliasname="value"]'); return;
return;
}
} else {
post('Incorrect usage of alias command. Usage: alias [aliasname="value"]'); return;
if (commandArray.length == 2 ) {
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;
case "analyze":
if (commandArray.length != 1) {