WSUS Offline Update Code Suggestions

WSUS Offline Update Code Suggestions

Postby Nologic » 05.07.2015, 07:13

These are coding suggestions for "UpdateGenerator.au3"

[Linky] (PasteBin)

This will drop the line count by several hundreds of lines.
These code chunks generally automatically support more versions of Windows & Office as they come out...by simply altering the $aMainArray...so less code being needed for future versions.

Some of the code has been tested...what has debug code commented out...the other stuff hasn't...but "should" work. Mostly since there is a lot of code reuse...just tweaked for certain things here and there.

Some folks may frown on my use of "magic vars", and rather use flat code, but this saves a boat load of code.

There are still several hundreds of lines more that could be refactored out...but I'll wait till I find out how this plays through, before I get over zealous about things.

If things look odd...I use a tab indent of 3 char...however PasteBin uses some crazy tab indent value...which makes things very hard to read...anyways be sure to copy from the RAW Paste Data...rather than the pretty markup.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 06.07.2015, 00:58

New code sample.

Original:
Code: Select all
Func ShowGUIInGerman()
  If ($CmdLine[0] > 0) Then
    Switch StringLower($CmdLine[1])
      Case "enu"
        Return False
      Case "deu"
        Return True
    EndSwitch
  EndIf
  Return ( (@OSLang = "0007") OR (@OSLang = "0407") OR (@OSLang = "0807") OR (@OSLang = "0C07") OR (@OSLang = "1007") OR (@OSLang = "1407") )
EndFunc


My Version:
Code: Select all
Func ShowGUIInGerman()
   If $CmdLine[0] > 0 Then
      If StringLower( $CmdLine[1] ) = 'deu' Then
         Return True
      Else
         Return False
      EndIf
   EndIf
   Return StringInStr( '0007|0407|0807|0C07|1007|1407' , @OSLang ) > 0
EndFunc


Okay I used "if" rather than "switch" mainly because the only "true" answer is German...anything else is "false". If the interface gets to offering more than English & German...then a switch would be called for.

I used StringInStr instead of that long OR statement...because I find it shorter and easier to read.

Now I noticed ShowGUIInGerman() is being called a LOT...shouldn't a var be set to it's return value...then that var checked where needed....as the result of ShowGUIInGerman() isn't going to change during the operation of the script.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 06.07.2015, 01:25

In my revised version of "IsLangOfficeChecked()" I left the return value obfuscated...this is bad form...so it should now read like this:
Code: Select all
Func IsLangOfficeChecked()
   For $ii = 1 To $aMainArray[0][0]
      If $aMainArray[$ii][2] <> '' Then
         $aSplit = StringSplit( $aMainArray[$ii][2] , '|' )
         For $jj = 1 To $aSplit[0]
            $sCurrentVar = StringReplace( $aSplit[$jj] & ' ' & $aMainArray[$ii][0] , ' ' , '_' )
            If IsCheckBoxChecked( Eval( $sCurrentVar )) Then
               Return True
            EndIf
         Next
      EndIf
   Next
EndFunc
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 06.07.2015, 04:46

All my revisions thus far applied.
[Linky] (PasteBin)

Okay latest changes...actually probably add a few lines of code. Basic style & structure changes...as there was nothing wrong with the original code.

Replaces Current Lines 3564-3615:
Code: Select all
      ; Restore Window And Show Success Dialog
      WinSetState( $maindlg , $maindlg , @SW_RESTORE )
      If $runany Then
         If IsCheckBoxChecked( $scripting ) Then
            If ShowGUIInGerman() Then
               $sMsgBoxTitle = 'Info'
               $sMsgBoxText  = 'Sammelskript "' & @ScriptDir & '\cmd\custom\RunAll.cmd" erstellt.' & @LF & _
                               'Möchten Sie das Skript nun prüfen?'
            Else
               $sMsgBoxTitle = 'Info'
               $sMsgBoxText  = 'Collection script "' & @ScriptDir & '\cmd\custom\RunAll.cmd" created.' & @LF & _
                               'Would you like to check the script now?'
            EndIf
            If MsgBox( 0x2044 , $sMsgBoxTitle , $sMsgBoxText ) = $msgbox_btn_yes Then ShowRunAll()
         Else
            If IsCheckBoxChecked( $shutdown ) Then
               Run( @SystemDir & '\shutdown.exe /s /f /t 5' , '' , @SW_HIDE )
               ExitLoop
            EndIf
            If IsCheckBoxChecked( $imageonly ) Then
               If ShowGUIInGerman() Then
                  $sMsgBoxTitle = 'Info'
                  $sMsgBoxText  = 'Image-Erstellung / Kopieren erfolgreich.'
               Else
                  $sMsgBoxTitle = 'Info'
                  $sMsgBoxText  = 'Image creation / copying successful.'
               EndIf
               MsgBox( 0x2040 , $sMsgBoxTitle , $sMsgBoxText )
            Else
               If ShowGUIInGerman() Then
                  $sMsgBoxTitle = 'Info'
                  $sMsgBoxText  = 'Herunterladen / Image-Erstellung / Kopieren erfolgreich.' & @LF & _
                                  'Möchten Sie nun die Protokolldatei auf mögliche Warnungen prüfen?'
               Else
                  $sMsgBoxTitle = 'Info'
                  $sMsgBoxText  = 'Download / image creation / copying successful.' & @LF & _
                                  'Would you like to check the log file for possible warnings now?'
               EndIf
               If MsgBox( 0x2044 , $sMsgBoxTitle , $sMsgBoxText ) = $msgbox_btn_yes Then ShowLogFile()
            EndIf
         EndIf
      Else
         If ShowGUIInGerman() Then
            $sMsgBoxTitle = 'Info'
            $sMsgBoxText  = 'Nichts zu tun!'
         Else
            $sMsgBoxTitle = 'Info'
            $sMsgBoxText  = 'Nothing to do!'
         EndIf
         MsgBox( 0x2040 , $sMsgBoxTitle , $sMsgBoxText )
      EndIf

   EndSwitch
