Save host and port by placeId (#613)

* Save host and port by placeId

* Bump to 5 months before clearing

* Fix indentation
This commit is contained in:
boatbomber
2022-09-22 20:03:09 -07:00
committed by GitHub
parent 6ec411a618
commit 003abe86bb
3 changed files with 48 additions and 4 deletions

View File

@@ -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,

View File

@@ -9,5 +9,5 @@ return strict("Config", {
expectedServerVersionString = "7.2 or newer",
protocolVersion = 4,
defaultHost = "localhost",
defaultPort = 34872,
})
defaultPort = "34872",
})

View File

@@ -16,6 +16,7 @@ local defaultSettings = {
playSounds = true,
typecheckingEnabled = false,
logLevel = "Info",
priorEndpoints = {},
}
local Settings = {}