Implement importing sets

This commit is contained in:
2025-01-04 22:37:53 +01:00
parent 29cd686eec
commit 9520269857

View File

@@ -262,6 +262,36 @@ function ListAliases()
end)
end
---@param text string
---@param size number
---@param deliminer string
---@return string[]
local function Partition(text, size, deliminer)
local words = {}
for word in text:gmatch("[^" .. deliminer .. "]+") do
words[#words + 1] = word
end
local ret = {}
local currentChunk = ""
for _, word in ipairs(words) do
if #currentChunk + #word + 1 <= size then
currentChunk = currentChunk .. deliminer .. word
else
if #currentChunk > 0 then
ret[#ret + 1] = currentChunk
end
currentChunk = word
end
end
if #currentChunk > 0 then
ret[#ret + 1] = currentChunk
end
return ret
end
function ExportSet(setName)
local set = ActionBarSaverReloaded.sets[setName]
@@ -280,8 +310,76 @@ function ExportSet(setName)
tostring(typeChar))
end
local str = table.concat(stringified, "|")
print(str)
local partitions = Partition(str, 160, "|")
for _, partition in ipairs(partitions) do
print("/abs import " .. setName .. " " .. partition)
end
end
local function ParseAction(action)
if not action or action == "" then
return nil
end
action = strtrim(action)
local slot, id, typeChar = string.match(action, "([^\\]+)\\([^\\]+)\\([^\\]+)")
if not typeChar then
print(string.format("Unknown action type '%s' in set '%s'", tostring(typeChar), tostring(setName)))
return
end
local type = inverseTypeMap[typeChar]
if not type then
print(string.format("Unknown action type '%s' in set '%s'", tostring(typeChar), tostring(setName)))
return
end
slot = tonumber(slot)
if not slot then
print(string.format("Unknown slot '%s' in set '%s'", tostring(slot), tostring(setName)))
return
end
if not id then
print(string.format("Unknown id '%s' in set '%s'", tostring(id), tostring(setName)))
return
end
return {
slot = slot,
id = id,
type = type
}
end
function ImportSet(setName, str)
if not setName or setName == "" then
print("Must provide a valid set name")
return
end
local set = ActionBarSaverReloaded.sets[setName] or {}
-- if set then
-- print(string.format("Set '%s' already exists", setName))
-- return
-- end
str = strtrim(str)
local data = {strsplit("|", str)}
for _, action in ipairs(data) do
local paction = ParseAction(action)
if paction then
set[paction.slot] = {
type = paction.type,
id = paction.id
}
end
end
-- /dump ActionBarSaverReloaded.sets["havoc"]
-- /dump ActionBarSaverReloaded.sets["havoc2"]
ActionBarSaverReloaded.sets[setName] = set
print(string.format("Imported set '%s'", setName))
end
function PrintUsage()
print("ABS Slash commands")
print("/abs save <set> - Saves your current action bar setup under the given <set>")