Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch for changes in the mods folder and rebuild on each change #2

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions BuildMod.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ SETLOCAL EnableDelayedExpansion ENABLEEXTENSIONS

set _mLUA="lua.exe"

:ReadArg
if NOT "%1"=="" (
SET _bIsUsingArgs=y
if "%1"=="-includeExtraFiles" (
SET _bIncludeExtraFilesArg=y
)
if "%1"=="-rebuildMod" (
SET _bRebuildModArg=y
)
if "%1"=="-simpleCombine" (
SET _bSimpleCombineArg=y
)
if "%1"=="-compositeCombine" (
SET _bCompositeCombineArg=y
)
if "%1"=="-nummericSuffixCombine" (
SET _bNummericSuffixCombineArg=y
)
if "%1"=="-copy" (
SET _bCopyAllArg=y
)
if "%1"=="-copySome" (
SET _bCopySomeArg=y
)
if "%1"=="-updateMbinCompiler" (
SET _bUpdateMbinCompilerArg=y
)
if "%1"=="-checkForConflict" (
SET _bCheckForConflictArg=y
)
if "%1"=="-recreatePakList" (
SET _bRecreatePakListArg=y
)
if "%1"=="-recreateMapFileTrees" (
SET _bRecreateMapFileTreesArg=y
)
SHIFT /1
GOTO :ReadArg
)
rem ------------- testing for administrator -------------------------------
set _bMyPath=%CD%
set _bSystem32=%SYSTEMROOT%\system32
Expand Down Expand Up @@ -227,12 +266,17 @@ FOR /r "%~dp0\ModExtraFilesToInclude" %%G in (*.*) do (
)
if %_bExtraFiles% EQU 0 goto :NO_EXTRAFILES

if defined _bIsUsingArgs (
if defined _bIncludeExtraFilesArg ( SET _bExtraFilesInPAK=y)
GOTO :SKIPCHOICE_EXTRAFILES
)
echo.
echo.^>^>^> There are Extra Files in the ModExtraFilesToInclude folder. If you INCLUDE them...
echo.^>^>^> ***** Remember, these files will OVERWRITE any existing ones in the created PAK *****
CHOICE /c:YN /m "??? Do you want to include them in the created PAK "
if %ERRORLEVEL% EQU 2 goto :NO_EXTRAFILES
if %ERRORLEVEL% EQU 1 SET _bExtraFilesInPAK=y
:SKIPCHOICE_EXTRAFILES

:NO_EXTRAFILES
rem -------------- end Check if ExtraFilesToInclude present ------------------------------
Expand Down Expand Up @@ -306,12 +350,17 @@ if %_bNumberScripts% EQU 0 (
goto :BYPASSPAKEXTRACT
)

if defined _bIsUsingArgs (
if defined _bRebuildModArg( SET _bBuildMODpak=y ) else ( SET _bNumberScripts=0 )
GOTO :SKIPCHOICE_REBUILDMOD
)
echo.-----------------------------------------------------------
echo.
echo.^>^>^> At least one script was found in the pak
CHOICE /c:YN /m "??? Do you want to rebuild the MOD pak using that script "
if %ERRORLEVEL% EQU 2 SET _bNumberScripts=0
if %ERRORLEVEL% EQU 1 SET _bBuildMODpak=y
:SKIPCHOICE_REBUILDMOD

