import os import shutil import zipfile def generate_mainscene(template_zip, output_dir, code_name, coach_count): extract_path = os.path.join(output_dir, "temp_extracted") generated_path = os.path.join(output_dir, f"{code_name}_MainScene") # Ensure output directory exists os.makedirs(output_dir, exist_ok=True) # Extract template with zipfile.ZipFile(template_zip, 'r') as zip_ref: zip_ref.extractall(extract_path) # Rename files and modify contents for root, dirs, files in os.walk(extract_path): for file in files: old_path = os.path.join(root, file) new_file_name = file.replace("${MapName}$", code_name) new_path = os.path.join(root, new_file_name) # Rename file if necessary if old_path != new_path: os.rename(old_path, new_path) # Replace content inside files with open(new_path, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() content = content.replace("${MapName}$", code_name) content = content.replace("${CoachCount}$", str(coach_count)) with open(new_path, 'w', encoding='utf-8') as f: f.write(content) # Move processed files to output directory shutil.move(extract_path, generated_path) print(f"MainScene generated at: {generated_path}") # Example usage generate_mainscene(r"M:\jd22pc\data\EngineData\MapTemplates\test\data\Classic", r"M:\jd22pc\data\EngineData\MapTemplates\test\output", "NewMap", 3)