Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/godot_windows.cpp
20901 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 "core/profiling/profiling.h"
34
#include "main/main.h"
35
36
#include <clocale>
37
#include <cstdio>
38
39
// For export templates, add a section; the exporter will patch it to enclose
40
// the data appended to the executable (bundled PCK).
41
#ifndef TOOLS_ENABLED
42
#if defined _MSC_VER
43
#pragma section("pck", read)
44
__declspec(allocate("pck")) static char dummy[8] = { 0 };
45
46
// Dummy function to prevent LTO from discarding "pck" section.
47
extern "C" char *__cdecl pck_section_dummy_call() {
48
return &dummy[0];
49
}
50
#if defined _AMD64_
51
#pragma comment(linker, "/include:pck_section_dummy_call")
52
#elif defined _X86_
53
#pragma comment(linker, "/include:_pck_section_dummy_call")
54
#endif
55
56
#elif defined __GNUC__
57
static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };
58
#endif
59
#endif
60
61
char *wc_to_utf8(const wchar_t *wc) {
62
int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);
63
char *ubuf = new char[ulen + 1];
64
WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);
65
ubuf[ulen] = 0;
66
return ubuf;
67
}
68
69
int widechar_main(int argc, wchar_t **argv) {
70
godot_init_profiler();
71
72
OS_Windows os(nullptr);
73
74
setlocale(LC_CTYPE, "");
75
76
char **argv_utf8 = new char *[argc];
77
78
for (int i = 0; i < argc; ++i) {
79
argv_utf8[i] = wc_to_utf8(argv[i]);
80
}
81
82
TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)
83
84
Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
85
86
if (err != OK) {
87
for (int i = 0; i < argc; ++i) {
88
delete[] argv_utf8[i];
89
}
90
delete[] argv_utf8;
91
92
if (err == ERR_HELP) { // Returned by --help and --version, so success.
93
return EXIT_SUCCESS;
94
}
95
return EXIT_FAILURE;
96
}
97
98
if (Main::start() == EXIT_SUCCESS) {
99
os.run();
100
} else {
101
os.set_exit_code(EXIT_FAILURE);
102
}
103
Main::cleanup();
104
105
for (int i = 0; i < argc; ++i) {
106
delete[] argv_utf8[i];
107
}
108
delete[] argv_utf8;
109
110
godot_cleanup_profiler();
111
return os.get_exit_code();
112
}
113
114
int _main() {
115
LPWSTR *wc_argv;
116
int argc;
117
int result;
118
119
wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
120
121
if (nullptr == wc_argv) {
122
wprintf(L"CommandLineToArgvW failed\n");
123
return 0;
124
}
125
126
result = widechar_main(argc, wc_argv);
127
128
LocalFree(wc_argv);
129
return result;
130
}
131
132
int main(int argc, char **argv) {
133
// override the arguments for the test handler / if symbol is provided
134
// TEST_MAIN_OVERRIDE
135
136
// _argc and _argv are ignored
137
// we are going to use the WideChar version of them instead
138
#if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)
139
__try {
140
return _main();
141
} __except (CrashHandlerException(GetExceptionInformation())) {
142
return 1;
143
}
144
#else
145
return _main();
146
#endif
147
}
148
149
HINSTANCE godot_hinstance = nullptr;
150
151
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
152
godot_hinstance = hInstance;
153
return main(0, nullptr);
154
}
155
156