Skip to content

Instantly share code, notes, and snippets.

@elpatron68
Created November 23, 2019 11:50
Show Gist options
  • Save elpatron68/97c93171a63b88adea70287e0b57a294 to your computer and use it in GitHub Desktop.
Save elpatron68/97c93171a63b88adea70287e0b57a294 to your computer and use it in GitHub Desktop.
@echo off
rem (c) 2019 M. Busche, elpatron@mailbox.org
echo " _ _ _ "
echo " | | | | | | "
echo " __ _ __| |_ _ _ __ __| | __ _| |_ ___ "
echo " / _` |/ _` | | | | '_ \ / _` |/ _` | __/ _ \"
echo "| (_| | (_| | |_| | |_) | (_| | (_| | || __/"
echo " \__,_|\__,_|\__,_| .__/ \__,_|\__,_|\__\___|"
echo " | | "
echo " |_| "
rem This script installs or updates the mod "AutoDrive" for Farming Simulator 19
rem Have fun!
rem =============================================================================
rem U S E R S E T T I N G S
rem =============================================================================
rem Set deployment mode:
rem * "ZIPFILE" creates a ZZZ_Courseplay.zip file in your mod directory
rem * "DIRECTORY" copies the Courseplay as subdirectory to your mod directory
rem Both modes work fine, you have the choice.
rem
set deployment="ZIPFILE"
rem set deployment="DIRECTORY"
rem =============================================================================
rem Set Farming Simulator version
set fsversion="2019"
rem set fsversion="2017"
rem =============================================================================
rem If you want the command window to close after run: set autoclose="YES".
rem Otherwise you have to hit a keystroke after the run - which enables you
rem to see what happened.
rem
rem set autoclose="YES"
set autoclose="NO"
rem =============================================================================
rem You should replace these with the full path to the files if you use this
rem script without the portable editions of Git and 7-Zip.
rem
set zipexe=C:\Program Files\7-Zip\7z.exe
set wgetexe=C:\ProgramData\chocolatey\lib\Wget\tools\wget.exe
rem =============================================================================
rem End of U S E R S E T T I N G S
rem =============================================================================
rem
rem =============================================================================
rem References:
rem
rem Colors: https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line
rem Lots of other code stolen from https://stackoverflow.com and other
rem helpful sites.
rem =============================================================================
if exist master.zip* del master.zip* /q
setlocal enabledelayedexpansion
rem Colored text only Win10+
for /f "tokens=2 delims=[]" %%x in ('ver') do set WINVER=%%x
set WINVER=%WINVER:Version =%
if "%WINVER:~0,3%"=="10." (
set colored=1
) else (
set colored=0
)
rem Title
if %colored% == 1 (
echo AutoDrive Github Updatescript v1.0
) else (
echo AutoDrive Github Updatescript v1.0
)
echo (c) 2019 elpatron@mailbox.org
echo .
rem wget.exe startable?
echo Checking for wget...
%wgetexe% --version > NUL
if not %errorlevel%==0 set wgetok="-1"
rem 7-Zip startable?
echo Checking for 7-Zip...
"%zipexe%" >NUL
if %errorlevel%==9009 goto zipok="-1"
if "%wgetok%"=="-1" goto exefehler
if "%zipok%"=="-1" goto exefehler
echo wget and 7-Zip are ok, lets move on...
echo.
rem Write a VBS file for getting Courseplay version from moddesc.xml
> "%TEMP%\getversion.vbs" (
echo.Dim oXml: Set oXml = CreateObject^("Microsoft.XMLDOM"^)
echo.oXml.Load WScript.Arguments.Item^(0^)
echo.Dim oDoc: Set oDoc = oXml.documentElement
echo.
echo.For Each node In oDoc.childNodes
echo. If node.nodeName = "version" Then
echo. WScript.Echo node.text
echo. End If
echo.Next
echo.Set oXml = Nothing
)
rem Write a VBS file for getting modfolder from gameSettings.xml
> "%TEMP%\moddir.vbs" (
echo.Dim oXml: Set oXml = CreateObject^("Microsoft.XMLDOM"^)
echo.oXml.Load WScript.Arguments.Item^(0^)
echo.Dim oDoc: Set oDoc = oXml.documentElement
echo.
echo.For Each node In oDoc.childNodes
echo. If ^(node.nodeName = "modsDirectoryOverride"^) And ^(node.getAttribute^("active"^) = "true"^) Then
echo. WScript.Echo node.getAttribute^("directory"^)
echo. End If
echo.Next
echo.Set oXml = Nothing
)
rem Setting `Documents` folder
for /f "skip=2 tokens=2*" %%A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do set "UserDocs=%%B"
rem Setting Mod directory
SETLOCAL
for /f "delims=" %%i in ('cscript %TEMP%\moddir.vbs "%UserDocs%\My Games\FarmingSimulator%fsversion%\gameSettings.xml" //Nologo') do set moddir=%%i
rem Setting Autodrive mod folder or file
if defined moddir (
set _dest=%moddir%\FS19_AutoDrive
) else (
set _dest=%UserDocs%\my games\FarmingSimulator%fsversion%\mods\FS19_AutoDrive
)
echo Deployment method is %deployment%.
if %deployment%=="DIRECTORY" (
set destination=%_dest%
) else (
set destination=%_dest%.zip
)
echo Your destination is: %destination%.
rem Is this a fresh installation or an update?
if exist "%destination%" (
set freshinstall="no"
echo Previous version found, switching to update mode.
) else (
set freshinstall="yes"
echo No previous version found, switching to fresh install mode..
)
rem Backup directory
set "curpath=%cd%"
set backupdir=.\adbackup
echo Your backup folder is: %curpath%\adbackup
rem Extract moddesc.xml from ZIPFILE
if %deployment%=="ZIPFILE" (
if %freshinstall%=="no" (
echo Extracting 'moddesc.xml' for version detection...
del /q "%TEMP%\moddesc.xml" 2>NUL
"%zipexe%" e "%destination%" -o"%TEMP%" moddesc.xml -r -aoa > NUL 2>&1
)
)
rem Get current AutoDrive version with vbs script
if exist .\adversion.txt (
del /q .\adversion.txt 1,2>NUL
)
rem ...from directory
if %deployment%=="DIRECTORY" (
if %freshinstall%=="no" (
cscript "%TEMP%\getversion.vbs" "%destination%\modDesc.xml" //Nologo >.\adversion.txt
)
) else (
rem from zip file
if %freshinstall%=="no" (
cscript "%TEMP%\getversion.vbs" "%TEMP%\moddesc.xml" //Nologo >.\adversion.txt
)
)
rem sleep 2 seconds
ping 127.0.0.1 -n 2 > nul
rem Read version from output file
if exist .\adversion.txt (
set /p version=<.\adversion.txt
set freshinstall="no"
del /q .\adversion.txt 1,2>NUL
) else (
set freshinstall="yes"
set version="0"
)
if %freshinstall%=="no" (
if %colored% == 1 (
echo Your currently installed Version is: %version%
) else (
echo Your currently installed Version is: %version%
)
)
rem Delete old checkout
echo Deleting temporary folder...
rd /s/q .\courseplay 2> NUL
rem Download master.zip
echo Downloading latest Autodrive from Github...
%wgetexe% https://github.com/Stephan-S/FS19_AutoDrive/archive/master.zip
rem Extract master.zip
"%zipexe%" x master.zip -o.\tmp -y
rem Get new Autodrive version information
cscript "%TEMP%\getversion.vbs" ".\tmp\\FS19_AutoDrive-master\FS19_AutoDrive\modDesc.xml" //Nologo >.\adversion.txt
set /p newversion=<.\adversion.txt
if %colored% == 1 (
echo Version from Github: %newversion%
) else (
echo Version from Github: %newversion%
)
if exist .\adversion.txt (
del /q .\adversion.txt 1,2>NUL
)
rem Do we have an update?
if "%newversion%"=="%version%" (
if %colored% == 1 (
echo No version update found in moddesc.xml.
) else (
echo No version update found in moddesc.xml.
)
rem rd /s/q .\tmp 2> NUL
rem goto ende
) else (
echo We have found an update.
)
rem Backup current version
if %deployment%=="DIRECTORY" (
set backupfile=%backupdir%\FS19_AutoDrive_backup-%version%.zip
) else (
set backupfile=%backupdir%\FS19_AutoDrive_%version%.zip
)
if %freshinstall%=="no" (
echo Creating a backup of your current AutoDrive...
mkdir %backupdir% 2> NUL
if %deployment%=="DIRECTORY" (
"%zipexe%" a -r "%backupfile%" "%destination%\*" >NUL 2>&1
) else (
copy "%destination%" %backupfile% > NUL
)
) else (
echo No former version found - this seems to be a fresh install. Creating new mod directory for Courseplay...
if %deployment%=="DIRECTORY" (
md "%destination%" > NUL
)
)
if not %backupfile%1==1 (
echo If you consider any problems, check your backup file: "%backupfile%"
)
rem Copy cloned directory to mod folder
if %deployment%=="DIRECTORY" (
echo Copying the update to your mod folder...
xcopy /S /E /H /Y /C /Q ".\tmp\FS19_AutoDrive-master\FS19_AutoDrive" "%destination%\" >NUL
) else (
echo Copying the updated AutoDrive directory as ZIP file to your mod folder...
"%zipexe%" a -r -tzip .\tmp\ad.zip .\\tmp\FS19_AutoDrive-master\FS19_AutoDrive\* >NUL 2>&1
copy .\tmp\ad.zip "%destination%" /y > NUL
)
rem Delete temporary folder
echo Deleting temporary folder...
rd /s/q .\tmp 2> NUL
if %colored% == 1 (
echo Sucessfully updated from %version% to %newversion%.
) else (
echo Sucessfully updated from %version% to %newversion%.
)
del master.zip 2> NUL
goto ende
:exefehler
if "%wgetok%"=="-1" (
echo Wget for Windows has to be installed and reside in PATH!
echo Download: https://www.heise.de/download/product/wget-3073
)
if "%zipok%"=="-1" (
echo 7-Zip has to be installed and reside in PATH!
echo Download: http://www.7-zip.org/download.html
)
goto ende
:ende
rem Cleanup
if exist "%TEMP%\getversion.vbs" (
del /q "%TEMP%\getversion.vbs" 2>NUL
)
if exist "%TEMP%\moddir.vbs" (
del /q "%TEMP%\moddir.vbs" 2>NUL
)
rem Goodbye
echo Bye, and thanks for the fish.
if %autoclose%=="NO" (
echo ^(Any key to exit^)
pause >NUL
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment