-- Mod Initialization

local modDirectory = g_currentModDirectory
local modName = g_currentModName

-- Source extension scripts
source(modDirectory .. "scr/bunkerSiloExtension.lua")
source(modDirectory .. "scr/FilltypeExtension.lua")
source(modDirectory .. "scr/BGA_FilltypeExtension.lua")
source(modDirectory .. "scr/FeedingRobot_Extension.lua")
source(modDirectory .. "scr/BaleFermentation_Grass_fermented.lua")
source(modDirectory .. "scr/AnimalFoodOverride.lua")
source(modDirectory .. "scr/MixerWagonEffectsExtension.lua")

local fillTypesXmlPath = modDirectory .. "xml/fillTypes.xml"
local heightTypesXmlPath = modDirectory .. "xml/densityMapHeightTypes.xml"

local ModLoader = {}
ModLoader.debug = false

function ModLoader:onLoadFillTypes(superFunc)
    superFunc(self)
    
    if fileExists(fillTypesXmlPath) then
        local xmlFile = XMLFile.load("ModFillTypes", fillTypesXmlPath, FillTypeManager.xmlSchema)
        if xmlFile ~= nil then
            self:loadFillTypes(xmlFile, modDirectory, false, modName)
            xmlFile:delete()
        elseif ModLoader.debug then
            Logging.warning("[%s] Failed to load custom fillTypes from '%s'", modName, fillTypesXmlPath)
        end
    end
end

function ModLoader:registerHeightTypes()
    if fileExists(heightTypesXmlPath) then
        table.insert(g_densityMapHeightManager.modDensityHeightMapTypeFilenames, heightTypesXmlPath)
    elseif ModLoader.debug then
        Logging.info("[%s] No densityMapHeightTypes.xml found, skipping height types registration.", modName)
    end
end

FillTypeManager.loadDefaultTypes = Utils.overwrittenFunction(FillTypeManager.loadDefaultTypes, ModLoader.onLoadFillTypes)

ModLoader:registerHeightTypes()