Repository navigation
Expand file tree
/
Copy pathgenerate_java.py
More file actions
233 lines (206 loc) · 9.75 KB
/
Copy pathgenerate_java.py
File metadata and controls
233 lines (206 loc) · 9.75 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Read my_flows.json and write a drop-in FlowTemplates.java for MicroBot.
The generated file replaces:
runelite-client/src/main/java/net/runelite/client/plugins/microbot/
util/mouse/naturalmouse/util/FlowTemplates.java
Every factory in FactoryTemplates.java (granny, averageComputerUser,
normalGamer, fastGamer, superFastGamer) pulls flow arrays from static
methods on FlowTemplates, so overwriting that one file is sufficient —
no other Java file needs to change.
Recorded flows are assigned to named methods in this order:
- slowStartupFlow (first flow — often the gentlest acceleration)
- slowStartup2Flow (second)
- variatingFlow (third)
- adjustingFlow (fourth)
- interruptedFlow (fifth)
- interruptedFlow2 (sixth)
- jaggedFlow (seventh)
- stoppingFlow (eighth)
- random (ninth — replaced by a fixed recorded flow)
If fewer than 9 flows were recorded the remaining slots fall back to
the original hardcoded values so the file always compiles.
"""
import json
import textwrap
from pathlib import Path
ROOT = Path(__file__).resolve().parent
FLOWS_JSON = ROOT / "microbot-exports" / "my_flows.json"
OUTPUT_JAVA = ROOT / "microbot-exports" / "FlowTemplates.java"
# ---------------------------------------------------------------------------
# Original hardcoded fallbacks (verbatim from upstream FlowTemplates.java)
# Used when not enough recorded flows are available.
# ---------------------------------------------------------------------------
FALLBACKS = {
"variatingFlow": [
10, 13, 14, 19, 16, 13, 15, 22, 56, 90, 97, 97, 66, 51, 50, 66, 91, 95, 87, 96,
98, 88, 70, 62, 57, 63, 79, 93, 98, 97, 100, 104, 83, 49, 37, 53, 68, 73, 61, 51,
64, 107, 103, 111, 94, 88, 95, 86, 88, 97, 108, 85, 86, 74, 72, 73, 58, 50, 50,
60, 62, 61, 52, 53, 44, 30, 21, 25, 21, 17, 16, 13, 8, 2, 6, 9, 6, 3, 7, 12,
13, 15, 11, 9, 9, 7, 6, 4, 1, 2, 3, 2, 2, 11, 15, 7, 1, 0, 0, 1,
],
"interruptedFlow": [
12, 11, 10, 20, 24, 19, 26, 15, 9, 9, 10, 24, 26, 30, 24, 49, 72, 60, 81, 113,
82, 99, 67, 10, 7, 7, 7, 10, 8, 7, 9, 6, 6, 7, 10, 11, 12, 8, 7, 3, 0, 2,
8, 10, 10, 12, 6, 4, 4, 3, 8, 11, 11, 11, 11, 13, 11, 20, 25, 18, 21, 23, 56,
40, 36, 58, 69, 60, 63, 51, 87, 71, 86, 66, 115, 97, 80, 65, 50, 66, 57, 24, 11,
11, 7, 3, 0, 0, 1, 3, 3, 5, 6, 12, 11, 7, 11, 17, 17, 23,
],
"interruptedFlow2": [
12, 11, 10, 20, 24, 19, 26, 15, 9, 9, 10, 24, 26, 30, 24, 49, 72, 60, 81, 113,
82, 99, 67, 10, 12, 8, 11, 15, 16, 17, 17, 12, 16, 37, 10, 25, 12, 11, 41, 10,
12, 11, 40, 36, 52, 61, 60, 64, 51, 82, 71, 81, 66, 105, 92, 59, 65, 51, 66, 54,
21, 21, 12, 40, 36, 58, 69, 60, 63, 51, 87, 71, 86, 66, 115, 97, 80, 65, 50, 66,
57, 24, 11, 11, 7, 3, 0, 0, 1, 3, 3, 5, 6, 12, 11, 7, 11, 17, 17, 23,
],
"slowStartupFlow": [
8, 5, 1, 1, 1, 2, 2, 3, 3, 3, 5, 7, 9, 10, 10, 11, 11, 11, 12, 12, 13,
15, 14, 13, 15, 15, 17, 17, 18, 18, 20, 19, 20, 20, 19, 20, 19, 20, 21, 22, 20,
17, 20, 22, 18, 20, 21, 18, 20, 20, 18, 20, 19, 21, 19, 19, 19, 19, 20, 19, 20,
21, 19, 19, 17, 21, 21, 17, 19, 18, 20, 18, 19, 24, 34, 43, 35, 40, 41, 42, 42,
38, 40, 40, 37, 36, 42, 40, 63, 85, 98, 92, 103, 102, 95, 86, 70, 52, 31, 19,
],
"slowStartup2Flow": [
7, 2, 1, 2, 2, 3, 5, 9, 10, 10, 11, 13, 13, 10, 4, 1, 1, 2, 3, 4, 6,
9, 11, 11, 10, 14, 11, 9, 2, 1, 2, 2, 3, 4, 8, 9, 10, 11, 11, 13, 13, 15,
14, 15, 18, 17, 19, 21, 20, 19, 18, 20, 20, 20, 20, 19, 20, 19, 19, 18, 20, 20,
19, 20, 18, 20, 21, 19, 21, 18, 19, 25, 37, 37, 35, 41, 43, 41, 41, 40, 48, 81,
108, 91, 88, 74, 46, 19, 46, 84, 35, 14, 19, 12, 13, 18, 38, 35, 11, 4,
],
"jaggedFlow": [
52, 106, 122, 8, 6, 117, 32, 2, 68, 34, 21, 81, 61, 86, 55, 4, 104, 21, 51, 8,
93, 90, 43, 65, 82, 31, 40, 115, 107, 13, 35, 73, 81, 67, 31, 79, 57, 100, 55,
64, 13, 54, 18, 68, 82, 61, 11, 84, 37, 20, 68, 33, 36, 55, 68, 75, 56, 20, 41,
120, 63, 72, 102, 49, 4, 48, 69, 50, 35, 49, 54, 19, 95, 121, 26, 78, 31, 62,
53, 123, 73, 22, 39, 72, 98, 33, 26, 5, 103, 23, 75, 35, 69, 33, 44, 12, 10,
101, 122, 19,
],
"stoppingFlow": [
8, 20, 39, 48, 66, 71, 79, 57, 29, 5, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 10, 12, 15, 19,
37, 60, 100, 103, 98, 82, 87, 74, 65, 51, 57, 54, 61, 46, 38, 16,
],
"adjustingFlow": [
1, 1, 1, 3, 8, 7, 2, 2, 4, 8, 6, 3, 7, 13, 18, 19, 24, 35, 26, 14, 31,
43, 49, 55, 61, 67, 61, 50, 43, 37, 30, 16, 5, 4, 4, 3, 3, 3, 4, 4, 3,
2, 2, 3, 10, 14, 10, 7, 5, 5,
],
"constantSpeed": [100.0] * 10,
}
# Assignment order: recorded flows are slotted into these method names in order.
SLOT_ORDER = [
"slowStartupFlow",
"slowStartup2Flow",
"variatingFlow",
"adjustingFlow",
"interruptedFlow",
"interruptedFlow2",
"jaggedFlow",
"stoppingFlow",
"random",
]
def _fmt_array(values, indent=8):
"""Format a double[] literal, wrapping at ~100 chars."""
nums = [str(round(float(v), 2)) for v in values]
prefix = " " * indent
lines = []
current = prefix
for i, n in enumerate(nums):
sep = ", " if i < len(nums) - 1 else ""
candidate = current + n + sep
if len(candidate) > 100 and current.strip():
lines.append(current.rstrip(", "))
current = prefix + n + sep
else:
current = candidate
if current.strip():
lines.append(current)
return ",\n".join(lines) if len(lines) > 1 else lines[0] if lines else prefix
def generate(flows_json_path=FLOWS_JSON, output_path=OUTPUT_JAVA):
flows_json_path = Path(flows_json_path)
output_path = Path(output_path)
if not flows_json_path.exists():
raise FileNotFoundError(
f"{flows_json_path} not found. Run 'Compile Flows.bat' first.")
with flows_json_path.open(encoding="utf-8") as f:
data = json.load(f)
recorded = data.get("flows", [])
n_recorded = len(recorded)
stats = data.get("stats", {})
# Build method bodies: recorded first, fallback for the rest.
methods = {}
for i, name in enumerate(SLOT_ORDER):
if i < n_recorded:
arr = recorded[i]
source_comment = f"// Recorded from {stats.get('total_move_events', '?')} mouse events"
else:
arr = FALLBACKS.get(name, FALLBACKS["constantSpeed"])
source_comment = "// Upstream fallback (not enough recorded data)"
if name == "random":
# 'random' in upstream generates a random array at runtime; we replace
# it with a fixed recorded profile so it is deterministic but human-derived.
method_comment = (
"// Originally generated random values at runtime.\n"
" // Replaced with a recorded human profile for consistency."
)
else:
method_comment = ""
methods[name] = (arr, source_comment, method_comment)
# Render each static method.
def render_method(name, arr, source_comment, method_comment):
body_lines = [f" public static double[] {name}() {{"]
if method_comment:
for line in method_comment.splitlines():
body_lines.append(f" {line}")
body_lines.append(f" {source_comment}")
body_lines.append(f" return new double[]{{")
body_lines.append(_fmt_array(arr, indent=16))
body_lines.append(f" }};")
body_lines.append(f" }}")
return "\n".join(body_lines)
# Count how many flows came from recordings vs fallback.
n_fallback = max(0, len(SLOT_ORDER) - n_recorded)
file_comment = textwrap.dedent(f"""\
/**
* Generated by MouseRecorder/generate_java.py
*
* Recorded flows : {n_recorded} (from {stats.get('total_move_events', '?')} desktop mouse events
* across {stats.get('recording_files', '?')} session(s))
* Fallback flows : {n_fallback} (original upstream values)
*
* To regenerate: run Compile Flows.bat in MouseRecorder\\
*
* Drop this file into:
* runelite-client/src/main/java/net/runelite/client/plugins/microbot/
* util/mouse/naturalmouse/util/FlowTemplates.java
* then rebuild MicroBot (mvn package -pl runelite-client -am -DskipTests).
*/""")
java = textwrap.dedent("""\
package net.runelite.client.plugins.microbot.util.mouse.naturalmouse.util;
import java.util.Arrays;
{file_comment}
public class FlowTemplates {{
{methods}
public static double[] constantSpeed() {{
double[] flowBuckets = new double[10];
Arrays.fill(flowBuckets, 100);
return flowBuckets;
}}
}}
""").format(
file_comment=file_comment,
methods="\n\n".join(
render_method(name, *methods[name]) for name in SLOT_ORDER
),
)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(java, encoding="utf-8")
return output_path, n_recorded, n_fallback
if __name__ == "__main__":
path, n_rec, n_fb = generate()
print(f"Written: {path}")
print(f" {n_rec} recorded flow(s), {n_fb} fallback(s)")
print(f"\nDrop this file into MicroBot at:")
print(" runelite-client/src/main/java/net/runelite/client/plugins/microbot/")
print(" util/mouse/naturalmouse/util/FlowTemplates.java")
print("then rebuild: mvn package -pl runelite-client -am -DskipTests")