

--/=============================== Sys ================================/--
    scexpTITLESTR = "Sc.Exp"
    scexpSFMPQSTR = "ScExp\\ScExpMPQed.exe"
    scexpSCEXPPATH = (grim.getregpair("HKEY_CURRENT_USER\\Software\\Grimoire\\","GrimPath") .. "\\ScExp\\")
    scexpCAMPBUILDPATHSTR = scexpSCEXPPATH .. "LastBuiltMap.w3x"
    scexpREGDIR = confregpath
--/========================
    scexpm = wehack.addmenu("Campaign Compiler (Sc Exp.)")
if scexpSCEXPPATH ~= "" and scexpSCEXPPATH ~= 0 then
    scexpcampbuilderm = TogMenuEntry:New(scexpm,"Enable CampaignBuilder",scexpUpdateReg,true)
    wehack.addmenuseparator(scexpm)
    scexpuselastcampm = TogMenuEntry:New(scexpm,"Use \"Recent Document\" as current campaign",scexpUpdateReg,true)
--/========================
    camppath = ""
    submappath = ""
    savemappath = ""

    function scexpGetCampPath()
        if scexpuselastcampm.checked then
            camppath = grim.getregpair("HKEY_CURRENT_USER\\Software\\Blizzard Entertainment\\WorldEdit\\","Campaign Editor - Recent Document 00")
        else
            camppath = ""
        end
    end

    function scexpCompileFile(filepath)
        compilemap_path(filepath) 
        if mapvalid then
            wehack.messagebox("Compiled to: \n" .. filepath,scexpTITLESTR,false)
        end
    end

    function scexpExportCampaignMapFromTo(campaignPath,submappath,SaveToPath)
        local cmdline = scexpSFMPQSTR .. " -e \"" .. campaignPath .. "\" " .. submappath .. " \"" .. SaveToPath .. "\""
        wehack.runprocess(cmdline)
    end
    
    function scexpImportCampaignMap()
        local cmdline = ""
        if grim.exists(savemappath) then
            cmdline = scexpSFMPQSTR .. " -i \"" .. camppath .. "\" \"" .. savemappath .. "\" \"" .. submappath .. "\""
            wehack.runprocess(cmdline)
        end
    end
    
--/========================
    
    function scexpCompileFileDialog()
        local filepath = wehack.openfiledialog("Warcraft 3 map (*.w3x)|*.w3x|", "w3x", "Source map to compile..", false)
        if filepath == "" then
            return
        end
        scexpCompileFile(filepath)        
    end
    
    function scexpCampaignExport()
        submappath = ""
        savemappath = ""
        scexpGetCampPath()
        if camppath == "" then
            camppath = wehack.openfiledialog("Warcraft 3 Campaign (*.w3n)|*.w3n|", "w3n", "Campaing to export..", false)
        end
        if camppath == "" then
            return 2
        end
        submappath = wehack.inputbox("Enter map name (in the campaign):",scexpTITLESTR,"1.w3x")
        if submappath == "" then
            return 2
        end
        savemappath = scexpCAMPBUILDPATHSTR
        --savemappath = wehack.savefiledialog("Warcraft 3 map (*.w3x)|*.w3x|", "w3x", "Save exported map to..")
        if savemappath == "" then
            return 1
        end
        scexpExportCampaignMapFromTo(camppath,submappath,savemappath)
        if not grim.exists(savemappath) then
            wehack.messagebox("Extracting error!\nFile \"" ..savemappath.. "\" not found." ,scexpTITLESTR,false)
            return 1
        end
        return 0
    end
    
    function scexpDoBuildCampaign()
        local result = scexpCampaignExport()
        if result == 0 then
            compilemap_path(savemappath)
            if mapvalid then
                scexpImportCampaignMap()
            end
        elseif result == 2 then
            return
        else
            wehack.messagebox("Campaign export error.",scexpTITLESTR,false)
        end
    end
    
    function scexpBuildCampaign()
        if scexpcampbuilderm.checked then
            scexpDoBuildCampaign()
        end
    end
    
--/=============================== Interface ================================/--
    function scexpAbout()
        wehack.messagebox("ScorpY's Expansion (Campaign builder)\n\nAuthor: ScorpioT1000\nThanks to: ShadoW DaemoN\n\n     Powered by XGM.ru\n               2009",scexpTITLESTR,false)
    end
    
    scexpcompilefilem = MenuEntry:New(scexpm,"Compile Map..",scexpCompileFileDialog)
    scexpcampexport = MenuEntry:New(scexpm,"Build campaign map..",scexpDoBuildCampaign)
    wehack.addmenuseparator(scexpm)
    scexpaboutm = MenuEntry:New(scexpm,"About..",scexpAbout)
    
	function scexpshowhelp()
		wehack.execprocess("starter.bat ./ScExp\\CampaignBuilder.html")
	end
	
	scexphelpm = MenuEntry:New(scexpm,"Campaign Builder Documentation...",scexpshowhelp)
	
    if grim.getregpair(scexpREGDIR,"Enable CampaignBuilder") == "off" then
        wehack.checkmenuentry(scexpcampbuilderm.menu,scexpcampbuilderm.id,0)
    end
    if grim.getregpair(scexpREGDIR,"Use \"Recent Document\" as current campaign") == "off" then
        wehack.checkmenuentry(scexpuselastcampm.menu,scexpuselastcampm.id,0)
    end
else
    MenuEntry:New(scexpm,"Error loading Grimoire path from registry!",nil)
end
