Project Zomboid

Project Zomboid

C | 地图合集(更新中)
This topic has been locked
Fragger  [developer] 13 Jul, 2024 @ 2:36am
Python
详见指南
Last edited by Fragger; 13 Jul, 2024 @ 5:11am
< >
Showing 1-2 of 2 comments
Fragger  [developer] 13 Jul, 2024 @ 2:37am 
自动提取单元格信息
import os import re def format_name(name): name = re.sub(r'[^\w\s]', '', name) # 移除所有非字母和数字字符 name = name.replace('_', ' ') # 将下划线替换为空格 return name def read_file(filename): try: with open(filename, 'r', encoding='utf-8') as file: return file.readlines() except FileNotFoundError: return [] def write_to_file(filename, lines): with open(filename, 'a', encoding='utf-8') as file: file.writelines(lines) def process_directory(directory, folder_number): maps_path = os.path.join(directory, 'mods') if not os.path.exists(maps_path): return {} lotheader_files = {} for root, dirs, files in os.walk(maps_path): for file in files: if file.endswith(".lotheader"): folder_name = os.path.basename(root) if folder_name not in lotheader_files: lotheader_files[folder_name] = [] lotheader_files[folder_name].append(file) return {folder_number: lotheader_files} def check_for_conflicts(file_path): with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() blocks = {} current_location = None for line in lines: line = line.strip() if re.match(r'\d+_\d+', line): coords = line.split() for coord in coords: if coord in blocks: blocks[coord].add(current_location) else: blocks[coord] = {current_location} else: current_location = line conflicts = [] for coord, locations in blocks.items(): if len(locations) > 1: conflicts.append(f"{' && '.join(locations)}\n{coord}\n") return conflicts def main(): base_path = '模组文件路径' output_file = '输出路径\\Detection_auto.txt' conflict_folder = '输出路径\\conflicts_auto' conflict_file_path = os.path.join(conflict_folder, 'conflicts.txt') if not os.path.exists(conflict_folder): os.makedirs(conflict_folder) folder_numbers = [name for name in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, name))] formatted_lines = [] for folder_number in folder_numbers: target_directory = os.path.join(base_path, folder_number) if not os.path.isdir(target_directory): continue lotheader_files = process_directory(target_directory, folder_number) for folder_number, files_dict in lotheader_files.items(): for folder_name, files in sorted(files_dict.items()): folder_name_formatted = format_name(folder_name) folder_name_with_number = f'{folder_name_formatted}({folder_number})' formatted_lines.append(f'{folder_name_with_number}\n') file_names = ' '.join(sorted([os.path.splitext(file)[0] for file in files])) formatted_lines.append(f'{file_names}\n') formatted_lines.append('\n') # Add an empty line between different folder entries # 读取现有内容,保证新内容添加到末尾 existing_content = read_file(output_file) if existing_content: formatted_lines = ['\n'] + formatted_lines # Add a newline before the new content if file is not empty # 写入格式化后的内容到输出文件 write_to_file(output_file, formatted_lines) # 检查重复区块坐标 conflicts = check_for_conflicts(output_file) if conflicts: with open(conflict_file_path, 'w', encoding='utf-8') as conflict_file: conflict_file.writelines(conflicts) if __name__ == '__main__': main()
Last edited by Fragger; 13 Jul, 2024 @ 2:41am
Fragger  [developer] 13 Jul, 2024 @ 2:40am 
提取指定目录的单元格信息
import os import re import tkinter as tk from tkinter import simpledialog def format_name(name): name = re.sub(r'[^\w\s]', '', name) name = name.replace('_', ' ') return name def read_file(filename): with open(filename, 'r', encoding='utf-8') as file: return file.readlines() def write_to_file(filename, lines): with open(filename, 'a', encoding='utf-8') as file: file.writelines(lines) def process_directory(directory): maps_path = os.path.join(directory, 'mods') if not os.path.exists(maps_path): return {} lotheader_files = {} for root, dirs, files in os.walk(maps_path): for file in files: if file.endswith(".lotheader"): folder_name = os.path.basename(root) if folder_name not in lotheader_files: lotheader_files[folder_name] = [] lotheader_files[folder_name].append(file) return lotheader_files def check_for_conflicts(file_path): with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() blocks = {} current_location = None for line in lines: line = line.strip() if re.match(r'\d+_\d+', line): coords = line.split() for coord in coords: if coord in blocks: blocks[coord].add(current_location) else: blocks[coord] = {current_location} else: current_location = line conflicts = [] for coord, locations in blocks.items(): if len(locations) > 1: conflicts.append(f"{' && '.join(locations)}\n{coord}\n") return conflicts def process_folder(folder_number, base_path, output_file): target_directory = os.path.join(base_path, folder_number) if not os.path.isdir(target_directory): print(f"指定的文件夹 {folder_number} 不存在") return lotheader_files = process_directory(target_directory) if not lotheader_files: print(f"未找到 {folder_number} 的 .lotheader 文件") return existing_content = read_file(output_file) if existing_content: formatted_lines = ['\n'] else: formatted_lines = [] for folder_name, files in sorted(lotheader_files.items()): folder_name_formatted = format_name(folder_name) folder_name_with_number = f'{folder_name_formatted}({folder_number})' formatted_lines.append(f'{folder_name_with_number}\n') file_names = ' '.join(sorted([os.path.splitext(file)[0] for file in files])) formatted_lines.append(f'{file_names}\n') write_to_file(output_file, formatted_lines) conflicts = check_for_conflicts(output_file) if conflicts: conflict_file_path = f'输出文件路径\\conflict_{folder_number}.txt' with open(conflict_file_path, 'w', encoding='utf-8') as conflict_file: conflict_file.writelines(conflicts) def main(): base_path = '模组文件路径' output_file = '输出文件路径\\Detection.txt' root = tk.Tk() root.withdraw() folder_numbers = simpledialog.askstring("输入文件夹编号", "请输入文件夹编号(用逗号分隔):") if not folder_numbers: return folder_numbers = [num.strip() for num in folder_numbers.split(',')] for folder_number in folder_numbers: process_folder(folder_number, base_path, output_file) if __name__ == '__main__': main()
< >
Showing 1-2 of 2 comments
Per page: 1530 50