perf: markørindeks i bygningslisten, cachet kildesøgning og kursus-bibliotek — 1.62.417
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
-- AD Course Library
|
||||
-- Bibliotek af beregnede Courseplay-kurser pr. "mark|bredde|form|indstillinger" (ADCourseCache.key). Registret i
|
||||
-- adCourseCache husker kun hver maskines SENESTE kursus: får maskinen en anden mark, er kurset væk, og næste gang
|
||||
-- beregnes det forfra (savegame8 27/10: 6 nye beregninger på 20 min, hver en frysning på 1,3-4 s). Biblioteket
|
||||
-- beholder en kopi af hvert kursus og gemmes i savegamet (ADSmartPickup_courses.xml, Courseplays eget kursusformat).
|
||||
-- Rene regler øverst (tests/test_adCourseLibrary.lua); spil-nær lim nederst (alt pcall'et).
|
||||
|
||||
-- info-log bag "Detaljeret log" (ADLog i adSettings.lua; uden ADLog — tests — direkte til Logging)
|
||||
local function logInfo(...) return (ADLog or Logging).info(...) end
|
||||
|
||||
ADCourseLibrary = {}
|
||||
|
||||
ADCourseLibrary.FILE_NAME = "ADSmartPickup_courses.xml"
|
||||
ADCourseLibrary.ROOT = "ADSmartPickupCourses"
|
||||
ADCourseLibrary.MAX_ENTRIES = 40
|
||||
ADCourseLibrary.LOG_PREFIX = "ADSmartPickup: "
|
||||
|
||||
-- key -> {course = Course, used = løbenummer}; used stiger ved hver brug (mindst brugte smides ud først)
|
||||
ADCourseLibrary.entries = {}
|
||||
ADCourseLibrary.counter = 0
|
||||
ADCourseLibrary.loaded = false
|
||||
ADCourseLibrary.writable = false -- filen må skrives (læst uden fejl, eller fandtes ikke)
|
||||
ADCourseLibrary.dirty = false -- noget er ændret siden sidste gem
|
||||
|
||||
-- ---- rene regler ----
|
||||
|
||||
-- Marken i en nøgle ("41|50.0|sig|off=..." -> 41); nil når nøglen ikke har formen.
|
||||
function ADCourseLibrary.fieldOfKey(key)
|
||||
if type(key) ~= "string" then return nil end
|
||||
return tonumber(key:match("^([^|]+)|"))
|
||||
end
|
||||
|
||||
-- Nøglerne der skal ud for at holde biblioteket på max poster: de mindst brugte (laveste used), ældste først.
|
||||
function ADCourseLibrary.evictions(entries, max)
|
||||
local keys = {}
|
||||
for key in pairs(entries or {}) do table.insert(keys, key) end
|
||||
if #keys <= (max or 0) then return {} end
|
||||
table.sort(keys, function(a, b)
|
||||
local ua, ub = entries[a].used or 0, entries[b].used or 0
|
||||
if ua ~= ub then return ua < ub end
|
||||
return a < b
|
||||
end)
|
||||
local drop = {}
|
||||
for index = 1, #keys - max do table.insert(drop, keys[index]) end
|
||||
return drop
|
||||
end
|
||||
|
||||
-- Nøglerne for en mark (fieldId) eller alle (nil); keepSig = behold poster med netop den formsignatur.
|
||||
function ADCourseLibrary.keysFor(entries, fieldId, keepSig)
|
||||
local keys = {}
|
||||
for key in pairs(entries or {}) do
|
||||
local sig = key:match("^[^|]+|[^|]+|([^|]+)|")
|
||||
if (fieldId == nil or ADCourseLibrary.fieldOfKey(key) == fieldId) and (keepSig == nil or sig ~= keepSig) then
|
||||
table.insert(keys, key)
|
||||
end
|
||||
end
|
||||
table.sort(keys)
|
||||
return keys
|
||||
end
|
||||
|
||||
-- ---- spil-nær lim ----
|
||||
|
||||
local function touch(entry)
|
||||
ADCourseLibrary.counter = ADCourseLibrary.counter + 1
|
||||
entry.used = ADCourseLibrary.counter
|
||||
end
|
||||
|
||||
local function isSingleVehicle(course)
|
||||
return course ~= nil and (course.nVehicles == nil or course.nVehicles == 1)
|
||||
end
|
||||
|
||||
local materialize -- (entry) -> course|nil; pakker et gemt kursus ud første gang det bruges (spil-lim nedenfor)
|
||||
|
||||
-- Kurset til nøglen (en kopi gives videre af ADCpBridge via cpCopyCourse), eller nil.
|
||||
function ADCourseLibrary.get(key)
|
||||
if key == nil then return nil end
|
||||
ADCourseLibrary.load()
|
||||
local entry = ADCourseLibrary.entries[key]
|
||||
if entry == nil then return nil end
|
||||
local course = materialize(entry)
|
||||
if course == nil then
|
||||
ADCourseLibrary.entries[key] = nil
|
||||
ADCourseLibrary.dirty = true
|
||||
return nil
|
||||
end
|
||||
touch(entry)
|
||||
return course
|
||||
end
|
||||
|
||||
-- Gem en kopi af kurset under nøglen. Findes nøglen, tæller det kun som brug — medmindre replace (et nyberegnet
|
||||
-- kursus afløser det gemte). Kun kurser til én maskine.
|
||||
function ADCourseLibrary.put(key, course, replace)
|
||||
if key == nil or not isSingleVehicle(course) or course.copy == nil then return end
|
||||
ADCourseLibrary.load()
|
||||
local entry = ADCourseLibrary.entries[key]
|
||||
if entry ~= nil and not replace then
|
||||
touch(entry)
|
||||
return
|
||||
end
|
||||
local ok, copy = pcall(course.copy, course)
|
||||
if not ok or copy == nil then
|
||||
Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: kunne ikke kopiere kursus: %s", tostring(copy))
|
||||
return
|
||||
end
|
||||
-- Course:copy tager ikke disse med: compacted = lige rækker gemmes som start+slut (ellers bliver filen stor)
|
||||
copy.compacted, copy.headlandClockwise, copy.islandHeadlandClockwise = course.compacted, course.headlandClockwise, course.islandHeadlandClockwise
|
||||
entry = {course = copy}
|
||||
touch(entry)
|
||||
ADCourseLibrary.entries[key] = entry
|
||||
ADCourseLibrary.dirty = true
|
||||
for _, drop in ipairs(ADCourseLibrary.evictions(ADCourseLibrary.entries, ADCourseLibrary.MAX_ENTRIES)) do
|
||||
ADCourseLibrary.entries[drop] = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- Glem kurser for en mark (nil = alle); keepSig = behold dem med markens nuværende form. Returnerer antal.
|
||||
function ADCourseLibrary.forget(fieldId, keepSig)
|
||||
ADCourseLibrary.load()
|
||||
local keys = ADCourseLibrary.keysFor(ADCourseLibrary.entries, fieldId, keepSig)
|
||||
for _, key in ipairs(keys) do ADCourseLibrary.entries[key] = nil end
|
||||
if #keys > 0 then ADCourseLibrary.dirty = true end
|
||||
return #keys
|
||||
end
|
||||
|
||||
local function getPath()
|
||||
return ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() ~= nil
|
||||
and (ADRunsStorage.getPath():gsub("[^/\\]+$", ADCourseLibrary.FILE_NAME)) or nil
|
||||
end
|
||||
|
||||
-- Courseplays klasser ligger i dens mod-miljø, den globale tabel FS25_Courseplay (zip-navnet) — som FS25_AutoDrive
|
||||
local function cpClass(name)
|
||||
local env = FS25_Courseplay
|
||||
return type(env) == "table" and env[name] or nil
|
||||
end
|
||||
|
||||
local schema = nil
|
||||
local function getSchema()
|
||||
if schema ~= nil then return schema end
|
||||
local courseManager = cpClass("CpCourseManager")
|
||||
if XMLSchema == nil or courseManager == nil or courseManager.registerXmlSchemaValues == nil then return nil end
|
||||
schema = XMLSchema.new("ADSmartPickupCourses")
|
||||
local key = ADCourseLibrary.ROOT .. ".course(?)"
|
||||
courseManager.registerXmlSchemaValues(schema, key)
|
||||
schema:register(XMLValueType.STRING, key .. "#adspKey", "ADSmartPickup: mark|bredde|form|indstillinger")
|
||||
schema:register(XMLValueType.INT, key .. "#adspUsed", "ADSmartPickup: senest brugt (løbenummer)")
|
||||
return schema
|
||||
end
|
||||
|
||||
-- Den gemte fil holdes åben, og et kursus pakkes først ud når det skal bruges: at læse alle kurser på én gang
|
||||
-- (Waypoint() slår terrænhøjden op for hvert punkt) ville selv give den frysning biblioteket skal fjerne (review 27/10).
|
||||
local openXml = nil
|
||||
|
||||
local function closeXml()
|
||||
if openXml ~= nil then
|
||||
openXml:delete()
|
||||
openXml = nil
|
||||
end
|
||||
end
|
||||
|
||||
materialize = function(entry)
|
||||
if entry.course ~= nil then return entry.course end
|
||||
local courseClass = cpClass("Course")
|
||||
if entry.xmlKey == nil or openXml == nil or courseClass == nil or courseClass.createFromXml == nil then return nil end
|
||||
local ok, course = pcall(courseClass.createFromXml, nil, openXml, entry.xmlKey)
|
||||
if not ok or not isSingleVehicle(course) then
|
||||
if not ok then Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: et gemt kursus kunne ikke læses: %s", tostring(course)) end
|
||||
return nil
|
||||
end
|
||||
entry.course, entry.xmlKey = course, nil
|
||||
return course
|
||||
end
|
||||
|
||||
-- Kun nøglerne læses; nil = filen kunne ikke åbnes/læses (så må den heller ikke overskrives).
|
||||
local function loadUnsafe()
|
||||
local path, xmlSchema = getPath(), getSchema()
|
||||
if path == nil or xmlSchema == nil or cpClass("Course") == nil then return nil end
|
||||
if fileExists ~= nil and not fileExists(path) then return 0 end
|
||||
local xmlFile = XMLFile.loadIfExists("adspCourses", path, xmlSchema)
|
||||
if xmlFile == nil then return nil end
|
||||
openXml = xmlFile
|
||||
local count = 0
|
||||
xmlFile:iterate(ADCourseLibrary.ROOT .. ".course", function(_, key)
|
||||
local libraryKey = xmlFile:getValue(key .. "#adspKey")
|
||||
if libraryKey ~= nil then
|
||||
local used = xmlFile:getValue(key .. "#adspUsed") or 0
|
||||
ADCourseLibrary.entries[libraryKey] = {xmlKey = key, used = used}
|
||||
ADCourseLibrary.counter = math.max(ADCourseLibrary.counter, used)
|
||||
count = count + 1
|
||||
end
|
||||
end)
|
||||
return count
|
||||
end
|
||||
|
||||
-- Indlæs biblioteket fra savegamet første gang det bruges. Fejler læsningen, skrives filen aldrig over (writable).
|
||||
function ADCourseLibrary.load()
|
||||
if ADCourseLibrary.loaded then return end
|
||||
ADCourseLibrary.loaded = true
|
||||
local ok, count = pcall(loadUnsafe)
|
||||
ADCourseLibrary.writable = ok and count ~= nil
|
||||
if not ok then
|
||||
Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek kunne ikke indlæses: %s", tostring(count))
|
||||
elseif count ~= nil and count > 0 then
|
||||
logInfo(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek: %d gemte kurser fundet", count)
|
||||
end
|
||||
end
|
||||
|
||||
local function saveUnsafe()
|
||||
local path, xmlSchema = getPath(), getSchema()
|
||||
if path == nil or xmlSchema == nil then return end
|
||||
-- filen skrives om: kurser der endnu ikke er pakket ud, læses først (kun når noget er ændret)
|
||||
local keys = {}
|
||||
for key, entry in pairs(ADCourseLibrary.entries) do
|
||||
if materialize(entry) ~= nil then table.insert(keys, key) end
|
||||
end
|
||||
table.sort(keys)
|
||||
closeXml()
|
||||
local xmlFile = XMLFile.create("adspCourses", path, ADCourseLibrary.ROOT, xmlSchema)
|
||||
if xmlFile == nil then return end
|
||||
local ok, err = pcall(function()
|
||||
for index, libraryKey in ipairs(keys) do
|
||||
local entry = ADCourseLibrary.entries[libraryKey]
|
||||
local key = string.format("%s.course(%d)", ADCourseLibrary.ROOT, index - 1)
|
||||
entry.course:saveToXml(xmlFile, key)
|
||||
xmlFile:setValue(key .. "#adspKey", libraryKey)
|
||||
xmlFile:setValue(key .. "#adspUsed", entry.used or 0)
|
||||
end
|
||||
xmlFile:save()
|
||||
end)
|
||||
xmlFile:delete()
|
||||
if not ok then error(err, 0) end
|
||||
ADCourseLibrary.dirty = false
|
||||
end
|
||||
|
||||
-- Skriv biblioteket i savegamet (når spillet gemmer) — kun når noget er ændret, og aldrig hvis læsningen fejlede.
|
||||
function ADCourseLibrary.save()
|
||||
if not ADCourseLibrary.loaded or not ADCourseLibrary.writable or not ADCourseLibrary.dirty then return end
|
||||
local ok, err = pcall(saveUnsafe)
|
||||
if not ok then Logging.warning(ADCourseLibrary.LOG_PREFIX .. "kursus-bibliotek kunne ikke gemmes: %s", tostring(err)) end
|
||||
end
|
||||
|
||||
-- Spillet lukker kortet: den åbne fil frigives
|
||||
function ADCourseLibrary.close()
|
||||
pcall(closeXml)
|
||||
end
|
||||
|
||||
function ADCourseLibrary.installSaveHook()
|
||||
if ItemSystem == nil or ItemSystem.save == nil or Utils == nil then return end
|
||||
ItemSystem.save = Utils.prependedFunction(ItemSystem.save, function() ADCourseLibrary.save() end)
|
||||
if FSBaseMission ~= nil and FSBaseMission.delete ~= nil then
|
||||
FSBaseMission.delete = Utils.prependedFunction(FSBaseMission.delete, function() ADCourseLibrary.close() end)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user