Merge pull request #80 from MrNuggelz/alias

Made alias names posix compilant and added global aliases
This commit is contained in:
danielyxie 2017-07-04 10:42:11 -05:00 committed by GitHub
commit b398c67f03
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(" ");
}

@ -10,6 +10,7 @@ function BitburnerSaveObject() {
this.FactionsSave = "";
this.SpecialServerIpsSave = "";
this.AliasesSave = "";
this.GlobalAliasesSave = "";
this.MessagesSave = "";
this.StockMarketSave = "";
this.VersionSave = "";
@ -36,6 +37,7 @@ BitburnerSaveObject.prototype.saveGame = function() {
this.FactionsSave = JSON.stringify(Factions);
this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps);
this.AliasesSave = JSON.stringify(Aliases);
this.GlobalAliasesSave = JSON.stringify(GlobalAliases);
this.MessagesSave = JSON.stringify(Messages);
this.StockMarketSave = JSON.stringify(StockMarket);
this.VersionSave = JSON.stringify(CONSTANTS.Version);
@ -69,6 +71,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);
@ -129,6 +140,7 @@ loadImportedGame = function(saveObj, saveString) {
var tempSpecialServerIps = null;
var tempAugmentations = null;
var tempAliases = null;
var tempGlobalAliases = null;
var tempMessages = null;
var tempStockMarket = null;
try {
@ -151,6 +163,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);
@ -215,6 +236,10 @@ loadImportedGame = function(saveObj, saveString) {
Aliases = tempAliases;
}
if (tempGlobalAliases) {
GlobalAliases = tempGlobalAliases;
}
if (tempMessages) {
Messages = tempMessages;
}
@ -291,6 +316,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) {