From 003abe86bbb8a461f2cd7604dd9e8f6efaddad70 Mon Sep 17 00:00:00 2001 From: boatbomber Date: Thu, 22 Sep 2022 20:03:09 -0700 Subject: [PATCH] Save host and port by placeId (#613) * Save host and port by placeId * Bump to 5 months before clearing * Fix indentation --- plugin/src/App/init.lua | 47 +++++++++++++++++++++++++++++++++++++++-- plugin/src/Config.lua | 4 ++-- plugin/src/Settings.lua | 1 + 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/plugin/src/App/init.lua b/plugin/src/App/init.lua index 707127d1..9f7e2ecf 100644 --- a/plugin/src/App/init.lua +++ b/plugin/src/App/init.lua @@ -44,8 +44,10 @@ local App = Roact.Component:extend("App") function App:init() preloadAssets() - self.host, self.setHost = Roact.createBinding("") - self.port, self.setPort = Roact.createBinding("") + local priorHost, priorPort = self:getPriorEndpoint() + self.host, self.setHost = Roact.createBinding(priorHost or "") + self.port, self.setPort = Roact.createBinding(priorPort or "") + self.patchInfo, self.setPatchInfo = Roact.createBinding({ changes = 0, timestamp = os.time(), @@ -85,6 +87,45 @@ function App:closeNotification(index: number) }) end +function App:getPriorEndpoint() + local priorEndpoints = Settings:get("priorEndpoints") + if not priorEndpoints then return end + + local place = priorEndpoints[tostring(game.PlaceId)] + if not place then return end + + return place.host, place.port +end + +function App:setPriorEndpoint(host: string, port: string) + local priorEndpoints = Settings:get("priorEndpoints") + if not priorEndpoints then + priorEndpoints = {} + end + + -- Clear any stale saves to avoid disc bloat + for placeId, endpoint in priorEndpoints do + if os.time() - endpoint.timestamp > 12_960_000 then + priorEndpoints[placeId] = nil + Log.trace("Cleared stale saved endpoint for {}", placeId) + end + end + + if host == Config.defaultHost and port == Config.defaultPort then + -- Don't save default + priorEndpoints[tostring(game.PlaceId)] = nil + else + priorEndpoints[tostring(game.PlaceId)] = { + host = host ~= Config.defaultHost and host or nil, + port = port ~= Config.defaultPort and port or nil, + timestamp = os.time(), + } + Log.trace("Saved last used endpoint for {}", game.PlaceId) + end + + Settings:set("priorEndpoints", priorEndpoints) +end + function App:getHostAndPort() local host = self.host:getValue() local port = self.port:getValue() @@ -200,6 +241,8 @@ function App:startSession() serveSession:onStatusChanged(function(status, details) if status == ServeSession.Status.Connecting then + self:setPriorEndpoint(host, port) + self:setState({ appStatus = AppStatus.Connecting, toolbarIcon = Assets.Images.PluginButton, diff --git a/plugin/src/Config.lua b/plugin/src/Config.lua index 3b85c3ee..8aae470d 100644 --- a/plugin/src/Config.lua +++ b/plugin/src/Config.lua @@ -9,5 +9,5 @@ return strict("Config", { expectedServerVersionString = "7.2 or newer", protocolVersion = 4, defaultHost = "localhost", - defaultPort = 34872, -}) \ No newline at end of file + defaultPort = "34872", +}) diff --git a/plugin/src/Settings.lua b/plugin/src/Settings.lua index 7bb592bf..0f23c86b 100644 --- a/plugin/src/Settings.lua +++ b/plugin/src/Settings.lua @@ -16,6 +16,7 @@ local defaultSettings = { playSounds = true, typecheckingEnabled = false, logLevel = "Info", + priorEndpoints = {}, } local Settings = {}