-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.py
More file actions
447 lines (373 loc) · 18 KB
/
Copy pathreplace.py
File metadata and controls
447 lines (373 loc) · 18 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# this is the main file
import shutil
import tempfile
from time import sleep
import tools.check_necessary_files as file_checker
import tools.index_folder as index_folder
import tools.extract_zip as extract_zip
import tools.index_wads as index_wads
import tools.extract_wad as extract_wad
import tools.index_bin as index_bin
import tools.run_ritobin as run_ritobin
import tools.index_py as index_py
import tools.search_replace as search_replace
import tools.config as config
import tools.search_wad_directories as search_wad_directories
import tools.compress_to_wad as compress_to_wad
import multiprocessing
from pathlib import Path
import argparse
import os
def setup_argparse():
parser = argparse.ArgumentParser(description='Process game files and perform search/replace operations')
parser.add_argument('--config', default='replace_config.json', help='Path to config file')
parser.add_argument('--dry-run', action='store_true', help='Show matches without making changes')
return parser
def extract_wad_worker(wad_file):
return extract_wad.run_wad_extractor(wad_file[1])
def run_ritobin_worker(bin_file):
return run_ritobin.run_ritobin(bin_file)
def run_parallel_process(items, worker_func, process_name):
if len(items) > 1:
num_cores = max(1, multiprocessing.cpu_count() - 1)
print(f"Processing {len(items)} {process_name} using {num_cores} cores")
try:
with multiprocessing.Pool(processes=num_cores) as pool:
results = pool.map(worker_func, items)
return results
except Exception as e:
print(f"Error during parallel {process_name} processing: {str(e)}")
return None
else:
return [worker_func(items[0])]
def clean_wad_directories(directories, modified_files_set=None):
"""
Delete all files except skin0.bin in each WAD directory.
Also remove any .bak backup files.
Only process directories from modified_files_set if provided.
"""
print("\nCleaning WAD directories - keeping only skin0.bin files...")
processed_count = 0
skipped_count = 0
for wad_dir in directories:
try:
wad_path = Path(wad_dir)
# Skip if this directory is not in modified_files_set (when provided)
if modified_files_set is not None:
# Find the skin directory this WAD belongs to
current = wad_path
skin_dir = None
while current.name != "extracted_zips" and current.parent != current:
if current.parent.name == "extracted_zips":
skin_dir = current
break
current = current.parent
if skin_dir and str(skin_dir) not in modified_files_set:
skipped_count += 1
print(f"Skipping unmodified WAD directory: {wad_dir}")
continue
if wad_path.exists():
file_count = 0
deleted_count = 0
for file_path in wad_path.glob('**/*'):
if file_path.is_file():
file_count += 1
# Keep only skin0.bin files, delete everything else including .bak files
if file_path.name != 'skin0.bin':
file_path.unlink()
deleted_count += 1
processed_count += 1
print(f"Cleaned {wad_dir}: Kept 'skin0.bin', deleted {deleted_count} of {file_count} files")
except Exception as e:
print(f"Error cleaning {wad_dir}: {str(e)}")
print(f"Total: {processed_count} WAD directories processed, {skipped_count} directories skipped")
if __name__ == "__main__":
# Clean up previous run directories
print("Cleaning up previous run directories...")
extracted_zips_dir = Path("./extracted_zips")
output_dir = Path("./output")
if extracted_zips_dir.exists():
print(f"Removing existing {extracted_zips_dir} directory...")
try:
shutil.rmtree(extracted_zips_dir)
print(f"Successfully removed {extracted_zips_dir}")
except Exception as e:
print(f"Error removing {extracted_zips_dir}: {str(e)}")
if output_dir.exists():
print(f"Removing existing {output_dir} directory...")
try:
shutil.rmtree(output_dir)
print(f"Successfully removed {output_dir}")
except Exception as e:
print(f"Error removing {output_dir}: {str(e)}")
checker = file_checker.FileChecker()
if not checker.verify_and_setup():
print("Missing required files. Please ensure all necessary files are present.")
exit(1)
parser = setup_argparse()
args = parser.parse_args()
# Index all archives in the current directory
archives = index_folder.index_archives("./skins")
print("Archives found:")
print(archives)
# Extract all archives
for archive in archives:
extract_zip.extract_zip_file(archive[1])
# Index all .wad.client files in the extracted_zips directory
wad_files = index_wads.find_wad_client_files("extracted_zips")
# Process WAD files in parallel
wad_results = run_parallel_process(
[wf[1] for wf in wad_files],
extract_wad.run_wad_extractor,
"WAD files"
)
# Index all skin0.bin files in the extracted_zips directory
print("Searching for skin0.bin files...")
bin_files = index_bin.find_skin_bin_files("extracted_zips")
print("skin0.bin files found:")
print(bin_files)
print("Quantity of skin0.bin files found:")
print(len(bin_files))
# Process bin files in parallel
bin_results = run_parallel_process(bin_files, run_ritobin_worker, "skin0.bin files")
if bin_results:
for bin_file, (success, message) in zip(bin_files, bin_results):
print(f"Processing {bin_file}: {message}")
if not success:
break
# Index all .py files in the extracted_zips directory
py_files = index_py.find_py_files("extracted_zips")
print("py_files found:")
print(py_files)
print("Quantity of py_files found:")
print(len(py_files))
# Search and Replace operation
modified_files = set() # Keep track of modified files
if py_files:
try:
# Load configuration
cfg = config.load_config(args.config)
# Validate configuration
if not cfg['search_pattern'] or not cfg['replacement']:
print("Please configure search_pattern and replacement in replace_config.json")
print("Example config:")
print("""
{
"search_pattern": "your_pattern_here",
"replacement": "your_replacement_here",
"backup": true,
"encoding": "utf-8"
}
""")
exit(1)
# Perform dry run first
print("\nPerforming dry run to show matches...")
results = search_replace.search_and_replace_in_files(
py_files,
cfg,
dry_run=True
)
# Display results
print("\nMatches found:")
for filepath, matched_lines, count, _ in results:
if count > 0:
print(f"\nFile: {filepath}")
print(f"Matches: {count}")
print("Context:")
for line in matched_lines:
print(f" {line}\n")
# Ask for confirmation if not in dry-run mode
if not args.dry_run:
total_matches = sum(count for _, _, count, _ in results)
if total_matches > 0:
response = input(f"\nFound {total_matches} matches. Proceed with replacement? (y/n): ")
if response.lower() == 'y':
results = search_replace.search_and_replace_in_files(
py_files,
cfg,
dry_run=False
)
print("\nReplacement complete. Backup files created.")
for filepath, _, count, backup_path in results:
if count > 0:
print(f"Modified: {filepath}")
print(f"Backup: {backup_path}")
# Add to modified files set - Store the base skin directory
# Extract the skin directory from the filepath
file_path = Path(filepath)
# Navigate up to the extracted_zips directory
skin_dir = None
current = file_path
while current.name != "extracted_zips" and current.parent != current:
if current.parent.name == "extracted_zips":
skin_dir = current
break
current = current.parent
if skin_dir:
modified_files.add(str(skin_dir))
print(f"Added to processing list: {skin_dir}")
else:
print("No matches found.")
except Exception as e:
print(f"Error during search and replace: {str(e)}")
if py_files: # Only if we processed files
print("\n" + "="*50)
print("All operations completed.")
print("="*50)
# Process .py files with ritobin
print("\nProcessing .py files with ritobin...")
py_file_paths = [filepath for _, filepath in py_files] # Extract just the filepaths
# Process py files in parallel
py_ritobin_results = run_parallel_process(
py_file_paths,
run_ritobin.run_ritobin,
"Python files"
)
if py_ritobin_results:
for py_file, (success, message) in zip(py_file_paths, py_ritobin_results):
print(f"Processing {py_file}: {message}")
if not success:
break
print("Searching for WAD directories...")
wad_directories = search_wad_directories.find_wad_directories("extracted_zips")
print("\nWAD directories found:")
print(wad_directories)
print("Quantity of WAD directories found:")
print(len(wad_directories))
# NEW: Clean WAD directories - keep only skin0.bin files
clean_wad_directories(wad_directories, modified_files if modified_files else None)
# Filter WAD directories to only process modified ones
if modified_files:
filtered_wad_directories = []
for wad_dir in wad_directories:
wad_path = Path(wad_dir)
current = wad_path
skin_dir = None
while current.name != "extracted_zips" and current.parent != current:
if current.parent.name == "extracted_zips":
skin_dir = current
break
current = current.parent
if skin_dir and str(skin_dir) in modified_files:
filtered_wad_directories.append(wad_dir)
print(f"Filtered: Processing {len(filtered_wad_directories)} of {len(wad_directories)} WAD directories")
wad_directories = filtered_wad_directories
# Process WAD directories in parallel
wad_results = run_parallel_process(
wad_directories,
compress_to_wad.compress_to_wad,
"WAD directories"
)
# delete all .wad directorties and everyting inside
print("\nRemoving processed WAD directories...")
for wad_dir in wad_directories:
try:
wad_path = Path(wad_dir)
if wad_path.exists():
shutil.rmtree(wad_path)
print(f"Removed: {wad_dir}")
except Exception as e:
print(f"Error removing {wad_dir}: {str(e)}")
# Create output directory if it doesn't exist
output_dir = Path("./output")
output_dir.mkdir(exist_ok=True)
# First, organize archives by their parent directory
archive_groups = {}
for original_archive, original_path in archives:
# Debug print
print(f"\nProcessing archive: {original_path}")
# Normalize path handling
original_path = Path(original_path).resolve()
parent_dir = original_path.parent.name
if parent_dir not in archive_groups:
archive_groups[parent_dir] = []
archive_groups[parent_dir].append((original_archive, str(original_path)))
# Process each group
for parent_dir, group_archives in archive_groups.items():
print(f"\nProcessing group: {parent_dir}")
# Create parent directory in output if needed
parent_output_dir = output_dir / parent_dir
parent_output_dir.mkdir(exist_ok=True)
for original_archive, original_path in group_archives:
try:
# Convert and normalize paths
original_path = Path(original_path)
is_fantome = original_path.suffix.lower() == '.fantome'
# Get the base name without extension
base_name = original_path.stem
if is_fantome:
base_name = Path(base_name).stem
# Create new archive path
new_archive_path = parent_output_dir / original_archive
# Construct and verify source directory path
source_dir = Path("extracted_zips") / base_name
source_dir = source_dir.resolve() # Resolve to absolute path
print(f"Checking source directory: {source_dir}")
print(f"Directory exists: {source_dir.exists()}")
if not source_dir.exists():
# Try alternate path construction
alt_source_dir = Path("extracted_zips") / parent_dir / base_name
alt_source_dir = alt_source_dir.resolve()
print(f"Trying alternate path: {alt_source_dir}")
print(f"Alternate exists: {alt_source_dir.exists()}")
if alt_source_dir.exists():
source_dir = alt_source_dir
else:
print(f"Warning: Neither path exists for: {original_archive}")
print(f"Tried:\n1. {source_dir}\n2. {alt_source_dir}")
continue
# Skip if not in modified_files (when we have modifications)
if modified_files and str(source_dir) not in modified_files:
print(f"Skipping unmodified archive: {original_archive}")
continue
print(f"Creating archive: {new_archive_path}")
if is_fantome:
# Handle .fantome files
temp_zip_path = parent_output_dir / f"{base_name}.zip"
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
# Copy directories with explicit existence checks
meta_dir = source_dir / "META"
wad_dir = source_dir / "WAD"
if meta_dir.exists():
shutil.copytree(meta_dir, temp_dir_path / "META")
if wad_dir.exists():
shutil.copytree(wad_dir, temp_dir_path / "WAD")
# Create zip archive
shutil.make_archive(
str(temp_zip_path.with_suffix('')),
'zip',
root_dir=str(temp_dir_path),
base_dir='.'
)
# Rename to .fantome if zip was created
if temp_zip_path.exists():
temp_zip_path.rename(new_archive_path)
else:
# Handle regular zip files
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
# Copy directories with explicit existence checks
meta_dir = source_dir / "META"
wad_dir = source_dir / "WAD"
if meta_dir.exists():
shutil.copytree(meta_dir, temp_dir_path / "META")
if wad_dir.exists():
shutil.copytree(wad_dir, temp_dir_path / "WAD")
# Create zip archive
shutil.make_archive(
str(new_archive_path.with_suffix('')),
'zip',
root_dir=str(temp_dir_path),
base_dir='.'
)
print(f"Successfully created: {new_archive_path}")
except Exception as e:
print(f"Error processing {original_archive}: {str(e)}")
import traceback
traceback.print_exc()
print("\nCompression completed. Output files are in the ./output directory")
# ...existing code...print("All operations completed.")
print('='*50)
print("Made by @koobzaar")
print("Any issues or feedback, please contact support@darkseal.org")