--[[
ChangedMotorSounds

Specialization to change the motor Sound of an vehicle. e. g. DLC vehicles

Author: 	Ifko[nator]
Date: 		07.09.2020
Version:	1.0

History:	v1.0 @07.09.2020 - initial implemation in FS 19
			v1.1 @22.10.2020 - added possibility to change the 'motorStartDuration' value
]]

ChangedMotorSounds = {};
ChangedMotorSounds.currentModDirectory = g_currentModDirectory;

function ChangedMotorSounds.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function ChangedMotorSounds.registerEventListeners(vehicleType)
	local functionNames = {
		"onPreLoad"
	};
	
	for _, functionName in ipairs(functionNames) do
		SpecializationUtil.registerEventListener(vehicleType, functionName, ChangedMotorSounds);
	end;
end;

function ChangedMotorSounds:onPreLoad(savegame)
	local modDesc = loadXMLFile("modDesc", ChangedMotorSounds.currentModDirectory .. "modDesc.xml");
	
	local isSoundExtensionBought = false;
	local soundExtensionConfigurationId = self.configurations["soundExtension"];
	
    if soundExtensionConfigurationId ~= nil then
		local storeItem = g_storeManager:getItemByXMLFilename(self.configFileName);
		
		if storeItem ~= nil then
            local soundExtensionConfiguration = storeItem.configurations["soundExtension"][soundExtensionConfigurationId];

            if soundExtensionConfiguration ~= nil then
                isSoundExtensionBought = soundExtensionConfiguration.index == 2;
            end;
        end;
	end;
	
	local vehicleNumber = 0;
	local i3dFilenameBase = getXMLString(self.xmlFile, "vehicle.base.filename");

	while true do
		local vehicleKey = "modDesc.newMotorSounds.newMotorSound(" .. tostring(vehicleNumber) .. ")";

		if not hasXMLProperty(modDesc, vehicleKey) then
			break;
		end;

		local externalMotorSoundFile = Utils.getNoNil(getXMLString(modDesc, vehicleKey .. "#externalMotorSoundFile"), "");
		local i3dFilename = Utils.getNoNil(getXMLString(modDesc, vehicleKey .. "#i3dFilename"), "");
		local modName = Utils.getNoNil(getXMLString(modDesc, vehicleKey .. "#modName"), "");
		local motorStartDuration = Utils.getNoNil(getXMLInt(modDesc, vehicleKey .. "#motorStartDuration"), 0);
		
		if externalMotorSoundFile ~= "" and i3dFilename ~= "" then
			if string.find(i3dFilenameBase, i3dFilename) then	
				local allowAddSound = true;

				if modName ~= "" then
					allowAddSound = string.find(self.configFileName, modName);
				end;

				if motorStartDuration > 0 then
					setXMLInt(self.xmlFile, "vehicle.motorized.motorStartDuration", motorStartDuration);
				end;

				if allowAddSound and isSoundExtensionBought then
					setXMLString(self.xmlFile, "vehicle.motorized.sounds#externalSoundFile", externalMotorSoundFile);
				end;
			end;
		end;

		vehicleNumber = vehicleNumber + 1;
	end;

	delete(modDesc);
end;

function ChangedMotorSounds.getFilename(filename, superFunc, baseDir)
	if filename == nil then
		printCallstack();

		return nil;
	end;

	local baseDirBackup = baseDir;

	baseDir = ChangedMotorSounds.currentModDirectory;

	if filename:sub(1, 1) == "$" and not fileExists(baseDir .. filename) then
		return filename:sub(2), false;
	elseif baseDirBackup == nil or baseDirBackup == "" and not fileExists(baseDir .. filename) then
		return filename, false;
	elseif filename == "" then
		return filename, true;
	end;

	if not fileExists(baseDir .. filename) then
		baseDir = baseDirBackup;
	end;

	return baseDir .. filename, true;
end;

Utils.getFilename = Utils.overwrittenFunction(Utils.getFilename, ChangedMotorSounds.getFilename);