feat: runs gemmes i savegameN/ADSmartPickup_runs.xml
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
-- AD Runs Storage
|
||||
-- Runs gemmes i savegame-mappen ved siden af AutoDrive_config.xml. Kun dette lag
|
||||
-- kender XMLFile; modellen (adRuns) ser kun flade poster.
|
||||
|
||||
ADRunsStorage = {}
|
||||
|
||||
ADRunsStorage.FILE_NAME = "ADSmartPickup_runs.xml"
|
||||
ADRunsStorage.ROOT = "ADSmartPickupRuns"
|
||||
ADRunsStorage.LOG_PREFIX = "ADSmartPickup: "
|
||||
|
||||
-- Samme opslag som AutoDrive.getXMLFile_new: mappen kendes efter første gem, ellers via indeks.
|
||||
function ADRunsStorage.getPath()
|
||||
local info = g_currentMission ~= nil and g_currentMission.missionInfo or nil
|
||||
if info == nil then
|
||||
return nil
|
||||
end
|
||||
if info.savegameDirectory ~= nil then
|
||||
return info.savegameDirectory .. "/" .. ADRunsStorage.FILE_NAME
|
||||
end
|
||||
if info.savegameIndex ~= nil and getUserProfileAppPath ~= nil then
|
||||
return getUserProfileAppPath() .. "savegame" .. tostring(info.savegameIndex) .. "/" .. ADRunsStorage.FILE_NAME
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function ADRunsStorage.writeRecords(xmlFile, records)
|
||||
xmlFile:setInt(ADRunsStorage.ROOT .. "#version", ADRuns.XML_VERSION)
|
||||
for index, record in ipairs(records) do
|
||||
local key = string.format("%s.run(%d)", ADRunsStorage.ROOT, index - 1)
|
||||
xmlFile:setInt(key .. "#id", record.id)
|
||||
xmlFile:setString(key .. "#name", record.name)
|
||||
xmlFile:setInt(key .. "#target", record.target)
|
||||
xmlFile:setString(key .. "#ingredientMode", record.ingredientMode)
|
||||
xmlFile:setString(key .. "#ingredients", record.ingredients)
|
||||
xmlFile:setString(key .. "#waitPool", record.waitPool)
|
||||
xmlFile:setInt(key .. "#loops", record.loops)
|
||||
for vehicleIndex, vehicleId in ipairs(record.vehicleIds) do
|
||||
xmlFile:setString(string.format("%s.vehicle(%d)#id", key, vehicleIndex - 1), vehicleId)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ADRunsStorage.readRecords(xmlFile)
|
||||
local records = {}
|
||||
xmlFile:iterate(ADRunsStorage.ROOT .. ".run", function(_, key)
|
||||
local record = {
|
||||
id = xmlFile:getInt(key .. "#id"),
|
||||
name = xmlFile:getString(key .. "#name"),
|
||||
target = xmlFile:getInt(key .. "#target"),
|
||||
ingredientMode = xmlFile:getString(key .. "#ingredientMode"),
|
||||
ingredients = xmlFile:getString(key .. "#ingredients"),
|
||||
waitPool = xmlFile:getString(key .. "#waitPool"),
|
||||
loops = xmlFile:getInt(key .. "#loops"),
|
||||
vehicleIds = {},
|
||||
}
|
||||
xmlFile:iterate(key .. ".vehicle", function(_, vehicleKey)
|
||||
local id = xmlFile:getString(vehicleKey .. "#id")
|
||||
if id ~= nil and id ~= "" then
|
||||
table.insert(record.vehicleIds, id)
|
||||
end
|
||||
end)
|
||||
table.insert(records, record)
|
||||
end)
|
||||
return records
|
||||
end
|
||||
|
||||
function ADRunsStorage.save(store)
|
||||
local path = ADRunsStorage.getPath()
|
||||
if path == nil then
|
||||
return false
|
||||
end
|
||||
local xmlFile = XMLFile.create("ADSmartPickupRuns", path, ADRunsStorage.ROOT)
|
||||
if xmlFile == nil then
|
||||
Logging.warning(ADRunsStorage.LOG_PREFIX .. "kunne ikke oprette %s", path)
|
||||
return false
|
||||
end
|
||||
ADRunsStorage.writeRecords(xmlFile, ADRuns.toRecords(store))
|
||||
xmlFile:save()
|
||||
xmlFile:delete()
|
||||
return true
|
||||
end
|
||||
|
||||
function ADRunsStorage.load()
|
||||
local path = ADRunsStorage.getPath()
|
||||
if path == nil or not fileExists(path) then
|
||||
return ADRuns.newStore()
|
||||
end
|
||||
local xmlFile = XMLFile.load("ADSmartPickupRuns", path)
|
||||
if xmlFile == nil then
|
||||
Logging.warning(ADRunsStorage.LOG_PREFIX .. "kunne ikke læse %s; starter uden runs", path)
|
||||
return ADRuns.newStore()
|
||||
end
|
||||
local records = ADRunsStorage.readRecords(xmlFile)
|
||||
xmlFile:delete()
|
||||
return ADRuns.fromRecords(records)
|
||||
end
|
||||
Reference in New Issue
Block a user