Repository navigation
Expand file tree
/
Copy pathCoreOptions.cs
More file actions
382 lines (312 loc) · 14.6 KB
/
Copy pathCoreOptions.cs
File metadata and controls
382 lines (312 loc) · 14.6 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
374
375
376
377
378
379
380
381
382
namespace Live2DPet.App;
/// <summary>
/// The command line of the core engine.
///
/// Everything here is either something the pet needs to run, or a way to measure
/// it. There is no editor, no pipe, no voice and no rules file, so there are no
/// flags for them: this file is the shape of the cut.
/// </summary>
internal sealed class CoreOptions
{
public string? ModelPath { get; private set; } = FindDefaultModel();
public bool ModelSpecified { get; private set; }
public int Width { get; private set; } = 640;
public int Height { get; private set; } = 900;
public bool TopMost { get; private set; } = true;
public bool Transparent { get; private set; } = true;
/// <summary>What the pet is drawn at, as a fraction of the window: the lever
/// the governor pulls. `--render-scale` pins it.</summary>
public float RenderScale { get; private set; } = 1f;
public bool RenderScalePinned { get; private set; }
/// <summary>The ceiling, in FPS. Zero means none at all; the default is the
/// display's own rate, which is what the eye can actually see.</summary>
public double TargetFps { get; private set; }
public bool FrameCapSpecified { get; private set; }
/// <summary>The floor the quality governor defends. This is the promise: at 60
/// the pet gives up render quality rather than frames.</summary>
public double MinFps { get; private set; } = 60;
public bool MinFpsSpecified { get; private set; }
public bool Governor { get; private set; } = true;
/// <summary>Which lever automatic mode spends first: frames or picture.
/// <c>--auto-plan quality|framerate</c>; the default is the same as the
/// full engine's.</summary>
public AutoPlan Plan { get; private set; } = AutoPlan.Quality;
public bool PlanSpecified { get; private set; }
/// <summary>Keep atlas pages on disk until something on them is visible.
/// <c>--lazy-textures</c>.</summary>
public bool LazyTextures { get; private set; }
/// <summary>The largest a texture page may be, in pixels on its long edge;
/// zero keeps the model's own size. <c>--texture-size</c>.</summary>
public int TextureSize { get; private set; }
public bool TextureSizeSpecified { get; private set; }
/// <summary>Parts to leave out, matched by part or drawable name.</summary>
public List<string> HiddenParts { get; } = [];
/// <summary>UV patches to leave out, in <c>drawable:u0,v0,u1,v1</c> form.</summary>
public List<string> DropUv { get; } = [];
public static int NormalizeTextureSize(int value) =>
value <= 0 ? 0 : Math.Clamp(value, 256, 8192);
public bool Batching { get; private set; } = true;
public bool SoftwareRasterizer { get; private set; }
public bool DebugLayer { get; private set; }
public bool UseDirectComposition { get; private set; } = true;
public bool ShapeWindow { get; private set; } = true;
public bool Tray { get; private set; } = true;
public bool ClickThrough { get; private set; }
public bool FocusFollowPointer { get; private set; } = true;
public bool SkipModelPass { get; private set; }
public bool SkipRenderPass { get; private set; }
public bool SkipClippingPass { get; private set; }
public bool SkipPresent { get; private set; }
public bool WaitForVBlank { get; private set; } = true;
public bool DebugMasks { get; private set; }
public bool MaskEveryFrame { get; private set; }
public bool NoMotion { get; private set; }
public bool NoEffects { get; private set; }
public bool NoPhysics { get; private set; }
public int ThreadPriority { get; private set; } = Platform.Win32.ThreadPriorityAboveNormal;
public int CpuAffinity { get; private set; }
public int ExitAfterSeconds { get; private set; }
public int CaptureFrame { get; private set; } = 10;
public string? CapturePath { get; private set; }
public string? LogPath { get; private set; }
public string? StatePath { get; private set; }
public (int X, int Y)? WindowPosition { get; private set; }
public float? LookX { get; private set; }
public float? LookY { get; private set; }
public int HitTestGrid { get; private set; }
/// <summary>
/// Where a model is looked for when `--model` is not given: the first
/// `*.model3.json` under a `models` folder beside the executable, then under
/// one in the working directory. Null when there is none, which the runner
/// reports rather than guessing.
/// </summary>
private static string? FindDefaultModel()
{
foreach (var root in new[] { AppContext.BaseDirectory, Environment.CurrentDirectory })
{
var folder = Path.Combine(root, "models");
if (!Directory.Exists(folder))
{
continue;
}
var found = Directory
.EnumerateFiles(folder, "*.model3.json", SearchOption.AllDirectories)
.OrderBy(path => path, StringComparer.OrdinalIgnoreCase)
.FirstOrDefault();
if (found is not null)
{
return found;
}
}
return null;
}
public static CoreOptions Parse(string[] args)
{
var options = new CoreOptions();
var i = 0;
string? Next() => i + 1 < args.Length ? args[++i] : null;
for (; i < args.Length; i++)
{
switch (args[i])
{
case "--model":
options.ModelPath = Next() ?? options.ModelPath;
options.ModelSpecified = true;
break;
case "--width":
options.Width = int.TryParse(Next(), out var width) ? Math.Max(64, width) : options.Width;
break;
case "--height":
options.Height = int.TryParse(Next(), out var height) ? Math.Max(64, height) : options.Height;
break;
case "--fps":
options.TargetFps = double.TryParse(Next(), out var fps) ? Math.Max(0, fps) : options.TargetFps;
options.FrameCapSpecified = true;
break;
case "--min-fps":
options.MinFps = double.TryParse(Next(), out var floor) ? Math.Max(0, floor) : options.MinFps;
options.MinFpsSpecified = true;
break;
case "--render-scale":
options.RenderScale = float.TryParse(Next(), out var scale) ? Math.Clamp(scale, 0.2f, 1f) : options.RenderScale;
options.RenderScalePinned = true;
break;
case "--governor":
options.Governor = !string.Equals(Next(), "off", StringComparison.OrdinalIgnoreCase);
break;
case "--auto-plan":
if (AutoPlans.TryParse(Next() ?? string.Empty, out var plan))
{
options.Plan = plan;
options.PlanSpecified = true;
}
break;
case "--lazy-textures":
options.LazyTextures = true;
break;
case "--texture-size":
options.TextureSize = NormalizeTextureSize(
int.TryParse(Next(), out var textureSize) ? textureSize : 0);
options.TextureSizeSpecified = true;
break;
case "--hide-part":
{
var part = Next();
if (!string.IsNullOrWhiteSpace(part))
{
options.HiddenParts.Add(part);
}
break;
}
case "--drop-uv":
{
var spec = Next();
if (!string.IsNullOrWhiteSpace(spec))
{
options.DropUv.Add(spec);
}
break;
}
case "--warp":
options.SoftwareRasterizer = true;
break;
case "--debug-layer":
options.DebugLayer = true;
break;
case "--no-dcomp":
options.UseDirectComposition = false;
break;
case "--no-shape":
options.ShapeWindow = false;
break;
case "--no-batch":
options.Batching = false;
break;
case "--no-model":
options.SkipModelPass = true;
break;
case "--no-render":
options.SkipRenderPass = true;
break;
case "--no-clip":
options.SkipClippingPass = true;
break;
case "--no-present":
options.SkipPresent = true;
break;
case "--no-vsync":
options.WaitForVBlank = false;
break;
case "--mask-every-frame":
options.MaskEveryFrame = true;
break;
case "--debug-masks":
options.DebugMasks = true;
break;
case "--tray":
options.Tray = !string.Equals(Next(), "off", StringComparison.OrdinalIgnoreCase);
break;
case "--click-through":
options.ClickThrough = true;
break;
case "--focus-follow":
options.FocusFollowPointer = !string.Equals(Next(), "off", StringComparison.OrdinalIgnoreCase);
break;
case "--no-motion":
options.NoMotion = true;
break;
case "--no-fx":
options.NoEffects = true;
break;
case "--no-physics":
options.NoPhysics = true;
break;
case "--thread-priority":
options.ThreadPriority = int.TryParse(Next(), out var priority) ? priority : options.ThreadPriority;
break;
case "--cpu-affinity":
options.CpuAffinity = int.TryParse(Next(), out var cores) ? Math.Max(0, cores) : 0;
break;
case "--exit-after":
options.ExitAfterSeconds = int.TryParse(Next(), out var seconds) ? Math.Max(0, seconds) : 0;
break;
case "--capture":
options.CapturePath = Next();
break;
case "--capture-frame":
options.CaptureFrame = int.TryParse(Next(), out var frame) ? Math.Max(0, frame) : options.CaptureFrame;
break;
case "--hit-test-grid":
options.HitTestGrid = int.TryParse(Next(), out var cell) ? Math.Max(0, cell) : 0;
break;
case "--log":
options.LogPath = Next();
break;
case "--state":
options.StatePath = Next();
break;
case "--window-position":
{
var parts = (Next() ?? string.Empty).Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (parts is { Length: 2 } && int.TryParse(parts[0], out var x) && int.TryParse(parts[1], out var y))
{
options.WindowPosition = (x, y);
}
break;
}
case "--look-x":
options.LookX = float.TryParse(Next(), out var lookX) ? lookX : null;
break;
case "--look-y":
options.LookY = float.TryParse(Next(), out var lookY) ? lookY : null;
break;
case "--help":
case "-h":
Help();
Environment.Exit(0);
break;
}
}
// The ceiling: what was typed, else the display's own refresh rate. Read
// from the monitor rather than assumed to be 60 - a 165 Hz panel told "60"
// would be capped at a third of what it can show.
if (!options.FrameCapSpecified)
{
var refresh = Platform.Win32.DesktopRefreshRate();
options.TargetFps = refresh > 0 ? refresh : 60;
}
if (!options.MinFpsSpecified && options.TargetFps > 0 && options.TargetFps < options.MinFps)
{
options.MinFps = options.TargetFps;
}
return options;
}
private static void Help() => Console.WriteLine(
"""
Live2DPetCore - the pet, the frame pacer and the quality governor, and nothing else.
--model <path> .model3.json to load (default: first one under models\)
--width/--height window size in pixels (default 640x900)
--fps <n> frame ceiling (default: the display's refresh rate, 0 = none)
--min-fps <n> the floor the governor defends (default 60)
--render-scale <f> pin the render quality, 0.2 to 1 (stands the governor down)
--governor <on|off> trade render quality for frame rate (default on)
--auto-plan <quality|framerate> which lever automatic mode spends first
--lazy-textures keep atlas pages on disk until the picture needs them
--texture-size <px> cap each atlas page's long edge (0 = as authored)
--hide-part <name> leave a part/drawable out; repeatable
--drop-uv <spec> leave a UV patch out; drawable:u0,v0,u1,v1
--no-motion do not play motions
--no-fx no blink, breath or gaze
--no-physics no physics
--exit-after <sec> quit by itself, 0 = run until closed
--log <path> where to write the run log
--state <path> where to keep the window position and quality
--window-position <x,y>
--click-through the pet takes no mouse input at all
--tray <on|off> the tray icon (default on)
--capture <path> write one frame to a 32-bit BMP
Measurement: --warp, --cpu-affinity <n>, --thread-priority <0|1|2>,
--no-shape, --no-batch, --no-model, --no-render, --no-clip, --no-present,
--no-vsync, --mask-every-frame, --hit-test-grid <n>
""");
}