Path: blob/master/platform/windows/godot_windows.cpp
20901 views
/**************************************************************************/1/* godot_windows.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "os_windows.h"3132#include "core/profiling/profiling.h"33#include "main/main.h"3435#include <clocale>36#include <cstdio>3738// For export templates, add a section; the exporter will patch it to enclose39// the data appended to the executable (bundled PCK).40#ifndef TOOLS_ENABLED41#if defined _MSC_VER42#pragma section("pck", read)43__declspec(allocate("pck")) static char dummy[8] = { 0 };4445// Dummy function to prevent LTO from discarding "pck" section.46extern "C" char *__cdecl pck_section_dummy_call() {47return &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#endif5455#elif defined __GNUC__56static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };57#endif58#endif5960char *wc_to_utf8(const wchar_t *wc) {61int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);62char *ubuf = new char[ulen + 1];63WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);64ubuf[ulen] = 0;65return ubuf;66}6768int widechar_main(int argc, wchar_t **argv) {69godot_init_profiler();7071OS_Windows os(nullptr);7273setlocale(LC_CTYPE, "");7475char **argv_utf8 = new char *[argc];7677for (int i = 0; i < argc; ++i) {78argv_utf8[i] = wc_to_utf8(argv[i]);79}8081TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)8283Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);8485if (err != OK) {86for (int i = 0; i < argc; ++i) {87delete[] argv_utf8[i];88}89delete[] argv_utf8;9091if (err == ERR_HELP) { // Returned by --help and --version, so success.92return EXIT_SUCCESS;93}94return EXIT_FAILURE;95}9697if (Main::start() == EXIT_SUCCESS) {98os.run();99} else {100os.set_exit_code(EXIT_FAILURE);101}102Main::cleanup();103104for (int i = 0; i < argc; ++i) {105delete[] argv_utf8[i];106}107delete[] argv_utf8;108109godot_cleanup_profiler();110return os.get_exit_code();111}112113int _main() {114LPWSTR *wc_argv;115int argc;116int result;117118wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);119120if (nullptr == wc_argv) {121wprintf(L"CommandLineToArgvW failed\n");122return 0;123}124125result = widechar_main(argc, wc_argv);126127LocalFree(wc_argv);128return result;129}130131int main(int argc, char **argv) {132// override the arguments for the test handler / if symbol is provided133// TEST_MAIN_OVERRIDE134135// _argc and _argv are ignored136// we are going to use the WideChar version of them instead137#if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)138__try {139return _main();140} __except (CrashHandlerException(GetExceptionInformation())) {141return 1;142}143#else144return _main();145#endif146}147148HINSTANCE godot_hinstance = nullptr;149150int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {151godot_hinstance = hInstance;152return main(0, nullptr);153}154155156