#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=BranderIcon.ico
#AutoIt3Wrapper_Res_Comment=OEM Brander
#AutoIt3Wrapper_Res_Description=Simple OEM Brander
#AutoIt3Wrapper_Res_Fileversion=2.5.0.0
#AutoIt3Wrapper_Res_ProductName=OEM Brander
#AutoIt3Wrapper_Res_ProductVersion=2.5.0.0
#AutoIt3Wrapper_Res_CompanyName=Someone
#AutoIt3Wrapper_Res_LegalCopyright=Someone
#AutoIt3Wrapper_Res_LegalTradeMarks=Someone
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_requestedExecutionLevel=RequireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <ColorConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <GDIPlus.au3>

Opt("TrayMenuMode", 1)
Opt("TrayIconHide", 1)
Opt("GUICloseOnESC", 0)

; This is not perfect and there may be hidden glitches that happen on newer builds of the W10 OS.
;
; Change/Tweak as you wish, this is just a simple code base to get started or expand on.
;
; Convert to other scripting languages if that makes things easier, Powershell can be a bit
; fussy more so on older versions of P/Shell on Windows 7/8.0 but with a bit of thought that
; could be sorted.

$Prog_Version = " Ver: 2.5"
$GUI_Icon = GUISetIcon(@ScriptDir & "\BranderIcon.ico")
Global $OSBuildNum = @OSBuild
Global $os = @OSVersion


; Check for OS's that this program will not work on or if it did would cause issues.
; So exit if they are detected.

If $os = "WIN_2008" Then
	MsgBox(0, "OEM Branding", "Sorry Server 2008 is not supported.")
	Exit
EndIf

If $os = "WIN_VISTA" Then
	MsgBox(0, "OEM Branding", "Sorry Vista is not supported.")
	Exit
 EndIf

If $os = "WIN_XP" Then
	MsgBox(0, "OEM Branding", "Sorry XP is not supported.")
	Exit
 EndIf

; Catch all for below Win7 to prevent major problems.
IF $OSBuildNum < "7599" then
	MsgBox(0, "OEM Branding", "Sorry this OS is not supported.")
	Exit
 EndIf

$HKLM = "HKLM"
ArchDetect()

$oem_branding_title = "OEM Branding   " & "[ " & @OSArch & " - " & @OSVersion & " " & @OSBuild & " ]  --" & $Prog_version
$Oem_Branding_title2 = "OEM Branding"
$script_title = "Information"
$sShowOrig = "Yes"

; Create GUI Framework area
$GUI = GUICreate($OEM_Branding_Title, 500, 390,-1,-1)

; 'Dark Mode'
$BkColor = 0xA0A0A0
GUISetBkColor($BkColor)

$Menu_File = GUICtrlCreateMenu("File")
$File_ExportReg = " "
$OEM_Location = ""

GUICtrlCreateMenuItem("", $Menu_File)
$File_ExportReg = GUICtrlCreateMenuItem("Registry Export OEM Data", $Menu_File)
$Exit_Menu = GUICtrlCreateMenuItem("Exit", $Menu_File)

$_About = GUICtrlCreateMenu("About")
$Show_About = GUICtrlCreateMenuItem("About OEM Branding Tool.", $_About)

$OEMLogo_Locate = ""
$OEMLogo = "0"
$OEMLogoShow = "0"
$OEMLogoTopLine = GUICtrlCreateLabel("  Current OEM Logo",45,2,-1,17)
GUICtrlSetBkColor(-1,0x229999)

$UseOEMLogo = GUICtrlCreateCheckbox("Add/Change OEM Logo", 220, 10)
GUICtrlSetTip(-1, "Add or change current OEM Logo.", "Add/Change OEM Logo",1,1)

; Find original logo and show it - on 64bit OS's redirection must be disabled temporary or it not show!
If @OSArch = "X64" Then DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1) ; 1 = Disable 64FsRedirection | 0 = Enable 64FsRedirection
$OrigOEMLogoPath = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Logo")

