-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_patch.bat
More file actions
118 lines (99 loc) · 3.81 KB
/
create_patch.bat
File metadata and controls
118 lines (99 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion
:: Change Directory
set "Script_DIR=%~dp0"
cd /d "%Script_DIR%"
:: Get Date
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "DATETIME=%%I"
set "DATE=!DATETIME:~0,8!"
:: ============================================
:: Diff Patch Generator
:: Compare !Original_DIR! with !Modified_DIR! folder
:: ============================================
:: Directory Setting
set "Original_DIR=Original"
set "Modified_DIR=Modified"
set "PATCH_FILE=unofficial_patch_!DATE!.patch"
set "DAT_FILE=!PATCH_FILE:.patch=.dat!"
:: Filter Setting (regex patterns to exclude from patch)
:: >> for extension, \.psd
:: >> for folder, /saves/
set "EXCLUDE_PATTERNS=\.psd\b|/saves/"
:: Check if git is available
where git >nul 2>&1
if errorlevel 1 (
echo [ERROR] git is not installed or not in PATH.
pause
exit /b 1
)
:: Check if Original Directory exists
if not exist !Original_DIR! (
echo [ERROR] !Original_DIR! folder not found.
pause
exit /b 1
)
:: Check if Modified Directory exists
if not exist !Modified_DIR! (
echo [ERROR] !Modified_DIR! folder not found.
pause
exit /b 1
)
echo ============================================
echo Diff Patch Generator
echo ============================================
echo.
echo Source (Original): !Original_DIR!
echo Target (Modified): !Modified_DIR!
echo Output: !PATCH_FILE!
echo.
:: Step 1: Generate patch
:: --no-index: compare two directories (not git history)
:: Exit code 1 = differences found (expected), 0 = no differences
echo [1/2] Generating git diff...
git diff --no-index --binary --full-index !Original_DIR! !Modified_DIR! > "!PATCH_FILE!" 2>nul
set "DIFF_EXIT=!errorlevel!"
if "!DIFF_EXIT!" == "0" (
del "!PATCH_FILE!" 2>nul
echo [WARNING] No differences found. Patch file not created.
pause
exit /b 0
)
if not exist "!PATCH_FILE!" (
echo [ERROR] Failed to create patch file.
pause
exit /b 1
)
for %%A in ("!PATCH_FILE!") do set "FILESIZE=%%~zA"
if "!FILESIZE!" == "0" (
del "!PATCH_FILE!"
echo [ERROR] Patch file is empty.
pause
exit /b 1
)
:: Step 2: Normalize paths
:: git diff --no-index produces: a/Original/game/file -> b/Modified/game/file
:: Normalize to: a/game/file -> b/game/file
:: Strip Original_DIR and Modified_DIR prefixes so patch applies with "git apply -p1"
echo [2/2] Normalizing paths and filtering...
powershell -Command "$origDir = '!Original_DIR!' -replace '\\','/'; $modDir = '!Modified_DIR!' -replace '\\','/'; $c = Get-Content '!PATCH_FILE!' -Raw -Encoding UTF8; $blocks = $c -split '(?=diff --git )'; $filtered = $blocks | Where-Object { $_ -notmatch '!EXCLUDE_PATTERNS!' }; $result = ($filtered -join '') -replace ('a/' + [regex]::Escape($origDir) + '/'), 'a/' -replace ('a/' + [regex]::Escape($modDir) + '/'), 'a/' -replace ('b/' + [regex]::Escape($origDir) + '/'), 'b/' -replace ('b/' + [regex]::Escape($modDir) + '/'), 'b/'; [System.IO.File]::WriteAllText('!PATCH_FILE!', $result, [System.Text.UTF8Encoding]::new($false))"
:: Step 3: Encode patch to Base64 (.dat) to prevent casual viewing
echo [3/3] Encoding patch to Base64...
powershell -Command "$bytes = [System.IO.File]::ReadAllBytes('!PATCH_FILE!'); $b64 = [Convert]::ToBase64String($bytes, [Base64FormattingOptions]::InsertLineBreaks); [System.IO.File]::WriteAllText('!DAT_FILE!', $b64, [System.Text.Encoding]::ASCII)"
if not exist "!DAT_FILE!" (
echo [ERROR] Failed to create encoded patch file.
pause
exit /b 1
)
echo.
echo [OK] Patch file created: !PATCH_FILE!
for %%A in ("!PATCH_FILE!") do echo [OK] Raw size: %%~zA bytes
echo [OK] Encoded file created: !DAT_FILE!
for %%A in ("!DAT_FILE!") do echo [OK] Encoded size: %%~zA bytes
echo.
echo To distribute, include:
echo - !DAT_FILE!
echo - apply_patch.bat
echo - PortableGit\ folder
echo.
pause