2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* godot_win.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "main/main.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "os_windows.h"
|
2014-02-21 11:00:23 +00:00
|
|
|
#include <locale.h>
|
2017-03-18 23:36:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
PCHAR *
|
|
|
|
CommandLineToArgvA(
|
|
|
|
PCHAR CmdLine,
|
|
|
|
int *_argc) {
|
|
|
|
PCHAR *argv;
|
|
|
|
PCHAR _argv;
|
|
|
|
ULONG len;
|
|
|
|
ULONG argc;
|
|
|
|
CHAR a;
|
|
|
|
ULONG i, j;
|
|
|
|
|
|
|
|
BOOLEAN in_QM;
|
|
|
|
BOOLEAN in_TEXT;
|
|
|
|
BOOLEAN in_SPACE;
|
|
|
|
|
|
|
|
len = strlen(CmdLine);
|
|
|
|
i = ((len + 2) / 2) * sizeof(PVOID) + sizeof(PVOID);
|
|
|
|
|
|
|
|
argv = (PCHAR *)GlobalAlloc(GMEM_FIXED,
|
|
|
|
i + (len + 2) * sizeof(CHAR));
|
|
|
|
|
|
|
|
_argv = (PCHAR)(((PUCHAR)argv) + i);
|
|
|
|
|
|
|
|
argc = 0;
|
|
|
|
argv[argc] = _argv;
|
|
|
|
in_QM = FALSE;
|
|
|
|
in_TEXT = FALSE;
|
|
|
|
in_SPACE = TRUE;
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
while ((a = CmdLine[i])) {
|
|
|
|
if (in_QM) {
|
|
|
|
if (a == '\"') {
|
|
|
|
in_QM = FALSE;
|
|
|
|
} else {
|
|
|
|
_argv[j] = a;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (a) {
|
|
|
|
case '\"':
|
|
|
|
in_QM = TRUE;
|
|
|
|
in_TEXT = TRUE;
|
|
|
|
if (in_SPACE) {
|
|
|
|
argv[argc] = _argv + j;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
in_SPACE = FALSE;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
if (in_TEXT) {
|
|
|
|
_argv[j] = '\0';
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
in_TEXT = FALSE;
|
|
|
|
in_SPACE = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
in_TEXT = TRUE;
|
|
|
|
if (in_SPACE) {
|
|
|
|
argv[argc] = _argv + j;
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
_argv[j] = a;
|
|
|
|
j++;
|
|
|
|
in_SPACE = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
_argv[j] = '\0';
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
(*_argc) = argc;
|
|
|
|
return argv;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char *wc_to_utf8(const wchar_t *wc) {
|
|
|
|
int ulen = WideCharToMultiByte(CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL);
|
|
|
|
char *ubuf = new char[ulen + 1];
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wc, -1, ubuf, ulen, NULL, NULL);
|
2014-02-21 11:00:23 +00:00
|
|
|
ubuf[ulen] = 0;
|
|
|
|
return ubuf;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int widechar_main(int argc, wchar_t **argv) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
OS_Windows os(NULL);
|
|
|
|
|
2014-02-21 11:00:23 +00:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
char **argv_utf8 = new char *[argc];
|
2015-05-04 18:26:49 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
2015-05-04 18:26:49 +00:00
|
|
|
argv_utf8[i] = wc_to_utf8(argv[i]);
|
2014-02-21 11:00:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 17:17:57 +00:00
|
|
|
Error err = Main::setup(argv_utf8[0], argc - 1, &argv_utf8[1]);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (err != OK)
|
2015-05-17 17:17:57 +00:00
|
|
|
return 255;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (Main::start())
|
|
|
|
os.run();
|
|
|
|
Main::cleanup();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
2014-02-21 11:00:23 +00:00
|
|
|
delete[] argv_utf8[i];
|
|
|
|
}
|
|
|
|
delete[] argv_utf8;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return os.get_exit_code();
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int main(int _argc, char **_argv) {
|
2015-05-04 18:26:49 +00:00
|
|
|
// _argc and _argv are ignored
|
|
|
|
// we are going to use the WideChar version of them instead
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 18:26:49 +00:00
|
|
|
LPWSTR *wc_argv;
|
2017-03-18 23:36:26 +00:00
|
|
|
int argc;
|
|
|
|
int result;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 18:26:49 +00:00
|
|
|
wc_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (NULL == wc_argv) {
|
2015-05-04 18:26:49 +00:00
|
|
|
wprintf(L"CommandLineToArgvW failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 18:26:49 +00:00
|
|
|
result = widechar_main(argc, wc_argv);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 18:26:49 +00:00
|
|
|
LocalFree(wc_argv);
|
|
|
|
return result;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 18:26:49 +00:00
|
|
|
HINSTANCE godot_hinstance = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
2015-05-04 18:26:49 +00:00
|
|
|
godot_hinstance = hInstance;
|
2017-03-18 23:36:26 +00:00
|
|
|
return main(0, NULL);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|