Skip to content

Commit 843a1c8

Browse files
committed
Catching exceptions inside launch() now
1 parent 5a65447 commit 843a1c8

File tree

14 files changed

+121
-163
lines changed

14 files changed

+121
-163
lines changed

Emulator/VACore/Components/Denise/PixelEngine.cpp

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -78,95 +78,6 @@ PixelEngine::_powerOn()
7878
clearAll();
7979
}
8080

81-
/*
82-
i64
83-
PixelEngine::getOption(Opt option) const
84-
{
85-
switch (option) {
86-
87-
case Opt::MON_PALETTE: return (i64)config.palette;
88-
case Opt::MON_BRIGHTNESS: return (i64)config.brightness;
89-
case Opt::MON_CONTRAST: return (i64)config.contrast;
90-
case Opt::MON_SATURATION: return (i64)config.saturation;
91-
92-
default:
93-
fatalError;
94-
}
95-
}
96-
97-
void
98-
PixelEngine::checkOption(Opt opt, i64 value)
99-
{
100-
switch (opt) {
101-
102-
case Opt::MON_PALETTE:
103-
104-
if (!PaletteEnum::isValid(value)) {
105-
throw CoreError(Fault::OPT_INV_ARG, PaletteEnum::keyList());
106-
}
107-
return;
108-
109-
case Opt::MON_BRIGHTNESS:
110-
111-
if (value < 0 || value > 100) {
112-
throw CoreError(Fault::OPT_INV_ARG, "0...100");
113-
}
114-
return;
115-
116-
case Opt::MON_CONTRAST:
117-
118-
if (value < 0 || value > 100) {
119-
throw CoreError(Fault::OPT_INV_ARG, "0...100");
120-
}
121-
return;
122-
123-
case Opt::MON_SATURATION:
124-
125-
if (value < 0 || value > 100) {
126-
throw CoreError(Fault::OPT_INV_ARG, "0...100");
127-
}
128-
return;
129-
130-
default:
131-
throw(Fault::OPT_UNSUPPORTED);
132-
}
133-
}
134-
135-
void
136-
PixelEngine::setOption(Opt option, i64 value)
137-
{
138-
switch (option) {
139-
140-
case Opt::MON_PALETTE:
141-
142-
config.palette = (Palette)value;
143-
updateRGBA();
144-
return;
145-
146-
case Opt::MON_BRIGHTNESS:
147-
148-
config.brightness = (isize)value;
149-
updateRGBA();
150-
return;
151-
152-
case Opt::MON_CONTRAST:
153-
154-
config.contrast = (isize)value;
155-
updateRGBA();
156-
return;
157-
158-
case Opt::MON_SATURATION:
159-
160-
config.saturation = (isize)value;
161-
updateRGBA();
162-
return;
163-
164-
default:
165-
fatalError;
166-
}
167-
}
168-
*/
169-
17081
void
17182
PixelEngine::setColor(isize reg, u16 value)
17283
{
@@ -213,9 +124,9 @@ PixelEngine::adjustRGB(u8 &r, u8 &g, u8 &b)
213124
if (palette == Palette::RGB) return;
214125

215126
// Normalize adjustment parameters
216-
double brightness = (monitor.getConfig().brightness - 500.0) / 10.0;
217-
double contrast = monitor.getConfig().contrast / 1000.0;
218-
double saturation = monitor.getConfig().saturation / 500.0;
127+
double brightness = (monitor.getConfig().brightness - 50.0);
128+
double contrast = monitor.getConfig().contrast / 100.0;
129+
double saturation = monitor.getConfig().saturation / 50.0;
219130

220131
// Convert RGB to YUV
221132
double y = 0.299 * r + 0.587 * g + 0.114 * b;

Emulator/VACore/Components/Denise/PixelEngine.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,7 @@ class PixelEngine final : public SubComponent {
171171

172172
const PixelEngineConfig &getConfig() const { return config; }
173173
const ConfigOptions &getOptions() const override { return options; }
174-
/*
175-
i64 getOption(Opt option) const override;
176-
void checkOption(Opt opt, i64 value) override;
177-
void setOption(Opt option, i64 value) override;
178-
*/
174+
179175

180176
//
181177
// Accessing color registers

Emulator/VACore/Foundation/Configurable.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ void
5555
Configurable::resetConfig(const Defaults &defaults, isize objid)
5656
{
5757
for (auto &option : getOptions()) {
58-
setOption(option, defaults.get(option, objid));
58+
59+
try {
60+
setOption(option, defaults.get(option, objid));
61+
} catch (...) {
62+
setOption(option, defaults.getFallback(option, objid));
63+
}
5964
}
6065
}
6166

Emulator/VACore/Foundation/Defaults.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Defaults::Defaults()
4242
setFallback(Opt::DENISE_FRAME_SKIPPING, 16);
4343

4444
setFallback(Opt::MON_PALETTE, (i64)Palette::COLOR);
45-
setFallback(Opt::MON_BRIGHTNESS, 500);
46-
setFallback(Opt::MON_CONTRAST, 1000);
47-
setFallback(Opt::MON_SATURATION, 500);
45+
setFallback(Opt::MON_BRIGHTNESS, 50);
46+
setFallback(Opt::MON_CONTRAST, 100);
47+
setFallback(Opt::MON_SATURATION, 50);
4848
setFallback(Opt::MON_CENTER, (i64)Center::AUTO);
4949
setFallback(Opt::MON_HCENTER, 600);
5050
setFallback(Opt::MON_VCENTER, 470);

Emulator/VACore/Peripherals/Monitor/Monitor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ Monitor::checkOption(Opt opt, i64 value)
110110
case Opt::MON_BRIGHTNESS:
111111
case Opt::MON_CONTRAST:
112112
case Opt::MON_SATURATION:
113+
114+
if (value < 0 || value > 100) {
115+
throw CoreError(Fault::OPT_INV_ARG, "0...100");
116+
}
117+
return;
118+
113119
case Opt::MON_HCENTER:
114120
case Opt::MON_VCENTER:
115121
case Opt::MON_HZOOM:

Emulator/VACore/Peripherals/Monitor/Monitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class Monitor final : public SubComponent {
134134
private:
135135

136136
void _dump(Category category, std::ostream& os) const override;
137-
137+
138138

139139
//
140140
// Methods from Configurable

GUI/Dialogs/Configuration/Configuration.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,6 @@ class Configuration {
2323
var gamePadManager: GamePadManager { return parent.gamePadManager }
2424
var ressourceManager: RessourceManager { return renderer.ressourceManager }
2525

26-
/*
27-
func map<T: BinaryFloatingPoint>(_ value: T, from: ClosedRange<T>, to: ClosedRange<T>) -> T {
28-
29-
let proportion = (value - from.lowerBound) / (from.upperBound - from.lowerBound)
30-
return to.lowerBound + proportion * (to.upperBound - to.lowerBound)
31-
}
32-
*/
33-
34-
/*
35-
func map(_ value: Float, from: ClosedRange<Float>, to: ClosedRange<Float>) -> Float {
36-
37-
let proportion = (value - from.lowerBound) / (from.upperBound - from.lowerBound)
38-
return to.lowerBound + proportion * (to.upperBound - to.lowerBound)
39-
}
40-
41-
func map(_ value: Float, from: ClosedRange<Float>) -> Int {
42-
43-
return Int(map(value, from: from, to: 0...100))
44-
}
45-
46-
func map(_ value: Int, to: ClosedRange<Float>) -> Float {
47-
48-
return map(Float(value), from: 0...100, to: to)
49-
}
50-
*/
51-
5226
//
5327
// Roms
5428
//

GUI/MyController.swift

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ extension MyController {
194194
// Setup window
195195
configureWindow()
196196

197+
// Create speed monitor
198+
speedometer = Speedometer()
199+
197200
// Launch the emulator
198201
launch()
199202

@@ -232,7 +235,7 @@ extension MyController {
232235
}
233236

234237
// Create speed monitor
235-
speedometer = Speedometer()
238+
// speedometer = Speedometer()
236239

237240
// Update toolbar
238241
toolbar.validateVisibleItems()
@@ -259,34 +262,42 @@ extension MyController {
259262
}
260263

261264
func launch() {
265+
266+
do {
262267

263-
// Pass in command line arguments as a RetroShell script
264-
var script = ""
265-
for arg in myAppDelegate.argv where arg.hasPrefix("-") {
266-
script = script + arg.dropFirst() + "\n"
267-
}
268-
emu?.retroShell.execute(script)
268+
// Pass in command line arguments as a RetroShell script
269+
var script = ""
270+
for arg in myAppDelegate.argv where arg.hasPrefix("-") {
271+
script = script + arg.dropFirst() + "\n"
272+
}
273+
emu?.retroShell.execute(script)
269274

270-
if BuildSettings.msgCallback {
271-
272-
// Convert 'self' to a void pointer
273-
let myself = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
274-
275-
emu.launch(myself) { (ptr, msg: Message) in
275+
if BuildSettings.msgCallback {
276+
277+
// Convert 'self' to a void pointer
278+
let myself = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
276279

277-
// Convert void pointer back to 'self'
278-
let myself = Unmanaged<MyController>.fromOpaque(ptr!).takeUnretainedValue()
280+
try emu.launch(myself) { (ptr, msg: Message) in
281+
282+
// Convert void pointer back to 'self'
283+
let myself = Unmanaged<MyController>.fromOpaque(ptr!).takeUnretainedValue()
284+
285+
// Process message in the main thread
286+
Task { @MainActor in myself.process(message: msg) }
287+
}
288+
289+
} else {
279290

280-
// Process message in the main thread
281-
Task { @MainActor in myself.process(message: msg) }
291+
try emu.launch()
282292
}
283293

284-
} else {
285-
286-
emu.launch()
294+
} catch {
295+
296+
// Something terrible happened
297+
mydocument.showLaunchAlert(error: error)
287298
}
288299
}
289-
300+
290301
//
291302
// Timer and message processing
292303
//

GUI/System/Errors.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ enum Failure {
8282
case cantDetach
8383
case cantExport(url: URL)
8484
case cantInsert
85+
case cantLaunch
8586
case cantOpen(url: URL)
8687
case cantRecord
8788
case cantRestore
@@ -153,6 +154,10 @@ enum Failure {
153154
case .cantInsert:
154155
return "Failed to insert disk."
155156

157+
case .cantLaunch:
158+
return "Failed to lauch the emulator." +
159+
"An unexpected exception has interrupted the internal startup procedure."
160+
156161
case let .cantOpen(url):
157162
return "\"\(url.lastPathComponent)\" can't be opened."
158163

@@ -309,6 +314,28 @@ extension MyDocument {
309314
return alert.runSheet(for: windowForSheet!)
310315
}
311316

317+
func showLaunchAlert(error: Error) {
318+
319+
var reason: String
320+
if let error = error as? CoreError {
321+
reason = error.what
322+
} else {
323+
reason = error.localizedDescription
324+
}
325+
326+
let alert = NSAlert()
327+
alert.alertStyle = .warning
328+
alert.icon = NSImage(named: "biohazard")
329+
alert.messageText = "The emulator failed to launch."
330+
alert.informativeText = "An unexpected exception interrupted the startup process. " +
331+
"Please report the following error on GitHub:\n\n\(reason)"
332+
alert.addButton(withTitle: "Exit")
333+
334+
if alert.runSheet(for: windowForSheet!) == .alertFirstButtonReturn {
335+
NSApp.terminate(self)
336+
}
337+
}
338+
312339
func proceedWithUnsavedFloppyDisks(drives: [FloppyDriveProxy]) -> Bool {
313340

314341
let modified = drives.filter { $0.info.hasModifiedDisk }

GUI/XIB files/Configuration.xib

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,23 +3644,23 @@ DQ
36443644
<slider horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dOW-wJ-mRR">
36453645
<rect key="frame" x="481" y="224" width="28" height="30"/>
36463646
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
3647-
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="1000" sliderType="circular" id="xtY-if-gfI"/>
3647+
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="100" sliderType="circular" id="xtY-if-gfI"/>
36483648
<connections>
36493649
<action selector="vidSaturationAction:" target="-2" id="AiN-oE-9My"/>
36503650
</connections>
36513651
</slider>
36523652
<slider horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Mnh-Qb-xHP">
36533653
<rect key="frame" x="403" y="224" width="28" height="30"/>
36543654
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
3655-
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="1000" sliderType="circular" id="qIK-h1-GO5"/>
3655+
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="100" sliderType="circular" id="qIK-h1-GO5"/>
36563656
<connections>
36573657
<action selector="vidContrastAction:" target="-2" id="AZn-Nd-fVB"/>
36583658
</connections>
36593659
</slider>
36603660
<slider horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="zzU-KL-7dh">
36613661
<rect key="frame" x="325" y="224" width="28" height="30"/>
36623662
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
3663-
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="1000" sliderType="circular" id="aV3-Mg-z8p"/>
3663+
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="100" sliderType="circular" id="aV3-Mg-z8p"/>
36643664
<connections>
36653665
<action selector="vidBrightnessAction:" target="-2" id="b8Z-ZI-JNc"/>
36663666
</connections>
@@ -3831,7 +3831,7 @@ DQ
38313831
<slider horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="jms-qV-MhZ">
38323832
<rect key="frame" x="403" y="83" width="28" height="30"/>
38333833
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
3834-
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" maxValue="1000" sliderType="circular" id="f2m-kS-Et4"/>
3834+
<sliderCell key="cell" continuous="YES" refusesFirstResponder="YES" alignment="left" sliderType="circular" id="f2m-kS-Et4"/>
38353835
<connections>
38363836
<action selector="vidVCenterAction:" target="-2" id="Y1Z-e0-8zo"/>
38373837
</connections>

0 commit comments

Comments
 (0)