Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/godot_windows.cpp
10277 views
1
/**************************************************************************/
2
/* godot_windows.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "os_windows.h"
32
33
#include "main/main.h"
34
35
#include <clocale>
36
#include <cstdio>
37
38
// For export templates, add a section; the exporter will patch it to enclose
39
// the data appended to the executable (bundled PCK).
40
#ifndef TOOLS_ENABLED
41
#if defined _MSC_VER
42
#pragma section("pck", read)
43
__declspec(allocate("pck")) static char dummy[8] = { 0 };
44
45
// Dummy function to prevent LTO from discarding "pck" section.
46
extern "C" char *__cdecl pck_section_dummy_call() {
47
return &dummy[0];
48
}
49
#if defined _AMD64_
50
#pragma comment(linker, "/include:pck_section_dummy_call")
51
#elif defined _X86_
52
#pragma comment(linker, "/include:_pck_section_dummy_call")
53
#endif
54
55
#elif defined __GNUC__
56
static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
57
#endif
58
#endif
59
60
char *wc_to_utf8(const wchar_t *wc) {
61
int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);
62
char *ubuf = new char[ulen + 1];
63
WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);
64
ubuf[ulen] = 0;
65
return ubuf;
66
}
67
68
int widechar_main(int argc, wchar_t **argv) {
69
OS_Windows os(nullptr);
70
71
setlocale(LC_CTYPE, "");
72
73
char **argv_utf8 = new char *[argc];
74
75
for (int i = 0; i < argc; ++i) {
76
argv_utf8[i] = wc_to_utf8(argv[i]);
77
}
78
79
TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)
80
81
Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
82
83
if (err != OK) {
84
for (int i = 0; i < argc; ++i) {
85
delete[] argv_utf8[i];
86
}
87
delete[] argv_utf8;
88
89
if (err == ERR_HELP) { // Returned by --help and --version, so success.
90
return EXIT_SUCCESS;
91
}
92
return EXIT_FAILURE;
93
}
94
95
if (Main::start() == EXIT_SUCCESS) {
96
os.run();
97
} else {
98
os.set_exit_code(EXIT_FAILURE);
99
}
100
Main::cleanup();
101
102
for (int i = 0; i < argc; ++i) {
103
delete[] argv_utf8[i];
104
}
105
delete[] argv_utf8;
106
107
return os.get_exit_code();
108
}
109
110
int _main() {
111
LPWSTR *wc_argv;
112
int argc;
113
int result;
114
115
wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
116
117
if (nullptr == wc_argv) {
118
wprintf(L"CommandLineToArgvW failed\n");
119
return 0;
120
}
121
122
result = widechar_main(argc, wc_argv);
123
124
LocalFree(wc_argv);
125
return result;
126
}
127
128
int main(int argc, char **argv) {
129
// override the arguments for the test handler / if symbol is provided
130
// TEST_MAIN_OVERRIDE
131
132
// _argc and _argv are ignored
133
// we are going to use the WideChar version of them instead
134
#if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)
135
__try {
136
return _main();
137
} __except (CrashHandlerException(GetExceptionInformation())) {
138
return 1;
139
}
140
#else
141
return _main();
142
#endif
143
}
144
145
HINSTANCE godot_hinstance = nullptr;
146
147
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
148
godot_hinstance = hInstance;
149
return main(0, nullptr);
150
}
151
152