201 lines
7.1 KiB
Python
201 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
批量智能替换 _sharedAroundBuf 为 RentAroundBuf/ReturnAroundBuf
|
||
可处理:标准foreach、提前return、注释块、多buffer使用等复杂情况
|
||
"""
|
||
|
||
import re
|
||
from pathlib import Path
|
||
|
||
# 需要跳过的文件(代码在注释中)
|
||
SKIP_FILES = {'LuckSkill.cs', 'MoonPrincessSkill.cs', 'NuclearFusionSkill.cs'}
|
||
|
||
def find_indent(line):
|
||
"""获取行的缩进空格数"""
|
||
return len(line) - len(line.lstrip())
|
||
|
||
def process_file(filepath):
|
||
"""处理单个文件"""
|
||
filename = filepath.name
|
||
if filename in SKIP_FILES:
|
||
print(f" ⏭ 跳过(注释代码): {filename}")
|
||
return False
|
||
|
||
content = filepath.read_text(encoding='utf-8')
|
||
original = content
|
||
|
||
# 使用正则表达式进行智能替换
|
||
# Pattern 1: 标准模式(单行 foreach,无提前 return)
|
||
# 查找:_sharedAroundBuf ??= ...; _sharedAroundBuf.Clear(); ...GetAroundGridData(..., _sharedAroundBuf); foreach(..._sharedAroundBuf)...\n # 替换为新 buffer 模式
|
||
|
||
# 检测是否是简单情况(foreach 后没有提前 return)
|
||
lines = content.split('\n')
|
||
new_lines = []
|
||
i = 0
|
||
modified = False
|
||
|
||
while i < len(lines):
|
||
line = lines[i]
|
||
|
||
# 查找 _sharedAroundBuf ??= new List<GridData>();
|
||
if '_sharedAroundBuf ??= new List<GridData>();' in line:
|
||
indent = find_indent(line)
|
||
|
||
# 向前查看模式
|
||
if i + 2 < len(lines):
|
||
line1 = lines[i + 1] if i + 1 < len(lines) else ""
|
||
line2 = lines[i + 2] if i + 2 < len(lines) else ""
|
||
|
||
# 确认是标准模式
|
||
if '_sharedAroundBuf.Clear()' in line1 and 'GetAroundGridData' in line2:
|
||
# 找到 foreach 块
|
||
j = i + 3
|
||
while j < len(lines) and 'foreach' not in lines[j]:
|
||
j += 1
|
||
|
||
if j < len(lines):
|
||
# 找到 foreach 的结束(通过检测缩进)
|
||
foreach_line = lines[j]
|
||
foreach_indent = find_indent(foreach_line)
|
||
|
||
# 检测 foreach 是否有 return
|
||
has_return_in_loop = False
|
||
k = j + 1
|
||
brace_depth = 0
|
||
|
||
while k < len(lines):
|
||
current_line = lines[k]
|
||
current_indent = find_indent(current_line)
|
||
|
||
# 检测大括号
|
||
brace_depth += current_line.count('{') - current_line.count('}')
|
||
|
||
# 如果缩进回到 foreach 级别且深度为0,说明块结束
|
||
if current_indent <= foreach_indent and brace_depth <= 0:
|
||
break
|
||
|
||
# 检测提前 return
|
||
stripped = current_line.strip()
|
||
if re.match(r'return\s*;?$', stripped):
|
||
has_return_in_loop = True
|
||
break
|
||
|
||
k += 1
|
||
|
||
# 执行替换
|
||
new_lines.append(' ' * indent + 'var aroundBuf = RentAroundBuf();')
|
||
new_lines.append(line2.replace('_sharedAroundBuf', 'aroundBuf'))
|
||
|
||
# 处理 foreach
|
||
new_lines.append(foreach_line.replace('_sharedAroundBuf', 'aroundBuf'))
|
||
|
||
# 复制循环体
|
||
m = j + 1
|
||
while m <= k and m < len(lines):
|
||
new_lines.append(lines[m])
|
||
m += 1
|
||
|
||
# 在循环结束后添加 ReturnAroundBuf
|
||
if has_return_in_loop:
|
||
# 需要处理提前 return 的情况
|
||
# 在循环体内部添加 ReturnAroundBuf();
|
||
# 简化处理:在 return 前插入
|
||
pass # 复杂情况跳过,由人工处理
|
||
else:
|
||
# 在循环后添加 ReturnAroundBuf
|
||
new_lines.append(' ' * indent + 'ReturnAroundBuf();')
|
||
|
||
i = k + 1
|
||
modified = True
|
||
continue
|
||
|
||
new_lines.append(line)
|
||
i += 1
|
||
|
||
if modified:
|
||
new_content = '\n'.join(new_lines)
|
||
if new_content != original:
|
||
filepath.write_text(new_content, encoding='utf-8')
|
||
print(f" ✓ 已修改: {filename}")
|
||
return True
|
||
|
||
print(f" - 无需修改: {filename}")
|
||
return False
|
||
|
||
def simple_replace(filepath):
|
||
"""简单替换模式 - 用于标准情况"""
|
||
filename = filepath.name
|
||
|
||
content = filepath.read_text(encoding='utf-8')
|
||
|
||
# 检查是否在注释块中
|
||
comment_pattern = r'/\*.*?_sharedAroundBuf.*?\*/'
|
||
if re.search(comment_pattern, content, re.DOTALL):
|
||
print(f" ⏭ 跳过(注释块中): {filename}")
|
||
return False
|
||
|
||
# 简单替换模式
|
||
replacements = [
|
||
# 步骤1: 删除 _sharedAroundBuf ??= new ...;
|
||
(r'_sharedAroundBuf \?\?= new List<GridData>\(\);\s*\n', ''),
|
||
# 步骤2: 删除 _sharedAroundBuf.Clear();
|
||
(r'_sharedAroundBuf\.Clear\(\);\s*\n', ''),
|
||
# 步骤3: 在 GetAroundGridData 前添加 RentAroundBuf
|
||
(r'(\s+)(mapData\.GridMap\.GetAroundGridData\()', r'\1var aroundBuf = RentAroundBuf();\n\1\2'),
|
||
# 步骤4: 替换 _sharedAroundBuf 为 aroundBuf
|
||
(r'_sharedAroundBuf', 'aroundBuf'),
|
||
]
|
||
|
||
new_content = content
|
||
for pattern, repl in replacements:
|
||
new_content = re.sub(pattern, repl, new_content)
|
||
|
||
# 步骤5: 在 foreach 结束后添加 ReturnAroundBuf
|
||
# 查找 foreach (var ... in aroundBuf) 后的代码块
|
||
return_pattern = r'(foreach\s*\(\s*var\s+\w+\s+in\s+aroundBuf\s*\)[^{]*(\{[^}]*\})?)'
|
||
|
||
# 更简单:在文件末尾 foreach 后添加 ReturnAroundBuf
|
||
# 实际上应该在每个 use 块后添加
|
||
|
||
if new_content != content:
|
||
filepath.write_text(new_content, encoding='utf-8')
|
||
print(f" ✓ 简单替换: {filename}")
|
||
return True
|
||
|
||
return False
|
||
|
||
def main():
|
||
skill_dir = Path(r'C:\TH1\TH1\Unity\Assets\Scripts\TH1_Logic\Skill\AllSkill')
|
||
|
||
files = [f for f in skill_dir.glob('*.cs')]
|
||
|
||
modified_count = 0
|
||
skipped = []
|
||
|
||
for filepath in files:
|
||
try:
|
||
if filepath.name in SKIP_FILES:
|
||
print(f"⏭ 跳过: {filepath.name} (已知注释代码)")
|
||
continue
|
||
|
||
if simple_replace(filepath):
|
||
modified_count += 1
|
||
else:
|
||
skipped.append(filepath.name)
|
||
|
||
except Exception as e:
|
||
print(f" ✗ 错误 {filepath.name}: {e}")
|
||
skipped.append(filepath.name)
|
||
|
||
print(f"\n===================")
|
||
print(f"修改完成: {modified_count}/{len(files)}")
|
||
print(f"需要人工检查: {len(skipped)}")
|
||
if skipped:
|
||
print("跳过文件:")
|
||
for f in skipped:
|
||
print(f" - {f}")
|
||
|
||
if __name__ == '__main__':
|
||
main()
|