From 07403eaaaa7011c4ae4bb49dfce1ea573c836151 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sun, 16 Jan 2022 10:41:02 -0500 Subject: [PATCH] Add ns.ui.getGameInfo() to retrieve game version Returns { version, commit, platform } --- src/Netscript/RamCostGenerator.ts | 1 + src/NetscriptFunctions/UserInterface.ts | 17 ++++++++++++++++- src/ScriptEditor/NetscriptDefinitions.d.ts | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 8092b775e..aeb35e1f8 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -368,6 +368,7 @@ export const RamCosts: IMap = { getStyles: 0, setStyles: 0, resetStyles: 0, + getGameInfo: 0, }, heart: { diff --git a/src/NetscriptFunctions/UserInterface.ts b/src/NetscriptFunctions/UserInterface.ts index ca5a581cc..55827c334 100644 --- a/src/NetscriptFunctions/UserInterface.ts +++ b/src/NetscriptFunctions/UserInterface.ts @@ -2,11 +2,13 @@ import { INetscriptHelper } from "./INetscriptHelper"; import { WorkerScript } from "../Netscript/WorkerScript"; import { IPlayer } from "../PersonObjects/IPlayer"; import { getRamCost } from "../Netscript/RamCostGenerator"; -import { IStyleSettings, UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions"; +import { GameInfo, IStyleSettings, UserInterface as IUserInterface, UserInterfaceTheme } from "../ScriptEditor/NetscriptDefinitions"; import { Settings } from "../Settings/Settings"; import { ThemeEvents } from "../ui/React/Theme"; import { defaultTheme } from "../Settings/Themes"; import { defaultStyles } from "../Settings/Styles"; +import { CONSTANTS } from "../Constants"; +import { hash } from "../hash/hash"; export function NetscriptUserInterface( player: IPlayer, @@ -84,6 +86,19 @@ export function NetscriptUserInterface( Settings.styles = { ...defaultStyles }; ThemeEvents.emit(); workerScript.log("ui.resetStyles", () => `Reinitialized styles to default`); + }, + + getGameInfo: function (): GameInfo { + helper.updateDynamicRam("getGameInfo", getRamCost(player, "ui", "getGameInfo")); + const version = CONSTANTS.VersionString; + const commit = hash(); + const platform = (navigator.userAgent.toLowerCase().indexOf(" electron/") > -1) ? 'Steam' : 'Browser'; + + const gameInfo = { + version, commit, platform, + } + + return gameInfo; } } } diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index 8fc99aaaf..f965071ed 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -4065,6 +4065,13 @@ interface UserInterface { * RAM cost: cost: 0 GB */ resetStyles(): void; + + /** + * Gets the current game information (version, commit, ...) + * @remarks + * RAM cost: 0 GB + */ + getGameInfo(): GameInfo; } /** @@ -6573,3 +6580,13 @@ interface IStyleSettings { fontFamily: string; lineHeight: number; } + +/** + * Game Information + * @internal + */ +interface GameInfo { + version: string; + commit: string; + platform: string; +}