-- 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 .. "#targets", record.targets or "") xmlFile:setString(key .. "#ingredientMode", record.ingredientMode) xmlFile:setString(key .. "#ingredients", record.ingredients) xmlFile:setString(key .. "#waitPool", record.waitPool) xmlFile:setInt(key .. "#loops", record.loops) xmlFile:setInt(key .. "#returnBelow", record.returnBelow or ADRuns.DEFAULT_RETURN_BELOW_PERCENT) -- v2: udkørsel xmlFile:setString(key .. "#kind", record.kind or "supply") xmlFile:setString(key .. "#sources", record.sources or "") -- v3: bygninger (flow-tavlen) xmlFile:setString(key .. "#sourceBuildings", record.sourceBuildings or "") xmlFile:setString(key .. "#targetBuildings", record.targetBuildings or "") xmlFile:setString(key .. "#products", record.products or "") xmlFile:setInt(key .. "#sellAt", record.sellAt or ADRuns.DEFAULT_SELL_AT_PERCENT) xmlFile:setInt(key .. "#storeAbove", record.storeAbove or ADRuns.DEFAULT_STORE_ABOVE_PERCENT) xmlFile:setInt(key .. "#collectRound", record.collectRound or 0) 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"), targets = xmlFile:getString(key .. "#targets"), ingredientMode = xmlFile:getString(key .. "#ingredientMode"), ingredients = xmlFile:getString(key .. "#ingredients"), waitPool = xmlFile:getString(key .. "#waitPool"), loops = xmlFile:getInt(key .. "#loops"), returnBelow = xmlFile:getInt(key .. "#returnBelow"), kind = xmlFile:getString(key .. "#kind"), sources = xmlFile:getString(key .. "#sources"), sourceBuildings = xmlFile:getString(key .. "#sourceBuildings"), targetBuildings = xmlFile:getString(key .. "#targetBuildings"), products = xmlFile:getString(key .. "#products"), sellAt = xmlFile:getInt(key .. "#sellAt"), storeAbove = xmlFile:getInt(key .. "#storeAbove"), collectRound = xmlFile:getInt(key .. "#collectRound"), 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