Jump to content

Tame the fox with Firemin


Rizonetech
 Share

Recommended Posts

Here is a scrpt I did some time ago. Firemin reduces the memory Firefox uses. I know that Windows will also periodically scan running processes and tell them to release their unused memory. You could argue that this makes Firemin unnecessary, and you would be spot on. However, It is my opinion that Windows does not clean out memory as often as I would like it to. Firemin runs the clean memory API call a few times per second. The method Firemin uses to decrease Firefox memory usage is not proven and the debate over if it works or not will go on until the end of time, but here is my opinion; If Firemin works for you, use it and if it does not, delete it!

Firemin-Options.jpg

You can download Firemin and Source Code at: https://www.rizonesoft.com/download/firemin/ or view the script below:

Firemin.au3

 

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Resources\Firemin.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=Firemin
#AutoIt3Wrapper_Res_Fileversion=2.0.8.2087
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=© 2014 Rizonesoft
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_Icon_Add=Resources\Facebook.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Twitter.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Palfarm.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\LinkedIn.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Google.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\RSS.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Info.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\PayPal.ico
#AutoIt3Wrapper_Res_File_Add=resources\gradbar.jpg, rt_rcdata, GRADBAR_IMAGE
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


Opt("GUICloseOnESC", 0)             ;1=ESC  closes, 0=ESC won't close
Opt("GUIOnEventMode", 1)            ;0=disabled, 1=OnEvent mode enabled
Opt("TrayOnEventMode", 1)
Opt("MustDeclareVars", 1)           ;0=no, 1=require pre-declare
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 3)


;~ #include <WindowsConstants.au3>
#include <StaticConstants.au3>
;~ #include <ButtonConstants.au3>
#include <GuiConstantsEx.au3>
#include <WinAPIProc.au3>
;~ #include <GuiEdit.au3>
#include <Misc.au3>

#include <UDF\Functions.au3>
#include <UDF\Resources.au3>
;#include <UDF\MemBoost.au3>


_Singleton(@ScriptName, 0)


Global $PATH_FIREFOX = ""
If @OSArch = "X64" Then
    $PATH_FIREFOX = @HomeDrive & "Program Files (x86)\Mozilla Firefox\firefox.exe"
Else
    $PATH_FIREFOX = @ProgramFilesDir & "\Mozilla Firefox\firefox.exe"
EndIf


Global $PATH_FIREFOXPROFILE = @AppDataDir & "\Mozilla\Firefox\Profiles"
Global $BIN_SQLITE = @ScriptDir & "\Bin\sqlite3.exe"
Global $USB_FIREFOX = @ScriptDir & "\FirefoxPortable.exe"
Global $USB_FIREFOXPROFILE = "\Data\profile"
Global $INI_FILE =  @ScriptDir & "\Firemin.ini"
Global $INI_BOOST = IniRead($INI_FILE, "Firemin", "Boost", 250)
Global $INI_LAUNCH = IniRead($INI_FILE, "Firemin", "Launch", 0)
Global $INI_FIREFOXPATH = IniRead($INI_FILE, "Firemin", "Path", $PATH_FIREFOX)

Global $fboGui, $tmnuAbout, $tmnuOptions, $tmnuOFire, $tmnuLaFire, $tmnuLaFSafe, $tmnuClose, $sldrMillisec
Global $msCount = 0, $lblSldValue, $radLchFire, $radLchSafe, $radLchNone, $chkAutStart
Global $inpFireLoc, $btnFireLoc, $ffPSize = 0, $dbScnd = 0
Global $aboutDlg


If Not FileExists($BIN_SQLITE) Then
    MsgBox(262160, "Firemin",    "Unfortunately the Optimize Firefox option will not be available, because the " & _
                                "‘\Bin\ sqlite3.exe’ could not be found. Why you assumed it was a good idea to delete it, we will never know.", 60)
EndIf


If $INI_LAUNCH = 1 Then _LaunchFirefox()
If $INI_LAUNCH = 2 Then _LaunchFirefoxSafeMode()


Func _LaunchFirefox()
    If Not ProcessExists("firefox.exe") Then
        ShellExecute($PATH_FIREFOX)
    EndIf
EndFunc


Func _LaunchFirefoxSafeMode()
    If Not ProcessExists("firefox.exe") Then
        ShellExecute($PATH_FIREFOX, "-safe-mode")
    EndIf
EndFunc


coreInterface()


Func coreInterface()

    ;~ Build Firemin Tray Menu
    $tmnuAbout = TrayCreateItem("About Firemin...")
    TrayCreateItem("")
    $tmnuOptions = TrayCreateItem("Options...")
    TrayCreateItem("")
    $tmnuOFire = TrayCreateItem("Optimize Firefox")
    If Not FileExists($BIN_SQLITE) Then TrayItemSetState($tmnuOFire, 128) ;~ $GUI_DISABLE = 128
    TrayCreateItem("")
    $tmnuLaFire = TrayCreateItem("Launch Firefox")
    $tmnuLaFSafe = TrayCreateItem("Launch Firefox in Safemode")
    TrayCreateItem("")
    $tmnuClose = TrayCreateItem("Close")

    TrayItemSetOnEvent($tmnuAbout, "_ShowAboutDlg")
    TrayItemSetOnEvent($tmnuOptions, "_OptionsDlg")
    TrayItemSetOnEvent($tmnuOFire, "_VacuumFirefox")
    TrayItemSetOnEvent($tmnuLaFire, "_LaunchFirefox")
    TrayItemSetOnEvent($tmnuLaFSafe, "_LaunchFirefoxSafeMode")
    TrayItemSetOnEvent($tmnuClose, "_CloseFiremin")

    TraySetState()
    TraySetClick(8)

    TraySetToolTip("Firemin " & FileGetVersion(@ScriptFullPath))

    AdlibRegister("_OptimizeFireFox", $INI_BOOST)

    While 1
        Sleep(100)
        _WinAPI_EmptyWorkingSet()
    WEnd

EndFunc


Func _CloseFiremin()
    TraySetState(2)
    Exit
EndFunc


Func _OptimizeFireFox()

    Local $aProcsList, $aPIniList

    $aPIniList = StringSplit("firefox.exe|plugin-container.exe", "|")

    $aProcsList = ProcessList()
    For $i = 1 To $aProcsList[0][0]
        For $x = 1 To $aPIniList[0]
            If $aProcsList[$i][0] = $aPIniList[$x] Then
                _WinAPI_EmptyWorkingSet($aProcsList[$i][1])
            EndIf
        Next
    Next

EndFunc


Func _VacuumFirefox()

    ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Warning, Timeout=60 ss
    If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(49,"Note","Firemin uses the SQLite Vacuum function to optimize the databases. Although this function is safe, it cannot be undone. If you’re not sure what the SQLite Vacuum function is, stop being daft and Google It!",60)
    Select
        Case $iMsgBoxAnswer = 1 ;OK
            TraySetToolTip("Optimizing or Breaking Firefox")
            TraySetState(4)

            If ProcessExists("firefox.exe") Then
                MsgBox(262192, "Firemin",     "For this function to work properly you will need to close Firefox. Yes, I mean you can close it know. " & _
                                            "Sorry about not being able to do it automatically, but the idiot who programmed Firemin is a lazy bugger and never got around to finishing the function.", 60)
            Else
                _VacuumDatabases()
            EndIf

        TraySetState(8)
        TraySetToolTip("Firefox " & FileGetVersion(@ScriptFullPath))

            Case $iMsgBoxAnswer = 2 ;Cancel
            Case $iMsgBoxAnswer = -1 ;Timeout

    EndSelect

