Path: blob/master/platform/windows/godot_windows.cpp
10277 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 "main/main.h"3334#include <clocale>35#include <cstdio>3637// For export templates, add a section; the exporter will patch it to enclose38// the data appended to the executable (bundled PCK).39#ifndef TOOLS_ENABLED40#if defined _MSC_VER41#pragma section("pck", read)42__declspec(allocate("pck")) static char dummy[8] = { 0 };4344// Dummy function to prevent LTO from discarding "pck" section.45extern "C" char *__cdecl pck_section_dummy_call() {46return &dummy[0];47}48#if defined _AMD64_49#pragma comment(linker, "/include:pck_section_dummy_call")50#elif defined _X86_51#pragma comment(linker, "/include:_pck_section_dummy_call")52#endif5354#elif defined __GNUC__55static const char dummy[8] __attribute__((section("pck"), used)) = { 0 };56#endif57#endif5859char *wc_to_utf8(const wchar_t *wc) {60int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, nullptr, 0, nullptr, nullptr);61char *ubuf = new char[ulen + 1];62WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, nullptr, nullptr);63ubuf[ulen] = 0;64return ubuf;65}6667int widechar_main(int argc, wchar_t **argv) {68OS_Windows os(nullptr);6970setlocale(LC_CTYPE, "");7172char **argv_utf8 = new char *[argc];7374for (int i = 0; i < argc; ++i) {75argv_utf8[i] = wc_to_utf8(argv[i]);76}7778TEST_MAIN_PARAM_OVERRIDE(argc, argv_utf8)7980Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);8182if (err != OK) {83for (int i = 0; i < argc; ++i) {84delete[] argv_utf8[i];85}86delete[] argv_utf8;8788if (err == ERR_HELP) { // Returned by --help and --version, so success.89return EXIT_SUCCESS;90}91return EXIT_FAILURE;92}9394if (Main::start() == EXIT_SUCCESS) {95os.run();96} else {97os.set_exit_code(EXIT_FAILURE);98}99Main::cleanup();100101for (int i = 0; i < argc; ++i) {102delete[] argv_utf8[i];103}104delete[] argv_utf8;105106return os.get_exit_code();107}108109int _main() {110LPWSTR *wc_argv;111int argc;112int result;113114wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);115116if (nullptr == wc_argv) {117wprintf(L"CommandLineToArgvW failed\n");118return 0;119}120121result = widechar_main(argc, wc_argv);122123LocalFree(wc_argv);124return result;125}126127int main(int argc, char **argv) {128// override the arguments for the test handler / if symbol is provided129// TEST_MAIN_OVERRIDE130131// _argc and _argv are ignored132// we are going to use the WideChar version of them instead133#if defined(CRASH_HANDLER_EXCEPTION) && defined(_MSC_VER)134__try {135return _main();136} __except (CrashHandlerException(GetExceptionInformation())) {137return 1;138}139#else140return _main();141#endif142}143144HINSTANCE godot_hinstance = nullptr;145146int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {147godot_hinstance = hInstance;148return main(0, nullptr);149}150151152