From 08c1af30738fac4e78a2ef4d94a63ef79550aa7d Mon Sep 17 00:00:00 2001 From: Not Date: Mon, 14 Nov 2022 22:17:30 +0100 Subject: [PATCH] insert checksums --- lb_store.lua | 62 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/lb_store.lua b/lb_store.lua index 5ca9f2f..4352d97 100644 --- a/lb_store.lua +++ b/lb_store.lua @@ -122,6 +122,26 @@ local function stat_str(stat) return "0" end +local function djb2(message) + local digest = 5381 + for c in message:gmatch(".") do + digest = (($ << 5) + $) + string.byte(c) + end + + return digest +end + +-- Produce a checksum by using the maps title, subtitle and zone +local function mapChecksum(mapnum) + local mh = mapheaderinfo[mapnum] + if not mh then + return nil + end + + local digest = string.format("%04x", djb2(mh.lvlttl..mh.subttl..mh.zonttl)) + return string.sub(digest, #digest - 3) +end + -- GLOBAL -- Save a record to the LiveStore and write to disk -- SaveRecord will replace the record holders previous record but it will not compare any record times @@ -141,6 +161,11 @@ local function SaveRecord(score, map, modeSep) for mapid, records in pairs(LiveStore) do for _, record in ipairs(records) do + -- Insert checksum if missing + if (not record.checksum) or record.checksum == "" then + record.checksum = mapChecksum(mapid) + end + f:write( mapid, "\t", record.name, "\t", @@ -149,7 +174,8 @@ local function SaveRecord(score, map, modeSep) record.time, "\t", table.concat(record.splits, " "), "\t", record.flags, "\t", - stat_str(record.stat), "\n" + stat_str(record.stat), "\t", + record.checksum or "", "\n" ) end end @@ -164,7 +190,7 @@ end addHook("NetVars", netvars) -local function score_t(map, name, skin, color, time, splits, flags, stat) +local function score_t(map, name, skin, color, time, splits, flags, stat, checksum) return { ["map"] = map, ["name"] = name, @@ -173,7 +199,8 @@ local function score_t(map, name, skin, color, time, splits, flags, stat) ["time"] = time, ["splits"] = splits, ["flags"] = flags, - ["stat"] = stat + ["stat"] = stat, + ["checksum"] = checksum } end @@ -206,6 +233,8 @@ local function parseScore(str) end end + local checksum = t[9] + return score_t( tonumber(t[1]), -- Map t[2], -- Name @@ -214,25 +243,24 @@ local function parseScore(str) tonumber(t[5]), -- Time splits, flags, - stats + stats, + checksum ) end rawset(_G, "lb_parse_score", parseScore) -- Load the livestore -do - if isserver then - local f = assert( - io.open(LEADERBOARD_FILE, "r"), - "Failed to open file: "..LEADERBOARD_FILE - ) +if isserver then + local f = assert( + io.open(LEADERBOARD_FILE, "r"), + "Failed to open file: "..LEADERBOARD_FILE + ) - for l in f:lines() do - local score = parseScore(l) - LiveStore[score.map] = $ or {} - table.insert(LiveStore[score.map], score) - end - - f:close() + for l in f:lines() do + local score = parseScore(l) + LiveStore[score.map] = $ or {} + table.insert(LiveStore[score.map], score) end + + f:close() end