Repository navigation
Expand file tree
/
Copy pathbuild_patch.py
More file actions
339 lines (269 loc) · 11.7 KB
/
Copy pathbuild_patch.py
File metadata and controls
339 lines (269 loc) · 11.7 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
"""
OS-agnostic patch build script for Legends mod
Replaces build_patch.sh with cross-platform Python implementation
"""
import os
import sys
import subprocess
import shutil
import argparse
import platform
from pathlib import Path
from buildscript.lib import VersionExtractor, BuildError, load_config
class PatchBuilder:
def __init__(self, source=None, build_dir=None, bb_dir=None):
self.current_dir = Path.cwd()
# Load config first
config = load_config(Path(__file__).parent / ".build_config.py")
# Use provided values, fall back to config, then to defaults
if build_dir is None:
build_dir = config.BUILD_DIR
if bb_dir is None:
bb_dir = config.BB_DIR
# Set default paths based on OS if still None
if bb_dir is None:
if platform.system() == "Windows":
bb_dir = r"c:\Steam\steamapps\common\Battle Brothers\data"
else:
bb_dir = os.path.expanduser(
"~/.local/share/Steam/steamapps/common/Battle Brothers/data"
)
self.bb_dir = Path(bb_dir)
self.build_dir = Path(build_dir)
self.version_extractor = VersionExtractor(self.current_dir)
# Get latest tag for assets version
self.latest_tag = self.version_extractor.get_legends_assets_version()
# Get commit hash if source not provided
if source is None:
source = self.get_commit_hash(self.latest_tag)
self.source = source
print(f"Source: {self.source}")
print(f"Build directory: {self.build_dir}")
print(f"Battle Brothers directory: {self.bb_dir}")
print(f"Latest asset tag: {self.latest_tag}")
def handle_exit(self, result, context=""):
"""Handle subprocess exit codes"""
if result.returncode != 0:
error_msg = f"Failed to build {context if context else 'patch'}!"
print(error_msg)
if result.stderr:
print(f"Error output: {result.stderr}")
raise BuildError(error_msg)
def get_commit_hash(self, tag):
"""Get commit hash for a given tag"""
try:
result = subprocess.run(
["git", "rev-parse", tag],
capture_output=True,
text=True,
cwd=self.current_dir,
)
if result.returncode == 0:
return result.stdout.strip()
else:
return ""
except FileNotFoundError:
print("Warning: git not found")
return ""
def build_helmets(self):
"""Build helmet scripts"""
print("Building helmets...")
# Remove and recreate helmet scripts directory
helmet_scripts_dir = self.current_dir / "helmet_scripts"
if helmet_scripts_dir.exists():
shutil.rmtree(helmet_scripts_dir)
helmet_scripts_dir.mkdir(exist_ok=True)
# Run helmet generation directly
from buildscript.python.make_legend_helmets import generate_legend_helmets
generate_legend_helmets(self.current_dir)
# Copy helmet scripts to build directory
build_helmet_dir = self.build_dir / "scripts" / "items" / "legend_helmets"
build_helmet_dir.mkdir(parents=True, exist_ok=True)
if helmet_scripts_dir.exists() and any(helmet_scripts_dir.iterdir()):
shutil.copytree(helmet_scripts_dir, build_helmet_dir, dirs_exist_ok=True)
def build_armor(self):
"""Build armor scripts"""
print("Building armors...")
# Remove and recreate armor scripts directory
armor_scripts_dir = self.current_dir / "legend_armor_scripts"
if armor_scripts_dir.exists():
shutil.rmtree(armor_scripts_dir)
armor_scripts_dir.mkdir(exist_ok=True)
# Run armor generation directly
from buildscript.python.make_legend_armor import generate_legend_armor
generate_legend_armor(self.current_dir)
# Copy armor scripts to build directory
build_armor_dir = self.build_dir / "scripts" / "items" / "legend_armor"
build_armor_dir.mkdir(parents=True, exist_ok=True)
if armor_scripts_dir.exists() and any(armor_scripts_dir.iterdir()):
shutil.copytree(armor_scripts_dir, build_armor_dir, dirs_exist_ok=True)
def build_enemies(self):
"""Build enemies"""
print("Building enemies...")
from buildscript.python.make_legend_enemies import generate_legend_enemies
generate_legend_enemies(self.current_dir)
def copy_mod_directories(self):
"""Copy mod directories to build directory"""
directories = ["mod_legends", "scripts", "ui"]
for dir_name in directories:
src_dir = self.current_dir / dir_name
dest_dir = self.build_dir / dir_name
if src_dir.exists():
print(f"Copying {dir_name} to {dest_dir} ...")
if dest_dir.exists():
# For scripts directory, merge instead of replace to preserve generated content
if dir_name == "scripts":
# Use the merge function from build_legends_mod
self._merge_directories(src_dir, dest_dir)
else:
shutil.rmtree(dest_dir)
shutil.copytree(src_dir, dest_dir)
else:
shutil.copytree(src_dir, dest_dir)
def _merge_directories(self, src_dir, dest_dir):
"""Merge source directory into destination, preserving existing content"""
for item in src_dir.rglob("*"):
if item.is_file():
# Calculate relative path and destination
rel_path = item.relative_to(src_dir)
dest_item = dest_dir / rel_path
# Create parent directories if they don't exist
dest_item.parent.mkdir(parents=True, exist_ok=True)
# Copy the file (overwrites existing files)
shutil.copy2(item, dest_item)
def create_initial_zip(self):
"""Create initial zip archive"""
import zipfile
zip_archive = self.version_extractor.artifact_name_mod()
zip_path = self.build_dir / zip_archive
# Change to build directory
original_cwd = os.getcwd()
os.chdir(self.build_dir)
try:
with zipfile.ZipFile(zip_archive, "w", zipfile.ZIP_DEFLATED) as zf:
# Only walk through the specific directories we want to include
for dir_name in ["mod_legends", "scripts", "ui"]:
if Path(dir_name).exists():
for root, dirs, files in os.walk(dir_name):
for file in files:
file_path = Path(root) / file
zf.write(file_path, file_path)
finally:
os.chdir(original_cwd)
return zip_archive
def add_scripts_to_zip(self, zip_archive):
"""Add scripts directory to existing zip"""
import zipfile
# Change to build directory
original_cwd = os.getcwd()
os.chdir(self.build_dir)
try:
with zipfile.ZipFile(zip_archive, "a", zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk("scripts"):
for file in files:
file_path = Path(root) / file
zf.write(file_path, file_path)
finally:
os.chdir(original_cwd)
def get_modified_files(self):
"""Get list of modified files since source commit"""
if not self.source:
return []
try:
result = subprocess.run(
["git", "diff", "--name-only", self.source, "HEAD"],
capture_output=True,
text=True,
cwd=self.current_dir,
)
if result.returncode == 0:
return result.stdout.strip().split("\n") if result.stdout.strip() else []
else:
print("Warning: Could not get git diff")
return []
except FileNotFoundError:
print("Warning: git not found")
return []
def copy_assets(self, zip_archive):
"""Copy assets if they have been modified"""
modified_files = self.get_modified_files()
# Filter files that are in 'gfx' or 'sounds' directories
filtered_files = [f for f in modified_files if f.startswith(("gfx/", "sounds/"))]
if filtered_files:
print("Modified asset files found, adding to archive...")
# Create temporary asset directories in build
for file_path in filtered_files:
src_file = self.current_dir / file_path
dest_file = self.build_dir / file_path
if src_file.exists():
dest_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_file, dest_file)
# Add to zip
original_cwd = os.getcwd()
os.chdir(self.build_dir)
try:
asset_dirs = set()
for file_path in filtered_files:
asset_dirs.add(file_path.split("/")[0])
# Use Python zipfile
import zipfile
with zipfile.ZipFile(zip_archive, "a", zipfile.ZIP_DEFLATED) as zf:
for asset_dir in asset_dirs:
if Path(asset_dir).exists():
for root, dirs, files in os.walk(asset_dir):
for file in files:
file_path = Path(root) / file
zf.write(file_path, file_path)
finally:
os.chdir(original_cwd)
def build(self):
"""Main build process"""
try:
print("Starting patch build process...")
# Remove and recreate build directory
if self.build_dir.exists():
shutil.rmtree(self.build_dir)
self.build_dir.mkdir(parents=True)
# Build script components (no brushes)
self.build_helmets()
self.build_armor()
self.build_enemies()
# Copy mod directories
self.copy_mod_directories()
# Create initial zip
zip_archive = self.create_initial_zip()
# Move final zip to Battle Brothers data directory
zip_path = self.build_dir / zip_archive
final_zip_path = self.bb_dir / zip_archive
# Ensure Battle Brothers directory exists
self.bb_dir.mkdir(parents=True, exist_ok=True)
shutil.move(zip_path, final_zip_path)
# Copy assets if modified
self.copy_assets(str(final_zip_path))
print(f"Patch build completed successfully! Created: {final_zip_path}")
except BuildError as e:
print(f"Patch build failed: {e}")
sys.exit(1)
except Exception as e:
print(f"Unexpected error: {e}")
sys.exit(1)
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(description="Build patch for Legends mod")
parser.add_argument("source", nargs="?", help="Source commit hash or branch")
parser.add_argument(
"build_dir",
nargs="?",
default="./build",
help="Build directory (default: ./build)",
)
parser.add_argument(
"--bb-dir",
help="Battle Brothers data directory (default: from .build_config.py or auto-detect)",
)
args = parser.parse_args()
builder = PatchBuilder(args.source, args.build_dir, args.bb_dir)
builder.build()
if __name__ == "__main__":
main()