2014-08-16 23:34:04 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright 2014 Ben Vanik. All rights reserved. *
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2015-05-02 10:42:51 +00:00
|
|
|
#include "xenia/base/main.h"
|
2014-08-16 23:34:04 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2015-07-01 16:02:04 +00:00
|
|
|
#include <gflags/gflags.h>
|
2014-08-16 23:34:04 +00:00
|
|
|
#include <io.h>
|
|
|
|
|
2015-12-27 19:53:37 +00:00
|
|
|
// Autogenerated by `xb premake`.
|
|
|
|
#include "build/version.h"
|
|
|
|
|
2015-08-30 01:06:30 +00:00
|
|
|
#include "xenia/base/logging.h"
|
2015-07-14 03:49:29 +00:00
|
|
|
#include "xenia/base/platform_win.h"
|
2015-05-02 10:42:51 +00:00
|
|
|
#include "xenia/base/string.h"
|
2014-08-16 23:34:04 +00:00
|
|
|
|
2015-05-02 10:42:51 +00:00
|
|
|
namespace xe {
|
2014-08-16 23:34:04 +00:00
|
|
|
|
|
|
|
bool has_console_attached_ = true;
|
|
|
|
|
|
|
|
bool has_console_attached() { return has_console_attached_; }
|
|
|
|
|
|
|
|
void AttachConsole() {
|
|
|
|
bool has_console = ::AttachConsole(ATTACH_PARENT_PROCESS) == TRUE;
|
|
|
|
if (!has_console) {
|
|
|
|
// We weren't launched from a console, so just return.
|
|
|
|
// We could alloc our own console, but meh:
|
|
|
|
// has_console = AllocConsole() == TRUE;
|
|
|
|
has_console_attached_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
has_console_attached_ = true;
|
|
|
|
|
|
|
|
auto std_handle = (intptr_t)GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
auto con_handle = _open_osfhandle(std_handle, _O_TEXT);
|
|
|
|
auto fp = _fdopen(con_handle, "w");
|
|
|
|
*stdout = *fp;
|
2014-08-17 04:19:21 +00:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2014-08-16 23:34:04 +00:00
|
|
|
|
|
|
|
std_handle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
con_handle = _open_osfhandle(std_handle, _O_TEXT);
|
|
|
|
fp = _fdopen(con_handle, "w");
|
|
|
|
*stderr = *fp;
|
2014-08-17 04:19:21 +00:00
|
|
|
setvbuf(stderr, nullptr, _IONBF, 0);
|
2014-08-16 23:34:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
int Main() {
|
2015-05-02 10:42:51 +00:00
|
|
|
auto entry_info = xe::GetEntryInfo();
|
2014-08-16 23:34:04 +00:00
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
// Convert command line to an argv-like format so we can share code/use
|
|
|
|
// gflags.
|
|
|
|
auto command_line = GetCommandLineW();
|
|
|
|
int argc;
|
|
|
|
wchar_t** argv = CommandLineToArgvW(command_line, &argc);
|
|
|
|
if (!argv) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-16 23:34:04 +00:00
|
|
|
google::SetUsageMessage(std::string("usage: ") +
|
2015-05-02 10:42:51 +00:00
|
|
|
xe::to_string(entry_info.usage));
|
2014-08-16 23:34:04 +00:00
|
|
|
google::SetVersionString("1.0");
|
|
|
|
|
|
|
|
// Convert all args to narrow, as gflags doesn't support wchar.
|
|
|
|
int argca = argc;
|
2015-08-07 03:17:01 +00:00
|
|
|
char** argva = reinterpret_cast<char**>(alloca(sizeof(char*) * argca));
|
2014-08-16 23:34:04 +00:00
|
|
|
for (int n = 0; n < argca; n++) {
|
|
|
|
size_t len = wcslen(argv[n]);
|
2015-08-07 03:17:01 +00:00
|
|
|
argva[n] = reinterpret_cast<char*>(alloca(len + 1));
|
2014-08-17 04:19:21 +00:00
|
|
|
wcstombs_s(nullptr, argva[n], len + 1, argv[n], _TRUNCATE);
|
2014-08-16 23:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse flags; this may delete some of them.
|
|
|
|
google::ParseCommandLineFlags(&argc, &argva, true);
|
|
|
|
|
|
|
|
// Widen all remaining flags and convert to usable strings.
|
|
|
|
std::vector<std::wstring> args;
|
|
|
|
for (int n = 0; n < argc; n++) {
|
2015-05-02 10:42:51 +00:00
|
|
|
args.push_back(xe::to_wstring(argva[n]));
|
2014-08-16 23:34:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-17 04:19:21 +00:00
|
|
|
// Setup COM on the main thread.
|
|
|
|
// NOTE: this may fail if COM has already been initialized - that's OK.
|
|
|
|
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
|
2015-08-30 01:06:30 +00:00
|
|
|
// Initialize logging. Needs parsed FLAGS.
|
|
|
|
xe::InitializeLogging(entry_info.name);
|
|
|
|
|
2015-12-27 19:53:37 +00:00
|
|
|
// Print version info.
|
|
|
|
XELOGI("Build: %s / %s on %s", XE_BUILD_BRANCH, XE_BUILD_COMMIT,
|
|
|
|
XE_BUILD_DATE);
|
|
|
|
|
2014-08-16 23:34:04 +00:00
|
|
|
// Call app-provided entry point.
|
|
|
|
int result = entry_info.entry_point(args);
|
|
|
|
|
|
|
|
google::ShutDownCommandLineFlags();
|
2015-08-01 06:48:24 +00:00
|
|
|
LocalFree(argv);
|
2014-08-16 23:34:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-01 06:48:24 +00:00
|
|
|
} // namespace xe
|
|
|
|
|
|
|
|
// Used in console mode apps; automatically picked based on subsystem.
|
|
|
|
int main(int argc_ignored, char** argv_ignored) { return xe::Main(); }
|
|
|
|
|
2014-08-16 23:34:04 +00:00
|
|
|
// Used in windowed apps; automatically picked based on subsystem.
|
|
|
|
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR command_line, int) {
|
|
|
|
// Attach a console so we can write output to stdout. If the user hasn't
|
|
|
|
// redirected output themselves it'll pop up a window.
|
2015-05-02 10:42:51 +00:00
|
|
|
xe::AttachConsole();
|
2014-08-16 23:34:04 +00:00
|
|
|
|
|
|
|
// Run normal entry point.
|
2015-08-01 06:48:24 +00:00
|
|
|
return xe::Main();
|
2014-08-16 23:34:04 +00:00
|
|
|
}
|
2014-12-20 21:54:55 +00:00
|
|
|
|
|
|
|
#if defined _M_IX86
|
2015-05-02 10:42:51 +00:00
|
|
|
#pragma comment( \
|
|
|
|
linker, \
|
2015-08-07 03:17:01 +00:00
|
|
|
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
|
2014-12-20 21:54:55 +00:00
|
|
|
#elif defined _M_IA64
|
2015-05-02 10:42:51 +00:00
|
|
|
#pragma comment( \
|
|
|
|
linker, \
|
2015-08-07 03:17:01 +00:00
|
|
|
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
|
2014-12-20 21:54:55 +00:00
|
|
|
#elif defined _M_X64
|
2015-05-02 10:42:51 +00:00
|
|
|
#pragma comment( \
|
|
|
|
linker, \
|
2015-08-07 03:17:01 +00:00
|
|
|
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
|
2014-12-20 21:54:55 +00:00
|
|
|
#else
|
2015-05-02 10:42:51 +00:00
|
|
|
#pragma comment( \
|
|
|
|
linker, \
|
2015-08-07 03:17:01 +00:00
|
|
|
"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
|
2014-12-20 21:54:55 +00:00
|
|
|
#endif
|