WEnd
SaveSettings()
Exit
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 06.07.2015, 10:39

My Latest tweaks:
[Linky] (PasteBin)

Mainly minor white space changes...the biggest difference is the use of ternary expressions in control labels.

Example This:
Code: Select all
;  Product Lifecycle group
$txtxpos = 2 * $txtxoffset
$txtypos = $txtypos + 2.5 * $txtyoffset
If ShowGUIInGerman() Then
  GUICtrlCreateGroup("Produkt-Lebenszyklus-Informationen", $txtxpos, $txtypos, $groupwidth, $groupheight_glb)
Else
  GUICtrlCreateGroup("Product lifecycle information", $txtxpos, $txtypos, $groupwidth, $groupheight_glb)
EndIf


Becomes This:
Code: Select all
; Product Lifecycle group
$txtxpos = 2 * $txtxoffset
$txtypos = $txtypos + 2.5 * $txtyoffset
GUICtrlCreateGroup( ShowGUIInGerman() ? 'Produkt-Lebenszyklus-Informationen' : 'Product lifecycle information' , _
            $txtxpos , $txtypos , $groupwidth , $groupheight_glb )


Past that...the big diff would be how the Donate button works.

From this:
Code: Select all
      ; Donate Button Pressed
      Case $btn_donate
         Run( @ComSpec & ' /D /C start ' & $donationURL )


To this:
Code: Select all
      ; Donate Button Pressed
      Case $btn_donate
         ShellExecute( $donationURL )


Which I think is nicer since you dont have a console popping up.

Changes are from line 1818 down...when compared to the prior link.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 07.07.2015, 03:00

Latest Revision:
[Linky](PasteBin)

More white space, and minor refactor's.

Things of note:
Moved AutoItSetOption(s) towards the top of the script.
Removed Dim from constants, since they both take local scope...anyways.
Changed some Dim's in functions to Local.

Probably the biggest thing is in-lining IF statement setting state for checkboxes. This is done since the checkboxes are not styled to be checked upon creation...so the only valid change in state...is to become checked.

So This:
Code: Select all
;  Office 2007 French
$txtxpos = $txtxpos + $txtwidth - 5
$o2k7_fra = GUICtrlCreateCheckbox(LanguageCaption($lang_token_fra, ShowGUIInGerman()), $txtxpos, $txtypos, $txtwidth + 10, $txtheight)
If IniRead($inifilename, $ini_section_o2k7, $lang_token_fra, $disabled) = $enabled Then
  GUICtrlSetState(-1, $GUI_CHECKED)
Else
  GUICtrlSetState(-1, $GUI_UNCHECKED)
EndIf


Becomes this:
Code: Select all
; Office 2007 French
$txtxpos = $txtxpos + $txtwidth - 5
$o2k7_fra = GUICtrlCreateCheckbox( LanguageCaption( $lang_token_fra , ShowGUIInGerman()) , $txtxpos , $txtypos , $txtwidth + 10 , $txtheight )
If IniRead( $inifilename , $ini_section_o2k7 , $lang_token_fra , $disabled ) = $enabled Then GUICtrlSetState( -1 , $GUI_CHECKED )


It's a small change but it adds up.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 07.07.2015, 07:50

Latest and greatest:
[Linky] (PasteBin)

More minor white space tweaks, removal of a function I made & two calls to it and another big move to magic vars. :)

I'll probably do one more update to this file...then call it a day.

I've taken the file from 3615 lines down to 1925 lines total...not bad.

File size has gone from 187kb to just 81kb.

I could do more to make things smaller...but that would involve doing stuff that would have the guy doing the GUI taking me outback and...well lets say it wouldn't be pretty. :shock:
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 07.07.2015, 11:25

Frigging...dang...frack..frail...

Okay here is a WORKING build...got my ass handed to me...over some silly copy & paste errors. :(

[Linky] (PasteBin)
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 08.07.2015, 05:35

New version:
[Linky] (PasteBin)

Made a number of var's in functions local
Some white space adjustments
Simplified some of my code
Made INI creation a bit more pretty (doesn't fix ones already created)
Fixed issue where my code wasn't reading from the INI for Global Windows settings (vista, win7,...)
Added Region tags, for better code folding in editor's that support such tags. (helps improve getting around in the source code)
Made comments for Groups & Tabs more visible in the source code.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41

Re: WSUS Offline Update Code Suggestions

Postby Nologic » 10.07.2015, 23:14

While there are still things left to do to "UpdateGenerator.au3", I decided to move over to "UpdateInstaller.au3" and tweak things there.

There honestly isn't a lot to do with this one...changing coding style a bit, saves some lines...but mostly what is there is required and I can't really get the massive code reductions I did with "UpdateGenerator.au3".

[Linky] (PasteBin)

I'll likely be making fewer posts...as there doesn't seem to be a real interest in what I'm doing...and at present I have no interest in forking the project. So getting moved to the back burner...as something of interest...and not a priority.
Nologic
 
Posts: 10
Joined: 05.07.2015, 06:41


Return to Anregungen / Suggestions

Who is online

Users browsing this forum: No registered users and 39 guests