Skip to content

Commit

Permalink
Merge pull request #271 from GMLambda/config-migration
Browse files Browse the repository at this point in the history
Config migration
  • Loading branch information
ZehMatt authored May 11, 2024
2 parents 68f5d89 + 6e7b345 commit 37e10c0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
0.9.21 (in development)
- Improved: lambda_gametype will now default to 'auto' and warns about a misconfiguration.

0.9.20
- Feature: Episode 1 support.
Expand Down
34 changes: 30 additions & 4 deletions gamemode/sh_gametypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function GM:LoadGameTypes()
if SERVER then
print("-- Loaded GameTypes --")
for k, v in pairs(GameTypes.Registered) do
print("> " .. k)
print(" > " .. k)
end
end

Expand Down Expand Up @@ -111,15 +111,41 @@ end

function GM:SetGameType(gametype, isFallback)
DbgPrint("SetGameType: " .. tostring(gametype))

local currentMap = game.GetMap():lower()
local idealGametype = GameTypes:GetByMap(currentMap)

if gametype == nil or gametype == "" then
if SERVER then
ErrorNoHalt("Warning: 'lambda_gametype' is empty, using 'auto'.\n")
else
print("Warning: 'lambda_gametype' is empty, using 'auto'.")
end
gametype = "auto"
end

DbgPrint("Current Gametype: " .. gametype)
DbgPrint("Ideal Gametype: " .. (idealGametype or "unknown"))

if gametype == "auto" then
DbgPrint("Trying to detect game type based on map")
local currentMap = game.GetMap():lower()
gametype = GameTypes:GetByMap(currentMap)
DbgPrint("Game type is set to auto, trying to detect gametype by map.")
if gametype ~= nil then
DbgPrint("Detected game type '" .. gametype .. "' for map " .. currentMap)
else
DbgPrint("No game type registered that contains the map " .. currentMap)
end
gametype = idealGametype
else
if idealGametype ~= nil and idealGametype ~= gametype then
local msg = ""
msg = msg .. "Warning: Server ConVar 'lambda_gametype' is set to '" .. gametype .. "', but the map is associated with '" .. idealGametype .. "'.\n"
msg = msg .. " - Set 'lambda_gametype' to '" .. idealGametype .. "' or 'auto' to use the correct gametype.\n"
if SERVER then
ErrorNoHalt(msg)
else
print(msg)
end
end
end

local gametypeData = GameTypes:Get(gametype)
Expand Down
3 changes: 2 additions & 1 deletion gamemode/sh_lambda_build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ GM.Website = "https://github.com/ZehMatt/Lambda"
-- Values will be overwritten by the CI for automated deployment.
GM.WorkshopID = "801875828"
GM.WorkshopBuild = false
GM.Version = "develop"
GM.Version = "develop"
GM.InternalVersion = 1
39 changes: 39 additions & 0 deletions gamemode/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,50 @@ function GM:ConflictRecovery()
end
end

local function MigrateConfig(cvarName, newValue)
local cvar = GetConVar(cvarName)
if cvar == nil then
print(" > Warning: Unable to find cvar: " .. cvarName .. " for migration.")
return
end
if isstring(newValue) then
cvar:SetString(newValue)
elseif isnumber(newValue) then
cvar:SetFloat(newValue)
elseif isbool(newValue) then
cvar:SetBool(newValue)
end
print(" > Migrated cvar '" .. cvarName .. "' to '" .. tostring(newValue) .. "'")
end

function GM:UpdateConfigs()
local lastVersion = cookie.GetNumber("lambda_internal_version", 0)
if lastVersion >= self.InternalVersion then
return
end

print("-- Config Migration to version " .. tostring(self.InternalVersion) .. " --")

if lastVersion == 0 then
-- Version 0.9.20 -> 0.9.21
if SERVER then
-- Set lambda_gametype to auto.
MigrateConfig("lambda_gametype", "auto")
else
-- Set lambda_physcannon_glow to 2.
MigrateConfig("lambda_physcannon_glow", "2")
end
end

cookie.Set("lambda_internal_version", self.InternalVersion)
end

-- First function called when the game mode is loaded.
function GM:OnGamemodeLoaded()
DbgPrint("GM:OnGamemodeLoaded")
self.OnGamemodeLoadedCalled = true
self.ServerStartupTime = GetSyncedTimestamp()
self:UpdateConfigs()
self:LoadGameTypes()
self:SetGameType(lambda_gametype:GetString())
self:InitSettings()
Expand Down

0 comments on commit 37e10c0

Please sign in to comment.