forked from ptklatt/UtinniPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.html
More file actions
373 lines (306 loc) · 11.9 KB
/
Copy pathpatterns.html
File metadata and controls
373 lines (306 loc) · 11.9 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Patterns — UtinniPlugins</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="site">
<h1><span class="accent">UtinniPlugins</span> — Idiomatic Utinni plugin design</h1>
<div class="sub">Cross-cutting conventions distilled from The Jawa Toolbox.</div>
</header>
<nav class="top">
<a href="index.html">Overview</a>
<a href="jawa-toolbox.html">The Jawa Toolbox</a>
<a href="patterns.html" class="active">Patterns</a>
<a href="sytners.html">Sytner's plugin</a>
<a href="../../Utinni/docs/index.html">↗ Utinni framework docs</a>
</nav>
<main>
<h1>Plugin patterns</h1>
<p>
Conventions distilled from <a href="jawa-toolbox.html">The Jawa Toolbox</a>. Adopting
them gives you a clean, idiomatic Utinni plugin.
</p>
<h2>1. Composition over inheritance</h2>
<p>
JT has zero deep class hierarchies. Every feature area is a top-level
<code><Feature>Impl</code> class plus one or more <code>SubPanel</code>s. The plugin class
(<code>TheJawaToolboxPlugin</code>) is <strong>a composition root</strong> — it instantiates each
<code>Impl</code>, hands them the shared settings/hotkey manager, and exposes them to
<code>FormMain</code> via <code>GetSubPanels()</code> / <code>GetForms()</code> / <code>GetStandalonePanels()</code>.
</p>
<pre><code>public class MyPlugin : IEditorPlugin
{
private readonly FeatureAImpl a;
private readonly FeatureBImpl b;
private readonly FeatureCImpl c;
public MyPlugin()
{
// shared state
var ini = new UtINI("settings.ini");
var hk = new HotkeyManager(false);
// wire features
a = new FeatureAImpl(ini, hk);
b = new FeatureBImpl(ini, hk, a); // B uses A
c = new FeatureCImpl(ini, hk);
hk.CreateSettings();
ini.Load();
}
// ...
}
</code></pre>
<p>
Why this matters: features are independently testable, easy to remove, and
their dependencies are explicit (you see exactly which features touch
which).
</p>
<h2>2. One <code>*Impl</code> per feature; panels are thin</h2>
<p>
A <code>SubPanel</code> should be presentation only — controls and event handlers that
forward to <code>Impl</code> methods. All callback registration, hotkey setup, undo
emission, and settings I/O happens in <code>Impl</code>.
</p>
<pre><code>// SnapshotPanel.cs (thin)
public class SnapshotPanel : SubPanel
{
private readonly WorldSnapshotImpl impl;
public SnapshotPanel(WorldSnapshotImpl impl) : base("Snapshot", true)
{
this.impl = impl;
// ... build controls
btnSave.Click += (s,e) => impl.Save();
btnAdd.Click += (s,e) => impl.AddNode(txtFile.Text);
gizmoTranslate.Click += (s,e) => impl.SetOperationModeToTranslate();
}
}
</code></pre>
<p>
This means <strong>swapping the UI</strong> (e.g. adding a second SubPanel that exposes
the same features differently) doesn't touch the business logic at all.
</p>
<h2>3. Marshal everything</h2>
<p>
The single most common bug in new plugins is calling a game API on the UI
thread, or touching WinForms from a callback. The rule is:
</p>
<table>
<thead>
<tr><th>You're on the…</th><th>You want to call a…</th><th>Do this</th></tr>
</thead>
<tbody>
<tr><td>UI thread (button, hotkey)</td><td>game API</td><td><code>GameCallbacks.AddMainLoopCall(() => Game.Foo())</code> or <code>GroundSceneCallbacks.AddUpdateLoopCall(...)</code></td></tr>
<tr><td>game thread (callback)</td><td>WinForms control</td><td><code>myControl.BeginInvoke((MethodInvoker)(() => myControl.Text = ...))</code></td></tr>
<tr><td>Background <code>Task</code></td><td>game API</td><td>enqueue a callback, await it via <code>TaskCompletionSource</code></td></tr>
<tr><td>Background <code>Task</code></td><td>WinForms control</td><td><code>Invoke</code> to UI</td></tr>
</tbody>
</table>
<p>
JT does this consistently — every <code>Impl</code> method that touches the game wraps
the call in a callback enqueue. Adopt the same style and a whole category of
bugs disappears.
</p>
<h2>4. Hotkeys with scope flags + dynamic enable</h2>
<p>
Define hotkeys once in the <code>Impl</code> constructor with <code>Name = "MyPlugin.X"</code>
prefixes. Use the scope flags:
</p>
<ul>
<li><code>OnGameFocusOnly</code> — most editor shortcuts should set this true (otherwise
they fire while the user is typing in a textbox in the side panel).</li>
<li><code>OverrideGameInput</code> — set true for hotkeys that should not also feed
through to SWG's input map (e.g. saving with <code>Ctrl+S</code> should not send <code>S</code>
to the chat window). It's not frame-perfect — for hard guarantees, call
<code>Client.SuspendInput()</code> / <code>ResumeInput()</code> directly in your handler.</li>
</ul>
<p>For context-sensitive shortcuts, toggle <code>Enabled</code> from a state callback:</p>
<pre><code>private void OnGizmoEnabled() =>
hotkeys.Hotkeys["MyPlugin.GizmoTranslate"].Enabled = true;
private void OnGizmoDisabled() =>
hotkeys.Hotkeys["MyPlugin.GizmoTranslate"].Enabled = false;
</code></pre>
<h2>5. Undo via events, never via the stack</h2>
<p>
Never touch <code>UndoRedoManager</code> directly from a plugin. Always raise
<code>IEditorPlugin.AddUndoCommand</code>:
</p>
<pre><code>owner.AddUndoCommand?.Invoke(owner,
new AddUndoCommandEventArgs(new MyCommand(before, after)));
</code></pre>
<p>
For continuous edits (gizmo drag, slider drag) capture <code>before</code> on the
edit-start callback (<code>OnGizmoEnabled</code>, <code>Slider.MouseDown</code>), capture <code>after</code>
on the edit-end callback (<code>OnGizmoDisabled</code>, <code>Slider.MouseUp</code>), and push
<strong>once</strong> with the pair.
</p>
<p>
For multi-object batched edits, write one <code>IUndoCommand</code> that holds a list
of (object, before, after) tuples — see the <code>Commands/WorldSnapshotCommands</code>
built-ins as the contract.
</p>
<h2>6. Settings as composition root</h2>
<p>
One <code>UtINI</code> per plugin, passed to every feature. Each feature owns its own
INI section name and seeds its defaults in <code>CreateSettings()</code>:
</p>
<pre><code>public class FeatureAImpl
{
public FeatureAImpl(UtINI ini, HotkeyManager hk)
{
ini.AddSetting("FeatureA", "defaultThing", "naboo", VtString);
ini.AddSetting("FeatureA", "autoStart", "false", VtBool);
// ...
}
}
</code></pre>
<p>
<code>AddSetting</code> is idempotent — re-adding doesn't clobber an existing value.
So features can call it from every constructor and the user's edits in
<code>settings.ini</code> are preserved.
</p>
<h2>7. Async polling for live UI</h2>
<p>
When you want a label to update every frame (player position, time-of-day,
free-cam speed), don't poll from a WinForms timer — you'll either poll too
slow or too fast. Instead:
</p>
<pre><code>public FeatureImpl()
{
Task.Run(UpdateView);
}
private async Task UpdateView()
{
while (!ShouldStop)
{
await Task.Delay(50); // 20 Hz
var tcs = new TaskCompletionSource<float>();
GroundSceneCallbacks.AddUpdateLoopCall(() =>
tcs.SetResult(Terrain.Get().GetTimeOfDay()));
var tod = await tcs.Task;
OnTodUpdated?.Invoke(tod); // panel subscribes, marshals to UI
}
}
</code></pre>
<p>Or simpler — re-enqueue an update-loop call each frame:</p>
<pre><code>void OnUpdate()
{
var t = Terrain.Get().GetTimeOfDay();
panel.BeginInvoke((MethodInvoker)(() => panel.UpdateTod(t)));
GroundSceneCallbacks.AddUpdateLoopCall(OnUpdate);
}
GroundSceneCallbacks.AddUpdateLoopCall(OnUpdate);
</code></pre>
<p>
Pick whichever feels cleaner; the JT mostly uses the <code>Task.Delay</code> pattern
for 10–20 Hz UI refresh.
</p>
<h2>8. Drag-drop as asset placement</h2>
<p>
The Object Browser's drag-drop pattern is general-purpose. Adapt it for any
"drag from a listbox / treeview, drop into the live game world":
</p>
<div class="mermaid">
sequenceDiagram
participant U as User
participant L as ListBox
participant DD as GameDragDropEventHandlers
participant W as Game world
participant S as Snapshot impl
U->>L: mousedown on item
L->>L: capture filename
U->>L: mousemove (with LMB)
L->>L: DoDragDrop(filename)
U->>W: drag over PanelGame
W->>DD: OnDragEnter fires
DD->>W: create temporary preview Object, addObjectNotifications
U->>W: drag-move
DD->>W: collide cursor → world point; reposition preview
U->>W: drop
DD->>S: snapshotImpl.AddNodeAt(previewPosition, filename)
DD->>W: destroy preview object
</div>
<p>
The trick is <strong>showing live feedback</strong> during drag (the preview object
follows the cursor) so the user knows what they're placing and where.
</p>
<h2>9. C++ shim only when necessary</h2>
<p>Don't write a C++ half unless you need one of:</p>
<ul>
<li><strong>Chat slash commands</strong> (<code>/foo</code>) — must register at <code>CuiChatWindow::ctor</code>
time, which is before the CLR is ready.</li>
<li><strong>A new detour</strong> that UtinniCore doesn't already wrap — you're adding a
<code>swg/<subsystem>/</code> entry.</li>
<li><strong>Per-frame work that's prohibitively expensive in managed code</strong> — rare;
the bridge is fast.</li>
</ul>
<p>
In every other case, write a <code>IPlugin</code> / <code>IEditorPlugin</code> in C# and use the
existing callbacks. If you do need C++, keep it to plugin scaffolding +
forwarding to managed events (e.g. via <code>EventHandler<T></code> raised on a shared
static instance).
</p>
<h2>10. Disable controls until a scene is loaded</h2>
<p>
Most editor controls only make sense inside a scene. Wire enable/disable on
<code>GameCallbacks</code>:
</p>
<pre><code>public MySubPanel()
{
GameCallbacks.AddSetupSceneCall(() =>
this.BeginInvoke((MethodInvoker)(() => SetEnabled(true))));
GameCallbacks.AddCleanupSceneCall(() =>
this.BeginInvoke((MethodInvoker)(() => SetEnabled(false))));
SetEnabled(false); // default
}
private void SetEnabled(bool enabled)
{
foreach (Control c in Controls)
if (c is UtinniButton or UtinniNumericUpDown or UtinniTextbox)
c.Enabled = enabled;
}
</code></pre>
<p>Better UX, fewer null-deref bugs in your handlers.</p>
<h2>11. Name things with plugin prefixes</h2>
<p>
For hotkeys, INI sections, and anything else that might collide with other
plugins, prefix:
</p>
<ul>
<li>Hotkey <code>Name</code> → <code>MyPlugin.SaveScene</code> not <code>SaveScene</code>.</li>
<li>Logger callouts → <code>[MyPlugin] ...</code> (use <code>Log.Info("[MyPlugin] " + msg)</code> or
rely on the <code>writeClassName</code> config flag).</li>
</ul>
<p>
This is also why <code>[Plugins] plugin_N</code> in <code>ut.ini</code> is keyed by <em>directory
name</em> — that directory name is your plugin's namespace.
</p>
<h2>12. Build outputs into <code>Plugins/<Name>/</code></h2>
<p>
The Directory.Build.props the VSIX wizard writes does this for you. If
you're hand-rolling a project:
</p>
<pre><code><PropertyGroup>
<OutputPath>$(SolutionDir)bin\$(Configuration)\Plugins\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
</code></pre>
<p>
And reference <code>UtinniCoreDotNet.dll</code> with <code>Private=False</code> so you don't
shadow the install's copy.
</p>
<h2>See also</h2>
<ul>
<li><a href="jawa-toolbox.html">The Jawa Toolbox</a> — the worked example these patterns
come from.</li>
<li><a href="../../Utinni/docs/index.html">Utinni docs</a> — the framework these
patterns sit on top of.</li>
</ul>
</main>
<footer>
UtinniPlugins — official plugins for the Utinni modding framework.
</footer>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({ startOnLoad: true, theme: "dark" });</script>
</body>
</html>