; Strip out those extra \ chars that can cause headaches in the registry for the variables being used/parsed later.
$OEMLogoPathStripped = StringReplace($OrigOEMLogoPath, "\\", "\")

$Drv = ""
$OrigOEMLogoImage = ""
$Add_Drive = "No"
$Drv = StringLeft($OEMLogoPathStripped,1)
IF $Drv = "\" then $Add_Drive = "Yes"
$Drv = ""

If $Add_Drive = "Yes" Then
	$Drv = @HomeDrive & $OEMLogoPathStripped
	$OEMLogoPathStripped = $Drv
EndIf

If FileExists($OEMLogoPathStripped) then
	$OEMLogo = GUICtrlCreatePic($OEMLogoPathStripped, 10, 20, 182, 144)
	GUICtrlSetTip(-1, $OEMLogoPathStripped, "Current OEM Logo Location",1,1)
	$OrigOEMLogoImage = $OEMLogoPathStripped
	$hh = GUICtrlCreateLabel("[Logo Orig Loc Path]", 370, 14)
	GUICtrlSetTip(-1, $OrigOEMLogoImage, "Show path to the original OEM Logo Location when first loaded",1,1)
EndIf


If @OSArch = "X64" Then DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); 1 = Disable 64FsRedirection | 0 = Enable 64FsRedirection
IF $OEMLogo = "" Then $OEMLogoShow = "0"
IF $OEMLogo = "0" then $OEMLogoShow = "0"
IF $OEMLogo <> "0" then $OEMLogoShow = "1"
IF $OEMLogoShow = "0" then ClearLogoImageBox()

$OEMLogo_SetLocate = GUICtrlCreateButton("Select a new OEM Logo", 220, 30)
GUICtrlSetTip(-1, "File Requests for OEM Logo {Must be in BMP format ONLY}.", "Find and Select OEM Logo",1,1)

GUICtrlSetState($OEMLogo_Locate, $GUI_DISABLE)
GUICtrlSetState($OEMLogo_SetLocate, $GUI_DISABLE)
$OEMLogoInfoLine = GUICtrlCreateLabel("Checking and Unchecking the box or cancelling the File " & @CRLF & "Requester will clear the currently loaded Logo image from " & @CRLF & "the screen but not from the registry. Press the 'Show" & @CRLF & "Information Screen' for details.",222,55,-1,-1)
GUICtrlSetColor($OEMLogoInfoLine, $COLOR_BLUE)

$wallpaper = GUICtrlCreateButton("Change Desktop's Background", 220, 110)
GUICtrlSetTip(-1, "This will allow you to change the Desktop's " & @CRLF & "Wallpaper/Background within the OS.", "Change Desktop's Background", 1, 1)

$OEM_Manuf = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Manufacturer")
$OEM_Model = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Model")
$Support_Hours = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportHours")
$Support_Phone = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportPhone")
$Support_Url   = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportURL")

GUICtrlCreateLabel("Manufacturer Name:", 10, 170)
$Man_Info = GUICtrlCreateInput($OEM_Manuf, 10, 185, 185)

GUICtrlCreateLabel("Model:", 10, 210)
$Mod_Info = GUICtrlCreateInput($OEM_Model, 10, 225, 185)

$SI_Y = 145
$SLWide = 255
$Use_SupportInfo = GUICtrlCreateCheckbox("Change support info", 220, $SI_Y, 185)
GUICtrlSetState(-1, $GUI_UNCHECKED)
GUICtrlSetTip(-1, "Checking this box will allow changing " & @CRLF & "of the Support information.", "Change Support Information", 1, 1)

$sl1 = $si_y + 25
$sl2 = $sl1 + 40
$sl3 = $sl2 + 40
$Sla = GUICtrlCreateLabel("Current Support Hours", 220,$SL1,$SLWide)
$SupportInfo_Line1 = GUICtrlCreateInput($Support_Hours, 220, $SL1 +15)

$Slb = GUICtrlCreateLabel("Current Support Phone", 220,$SL2,$SLWide)
$SupportInfo_Line2 = GUICtrlCreateInput($Support_Phone, 220, $SL2 +15)

$Slc = GUICtrlCreateLabel("Current Support URL", 220,$SL3,$SLWide)
$SupportInfo_Line3 = GUICtrlCreateInput($Support_URL, 220, $SL3 +15)

If Not IsChecked($Use_SupportInfo) Then
		GUICtrlSetState($SupportInfo_Line1, $GUI_DISABLE)
		GUICtrlSetState($SupportInfo_Line2, $GUI_DISABLE)
		GUICtrlSetState($SupportInfo_Line3, $GUI_DISABLE)
EndIf

$OEM_Temp = ""
$Process_OEMData = GUICtrlCreateButton("Apply OEM Branding", 50, 270)
GUICtrlSetTip(-1, "Applies any changed OEM data.", "Apply OEM Data",1,1)