EndFunc


Func _VacuumDatabases()

    ProgressOn("Breaking Firefox", "Initializing, I think", "Breaking Firefox")

    Local $ffProfile

    If FileExists($USB_FIREFOX) Then
        $ffProfile = @ScriptDir & $USB_FIREFOXPROFILE
    Else
        $ffProfile = $PATH_FIREFOXPROFILE
    EndIf

    $ffPSize = DirGetSize($ffProfile)

    Local $SearchHandle = FileFindFirstFile($ffProfile & "\*.*")
    If ($SearchHandle == -1) Then
        Return
    EndIf

    If (@error == 1) Then
;~         ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical, Timeout=60 ss
        If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(48, "Notice!", "Obviously we need something to scan. Good luck with finding support for this error.", 60)
        Return
    EndIf

    If Not _ScanProfileDir($SearchHandle, $ffProfile) Then

        ProgressOff()
        $ffPSize = 0
        $dbScnd = 0

        FileClose($SearchHandle)
        Return

    EndIf

EndFunc


Func _ScanProfileDir($SearchHandle, $ffProfile)

    Local $Return, $sQliteDb, $SecSearch, $dbScndPr

    While (True)

        $sQliteDb = FileFindNextFile($SearchHandle)
        If @error Then ExitLoop

        $Return &= $ffProfile & "\" & $sQliteDb

        If StringInStr(FileGetAttrib($ffProfile & "\" & $sQliteDb), "D") Then
            $SecSearch = FileFindFirstFile($ffProfile & "\" & $sQliteDb & "\*.*")
            $Return &= _ScanProfileDir($SecSearch, $ffProfile & "\" & $sQliteDb)
            FileClose($SecSearch)
        Else

            $dbScnd += FileGetSize($Return)
            $dbScndPr = Round(($dbScnd * 100) / $ffPSize)

            Local $aSplPath = StringSplit($Return, ".")
            If StringInStr($aSplPath[$aSplPath[0]], "sqlite") Then
                Sleep(50)
                If FileExists($BIN_SQLITE) Then ShellExecute(@ScriptDir & "\Bin\sqlite3.exe", Chr(34) & $Return & Chr(34) & " vacuum", "", "", @SW_HIDE)
                Local $aSplFile = StringSplit($Return, "\")
                ProgressSet($dbScndPr, "Breaking [ " & $aSplFile[$aSplFile[0]] & " ]", "Breaking Firefox (" & $dbScndPr & "%)")
            EndIf

        EndIf

        $Return = ""

    WEnd

    Return $Return

EndFunc


Func _OptionsDlg()

    Local Const $OptionsTitle = "Firemin " & _GetExecVersioning(@ScriptFullPath, 5) & " Options"

    If _Singleton($OptionsTitle, 1) Then

        Local $picGradBar, $lblSlDesc

        $fboGui = GUICreate($OptionsTitle, 350, 350, -1, -1)
        GUISetFont(8.5, 400, 0, "Verdana", $fboGui, 5)

        $picGradBar = GUICtrlCreatePic("", 25, 10, 300, 20)
        _ResourceSetImageToCtrl($picGradBar, "GRADBAR_IMAGE", $RT_RCDATA)
        $lblSldValue = GUICtrlCreateLabel(0, 127, 12, 100, 20, 0x1) ;~ $SS_CENTER = 0x1
        GUICtrlSetBkColor($lblSldValue, -2) ;~ $GUI_BKCOLOR_TRANSPARENT = -2

        $sldrMillisec = GUICtrlCreateSlider(25, 35, 300, 35)
        GUICtrlSetLimit($sldrMillisec, 1000, 100)
        GuiCtrlSetData($sldrMillisec, $INI_BOOST)

        _SetMilliseconds()
        GUICtrlSetOnEvent($sldrMillisec, "_SetMilliseconds")

        $lblSlDesc = GUICtrlCreateLabel("The faster you set the optimization the more processing power " & _
                                    "is needed, but the less memory Firefox uses and vice versa. " & _
                                    "Between 300 and 500 is a good setting.", 10, 85, 330, 55)
        GuiCtrlSetColor($lblSlDesc, 0x555555)

        $radLchFire = GUICtrlCreateRadio("Launch Firefox when Firemin starts", 20, 155, 330, 20)
        $radLchSafe = GUICtrlCreateRadio("Launch Firefox in Safemode when Firemin starts", 20, 173, 330, 20)
        $radLchNone = GUICtrlCreateRadio("Don't Launch Firefox when Firemin starts", 20, 191, 330, 20)
        $chkAutStart = GUICtrlCreateCheckbox(" Start Firemin when Windows starts", 20, 230, 330, 20)

        GUICtrlCreateGroup("Firefox location (firefox.exe)", 10, 275, 330, 60)
        $inpFireLoc = GUICtrlCreateInput($INI_FIREFOXPATH, 20, 300, 265, 20)
        GuiCtrlSetTip($inpFireLoc, $INI_FIREFOXPATH)
        $btnFireLoc = GUICtrlCreateButton("...", 290, 297, 30, 25)
        GUICtrlCreateGroup("", -99, -99, 1, 1)  ;close group

        GUICtrlSetOnEvent($btnFireLoc, "_BrowseFireLocation")

        If $INI_LAUNCH = 0 Then
            GUICtrlSetState($radLchNone, $GUI_CHECKED)
        ElseIf $INI_LAUNCH = 1 Then
            GUICtrlSetState($radLchFire, $GUI_CHECKED)
        ElseIf $INI_LAUNCH = 2 Then
            GUICtrlSetState($radLchSafe, $GUI_CHECKED)
        EndIf

        If FileExists(@StartupDir & "\Firemin.lnk") Then GUICtrlSetState($ChkAutStart, $GUI_CHECKED)

        GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseOptionsDlg", $fboGui)

        If FileExists($USB_FIREFOX) Then
            GUICtrlSetState($inpFireLoc, $GUI_DISABLE)
            GUICtrlSetState($btnFireLoc, $GUI_DISABLE)
        EndIf

    EndIf

    GUISetState(@SW_SHOW, $fboGui)

EndFunc


Func _SetMilliseconds()

    $INI_BOOST = GuiCtrlRead($sldrMillisec)

    GuiCtrlSetData($lblSldValue, $INI_BOOST)
    AdlibRegister("_OptimizeFireFox", $INI_BOOST)

EndFunc


Func _BrowseFireLocation()

    $INI_FIREFOXPATH = FileOpenDialog("Select Firefox Location", $INI_FIREFOXPATH, "Firefox (firefox.exe)", 1)
    GUICtrlSetData($inpFireLoc, $INI_FIREFOXPATH)

EndFunc


Func _CloseOptionsDlg()

    ;~ $GUI_CHECKED = 1
    If GUICtrlRead($radLchFire) = 1 Then
        $INI_LAUNCH = 1
    ElseIf GUICtrlRead($radLchSafe) = 1 Then
        $INI_LAUNCH = 2
    ElseIf GUICtrlRead($radLchNone) = 1 Then
        $INI_LAUNCH = 0
    EndIf

    If GUICtrlRead($chkAutStart) = 1 Then
        FileDelete(@StartupDir & "\Firemin.lnk")
        FileCreateShortcut(@ScriptFullPath, @StartupDir & "\Firemin.lnk", @StartupDir)
    Else
        FileDelete(@StartupDir & "\Firemin.lnk")
    EndIf

    IniWrite($INI_FILE, "Firemin", "Launch", $INI_LAUNCH)
    IniWrite($INI_FILE, "Firemin", "Boost", $INI_BOOST)
    If Not FileExists($USB_FIREFOX) Then
        $INI_FIREFOXPATH = GUICtrlRead($inpFireLoc)
        IniWrite($INI_FILE, "Firemin", "Path", $INI_FIREFOXPATH)
    EndIf

    GUISetState(@SW_HIDE, @GUI_WinHandle)

EndFunc


Func _ShowAboutDlg()
    _AboutDlg("Firemin", FileGetVersion(@ScriptFullPath))
EndFunc


Func _AboutDlg($aTitle = "", $aVer = "0.0.0.0")

    If _Singleton("About : " & $aTitle, 1) Then


        Local $aSpaceLabel, $aSpaceProg, $aBtnOK, $abMsg
        Local $aScriptDrive = StringSplit(@ScriptDir, "\")
        Local $DrvSpaceUsed = DriveSpaceTotal($aScriptDrive[1]) - DriveSpaceFree($aScriptDrive[1])
        Local $abTitle, $abVersion, $abCopyright, $abHome, $abGNU
        Local $abPayPal, $abRSS, $abFacebook, $abTwittter, $abPalfarm, $abLinkedIn, $abGoogle

        $aboutDlg = GUICreate("About : " & $aTitle, 400, 500, -1, -1, BitOR(0x00C00000, 0x80000000, 0x00080000)) ;~ $WS_CAPTION = 0x00C00000, $WS_POPUP = 0x80000000, $WS_SYSMENU = 0x00080000
        GUISetFont(8.5, 400, 0, "Verdana", $AboutDlg, 5)
        GUISetIcon(@ScriptFullPath, 207)

        GUISetOnEvent(-3, "_CloseAboutDlg", $AboutDlg) ; Global Const $GUI_EVENT_CLOSE = -3

        GUICtrlCreateIcon(@ScriptFullPath, 99, 10, 10, 64, 64)
        $abPayPal = GUICtrlCreateIcon(@ScriptFullPath, 208, 320, 0, 64, 64)
        GUICtrlSetTip($abPayPal, "Help us keep our software free.")
        GUICtrlSetCursor($abPayPal, 0)
        $abTitle = GUICtrlCreateLabel($aTitle, 88, 16, 220, 18)
        GuiCtrlSetFont($abTitle, 9, 800)
        $abVersion = GUICtrlCreateLabel("Version " & FileGetVersion(@ScriptFullPath), 88, 40, 220, 20)
        $abCopyright = GUICtrlCreateLabel("Copyright © 2014 Rizonesoft", 88, 55, 220, 20)
        GuiCtrlSetColor(-1, 0x555555)

        GUICtrlCreateLabel("Rizonesoft Home: ", 20, 90, 130, 15, $SS_RIGHT)
        $abHome = GUICtrlCreateLabel("www.rizonesoft.com", 155, 90, 280, 15)
        GuiCtrlSetFont($abHome, -1, -1, 4) ;Underlined
        GuiCtrlSetColor($abHome, 0x0000FF)
        GuiCtrlSetCursor($abHome, 0)
        $abGNU = GUICtrlCreateLabel("This program is free software: you can redistribute it and/or modify " & _
                                "it under the terms of the GNU General Public License as published by " & _
                                "the Free Software Foundation, either version 3 of the License, or " & _
                                "(at your option) any later version." & @CRLF & @CRLF & _
                                "This program is distributed in the hope that it will be useful, " & _
                                "but WITHOUT ANY WARRANTY; without even the implied warranty of " & _
                                "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the " & _
                                "GNU General Public License for more details.", 20, 125, 350, 180)
        GuiCtrlSetColor($abGNU, 0x555555)
        GUICtrlCreateLabel(    "Contributors: Derick Payne (Rizonesoft)", 20, 280, 350, 100)

        $abFacebook = GUICtrlCreateIcon(@ScriptFullPath, 201, 20, 450, 32, 32)
        GUICtrlSetCursor($abFacebook, 0)
        $abTwittter = GUICtrlCreateIcon(@ScriptFullPath, 202, 55, 450, 32, 32)
        GUICtrlSetCursor($abTwittter, 0)
        $abLinkedIn = GUICtrlCreateIcon(@ScriptFullPath, 204, 90, 450, 32, 32)
        GUICtrlSetCursor($abLinkedIn, 0)
        $abGoogle = GUICtrlCreateIcon(@ScriptFullPath, 205, 125, 450, 32, 32)
        GUICtrlSetCursor($abGoogle, 0)

        $aSpaceLabel =     GUICtrlCreateLabel("(" & $aScriptDrive[1] & ") " & Round(DriveSpaceFree($aScriptDrive[1]) / 1024, 1) & " GB free of " & _
                        Round(DriveSpaceTotal($aScriptDrive[1]) / 1024, 1) & " GB", 15, 380, 330, 15)
        $aSpaceProg = GUICtrlCreateProgress(15, 400, 360, 15)
        GUICtrlSetData($aSpaceProg, ($DrvSpaceUsed / DriveSpaceTotal($aScriptDrive[1])) * 100)

        $aBtnOK = GUICtrlCreateButton("OK", 255, 450, 123, 33, 0x0001) ;~ $BS_DEFPUSHBUTTON = 0x0001

        GUICtrlSetOnEvent($aBtnOK, "_CloseAboutDlg")
        GUICtrlSetOnEvent($abHome, "_HomePageClicked")
        GUICtrlSetOnEvent($abFacebook, "_OpenFacebook")
        GUICtrlSetOnEvent($abTwittter, "_FollowOnTwitter")
        GUICtrlSetOnEvent($abLinkedIn, "_OpenLinkedIn")
        GUICtrlSetOnEvent($abGoogle, "_OpenGooglePlus")

        GUISetOnEvent(-3, "_CloseAboutDlg") ;~ $GUI_EVENT_CLOSE = -3

    EndIf

    GUISetState(@SW_SHOW, $aboutDlg)

EndFunc


Func _CloseAboutDlg()
    GUISetState(@SW_HIDE, @GUI_WinHandle)
EndFunc


Func _HomePageClicked()
    ShellExecute("http://www.rizonesoft.com")
EndFunc


Func _OpenFacebook()
    ShellExecute("https://www.facebook.com/rizonesoft")
EndFunc


Func _FollowOnTwitter()
    ShellExecute("https://twitter.com/rizonesoft")
EndFunc


Func _OpenLinkedIn()
    ShellExecute("http://www.linkedin.com/in/rizonesoft")
EndFunc

Func _OpenGooglePlus()
    ShellExecute("https://plus.google.com/+Rizonesoftsa/posts")
EndFunc

Func _DonateSomething()
    ShellExecute("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7UGGCSDUZJPFE")
EndFunc

 

 

 

Edited by Rizonetech

Rizonesoft Open Source Home Scripts: Complete Internet Repair | Development: Rizonesoft SDK

Link to comment
Share on other sites

I have an opinion, I can't be bothered navigating to external unknown websites that may or may not be rife with malware viruses and drive by downloads.

Post your script here, unless you are just after hits on your website..

And what is the "clean memory API call" ? Is it anything like the ReduceMemory UDF which uses EmptyWorkingSet API and works on any process you pass to it?

EDIT:

For anyone else not like navigating away to look at code.

FireMin.au3

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Resources\Firemin.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=Firemin
#AutoIt3Wrapper_Res_Fileversion=2.0.8.2087
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=© 2014 Rizonesoft
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_Icon_Add=Resources\Facebook.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Twitter.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Palfarm.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\LinkedIn.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Google.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\RSS.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\Info.ico
#AutoIt3Wrapper_Res_Icon_Add=Resources\PayPal.ico
#AutoIt3Wrapper_Res_File_Add=resources\gradbar.jpg, rt_rcdata, GRADBAR_IMAGE
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****


Opt("GUICloseOnESC", 0)             ;1=ESC  closes, 0=ESC won't close
Opt("GUIOnEventMode", 1)            ;0=disabled, 1=OnEvent mode enabled
Opt("TrayOnEventMode", 1)
Opt("MustDeclareVars", 1)           ;0=no, 1=require pre-declare
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 3)


;~ #include <WindowsConstants.au3>
#include <StaticConstants.au3>
;~ #include <ButtonConstants.au3>
#include <GuiConstantsEx.au3>
#include <WinAPIProc.au3>
;~ #include <GuiEdit.au3>
#include <Misc.au3>

#include <UDF\Functions.au3>
#include <UDF\Resources.au3>
;#include <UDF\MemBoost.au3>


_Singleton(@ScriptName, 0)


Global $PATH_FIREFOX = ""
If @OSArch = "X64" Then
    $PATH_FIREFOX = @HomeDrive & "Program Files (x86)\Mozilla Firefox\firefox.exe"
Else
    $PATH_FIREFOX = @ProgramFilesDir & "\Mozilla Firefox\firefox.exe"
EndIf


Global $PATH_FIREFOXPROFILE = @AppDataDir & "\Mozilla\Firefox\Profiles"
Global $BIN_SQLITE = @ScriptDir & "\Bin\sqlite3.exe"
Global $USB_FIREFOX = @ScriptDir & "\FirefoxPortable.exe"
Global $USB_FIREFOXPROFILE = "\Data\profile"
Global $INI_FILE =  @ScriptDir & "\Firemin.ini"
Global $INI_BOOST = IniRead($INI_FILE, "Firemin", "Boost", 250)
Global $INI_LAUNCH = IniRead($INI_FILE, "Firemin", "Launch", 0)
Global $INI_FIREFOXPATH = IniRead($INI_FILE, "Firemin", "Path", $PATH_FIREFOX)

Global $fboGui, $tmnuAbout, $tmnuOptions, $tmnuOFire, $tmnuLaFire, $tmnuLaFSafe, $tmnuClose, $sldrMillisec
Global $msCount = 0, $lblSldValue, $radLchFire, $radLchSafe, $radLchNone, $chkAutStart
Global $inpFireLoc, $btnFireLoc, $ffPSize = 0, $dbScnd = 0
Global $aboutDlg


If Not FileExists($BIN_SQLITE) Then
    MsgBox(262160, "Firemin",   "Unfortunately the Optimize Firefox option will not be available, because the " & _
                                "‘\Bin\ sqlite3.exe’ could not be found. Why you assumed it was a good idea to delete it, we will never know.", 60)
EndIf


If $INI_LAUNCH = 1 Then _LaunchFirefox()
If $INI_LAUNCH = 2 Then _LaunchFirefoxSafeMode()


Func _LaunchFirefox()
    If Not ProcessExists("firefox.exe") Then
        ShellExecute($PATH_FIREFOX)
    EndIf
EndFunc


Func _LaunchFirefoxSafeMode()
    If Not ProcessExists("firefox.exe") Then
        ShellExecute($PATH_FIREFOX, "-safe-mode")
    EndIf
EndFunc


coreInterface()


Func coreInterface()

    ;~ Build Firemin Tray Menu
    $tmnuAbout = TrayCreateItem("About Firemin...")
    TrayCreateItem("")
    $tmnuOptions = TrayCreateItem("Options...")
    TrayCreateItem("")
    $tmnuOFire = TrayCreateItem("Optimize Firefox")
    If Not FileExists($BIN_SQLITE) Then TrayItemSetState($tmnuOFire, 128) ;~ $GUI_DISABLE = 128
    TrayCreateItem("")
    $tmnuLaFire = TrayCreateItem("Launch Firefox")
    $tmnuLaFSafe = TrayCreateItem("Launch Firefox in Safemode")
    TrayCreateItem("")
    $tmnuClose = TrayCreateItem("Close")

    TrayItemSetOnEvent($tmnuAbout, "_ShowAboutDlg")
    TrayItemSetOnEvent($tmnuOptions, "_OptionsDlg")
    TrayItemSetOnEvent($tmnuOFire, "_VacuumFirefox")
    TrayItemSetOnEvent($tmnuLaFire, "_LaunchFirefox")
    TrayItemSetOnEvent($tmnuLaFSafe, "_LaunchFirefoxSafeMode")
    TrayItemSetOnEvent($tmnuClose, "_CloseFiremin")

    TraySetState()
    TraySetClick(8)

    TraySetToolTip("Firemin " & FileGetVersion(@ScriptFullPath))

    AdlibRegister("_OptimizeFireFox", $INI_BOOST)

    While 1
        Sleep(100)
        _WinAPI_EmptyWorkingSet()
    WEnd

EndFunc


Func _CloseFiremin()
    TraySetState(2)
    Exit
EndFunc


Func _OptimizeFireFox()

    Local $aProcsList, $aPIniList

    $aPIniList = StringSplit("firefox.exe|plugin-container.exe", "|")

    $aProcsList = ProcessList()
    For $i = 1 To $aProcsList[0][0]
        For $x = 1 To $aPIniList[0]
            If $aProcsList[$i][0] = $aPIniList[$x] Then
                _WinAPI_EmptyWorkingSet($aProcsList[$i][1])
            EndIf
        Next
    Next

EndFunc


Func _VacuumFirefox()

    ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK and Cancel, Icon=Warning, Timeout=60 ss
    If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(49,"Note","Firemin uses the SQLite Vacuum function to optimize the databases. Although this function is safe, it cannot be undone. If you’re not sure what the SQLite Vacuum function is, stop being daft and Google It!",60)
    Select
        Case $iMsgBoxAnswer = 1 ;OK
            TraySetToolTip("Optimizing or Breaking Firefox")
            TraySetState(4)

            If ProcessExists("firefox.exe") Then
                MsgBox(262192, "Firemin",   "For this function to work properly you will need to close Firefox. Yes, I mean you can close it know. " & _
                                            "Sorry about not being able to do it automatically, but the idiot who programmed Firemin is a lazy bugger and never got around to finishing the function.", 60)
            Else
                _VacuumDatabases()
            EndIf

        TraySetState(8)
        TraySetToolTip("Firefox " & FileGetVersion(@ScriptFullPath))

            Case $iMsgBoxAnswer = 2 ;Cancel
            Case $iMsgBoxAnswer = -1 ;Timeout

    EndSelect

EndFunc


Func _VacuumDatabases()

    ProgressOn("Breaking Firefox", "Initializing, I think", "Breaking Firefox")

    Local $ffProfile

    If FileExists($USB_FIREFOX) Then
        $ffProfile = @ScriptDir & $USB_FIREFOXPROFILE
    Else
        $ffProfile = $PATH_FIREFOXPROFILE
    EndIf

    $ffPSize = DirGetSize($ffProfile)

    Local $SearchHandle = FileFindFirstFile($ffProfile & "\*.*")
    If ($SearchHandle == -1) Then
        Return
    EndIf

    If (@error == 1) Then
;~      ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical, Timeout=60 ss
        If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(48, "Notice!", "Obviously we need something to scan. Good luck with finding support for this error.", 60)
        Return
    EndIf

    If Not _ScanProfileDir($SearchHandle, $ffProfile) Then

        ProgressOff()
        $ffPSize = 0
        $dbScnd = 0

        FileClose($SearchHandle)
        Return

    EndIf

EndFunc


Func _ScanProfileDir($SearchHandle, $ffProfile)

    Local $Return, $sQliteDb, $SecSearch, $dbScndPr

    While (True)

        $sQliteDb = FileFindNextFile($SearchHandle)
        If @error Then ExitLoop

        $Return &= $ffProfile & "\" & $sQliteDb

        If StringInStr(FileGetAttrib($ffProfile & "\" & $sQliteDb), "D") Then
            $SecSearch = FileFindFirstFile($ffProfile & "\" & $sQliteDb & "\*.*")
            $Return &= _ScanProfileDir($SecSearch, $ffProfile & "\" & $sQliteDb)
            FileClose($SecSearch)
        Else

            $dbScnd += FileGetSize($Return)
            $dbScndPr = Round(($dbScnd * 100) / $ffPSize)

            Local $aSplPath = StringSplit($Return, ".")
            If StringInStr($aSplPath[$aSplPath[0]], "sqlite") Then
                Sleep(50)
                If FileExists($BIN_SQLITE) Then ShellExecute(@ScriptDir & "\Bin\sqlite3.exe", Chr(34) & $Return & Chr(34) & " vacuum", "", "", @SW_HIDE)
                Local $aSplFile = StringSplit($Return, "\")
                ProgressSet($dbScndPr, "Breaking [ " & $aSplFile[$aSplFile[0]] & " ]", "Breaking Firefox (" & $dbScndPr & "%)")
            EndIf

        EndIf

        $Return = ""

    WEnd

    Return $Return

EndFunc


Func _OptionsDlg()

    Local Const $OptionsTitle = "Firemin " & _GetExecVersioning(@ScriptFullPath, 5) & " Options"

    If _Singleton($OptionsTitle, 1) Then

        Local $picGradBar, $lblSlDesc

        $fboGui = GUICreate($OptionsTitle, 350, 350, -1, -1)
        GUISetFont(8.5, 400, 0, "Verdana", $fboGui, 5)

        $picGradBar = GUICtrlCreatePic("", 25, 10, 300, 20)
        _ResourceSetImageToCtrl($picGradBar, "GRADBAR_IMAGE", $RT_RCDATA)
        $lblSldValue = GUICtrlCreateLabel(0, 127, 12, 100, 20, 0x1) ;~ $SS_CENTER = 0x1
        GUICtrlSetBkColor($lblSldValue, -2) ;~ $GUI_BKCOLOR_TRANSPARENT = -2

        $sldrMillisec = GUICtrlCreateSlider(25, 35, 300, 35)
        GUICtrlSetLimit($sldrMillisec, 1000, 100)
        GuiCtrlSetData($sldrMillisec, $INI_BOOST)

        _SetMilliseconds()
        GUICtrlSetOnEvent($sldrMillisec, "_SetMilliseconds")

        $lblSlDesc = GUICtrlCreateLabel("The faster you set the optimization the more processing power " & _
                                    "is needed, but the less memory Firefox uses and vice versa. " & _
                                    "Between 300 and 500 is a good setting.", 10, 85, 330, 55)
        GuiCtrlSetColor($lblSlDesc, 0x555555)

        $radLchFire = GUICtrlCreateRadio("Launch Firefox when Firemin starts", 20, 155, 330, 20)
        $radLchSafe = GUICtrlCreateRadio("Launch Firefox in Safemode when Firemin starts", 20, 173, 330, 20)
        $radLchNone = GUICtrlCreateRadio("Don't Launch Firefox when Firemin starts", 20, 191, 330, 20)
        $chkAutStart = GUICtrlCreateCheckbox(" Start Firemin when Windows starts", 20, 230, 330, 20)

        GUICtrlCreateGroup("Firefox location (firefox.exe)", 10, 275, 330, 60)
        $inpFireLoc = GUICtrlCreateInput($INI_FIREFOXPATH, 20, 300, 265, 20)
        GuiCtrlSetTip($inpFireLoc, $INI_FIREFOXPATH)
        $btnFireLoc = GUICtrlCreateButton("...", 290, 297, 30, 25)
        GUICtrlCreateGroup("", -99, -99, 1, 1)  ;close group

        GUICtrlSetOnEvent($btnFireLoc, "_BrowseFireLocation")

        If $INI_LAUNCH = 0 Then
            GUICtrlSetState($radLchNone, $GUI_CHECKED)
        ElseIf $INI_LAUNCH = 1 Then
            GUICtrlSetState($radLchFire, $GUI_CHECKED)
        ElseIf $INI_LAUNCH = 2 Then
            GUICtrlSetState($radLchSafe, $GUI_CHECKED)
        EndIf

        If FileExists(@StartupDir & "\Firemin.lnk") Then GUICtrlSetState($ChkAutStart, $GUI_CHECKED)

        GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseOptionsDlg", $fboGui)

        If FileExists($USB_FIREFOX) Then
            GUICtrlSetState($inpFireLoc, $GUI_DISABLE)
            GUICtrlSetState($btnFireLoc, $GUI_DISABLE)
        EndIf

    EndIf

    GUISetState(@SW_SHOW, $fboGui)

EndFunc


Func _SetMilliseconds()

    $INI_BOOST = GuiCtrlRead($sldrMillisec)

    GuiCtrlSetData($lblSldValue, $INI_BOOST)
    AdlibRegister("_OptimizeFireFox", $INI_BOOST)

EndFunc


Func _BrowseFireLocation()

    $INI_FIREFOXPATH = FileOpenDialog("Select Firefox Location", $INI_FIREFOXPATH, "Firefox (firefox.exe)", 1)
    GUICtrlSetData($inpFireLoc, $INI_FIREFOXPATH)

EndFunc


Func _CloseOptionsDlg()

    ;~ $GUI_CHECKED = 1
    If GUICtrlRead($radLchFire) = 1 Then
        $INI_LAUNCH = 1
    ElseIf GUICtrlRead($radLchSafe) = 1 Then
        $INI_LAUNCH = 2
    ElseIf GUICtrlRead($radLchNone) = 1 Then
        $INI_LAUNCH = 0
    EndIf

    If GUICtrlRead($chkAutStart) = 1 Then
        FileDelete(@StartupDir & "\Firemin.lnk")
        FileCreateShortcut(@ScriptFullPath, @StartupDir & "\Firemin.lnk", @StartupDir)
    Else
        FileDelete(@StartupDir & "\Firemin.lnk")
    EndIf

    IniWrite($INI_FILE, "Firemin", "Launch", $INI_LAUNCH)
    IniWrite($INI_FILE, "Firemin", "Boost", $INI_BOOST)
    If Not FileExists($USB_FIREFOX) Then
        $INI_FIREFOXPATH = GUICtrlRead($inpFireLoc)
        IniWrite($INI_FILE, "Firemin", "Path", $INI_FIREFOXPATH)
    EndIf

    GUISetState(@SW_HIDE, @GUI_WinHandle)

EndFunc


Func _ShowAboutDlg()
    _AboutDlg("Firemin", FileGetVersion(@ScriptFullPath))
EndFunc


Func _AboutDlg($aTitle = "", $aVer = "0.0.0.0")

    If _Singleton("About : " & $aTitle, 1) Then


        Local $aSpaceLabel, $aSpaceProg, $aBtnOK, $abMsg
        Local $aScriptDrive = StringSplit(@ScriptDir, "\")
        Local $DrvSpaceUsed = DriveSpaceTotal($aScriptDrive[1]) - DriveSpaceFree($aScriptDrive[1])
        Local $abTitle, $abVersion, $abCopyright, $abHome, $abGNU
        Local $abPayPal, $abRSS, $abFacebook, $abTwittter, $abPalfarm, $abLinkedIn, $abGoogle

        $aboutDlg = GUICreate("About : " & $aTitle, 400, 500, -1, -1, BitOR(0x00C00000, 0x80000000, 0x00080000)) ;~ $WS_CAPTION = 0x00C00000, $WS_POPUP = 0x80000000, $WS_SYSMENU = 0x00080000
        GUISetFont(8.5, 400, 0, "Verdana", $AboutDlg, 5)
        GUISetIcon(@ScriptFullPath, 207)

        GUISetOnEvent(-3, "_CloseAboutDlg", $AboutDlg) ; Global Const $GUI_EVENT_CLOSE = -3

        GUICtrlCreateIcon(@ScriptFullPath, 99, 10, 10, 64, 64)
        $abPayPal = GUICtrlCreateIcon(@ScriptFullPath, 208, 320, 0, 64, 64)
        GUICtrlSetTip($abPayPal, "Help us keep our software free.")
        GUICtrlSetCursor($abPayPal, 0)
        $abTitle = GUICtrlCreateLabel($aTitle, 88, 16, 220, 18)
        GuiCtrlSetFont($abTitle, 9, 800)
        $abVersion = GUICtrlCreateLabel("Version " & FileGetVersion(@ScriptFullPath), 88, 40, 220, 20)
        $abCopyright = GUICtrlCreateLabel("Copyright © 2014 Rizonesoft", 88, 55, 220, 20)
        GuiCtrlSetColor(-1, 0x555555)

        GUICtrlCreateLabel("Rizonesoft Home: ", 20, 90, 130, 15, $SS_RIGHT)
        $abHome = GUICtrlCreateLabel("www.rizonesoft.com", 155, 90, 280, 15)
        GuiCtrlSetFont($abHome, -1, -1, 4) ;Underlined
        GuiCtrlSetColor($abHome, 0x0000FF)
        GuiCtrlSetCursor($abHome, 0)
        $abGNU = GUICtrlCreateLabel("This program is free software: you can redistribute it and/or modify " & _
                                "it under the terms of the GNU General Public License as published by " & _
                                "the Free Software Foundation, either version 3 of the License, or " & _
                                "(at your option) any later version." & @CRLF & @CRLF & _
                                "This program is distributed in the hope that it will be useful, " & _
                                "but WITHOUT ANY WARRANTY; without even the implied warranty of " & _
                                "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the " & _
                                "GNU General Public License for more details.", 20, 125, 350, 180)
        GuiCtrlSetColor($abGNU, 0x555555)
        GUICtrlCreateLabel( "Contributors: Derick Payne (Rizonesoft)", 20, 280, 350, 100)

        $abFacebook = GUICtrlCreateIcon(@ScriptFullPath, 201, 20, 450, 32, 32)
        GUICtrlSetCursor($abFacebook, 0)
        $abTwittter = GUICtrlCreateIcon(@ScriptFullPath, 202, 55, 450, 32, 32)
        GUICtrlSetCursor($abTwittter, 0)
        $abLinkedIn = GUICtrlCreateIcon(@ScriptFullPath, 204, 90, 450, 32, 32)
        GUICtrlSetCursor($abLinkedIn, 0)
        $abGoogle = GUICtrlCreateIcon(@ScriptFullPath, 205, 125, 450, 32, 32)
        GUICtrlSetCursor($abGoogle, 0)

        $aSpaceLabel =  GUICtrlCreateLabel("(" & $aScriptDrive[1] & ") " & Round(DriveSpaceFree($aScriptDrive[1]) / 1024, 1) & " GB free of " & _
                        Round(DriveSpaceTotal($aScriptDrive[1]) / 1024, 1) & " GB", 15, 380, 330, 15)
        $aSpaceProg = GUICtrlCreateProgress(15, 400, 360, 15)
        GUICtrlSetData($aSpaceProg, ($DrvSpaceUsed / DriveSpaceTotal($aScriptDrive[1])) * 100)

        $aBtnOK = GUICtrlCreateButton("OK", 255, 450, 123, 33, 0x0001) ;~ $BS_DEFPUSHBUTTON = 0x0001

        GUICtrlSetOnEvent($aBtnOK, "_CloseAboutDlg")
        GUICtrlSetOnEvent($abHome, "_HomePageClicked")
        GUICtrlSetOnEvent($abFacebook, "_OpenFacebook")
        GUICtrlSetOnEvent($abTwittter, "_FollowOnTwitter")
        GUICtrlSetOnEvent($abLinkedIn, "_OpenLinkedIn")
        GUICtrlSetOnEvent($abGoogle, "_OpenGooglePlus")

        GUISetOnEvent(-3, "_CloseAboutDlg") ;~ $GUI_EVENT_CLOSE = -3

    EndIf

    GUISetState(@SW_SHOW, $aboutDlg)

EndFunc


Func _CloseAboutDlg()
    GUISetState(@SW_HIDE, @GUI_WinHandle)
EndFunc


Func _HomePageClicked()
    ShellExecute("http://www.rizonesoft.com")
EndFunc


Func _OpenFacebook()
    ShellExecute("https://www.facebook.com/rizonesoft")
EndFunc


Func _FollowOnTwitter()
    ShellExecute("https://twitter.com/rizonesoft")
EndFunc


Func _OpenLinkedIn()
    ShellExecute("http://www.linkedin.com/in/rizonesoft")
EndFunc

Func _OpenGooglePlus()
    ShellExecute("https://plus.google.com/+Rizonesoftsa/posts")
EndFunc

Func _DonateSomething()
    ShellExecute("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7UGGCSDUZJPFE")
EndFunc 

Functions.au3

Func _GetExecVersioning($sExecPath, $iFlag = 6)

    If FileExists($sExecPath) Then
        Local $verReturn = FileGetVersion($sExecPath)
        Local $splReturn = StringSplit($verReturn, ".")

        If $splReturn[0] >= 4 Then
            If $iFlag = 1 Then
                Return $splReturn[1]
            ElseIf $iFlag = 2 Then
                Return $splReturn[2]
            ElseIf $iFlag = 3 Then
                Return $splReturn[3]
            ElseIf $iFlag = 4 Then
                Return $splReturn[4]
            ElseIf $iFlag = 5 Then
                Return $splReturn[1] & " : Build " & $splReturn[4]
            ElseIf $iFlag = 6 Then
                Return $verReturn
            EndIf
        EndIf
    Else
        Return "0000001" ;ERROR CODE: DOORS VERSIONING: PATH TO EXECUTABLE NOT FOUND
    EndIf
EndFunc ;==>_GetDoorsVersion

Resources.au3

#include-once
#include <Constants.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <Memory.au3>

_GDIPlus_Startup()

Global Const $RT_CURSOR = 1
Global Const $RT_BITMAP = 2
Global Const $RT_ICON = 3
Global Const $RT_MENU = 4
Global Const $RT_DIALOG = 5
Global Const $RT_STRING = 6
Global Const $RT_FONTDIR = 7
Global Const $RT_FONT = 8
Global Const $RT_ACCELERATOR = 9
Global Const $RT_RCDATA = 10
Global Const $RT_MESSAGETABLE = 11
Global Const $RT_GROUP_CURSOR = 12
Global Const $RT_GROUP_ICON = 14
Global Const $RT_VERSION = 16
Global Const $RT_DLGINCLUDE = 17
Global Const $RT_PLUGPLAY = 19
Global Const $RT_VXD = 20
Global Const $RT_ANICURSOR = 21
Global Const $RT_ANIICON = 22
Global Const $RT_HTML = 23
Global Const $RT_MANIFEST = 24

Global Const $SND_RESOURCE = 0x00040004
Global Const $SND_SYNC = 0x0
Global Const $SND_ASYNC = 0x1
Global Const $SND_MEMORY = 0x4
Global Const $SND_LOOP = 0x8
Global Const $SND_NOSTOP = 0x10
Global Const $SND_NOWAIT = 0x2000
Global Const $SND_PURGE = 0x40

Func _ResourceGet($ResName, $ResType = 10, $ResLang = 0, $DLL = -1) ; $RT_RCDATA = 10
    Local Const $IMAGE_BITMAP = 0
    Local $hInstance, $hBitmap, $InfoBlock, $GlobalMemoryBlock, $MemoryPointer, $ResSize

    If $DLL = -1 Then
      $hInstance = _WinAPI_GetModuleHandle("")
    Else
      $hInstance = _WinAPI_LoadLibraryEx($DLL, $LOAD_LIBRARY_AS_DATAFILE)
    EndIf
    If $hInstance = 0 Then Return SetError(1, 0, 0)

    If $ResType = $RT_BITMAP Then
        $hBitmap = _WinAPI_LoadImage($hInstance, $ResName, $IMAGE_BITMAP, 0, 0, 0)
        If @error Then Return SetError(2, 0, 0)
        Return $hBitmap ; returns handle to Bitmap
    EndIf

    If $ResLang <> 0 Then
        $InfoBlock = DllCall("kernel32.dll", "ptr", "FindResourceExW", "ptr", $hInstance, "long", $ResType, "wstr", $ResName, "short", $ResLang)
    Else
        $InfoBlock = DllCall("kernel32.dll", "ptr", "FindResourceW", "ptr", $hInstance, "wstr", $ResName, "long", $ResType)
    EndIf

    If @error Then Return SetError(3, 0, 0)
    $InfoBlock = $InfoBlock[0]
    If $InfoBlock = 0 Then Return SetError(4, 0, 0)

    $ResSize = DllCall("kernel32.dll", "dword", "SizeofResource", "ptr", $hInstance, "ptr", $InfoBlock)
    If @error Then Return SetError(5, 0, 0)
    $ResSize = $ResSize[0]
    If $ResSize = 0 Then Return SetError(6, 0, 0)

    $GlobalMemoryBlock = DllCall("kernel32.dll", "ptr", "LoadResource", "ptr", $hInstance, "ptr", $InfoBlock)
    If @error Then Return SetError(7, 0, 0)
    $GlobalMemoryBlock = $GlobalMemoryBlock[0]
    If $GlobalMemoryBlock = 0 Then Return SetError(8, 0, 0)

    $MemoryPointer = DllCall("kernel32.dll", "ptr", "LockResource", "ptr", $GlobalMemoryBlock)
    If @error Then Return SetError(9, 0, 0)
    $MemoryPointer = $MemoryPointer[0]
    If $MemoryPointer = 0 Then Return SetError(10, 0, 0)

    If $DLL <> -1 Then _WinAPI_FreeLibrary($hInstance)
    If @error Then Return SetError(11, 0, 0)

    SetExtended($ResSize)
    Return $MemoryPointer
EndFunc

; for ANSI strings
Func _ResourceGetAsString($ResName, $ResType = 10, $ResLang = 0, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResPointer, $ResSize, $struct

    $ResPointer = _ResourceGet($ResName, $ResType, $ResLang, $DLL)
    If @error Then
        SetError(1, 0, 0)
        Return ''
    EndIf
    $ResSize = @extended
    $struct = DllStructCreate("char[" & $ResSize & "]", $ResPointer)
    Return DllStructGetData($struct, 1) ; returns string
EndFunc

; for Unicode strings (Widechar)
Func _ResourceGetAsStringW($ResName, $ResType = 10, $ResLang = 0, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResPointer, $ResSize, $struct

    $ResPointer = _ResourceGet($ResName, $ResType, $ResLang, $DLL)
    If @error Then
        SetError(1, 0, 0)
        Return ''
    EndIf
    $ResSize = @extended
    $struct = DllStructCreate("wchar[" & $ResSize & "]", $ResPointer)
    Return DllStructGetData($struct, 1) ; returns string
EndFunc

; _ResourceGetAsBytes() doesn't work for RT_BITMAP type
; because _ResourceGet() returns hBitmap instead of memory pointer in this case
Func _ResourceGetAsBytes($ResName, $ResType = 10, $ResLang = 0, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResPointer, $ResSize

    $ResPointer = _ResourceGet($ResName, $ResType, $ResLang, $DLL)
    If @error Then Return SetError(1, 0, 0)
    $ResSize = @extended
    Return DllStructCreate("byte[" & $ResSize & "]", $ResPointer) ; returns struct with bytes
EndFunc

; returned hImage can be used in many GDI+ functions:
; $width =  _GDIPlus_ImageGetWidth ($hImage)
; $height = _GDIPlus_ImageGetHeight($hImage)
Func _ResourceGetAsImage($ResName, $ResType = 10, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResData, $nSize, $hData, $pData, $pStream, $pBitmap, $hBitmap, $hImage

    $ResData = _ResourceGet($ResName, $ResType, 0, $DLL)
    If @error Then Return SetError(1, 0, 0)
    $nSize = @extended

    If $ResType = $RT_BITMAP Then
        ; $ResData is hBitmap type
        $hImage = _GDIPlus_BitmapCreateFromHBITMAP($ResData)
    Else
        ; $ResData is memory pointer
        ; thanks ProgAndy
        $hData = _MemGlobalAlloc($nSize,2)
        $pData = _MemGlobalLock($hData)
        _MemMoveMemory($ResData,$pData,$nSize)
        _MemGlobalUnlock($hData)
        $pStream = DllCall( "ole32.dll","int","CreateStreamOnHGlobal", "ptr",$hData, "int",1, "ptr*",0)
        $pStream = $pStream[3]
        $hImage = DllCall($ghGDIPDll,"int","GdipCreateBitmapFromStream", "ptr",$pStream, "ptr*",0)
        $hImage = $hImage[2]
        _WinAPI_DeleteObject($pStream)
        ; next line must be commented otherwise animated GIFs will not work
;~      _MemGlobalFree($hData)
    EndIf

    Return $hImage ; hImage type
EndFunc

Func _ResourceGetAsBitmap($ResName, $ResType = 10, $DLL = -1) ; $RT_RCDATA = 10
    Local $hImage = _ResourceGetAsImage($ResName, $ResType, $DLL)
    If @error Then Return SetError(1, 0, 0)
    Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    Return $hBitmap ; hBitmap type
EndFunc

Func _ResourceSaveToFile($FileName, $ResName, $ResType = 10, $ResLang = 0, $CreatePath = 0, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResStruct, $ResSize, $FileHandle, $hImage

    If $CreatePath Then $CreatePath = 8 ; mode 8 = Create directory structure if it doesn't exist in FileOpen()

    If $ResType = $RT_BITMAP Then
        ; workaround: for RT_BITMAP _ResourceGetAsBytes() doesn't work so use _ResourceGetAsImage()
        $hImage = _ResourceGetAsImage($ResName, $ResType)
        If @error Then Return SetError(10, 0, 0)

        ; create filepath if doesn't exist
        $FileHandle = FileOpen($FileName, 2+16+$CreatePath)
        If @error Then Return SetError(11, 0, 0)
        FileClose($FileHandle)
        If @error Then Return SetError(12, 0, 0)

        _GDIPlus_ImageSaveToFile($hImage, $FileName)
        _GDIPlus_ImageDispose($hImage)

        $ResSize = FileGetSize($FileName)
    Else
        ; standard way
        $ResStruct = _ResourceGetAsBytes($ResName, $ResType, $ResLang, $DLL)
        If @error Then Return SetError(1, 0, 0)
        $ResSize = DllStructGetSize($ResStruct)

        $FileHandle = FileOpen($FileName, 2+16+$CreatePath)
        If @error Then Return SetError(2, 0, 0)
        FileWrite($FileHandle, DllStructGetData($ResStruct, 1))
        If @error Then Return SetError(3, 0, 0)
        FileClose($FileHandle)
        If @error Then Return SetError(4, 0, 0)
    EndIf

    Return $ResSize
EndFunc

Func _ResourceSetImageToCtrl($CtrlId, $ResName, $ResType = 10, $DLL = -1) ; $RT_RCDATA = 10
    Local $ResData, $nSize, $hData, $pData, $pStream, $pBitmap, $hBitmap

    $ResData = _ResourceGet($ResName, $ResType, 0, $DLL)
    If @error Then Return SetError(1, 0, 0)
    $nSize = @extended

    If $ResType = $RT_BITMAP Then
        _SetBitmapToCtrl($CtrlId, $ResData)
        If @error Then Return SetError(2, 0, 0)
    Else
        ; thanks ProgAndy
        ; for other types than BITMAP use GDI+ for converting to bitmap first
        $hData = _MemGlobalAlloc($nSize,2)
        $pData = _MemGlobalLock($hData)
        _MemMoveMemory($ResData,$pData,$nSize)
        _MemGlobalUnlock($hData)
        $pStream = DllCall( "ole32.dll","int","CreateStreamOnHGlobal", "ptr",$hData, "int",1, "ptr*",0)
        $pStream = $pStream[3]
        $pBitmap = DllCall($ghGDIPDll,"int","GdipCreateBitmapFromStream", "ptr",$pStream, "ptr*",0)
        $pBitmap = $pBitmap[2]
        $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($pBitmap)
        _SetBitmapToCtrl($CtrlId, $hBitmap)
        If @error Then SetError(3, 0, 0)
        _GDIPlus_BitmapDispose($pBitmap)
        _WinAPI_DeleteObject($pStream)
        _MemGlobalFree($hData)
    EndIf

    Return 1
EndFunc

; internal helper function
; thanks for improvements Melba
Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $STM_GETIMAGE = 0x0173
    Local Const $BM_SETIMAGE = 0xF7
    Local Const $BM_GETIMAGE = 0xF6
    Local Const $IMAGE_BITMAP = 0
    Local Const $SS_BITMAP = 0x0E
    Local Const $BS_BITMAP = 0x0080
    Local Const $GWL_STYLE = -16

    Local $hWnd, $hPrev, $Style, $iCtrl_SETIMAGE, $iCtrl_GETIMAGE, $iCtrl_BITMAP

    $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

    $CtrlId = _WinAPI_GetDlgCtrlID($hWnd) ; support for $CtrlId = -1
    If @error Then Return SetError(2, 0, 0)

    ; determine control class and adjust constants accordingly
    Switch _WinAPI_GetClassName($CtrlId)
        Case "Button" ; button,checkbox,radiobutton,groupbox
            $iCtrl_SETIMAGE = $BM_SETIMAGE
            $iCtrl_GETIMAGE = $BM_GETIMAGE
            $iCtrl_BITMAP = $BS_BITMAP
        Case "Static" ; picture,icon,label
            $iCtrl_SETIMAGE = $STM_SETIMAGE
            $iCtrl_GETIMAGE = $STM_GETIMAGE
            $iCtrl_BITMAP = $SS_BITMAP
        Case Else
            Return SetError(3, 0, 0)
    EndSwitch

    ; set SS_BITMAP/BS_BITMAP style to the control
    $Style = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE)
    If @error Then Return SetError(4, 0, 0)
    _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, BitOR($Style, $iCtrl_BITMAP))
    If @error Then Return SetError(5, 0, 0)

    ; set image to the control
    $hPrev  = _SendMessage($hWnd, $iCtrl_SETIMAGE, $IMAGE_BITMAP, $hBitmap)
    If @error Then Return SetError(6, 0, 0)
    If $hPrev Then _WinAPI_DeleteObject($hPrev)

    Return 1
EndFunc

; thanks Larry,ProgAndy
; MSDN: http://msdn2.microsoft.com/en-us/library/ms712879.aspx
; default flag is $SND_SYNC = 0
Func _ResourcePlaySound($ResName, $Flag = 0, $DLL = -1)
    Local $hInstance

    If $DLL = -1 Then
      $hInstance = 0
    Else
      $hInstance = _WinAPI_LoadLibraryEx($DLL, $LOAD_LIBRARY_AS_DATAFILE)
    EndIf

    Local $ret = DllCall("winmm.dll", "int", "PlaySound", "str", $ResName, "hwnd", $hInstance, "int", BitOr($SND_RESOURCE,$Flag))
    If @error Then Return SetError(1, 0, 0)

    If $DLL <> -1 Then _WinAPI_FreeLibrary($hInstance)
    If @error Then Return SetError(2, 0, 0)

    Return $ret[0]
EndFunc
 

Licence 

 

Firemin will eliminate all memory leaks in Firefox.

Copyright © 2014  Rizonesoft (www.rizonesoft.com)
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

EDIT2: Nice GUI

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I have an opinion, I can't be bothered navigating to external unknown websites that may or may not be rife with malware viruses and drive by downloads.

Post your script here, unless you are just after hits on your website..

And what is the "clean memory API call" ? Is it anything like the ReduceMemory UDF which uses EmptyWorkingSet API and works on any process you pass to it?

 

I will post the script next time. I did not post the links for hits to my site. Just want to give everyone a location to get the resources and executable also. I'm getting enough hits as is without me needing to spam forums.

Firemin uses the EmptyWorkingSet API.

Thank you for your comment.

Rizonesoft Open Source Home Scripts: Complete Internet Repair | Development: Rizonesoft SDK

Link to comment
Share on other sites

I looked at your site, it's decent with some great software, but my only critique would be some rather grandiose claims about what your memory boosting and indeed this apps can do.

As I understand it emptyworkingset can cause problems when called directly and especially on third party processes. I'm not knocking it, because I've used the method myself, but only with buggy leaky apps.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I looked at your site, it's decent with some great software, but my only critique would be some rather grandiose claims about what your memory boosting and indeed this apps can do.

As I understand it emptyworkingset can cause problems when called directly and especially on third party processes. I'm not knocking it, because I've used the method myself, but only with buggy leaky apps.

 

You are absolutely right. When I wrote Memory Booster and Firemin, I was still running Windows XP and can't remember the Firefox build, but it was one with bad memory leaks. When I tested the apps it actually improved the performance of my system and solved the memory leak issues in Firefox. On my Windows 8 system, I do not notice much difference. It is the choice of the user to use the apps. Personally, I do not use them anymore, because I have 16GB RAM now and believe in letting Windows do its thing.

That all said, I keep the programs and descriptions as is, because it still works very well for some people. Memory booster has an option to control process priority that will also help with system performance. About the claims, it was all freeware and had to "sell" it, but tried to keep it as honest as possible.

Edited by Rizonetech

Rizonesoft Open Source Home Scripts: Complete Internet Repair | Development: Rizonesoft SDK

Link to comment
Share on other sites

Of course not. It was after I read about the problems emptyworking set could possibly cause, I started using it only when the amount used began to be a problem for my shameful 1GB memory :)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...