76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
import os
|
|
import re
|
|
|
|
GAMES_DIR = os.path.expanduser("~/Project/basic1/games/lua_examples")
|
|
|
|
def fix_file(filepath):
|
|
with open(filepath, 'r') as f:
|
|
content = f.read()
|
|
|
|
original_content = content
|
|
|
|
# 1. Fix corrupted function arguments (removing the inserted ", 2")
|
|
# game.width(, 2) -> game.width()
|
|
content = re.sub(r'game\.width\(\s*,\s*2\s*\)', 'game.width()', content)
|
|
content = re.sub(r'game\.height\(\s*,\s*2\s*\)', 'game.height()', content)
|
|
|
|
# tostring(val, 2) -> tostring(val)
|
|
# This regex looks for tostring(..., 2) where ... is not empty
|
|
content = re.sub(r'tostring\(([^,]+),\s*2\s*\)', r'tostring(\1)', content)
|
|
|
|
# math.floor(val, 2) -> math.floor(val)
|
|
content = re.sub(r'math\.floor\(([^,]+),\s*2\s*\)', r'math.floor(\1)', content)
|
|
|
|
# 2. Fix renderer.text_scaled calls that are missing the scale argument
|
|
# The bad regex changed renderer.text(x, y, txt, on) -> renderer.text_scaled(x, y, txt, on)
|
|
# It should be renderer.text_scaled(x, y, txt, on, 2)
|
|
|
|
# We look for renderer.text_scaled calls that end with ", true)" or ", false)"
|
|
# and verify they don't already have a 5th argument.
|
|
|
|
def add_scale_arg(match):
|
|
full_match = match.group(0)
|
|
# Check if it already ends with ", 2)" or similar integer
|
|
if re.search(r',\s*\d+\s*\)$', full_match):
|
|
return full_match
|
|
|
|
# It ends in boolean, so append ", 2"
|
|
return full_match.replace(')', ', 2)')
|
|
|
|
# Regex for renderer.text_scaled(..., true) or (..., false)
|
|
# We use a pattern that matches the function call until the closing paren
|
|
# This is tricky with nested parens, but usually text calls are simple.
|
|
# Let's match line-by-line or simple calls.
|
|
|
|
lines = content.split('\n')
|
|
new_lines = []
|
|
for line in lines:
|
|
if 'renderer.text_scaled' in line:
|
|
# If line ends with "true)" or "false)", append ", 2)"
|
|
# Be careful not to double add if I run this script twice.
|
|
# And handle cases where it might be `true)` with spaces.
|
|
|
|
# Simple check: does it end with `true)` or `false)`?
|
|
# And does it NOT have `, 2)`?
|
|
if re.search(r',\s*(true|false)\s*\)', line):
|
|
line = re.sub(r'\)\s*$', ', 2)', line)
|
|
new_lines.append(line)
|
|
|
|
content = '\n'.join(new_lines)
|
|
|
|
if content != original_content:
|
|
print(f"Fixing {filepath}...")
|
|
with open(filepath, 'w') as f:
|
|
f.write(content)
|
|
return True
|
|
return False
|
|
|
|
print(f"Scanning {GAMES_DIR}...")
|
|
count = 0
|
|
for filename in os.listdir(GAMES_DIR):
|
|
if filename.endswith(".lua"):
|
|
if fix_file(os.path.join(GAMES_DIR, filename)):
|
|
count += 1
|
|
|
|
print(f"Fixed {count} files.")
|