From 6ff3d1eb36e0e7fe7b2374d7903aaa8c3e6e58d2 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Tue, 28 Nov 2017 21:53:20 +0000 Subject: [PATCH 1/9] Fix for the self-update feature and misc Also minor but significant improvement to the merge function: it no longer writes temp files to the disk while processing data. --- updater.bat | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/updater.bat b/updater.bat index 18830da..12dae37 100644 --- a/updater.bat +++ b/updater.bat @@ -28,10 +28,40 @@ IF /I "%~1"=="-multioverrides" ( IF /I "%~1"=="-merge" ( SET _merge=1 ) +IF /I "%~1"=="-updatebatch" ( + SET _updateb=1 +) SHIFT GOTO parse :endparse ECHO. +IF DEFINED _updateb ( + IF NOT "!_myname:~0,9!"=="[updated]" ( + ECHO Checking updater version... + ECHO. + DEL /F "[updated]!_myname!.bat" 2>nul + REM Uncomment the next line and comment the powershell call for testing. + REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/updater.bat', '[updated]!_myname!.bat')" >nul + IF EXIST "[updated]!_myname!.bat" ( + START CMD /C "[updated]!_myname!.bat" !_myparams! + EXIT /B + ) ELSE ( + ECHO Failed. Make sure PowerShell is allowed internet access. + ECHO. + PAUSE + EXIT /B + ) + ) ELSE ( + IF EXIST "!_myname:~9!.bat" ( + DEL /F "!_myname:~9!.bat" + CALL :begin + REN "!_myname!.bat" "!_myname:~9!.bat" + EXIT /B + ) + ) +) +:begin SET /A "_line=0" IF NOT EXIST user.js ( ECHO user.js not detected in the current directory. @@ -106,8 +136,8 @@ IF EXIST user.js ( ECHO. COPY /B /V /Y user.js-overrides\*.js user-overrides CALL :merge user-overrides user-overrides-merged.js - COPY /B /V /Y user.js+user-overrides-merged.js temp2 - CALL :merge temp2 user.js + COPY /B /V /Y user.js+user-overrides-merged.js updatertempfile + CALL :merge updatertempfile user.js ) ELSE ( ECHO. ECHO Appending... @@ -120,8 +150,8 @@ IF EXIST user.js ( IF EXIST "user-overrides.js" ( IF DEFINED _merge ( ECHO Merging user-overrides.js... - COPY /B /V /Y user.js+user-overrides.js temp2 - CALL :merge temp2 user.js + COPY /B /V /Y user.js+user-overrides.js updatertempfile + CALL :merge updatertempfile user.js ) ELSE ( ECHO Appending user-overrides.js... ECHO. @@ -182,8 +212,7 @@ FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( SET _pref=!_pref:"=""! FIND /I "!_pref!" %~2 >nul 2>&1 IF ERRORLEVEL 1 ( - FIND /I "!_pref!" %~1 >temp123 - FOR /F "tokens=* delims=" %%X IN (temp123) DO ( + FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( SET "_temp=%%X" SET "_temp=!_temp: =!" IF /I "user_pref"=="!_temp:~0,9!" ( @@ -201,6 +230,5 @@ FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( ENDLOCAL ) ENDLOCAL -DEL /F %~1 temp123 >nul GOTO :EOF REM ############################ From bdf69cd19895a6fec858fbef86cddc3eb51ed128 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 04:33:02 +0000 Subject: [PATCH 2/9] misc To account for the possibility of the user running the script silently in the background. PAUSE would leave an instance in memory doing nothing indefinitely. I was going to use TIMEOUT but PING performs better. --- updater.bat | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/updater.bat b/updater.bat index 12dae37..7a70f28 100644 --- a/updater.bat +++ b/updater.bat @@ -49,7 +49,7 @@ IF DEFINED _updateb ( ) ELSE ( ECHO Failed. Make sure PowerShell is allowed internet access. ECHO. - PAUSE + PING -n 301 127.0.0.1>nul EXIT /B ) ) ELSE ( @@ -58,6 +58,9 @@ IF DEFINED _updateb ( CALL :begin REN "!_myname!.bat" "!_myname:~9!.bat" EXIT /B + ) ELSE ( + ECHO. + ECHO The [updated] label is reserved. Do not run an [updated] script directly, or rename it to something else before you run it. ) ) ) @@ -230,5 +233,6 @@ FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( ENDLOCAL ) ENDLOCAL +DEL /F %1 >nul GOTO :EOF REM ############################ From b1fd2ba48b48227a58f3884c5fe019a26cb3c607 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 04:50:01 +0000 Subject: [PATCH 3/9] minor fix What would we want a warning that we can't get to read for? --- updater.bat | 2 ++ 1 file changed, 2 insertions(+) diff --git a/updater.bat b/updater.bat index 7a70f28..71576cd 100644 --- a/updater.bat +++ b/updater.bat @@ -61,6 +61,8 @@ IF DEFINED _updateb ( ) ELSE ( ECHO. ECHO The [updated] label is reserved. Do not run an [updated] script directly, or rename it to something else before you run it. + PING -n 301 127.0.0.1>nul + EXIT /B ) ) ) From ff9370b7142f04201b07a28fd83d3a390b8bc50d Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 04:56:33 +0000 Subject: [PATCH 4/9] fix for hiding powershell output Everything in a line after a powershell call is considered as being called from PowerShell. >nul didn't work because of that. Enclosing the line in brackets should fix it. --- updater.bat | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/updater.bat b/updater.bat index 71576cd..bada83f 100644 --- a/updater.bat +++ b/updater.bat @@ -42,7 +42,9 @@ IF DEFINED _updateb ( DEL /F "[updated]!_myname!.bat" 2>nul REM Uncomment the next line and comment the powershell call for testing. REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" - powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/updater.bat', '[updated]!_myname!.bat')" >nul + ( + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/updater.bat', '[updated]!_myname!.bat')" + ) >nul 2>&1 IF EXIST "[updated]!_myname!.bat" ( START CMD /C "[updated]!_myname!.bat" !_myparams! EXIT /B @@ -128,7 +130,9 @@ IF EXIST user.js ( ECHO. ) ECHO Retrieving latest user.js file from github repository... -powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js')" >nul +( + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js')" +) >nul 2>&1 ECHO. IF EXIST user.js ( IF DEFINED _multi ( From c9e7f1ccfa6ff028151a5b7cf0dc2f60303dd831 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 12:08:15 +0000 Subject: [PATCH 5/9] 2nd merge optimisation While I figure out a fix for the missing characters... Enclosing the whole merging loop in parentheses and replacing the source file with the entire output at once is more efficient than appending individual lines with >>%~2. The script doesn't have to wait for the HD to continue processing. --- updater.bat | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/updater.bat b/updater.bat index bada83f..6eb9a3b 100644 --- a/updater.bat +++ b/updater.bat @@ -39,7 +39,7 @@ IF DEFINED _updateb ( IF NOT "!_myname:~0,9!"=="[updated]" ( ECHO Checking updater version... ECHO. - DEL /F "[updated]!_myname!.bat" 2>nul + IF EXIST "[updated]!_myname!.bat" ( DEL /F "[updated]!_myname!.bat" ) REM Uncomment the next line and comment the powershell call for testing. REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" ( @@ -207,37 +207,38 @@ EXIT /B REM ###### Merge function ###### :merge -DEL /F %2 2>nul SETLOCAL disabledelayedexpansion -FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( - SET "_pref=%%H" - SETLOCAL enabledelayedexpansion - SET "_temp=!_pref: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( - FOR /F "delims=," %%S IN ("!_pref!") DO ( - SET "_pref=%%S" - ) - SET _pref=!_pref:"=""! - FIND /I "!_pref!" %~2 >nul 2>&1 - IF ERRORLEVEL 1 ( - FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( - SET "_temp=%%X" - SET "_temp=!_temp: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - SET "_pref=%%X" - ) +( + FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( + SET "_pref=%%H" + SETLOCAL enabledelayedexpansion + SET "_temp=!_pref: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( + FOR /F "delims=," %%S IN ("!_pref!") DO ( + SET "_pref=%%S" ) - ECHO(!_pref!>>%~2 + SET _pref=!_pref:"=""! + FIND /I "!_pref!" %~2 >nul 2>&1 + IF ERRORLEVEL 1 ( + FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( + SET "_temp=%%X" + SET "_temp=!_temp: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + SET "_pref=%%X" + ) + ) + ECHO(!_pref! + ) + ) ELSE ( + ECHO(!_pref! ) ) ELSE ( - ECHO(!_pref!>>%~2 + ECHO(!_pref! ) - ) ELSE ( - ECHO(!_pref!>>%~2 + ENDLOCAL ) - ENDLOCAL -) +)>%~2 ENDLOCAL DEL /F %1 >nul GOTO :EOF From a126a3210907093af30156fbbeba06666eaa5ab9 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 15:18:23 +0000 Subject: [PATCH 6/9] Revert "2nd merge optimisation" This reverts commit c9e7f1ccfa6ff028151a5b7cf0dc2f60303dd831. --- updater.bat | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/updater.bat b/updater.bat index 6eb9a3b..bada83f 100644 --- a/updater.bat +++ b/updater.bat @@ -39,7 +39,7 @@ IF DEFINED _updateb ( IF NOT "!_myname:~0,9!"=="[updated]" ( ECHO Checking updater version... ECHO. - IF EXIST "[updated]!_myname!.bat" ( DEL /F "[updated]!_myname!.bat" ) + DEL /F "[updated]!_myname!.bat" 2>nul REM Uncomment the next line and comment the powershell call for testing. REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" ( @@ -207,38 +207,37 @@ EXIT /B REM ###### Merge function ###### :merge +DEL /F %2 2>nul SETLOCAL disabledelayedexpansion -( - FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( - SET "_pref=%%H" - SETLOCAL enabledelayedexpansion - SET "_temp=!_pref: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( - FOR /F "delims=," %%S IN ("!_pref!") DO ( - SET "_pref=%%S" - ) - SET _pref=!_pref:"=""! - FIND /I "!_pref!" %~2 >nul 2>&1 - IF ERRORLEVEL 1 ( - FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( - SET "_temp=%%X" - SET "_temp=!_temp: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - SET "_pref=%%X" - ) +FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( + SET "_pref=%%H" + SETLOCAL enabledelayedexpansion + SET "_temp=!_pref: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( + FOR /F "delims=," %%S IN ("!_pref!") DO ( + SET "_pref=%%S" + ) + SET _pref=!_pref:"=""! + FIND /I "!_pref!" %~2 >nul 2>&1 + IF ERRORLEVEL 1 ( + FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( + SET "_temp=%%X" + SET "_temp=!_temp: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + SET "_pref=%%X" ) - ECHO(!_pref! ) - ) ELSE ( - ECHO(!_pref! + ECHO(!_pref!>>%~2 ) ) ELSE ( - ECHO(!_pref! + ECHO(!_pref!>>%~2 ) - ENDLOCAL + ) ELSE ( + ECHO(!_pref!>>%~2 ) -)>%~2 + ENDLOCAL +) ENDLOCAL DEL /F %1 >nul GOTO :EOF From 635cacfe547e0b992bae0c573c9ea3b3cbcb8f2a Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 15:49:21 +0000 Subject: [PATCH 7/9] Replace PING with TIMEOUT Otherwise earthing's nosy firewall bitches about it. --- updater.bat | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/updater.bat b/updater.bat index bada83f..a93c65a 100644 --- a/updater.bat +++ b/updater.bat @@ -39,7 +39,7 @@ IF DEFINED _updateb ( IF NOT "!_myname:~0,9!"=="[updated]" ( ECHO Checking updater version... ECHO. - DEL /F "[updated]!_myname!.bat" 2>nul + IF EXIST "[updated]!_myname!.bat" ( DEL /F "[updated]!_myname!.bat" ) REM Uncomment the next line and comment the powershell call for testing. REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" ( @@ -51,7 +51,7 @@ IF DEFINED _updateb ( ) ELSE ( ECHO Failed. Make sure PowerShell is allowed internet access. ECHO. - PING -n 301 127.0.0.1>nul + TIMEOUT 300 EXIT /B ) ) ELSE ( @@ -63,7 +63,7 @@ IF DEFINED _updateb ( ) ELSE ( ECHO. ECHO The [updated] label is reserved. Do not run an [updated] script directly, or rename it to something else before you run it. - PING -n 301 127.0.0.1>nul + TIMEOUT 300 EXIT /B ) ) From 3737b65f5392caec75d78a18797e8c59be5c103a Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Wed, 29 Nov 2017 16:15:20 +0000 Subject: [PATCH 8/9] Fixed issue with commented-out user_pref lines It ended up being a mixture of the previous commit and the fix. It writes a temporary file on the go that only holds preferences, and generates the target file at once at the end. It's slower than before, but it works. --- updater.bat | 54 +++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/updater.bat b/updater.bat index a93c65a..d8f5517 100644 --- a/updater.bat +++ b/updater.bat @@ -207,38 +207,40 @@ EXIT /B REM ###### Merge function ###### :merge -DEL /F %2 2>nul SETLOCAL disabledelayedexpansion -FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( - SET "_pref=%%H" - SETLOCAL enabledelayedexpansion - SET "_temp=!_pref: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( - FOR /F "delims=," %%S IN ("!_pref!") DO ( - SET "_pref=%%S" - ) - SET _pref=!_pref:"=""! - FIND /I "!_pref!" %~2 >nul 2>&1 - IF ERRORLEVEL 1 ( - FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( - SET "_temp=%%X" - SET "_temp=!_temp: =!" - IF /I "user_pref"=="!_temp:~0,9!" ( - SET "_pref=%%X" - ) +( + FOR /F "tokens=1,* delims=]" %%G IN ('find /n /v "" ^< "%~1"') DO ( + SET "_pref=%%H" + SETLOCAL enabledelayedexpansion + SET "_temp=!_pref: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + IF /I NOT "user.js.parrot"=="!_temp:~12,14!" ( + FOR /F "delims=," %%S IN ("!_pref!") DO ( + SET "_pref=%%S" ) - ECHO(!_pref!>>%~2 + SET _pref=!_pref:"=""! + FIND /I "!_pref!" updatertempfile1 >nul 2>&1 + IF ERRORLEVEL 1 ( + FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO ( + SET "_temp=%%X" + SET "_temp=!_temp: =!" + IF /I "user_pref"=="!_temp:~0,9!" ( + SET "_pref=%%X" + ) + ) + ECHO(!_pref! + ECHO(!_pref!>>updatertempfile1 + ) + ) ELSE ( + ECHO(!_pref! ) ) ELSE ( - ECHO(!_pref!>>%~2 + ECHO(!_pref! ) - ) ELSE ( - ECHO(!_pref!>>%~2 + ENDLOCAL ) - ENDLOCAL -) +)>%~2 ENDLOCAL -DEL /F %1 >nul +DEL /F %1 updatertempfile1 >nul GOTO :EOF REM ############################ From 0a976605067bd41130c37f21f1e14368850079b1 Mon Sep 17 00:00:00 2001 From: earthlng Date: Wed, 29 Nov 2017 23:20:00 +0100 Subject: [PATCH 9/9] updater.bat v3.0 FINAL THE REAL FUCKING DEAL this time - finally! THANKS @claustromaniac !! --- updater.bat | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/updater.bat b/updater.bat index d8f5517..997f7d5 100644 --- a/updater.bat +++ b/updater.bat @@ -56,9 +56,10 @@ IF DEFINED _updateb ( ) ) ELSE ( IF EXIST "!_myname:~9!.bat" ( - DEL /F "!_myname:~9!.bat" + REN "!_myname:~9!.bat" "!_myname:~9!.old" CALL :begin REN "!_myname!.bat" "!_myname:~9!.bat" + DEL /F "!_myname:~9!.old" EXIT /B ) ELSE ( ECHO.