1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.utils.crashdump;
8 import std.file : write;
9 //import i18n;
10 import std.stdio;
11 import std.path;
12 import std.traits;
13 import std.array;
14 import i18n;
15 
16 string genCrashDump(T...)(Throwable t, T state) {
17     string[] args;
18     static foreach(i; 0 .. state.length) {
19         args ~= serializeToPrettyJson(state[i]);
20     }
21     Appender!string str;
22     str.put("=== Args State ===\n");
23     str.put(args.join(",\n"));
24     str.put("\n\n=== Exception ===\n");
25     str.put(t.toString());
26     return str.data;
27 }
28 
29 version(Windows) {
30     pragma(lib, "user32.lib");
31     pragma(lib, "shell32.lib");
32     import core.sys.windows.winuser : MessageBoxW;
33     import std.utf : toUTF16z, toUTF8;
34     import std.string : fromStringz;
35 
36     private string getDesktopDir() {
37         import core.sys.windows.windows;
38         import core.sys.windows.shlobj;
39         wstring desktopDir = new wstring(MAX_PATH);
40         SHGetSpecialFolderPath(HWND_DESKTOP, cast(wchar*)desktopDir.ptr, CSIDL_DESKTOP, FALSE);
41         return (cast(wstring)fromStringz!wchar(desktopDir.ptr)).toUTF8;
42     }
43 
44     private void ShowMessageBox(string message, string title) {
45         MessageBoxW(null, toUTF16z(message), toUTF16z(title), 0);
46     }
47 
48     void crashdump(T...)(Throwable throwable, T state) {
49         write(buildPath(getDesktopDir(), "inochi-creator-crashdump.txt"), genCrashDump!T(throwable, state));
50 
51         ShowMessageBox(
52             _("The application has unexpectedly crashed\nPlease send the developers the inochi-creator-crashdump.txt which has been put on your desktop\nVia https://github.com/Inochi2D/inochi-creator/issues"),
53             _("Inochi Creator Crashdump")
54         );
55     }
56 }
57 
58 version(Posix) {
59     void crashdump(T...)(Throwable throwable, T state) {
60         write(expandTilde("~/inochi-creator-crashdump.txt"), genCrashDump!T(throwable, state));
61         writeln(_("\n\n\n===   Inochi Creator has crashed   ===\nPlease send us the inochi-creator-crashdump.txt file in your home folder\nAttach the file as a git issue @ https://github.com/Inochi2D/inochi-creator/issues"));
62     }
63 }