import os
import re
import chardet
def process_cpp_files(directory):
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.cpp'):
file_path = os.path.join(root, file)
process_file(file_path)
def process_file(file_path):
# Detect encoding
with open(file_path, 'rb') as f:
rawdata = f.read()
encoding = chardet.detect(rawdata)['encoding']
# Convert to UTF-8 if not already
if encoding and encoding.lower() != 'utf-8':
with open(file_path, 'r', encoding=encoding) as f:
content = f.read()
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
modified_lines = []
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
modified_line = process_line(line)
modified_lines.append(modified_line)
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(modified_lines)
def process_line(line):
if 'ChatPacket(CHAT_TYPE_INFO, "' in line:
# Using regular expression to find and replace the second argument
modified_line = re.sub(r'(ChatPacket\(CHAT_TYPE_INFO, )("[^"]+")', r'\1LC_TEXT(\2)', line)
return modified_line
else:
return line
if __name__ == "__main__":
process_cpp_files("...\\source\\server\\game\\src")
print("Processing complete.")