$Clear_OEM_Data = GUICtrlCreateButton("Clear OEM Branding", 50,300)
GUICtrlSetTip(-1, "Clears OEM Branding data from the registry." & @CRLF & "         **** See WARNING! ****", "Clear OEM Data",1,1)

$ShowOSBasicInfoScreen = GUICtrlCreateButton("Show Information Screen", 50,330)
GUICtrlSetTip(-1, "Shows the OS's Basic Information Screen." & @CRLF & @CRLF & "This may not always work correctly " & @CRLF & "after Windows 10 20H2.", "Show OEM Information",1,1)

Export_OEMInfo()

GUISetState(@SW_SHOW)

$WrongPSize = "No"
$iX = ""
$iY = ""

; Main GUI loop...

While 1
$iMsg = GUIGetMsg()
	Switch $iMsg

		Case $GUI_EVENT_CLOSE, $Exit_Menu
			Exit

		Case $File_ExportReg
			Export_OEMInfo()

		Case $OEMLogo_SetLocate
			$OEM_Location = FileOpenDialog("Select OEM Logo ---> NOTE: Some converted to BMP images may not work due to not fully compliant.", @ScriptDir, "Bitmap (*.bmp)")
			GUICtrlSetData($OEMLogo_Locate, $OEM_Location)

		if FileExists($OEM_Location) then _ImageGetSize($OEM_Location)

		if $WrongPSize = "Yes" then
			$OEMLogoShow = "0"
			MsgBox($MB_SYSTEMMODAL, "Image Size Error", "The selected Logo's dimensions are not the correct 120 x 120 pixels required by the OS." & @CRLF & @CRLF & _
			"Selected File Details:" & @CRLF & "Height : " & $iX & @CRLF & "Width  : " & $iY,60,$GUI)
			$OEMLogo_Clear = GUICtrlCreateLabel(" ",10, 20, 182, 144)
			GUICtrlSetTip(-1, $OEMLogoPathStripped, "Current OEM Logo Location",1,1)

		EndIf

			if FileExists($OEM_Location) and $WrongPSize = "No" then
				$OEMLogo = GUICtrlCreatePic($OEM_Location, 10, 20, 182, 144)
				GUICtrlSetTip(-1, $OEM_Location, "Current OEM Logo Location",1,1)
				$OEMLogoShow = "1"
				$sShowOrig = "No"
			EndIf

			If $OEM_Location = "" Then
				$OEMLogoShow = "0"
				MsgBox($MB_SYSTEMMODAL, $Script_Title, "No image file was selected or selection cancelled.",60,$GUI)
				$OEMLogo_Clear = GUICtrlCreateLabel(" ",10, 20, 182, 144)
				$sShowOrig = "Yes"
			EndIf

			IF $OEMLogoShow = "0" then
				 ClearLogoImageBox()
			EndIf

			If $OEM_Location <> "" and $OEMLogoShow <> "0" Then
					 GUICtrlSetImage($OEMLogo, $OEM_Location)
					GUICtrlSetTip(-1, $OEM_Location, "Current OEM Logo Location",1,1)
					$sShowOrig = "No"
			 EndIf

		Case $UseOEMLogo
			If Not IsChecked($UseOEMLogo) Then
				GUICtrlSetState($OEMLogo_Locate, $GUI_DISABLE)
				GUICtrlSetState($OEMLogo_SetLocate, $GUI_DISABLE)
				IF $OEMLogo <> "0" then GUICtrlSetState($OEMLogo, $GUI_HIDE)
				 $OEMLogo = "0"
				 $OEM_Location = ""
				 ClearLogoImageBox()

			Else

				GUICtrlSetState($OEMLogo_Locate, $GUI_ENABLE)
				GUICtrlSetState($OEMLogo_SetLocate, $GUI_ENABLE)
				IF $OEMLogo <> "0" then GUICtrlSetState($OEMLogo, $GUI_SHOW)

			EndIf

		Case $Use_SupportInfo
			If Not IsChecked($Use_SupportInfo) Then
				GUICtrlSetState($SupportInfo_Line1, $GUI_DISABLE)
				GUICtrlSetState($SupportInfo_Line2, $GUI_DISABLE)
				GUICtrlSetState($SupportInfo_Line3, $GUI_DISABLE)
			Else
				GUICtrlSetState($SupportInfo_Line1, $GUI_ENABLE)
				GUICtrlSetState($SupportInfo_Line2, $GUI_ENABLE)
				GUICtrlSetState($SupportInfo_Line3, $GUI_ENABLE)
			EndIf

		Case $Wallpaper
			; Opens the PC Settings 'Backgrounds' on w8.x/10 OR the Control Panel's section for changing the 'Backgrounds' on win7.
			IF $OS = "WIN_7" then
				run("explorer.exe shell:::{ED834ED6-4B5A-4bfe-8F11-A626DCB6A921} -Microsoft.Personalization\pageWallpaper")
			EndIf

			IF $OS = "WIN_10" or $OS = "WIN_81" or $OS = "WIN_8" then
				run("explorer.exe ms-settings:personalization-background")
			EndIf

		Case $Process_OEMData
			$sLogoFileName = ""
			$NewOEMLocation = ""

			$verify4 = MsgBox($MB_ICONWARNING + $MB_YESNO + $MB_DEFBUTTON2,"Insert OEM Information and Logo","Are you sure you wish to add the current OEM Information into the Registry ?" & @CRLF & @CRLF & "*** This is instantly applied if you press 'YES' ***" & @CRLF & @CRLF & "This does not affect the Background/Wallpaper image.",0,$GUI)

				Select
					Case $verify4 = 6

				IF $OEM_Location <> "" then
					$sLogoFileName = "OEMLogo.bmp"
					$NewOEMLocation = @WindowsDir & "\" & $sLogoFileName

					IF $sLogoFileName <> "" then
						FileCopy($OEM_Location, @WindowsDir & "\" & $sLogoFileName, $FC_OVERWRITE)
						RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation", "Logo", "REG_SZ", $NewOEMLocation)
					EndIf

				EndIf

			RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation", "Manufacturer", "REG_SZ" ,GUICtrlRead($Man_Info))
			RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation", "Model", "REG_SZ", GUICtrlRead($Mod_Info))

				If IsChecked($Use_SupportInfo) Then
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportHours", "REG_SZ", GUICtrlRead($SupportInfo_Line1))
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportPhone", "REG_SZ", GUICtrlRead($SupportInfo_Line2))
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportURL", "REG_SZ", GUICtrlRead($SupportInfo_Line3))
				EndIf

			MsgBox(0,"Information","OEM Data Applied.",10)
			Refresh_Data()

		Case $verify4 = 7
						; NO
						MsgBox(0,"Information","OEM Data was NOT Applied.",2)
		EndSelect


		Case $Clear_OEM_Data
			$verify = MsgBox($MB_ICONWARNING + $MB_YESNO + $MB_DEFBUTTON2,"Delete Current OEM Information","Are you sure you wish to delete the current OEM Information from the Registry ?" & @CRLF & @CRLF & "*** This is instantly applied if you press 'YES' ***" & @CRLF & @CRLF & "This does not affect the Background/Wallpaper image.",0,$GUI)

				Select
					Case $verify = 6
                            ; YES
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Logo", "REG_SZ", "")
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Manufacturer", "REG_SZ" ,"")
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Model", "REG_SZ", "")
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportHours", "REG_SZ", "")
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportPhone", "REG_SZ", "")
					RegWrite($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportURL", "REG_SZ", "")

					ClearLogoImageBox()
					GUICtrlSetState($UseOEMLogo, $GUI_UNCHECKED)
					GUICtrlSetState($Use_SupportInfo, $GUI_UNCHECKED)
					GUICtrlSetState($OEMLogo_Locate, $GUI_DISABLE)
					GUICtrlSetState($OEMLogo_SetLocate, $GUI_DISABLE)

					GUICtrlSetData($Man_Info, "")
					GUICtrlSetData($Mod_Info, "")
					GUICtrlSetData($SupportInfo_Line1, "")
					GUICtrlSetData($SupportInfo_Line2, "")
					GUICtrlSetData($SupportInfo_Line3, "")

					Case $verify = 7
                            ; NO
							MsgBox(0,"Information","OEM Data was NOT Cleared",2)
				EndSelect

		Case $Show_About
			$OtherAboutInfo = @CRLF & @CRLF & "Note: You may not be able to access some OS folders such as 'system32\oobe\info', this is something to do with either permissions or re-direction glitches on some OS's."
			MsgBox(32, $OEM_Branding_Title, "OEM Branding Tool is quick way of handling the OEM Information such as the Logo, Manufacture, Model and any support data." & $OtherAboutInfo)

		Case $ShowOSBasicInfoScreen
			$win1020H2Plus = 0

			If @OSVersion = "win_10" And @OSBuild > 19043 Then
				$win1020H2Plus = 1
			EndIf

			IF $win1020H2Plus = "0" then Run("explorer.exe shell:::{BB06C0E4-D293-4f75-8A90-CB05B6477EEE}")
			IF $win1020H2Plus = "1" then MsgBox (4096,"Show OEM Information","The old OEM Information display is sadly no longer available in this OS Build {" & @OSBuild & "}.",120)

	EndSwitch
WEnd

Func IsChecked($control)
	Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>IsChecked

Func OEMCheck()

EndFunc

Func ArchDetect()
        If @OSArch = "X86" Then
            $HKLM = "HKEY_LOCAL_MACHINE"
			$OsArch = "x86"
        EndIf
        If @OSArch = "X64" Then
            $HKLM = "HKEY_LOCAL_MACHINE64"
			$OsArch = "x64"
        EndIf
EndFunc

Func ClearLogoImageBox()
	$OEMLogo_Clear = GUICtrlCreateLabel(" ",10, 20, 182, 144)
	$OEMLogo_Msg = GUICtrlCreateLabel("Any loaded OEM Logo " & @CRLF & "     Will Show Here",40, 60, 182,100)
	$OEMLogo = "0"
	$OEM_Location = ""
EndFunc

Func Refresh_Data()

IF $sShowOrig = "Yes" and FileExists($OEMLogoPathStripped) then
	$OEMLogo = GUICtrlCreatePic($OEMLogoPathStripped, 10, 20, 182, 144)
	GUICtrlSetTip(-1, $OEMLogoPathStripped, "Current OEM Logo Location",1,1)
EndIf

IF $sShowOrig = "No" Then

			If $OEM_Location <> "" and $OEMLogoShow <> "0" Then
					 GUICtrlSetImage($OEMLogo, $OEM_Location)
					GUICtrlSetTip(-1, $OEM_Location, "Current OEM Logo Location",1,1)
			EndIf

EndIf

$OEM_Manuf = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Manufacturer")
$OEM_Model = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","Model")
$Support_Hours = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportHours")
$Support_Phone = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportPhone")
$Support_Url   = RegRead($HKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation","SupportURL")

GUICtrlSetData($Man_Info, $OEM_Manuf)
GUICtrlSetData($Mod_Info, $OEM_Model)
GUICtrlSetData($SupportInfo_Line1, $Support_Hours)
GUICtrlSetData($SupportInfo_Line2, $Support_Phone)
GUICtrlSetData($SupportInfo_Line3, $Support_Url)

EndFunc

Func Export_OEMInfo()
 ; This was a utter b-----d to get to work as 64 bit OS's throws a curve ball with registry stuff!

	  $verify2 = MsgBox($MB_ICONWARNING + $MB_YESNO + $MB_DEFBUTTON2,"Export Current OEM Information Registry","Do you wish to export the current registry OEM Information ?",0,$GUI)

				Select
					Case $verify2 = 6
						; YES -- Save OEM Info from registry.
					 $NumRand = Random(1, 200, 1)
                     If @OSArch = "X64" Then Run(@WindowsDir & "\sysnative\cmd.exe " & "/c REG EXPORT HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation " & @UserProfileDir & "\Desktop\OEMInfo" & $NumRand & ".reg","",@SW_HIDE)
					 If @OSArch = "X86" Then Run(@ComSpec & " /c REG EXPORT HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation " & @UserProfileDir & "\Desktop\OEMInfo" & $NumRand & ".reg","",@SW_HIDE)

					Case $verify2 = 7
                            ; NO -- Do nothing and return control.
				EndSelect

EndFunc

Func _ImageGetSize($sImage)
$iX = ""
$iY = ""
$WrongPSize = "No"
_GDIPlus_Startup()
$hImage2 = _GDIPlus_ImageLoadFromFile($sImage)
$iX = _GDIPlus_ImageGetWidth($hImage2)
$iY = _GDIPlus_ImageGetHeight($hImage2)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_Shutdown()
IF $iX < "120" or $iX > "120" then $WrongPSize = "Yes"
IF $iY < "120" or $iY > "120" then $WrongPSize = "Yes"
Return $WrongPSize
EndFunc

