From a3c104ca4e670e080034f5f3d394283658505be5 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Tue, 23 May 2017 10:12:09 -0500 Subject: [PATCH] Added aliases --- src/Alias.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/Constants.js | 3 ++- src/SaveObject.js | 3 +++ src/Terminal.js | 4 ++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/Alias.js diff --git a/src/Alias.js b/src/Alias.js new file mode 100644 index 000000000..7d6ad52f3 --- /dev/null +++ b/src/Alias.js @@ -0,0 +1,44 @@ +/* Alias.js */ +Aliases = {}; + +//Print all aliases to terminal +function printAliases() { + for (var name in Aliases) { + if (Aliases.hasOwnProperty(name)) { + post("alias " + name + "=" + Aliases[name]); + } + } +} + +//True if successful, false otherwise +function parseAliasDeclaration(dec) { + var re = /([^=]+)="(.+)"/; + var matches = dec.match(re); + if (matches == null || matches.length != 3) {return false;} + addAlias(matches[1], matches[2]); + return true; +} + +function addAlias(name, value) { + Aliases[name] = value; +} + +function getAlias(name) { + if (Aliases.hasOwnProperty(name)) { + return Aliases[name]; + } + return null; +} + +//Returns the original string with any aliases substituted in +//Aliases only applied to "whole words", one level deep +function substituteAliases(origCommand) { + var commandArray = origCommand.split(" "); + for (var i = 0; i < commandArray.length; ++i) { + var alias = getAlias(commandArray[i]); + if (alias != null) { + commandArray[i] = alias; + } + } + return commandArray.join(" "); +} \ No newline at end of file diff --git a/src/Constants.js b/src/Constants.js index 0c01f6d78..71c405803 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -128,7 +128,8 @@ CONSTANTS = { CrimeHeist: "pull off the ultimate heist", //Text that is displayed when the 'help' command is ran in Terminal - HelpText: "analyze Get statistics and information about current machine
" + + HelpText: 'alias [name="value"] Create aliases for Terminal commands, or list existing aliases
' + + "analyze Get statistics and information about current machine
" + "clear Clear all text on the terminal
" + "cls See 'clear' command
" + "connect [ip/hostname] Connects to the machine given by its IP or hostname
" + diff --git a/src/SaveObject.js b/src/SaveObject.js index 06470ff97..20a77549d 100644 --- a/src/SaveObject.js +++ b/src/SaveObject.js @@ -10,6 +10,7 @@ function BitburnerSaveObject() { this.FactionsSave = ""; this.SpecialServerIpsSave = ""; this.AugmentationsSave = ""; + this.AliasesSave = ""; } BitburnerSaveObject.prototype.saveGame = function() { @@ -19,6 +20,7 @@ BitburnerSaveObject.prototype.saveGame = function() { this.FactionsSave = JSON.stringify(Factions); this.SpecialServerIpsSave = JSON.stringify(SpecialServerIps); this.AugmentationsSave = JSON.stringify(Augmentations); + this.AliasesSave = JSON.stringify(Aliases); var saveString = btoa(unescape(encodeURIComponent(JSON.stringify(this)))); window.localStorage.setItem("bitburnerSave", saveString); @@ -41,6 +43,7 @@ loadGame = function(saveObj) { Factions = JSON.parse(saveObj.FactionsSave, Reviver); SpecialServerIps = JSON.parse(saveObj.SpecialServerIpsSave, Reviver); Augmentations = JSON.parse(saveObj.AugmentationsSave, Reviver); + Aliases = JSON.parse(saveObj.AliasesSave, Reviver); return true; } diff --git a/src/Terminal.js b/src/Terminal.js index 4a8b8a77d..c7bd2f9b1 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -360,6 +360,10 @@ var Terminal = { } Terminal.commandHistoryIndex = Terminal.commandHistory.length; + //Process any aliases + command = substituteAliases(command); + console.log("command after alises: " + command); + //Only split the first space var commandArray = command.split(" "); if (commandArray.length > 1) {