-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
203 lines (178 loc) · 8.46 KB
/
Copy pathProgram.cs
File metadata and controls
203 lines (178 loc) · 8.46 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
using Avalonia;
using System;
using System.Linq;
using System.Reflection;
using ReadZen.App.Services;
using Velopack;
namespace ReadZen.App;
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
// Headless tool mode: regenerate segment maps and exit (the C# replacement
// for the retired eng/tools/build-structural-segments.js — audit P3.1c 2/2).
// Must run before Velopack/single-instance/Avalonia: no UI, no side effects.
if (args.Length > 0 && args[0] == "--build-segments")
{
Environment.Exit(SegmentMapGenerator.Run(args, Console.Out));
return;
}
// Headless tool mode: full-rebuild a shippable search index for a corpus into an
// output dir, then exit (S7 bundled prebuilt index). CI runs this to stage the
// artifacts into Assets/PrebuiltIndex before publish; the app then seeds them into a
// virgin user index root on first run. Must run before Velopack/single-instance/
// Avalonia: no UI, no side effects beyond the output dir.
if (args.Length > 0 && args[0] == "--build-search-index")
{
Environment.Exit(SearchIndexService.RunHeadlessBuild(args, Console.Out));
return;
}
// Headless tool mode: bake the machine-independent bundled nav-status cache (v5)
// for the CBETA corpus under a parent root, then exit (NAV_CACHE_REDESIGN §4.1,
// PR-NV5). CI runs this in prebuild-index to stage Assets/Data/nav-cache.cbeta.json
// (committed as an asset in PR-NV6); the app then adopts it on launch for a
// zero-rebuild fresh install. Must run before Velopack/single-instance/Avalonia:
// no UI, no side effects beyond the output file.
if (args.Length > 0 && args[0] == "--build-nav-cache")
{
Environment.Exit(IndexCacheService.RunHeadlessBuild(args, Console.Out));
return;
}
// Headless tool mode: print the current search index family BuildGuids and exit
// (S7 bundled prebuilt index, PR-S3). CI's release workflow calls this on the release
// binary itself to (a) assert the staged bundle manifest guid == the binary's guid and
// (b) drive the guid-bundle-guard (a guid bump with no matching bundle fails the tag).
// Must run before Velopack/single-instance/Avalonia: no UI, no side effects.
if (args.Length > 0 && args[0] == "--print-build-guid")
{
Environment.Exit(SearchIndexService.RunPrintBuildGuid(Console.Out));
return;
}
// Global exception handlers — catch crashes that happen during rendering
// or on background threads, so Linux users see the real error, not just
// "Dispatcher shut down".
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
Console.Error.WriteLine($"FATAL UNHANDLED: {e.ExceptionObject}");
Console.Error.Flush();
};
System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (_, e) =>
{
Console.Error.WriteLine($"UNOBSERVED TASK: {e.Exception}");
Console.Error.Flush();
};
// Velopack MUST run before anything else — it intercepts --veloapp-install /
// --veloapp-update / --veloapp-restarted command-line flags and exits the
// process early when appropriate. Calling this on builds that weren't packaged
// with `vpk pack` is a no-op, so it's safe for zip-extract users too.
try
{
// Run() reads command-line args from Environment internally; we don't
// need to pass them through. This is a no-op on non-Velopack builds.
VelopackApp.Build().Run();
}
catch
{
// Never block startup on a Velopack failure — the app must still run
// from a plain zip extract even if Velopack's state is missing/corrupt.
}
SingleInstanceManager? singleInstance = null;
try
{
singleInstance = new SingleInstanceManager();
if (!singleInstance.TryAcquireOrForward(args))
{
// Another instance is already running; we forwarded our URI. Exit.
singleInstance.Dispose();
return;
}
App.SingleInstance = singleInstance;
}
catch
{
// Single-instance check failed — proceed anyway.
// Deep links from second instances won't work, but the app will launch.
}
App.StartupArgs = args;
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
try { singleInstance?.Dispose(); } catch { }
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
{
var builder = AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
// Set AFTER .LogToTrace() so our sink takes precedence (LogToTrace may overwrite)
Avalonia.Logging.Logger.Sink = new StderrLogSink();
// On Linux, configure X11 with software rendering fallback and disable
// DBus menu to avoid Wayland dispatcher shutdown crashes (Issue #19523).
// Uses reflection because Avalonia.X11 types are only available at runtime
// on Linux (not at compile time on Windows).
if (OperatingSystem.IsLinux())
{
try
{
var x11OptionsType = Type.GetType("Avalonia.X11.X11PlatformOptions, Avalonia.X11");
var renderModeType = Type.GetType("Avalonia.X11.X11RenderingMode, Avalonia.X11");
if (x11OptionsType != null && renderModeType != null)
{
var options = Activator.CreateInstance(x11OptionsType)!;
// RenderingMode = [Glx, Software]
var glx = Enum.Parse(renderModeType, "Glx");
var sw = Enum.Parse(renderModeType, "Software");
var arr = Array.CreateInstance(renderModeType, 2);
arr.SetValue(glx, 0);
arr.SetValue(sw, 1);
x11OptionsType.GetProperty("RenderingMode")?.SetValue(options, arr);
// UseDBusMenu = false
x11OptionsType.GetProperty("UseDBusMenu")?.SetValue(options, false);
// builder.With(options) — call the generic With<T> method
var withMethod = typeof(AppBuilder).GetMethods()
.Where(m => m.Name == "With" && m.IsGenericMethod)
.FirstOrDefault();
if (withMethod != null)
{
var generic = withMethod.MakeGenericMethod(x11OptionsType);
builder = (AppBuilder)generic.Invoke(builder, new[] { options })!;
}
}
}
catch
{
// X11 types may not be available on some configurations — proceed without
}
}
return builder;
}
}
/// <summary>
/// Writes Avalonia's internal log messages (errors, warnings) to stderr.
/// On Linux, the X11 backend logs the real crash cause before the dispatcher
/// shuts down — but only if a sink is attached.
/// </summary>
internal sealed class StderrLogSink : Avalonia.Logging.ILogSink
{
public bool IsEnabled(Avalonia.Logging.LogEventLevel level, string area)
=> level >= Avalonia.Logging.LogEventLevel.Fatal;
public void Log(Avalonia.Logging.LogEventLevel level, string area, object? source, string messageTemplate)
{
try { Console.Error.WriteLine($"[Avalonia {level}] {area}: {messageTemplate}"); Console.Error.Flush(); } catch { }
}
public void Log(Avalonia.Logging.LogEventLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues)
{
try
{
// Don't try string.Format — Avalonia uses {PropertyName} not {0} style
var vals = propertyValues != null ? string.Join(", ", propertyValues) : "";
Console.Error.WriteLine($"[Avalonia {level}] {area}: {messageTemplate} [{vals}]");
Console.Error.Flush();
}
catch { }
}
}