I don't know whether anything like this already exists, but I threw together a Lua 5.1 script for reordering groups in the mission.sqm
Code: Select all
local function processMission(mode)
-- group item iterator
local function groups(groupStr)
local index = 1
return function ()
local s, e = string.find(groupStr, "class Item%d+.-%b{};", index)
if s then
index = e + 1
return string.sub(groupStr, s, e)
else return nil end
end
end
-- load sqm mission into string
local fl = io.open("mission.sqm")
local mission = fl:read("*a")
fl:close()
-- make backup of mission.sqm
local fl = io.open("mission_backup.sqm", "w")
fl:write(mission)
fl:close()
-- search for group definitions segment
local gr_start, gr_end = string.find(mission, "class Groups.-%b{};")
if mode == "output" then
local fl = io.open("reorder_list.txt", "w")
-- use group iterator
for gr in groups(string.sub(mission, gr_start, gr_end)) do
-- if first element in group has description then write it to file
local writeStr = gr:match("description=(.-);")
if writeStr then fl:write(writeStr .. "\n") end
end
fl:close()
print("\n ..DONE..\n")
elseif mode == "order" then
local orderedList = {}
-- make numbered table of ordered groups
for line in io.lines("reorder_list.txt") do
if string.match(line, "(\".-\")") then table.insert(orderedList, line) end
end
local groupList = {}
-- if group leader has description: add group to table with
-- description as the key
-- else add group with numbered key
for gr in groups(string.sub(mission, gr_start, gr_end)) do
local v1, v2 = string.find(gr, "description=.-;")
if v1 then
local desc = gr:match("description=(.-);")
groupList[desc] = gr
else table.insert(groupList, gr) end
end
-- open sqm and write everything before group item definitions
local missionFile = io.open("mission.sqm", "w")
local v1, v2 = string.find(mission, "items=%d+;", gr_start)
missionFile:write(string.sub(mission, 1, v2+1))
-- increment item number for group
local function incItem(str, num)
local v1, v2 = string.find(str, "class Item%d+")
return "\n\t\tclass Item" .. string.gsub(string.sub(str, v1, v2), "class Item(%d+)", tostring(num)) .. string.sub(str, v2 + 1)
end
-- starting from 0, write the ordered group items to sqm
local index = 0
for i, v in ipairs(orderedList) do
missionFile:write(incItem(groupList[v], index))
index = index + 1
end
-- write all other undescribed groups from last index
for i, v in ipairs(groupList) do
missionFile:write(incItem(v, index))
index = index + 1
end
-- write everything else after group definitions
missionFile:write("\n\t}" .. string.sub(mission, gr_end))
missionFile:close()
print("\n ..DONE..\n")
end
end
io.write("\tmission.sqm reorder\n\t-------------------\n")
io.write("After choosing to output groups (Option 1), rearrange the element leaders in reorder_list.txt so that each has its own line. Save the file and then choose reorder groups (Option 2).")
io.write("\n\n 1. Ouput groups\n 2. Reorder groups\n 3. Quit\n\n")
while true do
io.write(" >")
local ch = io.read()
if ch == "1" then
processMission("output")
elseif ch == "2" then
-- check if group_order.txt exists
local fl = io.open("reorder_list.txt","r")
if fl ~= nil then
io.close(fl)
processMission("order")
else print("No reorder_list.txt file. Choose option 1 first!") end
elseif ch == "3" or ch == "q" then break end
end
If you want to test it, install a Lua 5.1 interpreter (will probably also work with 5.2). Save the code above as something like "reorder.lua" and put it in the folder of the mission you want to use it on. FOR THE LOVE OF GOD backup your mission.sqm file. Execute the script, and choosing the first option will spit out a text file containing the description entry of the most senior unit in each group. If there's no description it won't be listed. Reorder the names by hand so that each has its own line in the file. Then choose to reorder in the script and it should be done. The units from the text file are bumped up to Item0, Item1, etc.
I have no experience with sqm files so don't know whether I've just made a bunch of garbage that seems to work at the moment.