Repository navigation
Expand file tree
/
Copy pathsource_position_runtime.py
More file actions
127 lines (110 loc) · 4.11 KB
/
Copy pathsource_position_runtime.py
File metadata and controls
127 lines (110 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import re
import shutil
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent
FFDEC_CLI = Path(r"C:\Program Files (x86)\FFDec\ffdec-cli.exe")
LEVEL_TEMPLATE = ROOT / "scratch" / "ffdec_export" / "scripts" / "level.as"
GAME_TEMPLATE = ROOT / "scratch" / "ffdec_export" / "scripts" / "Game.as"
CONTACT_LISTENER_TEMPLATE = ROOT / "scratch" / "copy_export" / "scripts" / "ContactListener.as"
PORT_DECLARATION_PATTERN = re.compile(
r"internal var aiSocketPort:Number = \d+;"
)
PORT_FALLBACK_PATTERN = re.compile(
r"aiSocketPort = \d+;"
)
def _slugify(value):
text = re.sub(r"[^a-zA-Z0-9_-]+", "-", str(value).strip()).strip("-")
return text or "default"
def _patched_level_source(port):
if not LEVEL_TEMPLATE.exists():
raise FileNotFoundError(f"Instrumented level template not found: {LEVEL_TEMPLATE}")
source = LEVEL_TEMPLATE.read_text(encoding="utf-8")
source, declaration_count = PORT_DECLARATION_PATTERN.subn(
f"internal var aiSocketPort:Number = {int(port)};",
source,
count=1,
)
if declaration_count != 1:
raise RuntimeError("Could not update aiSocketPort declaration in level.as")
source, fallback_count = PORT_FALLBACK_PATTERN.subn(
f"aiSocketPort = {int(port)};",
source,
count=1,
)
if fallback_count < 1:
raise RuntimeError("Could not update aiSocketPort fallback in level.as")
return source
def _patched_game_source(port):
if not GAME_TEMPLATE.exists():
raise FileNotFoundError(f"Instrumented Game template not found: {GAME_TEMPLATE}")
source = GAME_TEMPLATE.read_text(encoding="utf-8")
source, declaration_count = PORT_DECLARATION_PATTERN.subn(
f"internal var aiSocketPort:Number = {int(port)};",
source,
count=1,
)
if declaration_count != 1:
raise RuntimeError("Could not update aiSocketPort declaration in Game.as")
return source
def _contact_listener_source():
if not CONTACT_LISTENER_TEMPLATE.exists():
raise FileNotFoundError(
f"Fixed ContactListener template not found: {CONTACT_LISTENER_TEMPLATE}"
)
return CONTACT_LISTENER_TEMPLATE.read_text(encoding="utf-8")
def build_runtime_instrumented_swf(base_swf_path, runtime_dir, port, cache_key=None):
base_swf = Path(base_swf_path)
if not base_swf.exists():
raise FileNotFoundError(f"Base SWF not found: {base_swf}")
if not FFDEC_CLI.exists():
raise FileNotFoundError(f"FFDec CLI not found: {FFDEC_CLI}")
runtime_root = Path(runtime_dir) / "instrumented_swfs"
runtime_root.mkdir(parents=True, exist_ok=True)
stem = _slugify(cache_key or base_swf.stem)
output_path = runtime_root / f"{stem}-aiport-{int(port)}.swf"
template_mtime = max(
LEVEL_TEMPLATE.stat().st_mtime,
GAME_TEMPLATE.stat().st_mtime,
CONTACT_LISTENER_TEMPLATE.stat().st_mtime,
)
base_mtime = base_swf.stat().st_mtime
if output_path.exists():
output_mtime = output_path.stat().st_mtime
if output_mtime >= max(base_mtime, template_mtime):
return str(output_path)
temp_dir = Path(
tempfile.mkdtemp(
prefix=f"ffdec-port-{int(port)}-",
dir=str(runtime_root),
)
)
try:
(temp_dir / "level.as").write_text(_patched_level_source(port), encoding="utf-8")
(temp_dir / "Game.as").write_text(_patched_game_source(port), encoding="utf-8")
(temp_dir / "ContactListener.as").write_text(
_contact_listener_source(),
encoding="utf-8",
)
temp_output = temp_dir / output_path.name
command = [
str(FFDEC_CLI),
"-onerror",
"ignore",
"-importScript",
str(base_swf),
str(temp_output),
str(temp_dir),
]
subprocess.run(
command,
check=True,
capture_output=True,
text=True,
)
shutil.copy2(temp_output, output_path)
return str(output_path)
finally:
shutil.rmtree(temp_dir, ignore_errors=True)