if defined _bBuildMODpak (
set _bNoMod=
Expand Down Expand Up @@ -341,6 +390,21 @@ if defined _bNoMod goto :EXECUTE
if defined _mSIMPLE goto :SIMPLE_MODE
if %_bNumberScripts% EQU 1 goto :SIMPLE_MODE

if defined _bIsUsingArgs (
if defined _bSimpleCombineArg (
SET _bCOMBINE_MODS=1
goto :SKIPCHOICE_COPY
)
if defined _bNummericSuffixCombineArg (
SET _bCOMBINE_MODS=2
goto :SKIPCHOICE_COPY
)
if defined _bCompositeCombineArg (
SET _bCOMBINE_MODS=3
goto :SKIPCHOICE_COPY
)
goto :SKIPCHOICE_COMBINE
)
echo.
echo.^>^>^> INDIVIDUAL PAKs may or may not work together depending on the EXML files they change
echo. If they modify the same original EXML files, the last one loaded will win and the other changes will be lost...
Expand Down Expand Up @@ -374,25 +438,52 @@ CHOICE /c:yn /m "??? Do you want to use a NUMERIC suffix[Y] or the current DATE-
if %ERRORLEVEL% EQU 2 SET _bCOMBINE_MODS=1
if %ERRORLEVEL% EQU 1 SET _bCOMBINE_MODS=2
goto :CONTINUE_EXECUTION2
:SKIPCHOICE_COMBINE

:CONTINUE_EXECUTION1
if defined _bIsUsingArgs (
if defined _bCopyAllArg (
SET _bCOPYtoNMS=ALL
goto :SKIPCHOICE_COPY
)
if defined _bCopySomeArg (
SET _bCOPYtoNMS=SOME
goto :SKIPCHOICE_COPY
)
SET _bCOPYtoNMS=NONE
goto :SKIPCHOICE_COPY
)
echo.
CHOICE /c:NSA /m "??? Would you like or [N]ot to COPY [S]ome or [A]ll Created Mod PAKs to your game folder and DELETE [DISABLEMODS.TXT] "
if %ERRORLEVEL% EQU 3 SET _bCOPYtoNMS=ALL
if %ERRORLEVEL% EQU 2 SET _bCOPYtoNMS=SOME
if %ERRORLEVEL% EQU 1 SET _bCOPYtoNMS=NONE
:SKIPCHOICE_COPY

:CONTINUE_EXECUTION2
if %_bCOMBINE_MODS% NEQ 0 SET _bCOPYtoNMS=SOME
goto :EXECUTE

:SIMPLE_MODE
if %_bNumberScripts% EQU 0 goto :EXECUTE
if defined _bIsUsingArgs (
if defined _bCopyAllArg (
SET _bCOPYtoNMS=ALL
goto :SKIPCHOICE_SIMPLECOPY
)
if defined _bCopySomeArg (
SET _bCOPYtoNMS=ALL
goto :SKIPCHOICE_SIMPLECOPY
)
SET _bCOPYtoNMS=NONE
goto :SKIPCHOICE_SIMPLECOPY
)
echo.
CHOICE /c:YN /m "??? Would you like to COPY the created Mod PAKs to your game folder and DELETE [DISABLEMODS.TXT] "
if %ERRORLEVEL% EQU 2 SET _bCOPYtoNMS=NONE
if %ERRORLEVEL% EQU 1 SET _bCOPYtoNMS=ALL
rem -------- user options end here -----------
:SKIPCHOICE_SIMPLECOPY

:EXECUTE

Expand Down Expand Up @@ -437,6 +528,14 @@ if defined _mSIMPLE goto :SIMPLE_MODE1
echo.
if not exist "MBINCompiler.exe" CALL MBINCompilerDownloader.bat & goto :CONTINUE_EXECUTION2

if defined _bIsUsingArgs (
if defined _bUpdateMbinCompilerArg (
goto :SKIPCHOICE_UPDATECOMPILER
) else (
goto :CONTINUE_EXECUTION2
)
GOTO :SKIPCHOICE_UPDATECOMPILER
)
Del /f /q /s "MBINCompilerVersion.txt" 1>NUL 2>NUL
MBINCompiler.exe version -q >>MBINCompilerVersion.txt
set /p _bMBINCompilerVersion=<MBINCompilerVersion.txt
Expand All @@ -445,6 +544,7 @@ echo.^>^>^> Your current MBINCompiler is version: %_bMBINCompilerVersion%
echo.
CHOICE /c:yn /t 30 /d y /m "??? Do you need to UPDATE MBINCompiler.exe, (default Y in 30 seconds)"
if %ERRORLEVEL% EQU 2 goto :CONTINUE_EXECUTION2
:SKIPCHOICE_UPDATECOMPILER

:SIMPLE_MODE1
echo.
Expand All @@ -468,10 +568,15 @@ if defined _mVERBOSE (
)

rem ------------- Conflict detection or not? -------------
if defined _bIsUsingArgs (
if defined _bCheckForConflictArg ( set _bCheckMODSconflicts= 1 ) else ( set _bCheckMODSconflicts= 2 )
GOTO :SKIPCHOICE_CHECKFORCONFLICT
)
echo.
CHOICE /c:yn /m "??? Would you like to check your NMS MODS for conflict?"
if %ERRORLEVEL% EQU 2 set _bCheckMODSconflicts=2
if %ERRORLEVEL% EQU 1 set _bCheckMODSconflicts=1
:SKIPCHOICE_CHECKFORCONFLICT
rem ------------- end Conflict detection or not? -------------

rem ************************** start PAK_LISTs creation section ********************************
Expand Down Expand Up @@ -502,11 +607,16 @@ goto :NoNeedToAsk

:Ask
if not exist "..\DEBUG.txt" goto :NoNeedToAsk
if defined _bIsUsingArgs (
if defined _bRecreatePakListArg ( set _bRecreatePAKList=1) else ( set _bRecreatePAKList=2)
GOTO :SKIPCHOICE_RECREATEPAKLIST
)
echo.
REM echo.^>^>^> If there was a NMS update, it is recommended to recreate this list
CHOICE /c:yn /m "??? Do you want to RECREATE the NMS PAK file list"
if %ERRORLEVEL% EQU 2 set _bRecreatePAKList=2
if %ERRORLEVEL% EQU 1 set _bRecreatePAKList=1
:SKIPCHOICE_RECREATEPAKLIST

if %_bRecreatePAKList% EQU 1 (
CALL PSARC_LIST_PAKS.BAT
Expand Down Expand Up @@ -538,8 +648,13 @@ if defined _mWbertro (
goto :START
)

if defined _bIsUsingArgs (
if defined _bRecreateMapFileTreesArg ( set _bRecreateMapFileTrees=1)
GOTO :SKIPCHOICE_RECREATEMAPLIST
)
CHOICE /c:yn /m "??? Do you want to (RE)CREATE the MapFileTrees files DURING script processing"
if %ERRORLEVEL% EQU 1 (set _bRecreateMapFileTrees=1)
:SKIPCHOICE_RECREATEMAPLIST
rem ************************** end MapFileTrees creation section ********************************

:START
Expand Down Expand Up @@ -694,6 +809,7 @@ if %_bNumberPAKs% GTR 0 (
echo.---------------------------------------------------------
if defined _mSIMPLE goto :SIMPLE_MODE2
if defined _min_subprocess goto :SIMPLE_MODE2
if defined _bIsUsingArgs goto :SIMPLE_MODE2
timeout /t 5

:SIMPLE_MODE2
Expand Down
2 changes: 2 additions & 0 deletions WatchMods.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%cd%\w.ps1'"
49 changes: 49 additions & 0 deletions w.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# set the arguments to be passed to the builder
azilvl marked this conversation as resolved.
Show resolved Hide resolved
$args = "-copy","-recreateMapFileTrees"
function Get-CurrentDirectory {
return Split-Path $MyInvocation.ScriptName
}
$pathToMonitor = Join-Path (Get-CurrentDirectory) "ModScript"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $pathToMonitor
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$cachedLastWriteTime = [long]([DateTime]::MinValue.Ticks)

$action = {
$path = $Event.SourceEventArgs.FullPath
$lastWriteTime = [long](Get-ItemProperty -Path $path -Name LastWriteTime).LastWriteTime.Ticks
if (($lastWriteTime - $cachedLastWriteTime ) -gt 100000) {
$changeType = $Event.SourceEventArgs.ChangeType
write-host "`n$(Get-Date -Format HH:mm:ss), $changeType, $path"
$cachedLastWriteTime = $lastWriteTime
Start-Process BuildMod.bat -ArgumentList $args
}

}
$handlers = . {
Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $Action -SourceIdentifier FSChange
# Register-ObjectEvent -InputObject $watcher -EventName Created -Action $Action -SourceIdentifier FSCreate
# Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $Action -SourceIdentifier FSDelete
# Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $Action -SourceIdentifier FSRename
}

Write-Host "Watching for changes to /ModScript"

try {
do {
Wait-Event -Timeout 1
Write-Host "." -NoNewline

} while ($true)
}
finally {
Unregister-Event -SourceIdentifier FSChange
# Unregister-Event -SourceIdentifier FSCreate
# Unregister-Event -SourceIdentifier FSDelete
# Unregister-Event -SourceIdentifier FSRename
$handlers | Remove-Job
$watcher.EnableRaisingEvents = $false
$watcher.Dispose()
}