From 7d6ea1f5cdbf317c475286f8701fd815fb2d6f62 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 4 Jan 2017 23:36:36 +0000 Subject: [PATCH] Create DLL for openrct2 with win as a wrapper --- openrct2.sln | 15 +++++ src/openrct2-dll/openrct2-dll.cpp | 80 ++++++++++++++++++++++++ src/openrct2-dll/openrct2-dll.vcxproj | 62 +++++++++++++++++++ src/openrct2-win/openrct2-win.cpp | 88 ++------------------------- src/openrct2-win/openrct2-win.vcxproj | 5 +- src/openrct2/OpenRCT2.h | 2 +- 6 files changed, 164 insertions(+), 88 deletions(-) create mode 100644 src/openrct2-dll/openrct2-dll.cpp create mode 100644 src/openrct2-dll/openrct2-dll.vcxproj diff --git a/openrct2.sln b/openrct2.sln index 7933d2540c..3998763de4 100644 --- a/openrct2.sln +++ b/openrct2.sln @@ -17,6 +17,12 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{480B577D-4E4A-4757-9A42-28A9AD33E6B0}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "openrct2-win", "src\openrct2-win\openrct2-win.vcxproj", "{7A9A57D5-7006-4208-A290-5491BA3C8808}" + ProjectSection(ProjectDependencies) = postProject + {7B8DB129-79EF-417E-B372-8A18E009D261} = {7B8DB129-79EF-417E-B372-8A18E009D261} + {D24D94F6-2A74-480C-B512-629C306CE92F} = {D24D94F6-2A74-480C-B512-629C306CE92F} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "openrct2-dll", "src\openrct2-dll\openrct2-dll.vcxproj", "{7B8DB129-79EF-417E-B372-8A18E009D261}" ProjectSection(ProjectDependencies) = postProject {D24D94F6-2A74-480C-B512-629C306CE92F} = {D24D94F6-2A74-480C-B512-629C306CE92F} EndProjectSection @@ -58,6 +64,14 @@ Global {7A9A57D5-7006-4208-A290-5491BA3C8808}.Release|Win32.Build.0 = Release|Win32 {7A9A57D5-7006-4208-A290-5491BA3C8808}.Release|x64.ActiveCfg = Release|x64 {7A9A57D5-7006-4208-A290-5491BA3C8808}.Release|x64.Build.0 = Release|x64 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Debug|Win32.Build.0 = Debug|Win32 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Debug|x64.ActiveCfg = Debug|x64 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Debug|x64.Build.0 = Debug|x64 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Release|Win32.ActiveCfg = Release|Win32 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Release|Win32.Build.0 = Release|Win32 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Release|x64.ActiveCfg = Release|x64 + {7B8DB129-79EF-417E-B372-8A18E009D261}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -67,5 +81,6 @@ Global {57E60BA1-FB76-4316-909E-C1449C142327} = {480B577D-4E4A-4757-9A42-28A9AD33E6B0} {62B020FA-E4FB-4C6E-B32A-DC999470F155} = {480B577D-4E4A-4757-9A42-28A9AD33E6B0} {7A9A57D5-7006-4208-A290-5491BA3C8808} = {2202A816-377D-4FA0-A7AF-7D4105F8A4FB} + {7B8DB129-79EF-417E-B372-8A18E009D261} = {2202A816-377D-4FA0-A7AF-7D4105F8A4FB} EndGlobalSection EndGlobal diff --git a/src/openrct2-dll/openrct2-dll.cpp b/src/openrct2-dll/openrct2-dll.cpp new file mode 100644 index 0000000000..622160bb42 --- /dev/null +++ b/src/openrct2-dll/openrct2-dll.cpp @@ -0,0 +1,80 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#define WIN32_LEAN_AND_MEAN + +#include +#include +#include +#include +#include + +#define DLLEXPORT extern "C" __declspec(dllexport) + +static char * * GetCommandLineArgs(int argc, wchar_t * * argvW); +static void FreeCommandLineArgs(int argc, char * * argv); +static char * ConvertUTF16toUTF8(const wchar_t * src); + +DLLEXPORT int LaunchOpenRCT2(int argc, wchar_t * * argvW) +{ + char * * argv = GetCommandLineArgs(argc, argvW); + if (argv == nullptr) + { + puts("Unable to fetch command line arguments."); + return -1; + } + + int exitCode = RunOpenRCT2(argc, argv); + FreeCommandLineArgs(argc, argv); + return exitCode; +} + +static char * * GetCommandLineArgs(int argc, wchar_t * * argvW) +{ + // Allocate UTF-8 strings + char * * argv = (char * *)malloc(argc * sizeof(char *)); + if (argv == nullptr) + { + return false; + } + + // Convert to UTF-8 + for (int i = 0; i < argc; i++) + { + argv[i] = ConvertUTF16toUTF8(argvW[i]); + } + + return argv; +} + +static void FreeCommandLineArgs(int argc, char * * argv) +{ + // Free argv + for (int i = 0; i < argc; i++) + { + free(argv[i]); + } + free(argv); +} + +static char * ConvertUTF16toUTF8(const wchar_t * src) +{ + int srcLen = lstrlenW(src); + int sizeReq = WideCharToMultiByte(CP_UTF8, 0, src, srcLen, nullptr, 0, nullptr, nullptr); + char * result = (char *)calloc(1, sizeReq + 1); + WideCharToMultiByte(CP_UTF8, 0, src, srcLen, result, sizeReq, nullptr, nullptr); + return result; +} diff --git a/src/openrct2-dll/openrct2-dll.vcxproj b/src/openrct2-dll/openrct2-dll.vcxproj new file mode 100644 index 0000000000..3cb9c34dfe --- /dev/null +++ b/src/openrct2-dll/openrct2-dll.vcxproj @@ -0,0 +1,62 @@ + + + + ..\..\ + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {7B8DB129-79EF-417E-B372-8A18E009D261} + openrct2-dll + openrct2-dll + + + DynamicLibrary + + + + openrct2 + $(SolutionDir)bin;$(LibraryPath) + + + + __NOENTRYPOINT__;%(PreprocessorDefinitions) + + + + + $(IntDir)\%(RelativeDir) + $(OPENRCT2_CL_ADDITIONALOPTIONS) %(AdditionalOptions) + + + openrct2-lib.lib;%(AdditionalDependencies) + + + MachineX86 + MachineX64 + + + + + + + + + + \ No newline at end of file diff --git a/src/openrct2-win/openrct2-win.cpp b/src/openrct2-win/openrct2-win.cpp index 29736969cf..24084a6c27 100644 --- a/src/openrct2-win/openrct2-win.cpp +++ b/src/openrct2-win/openrct2-win.cpp @@ -16,98 +16,18 @@ #define WIN32_LEAN_AND_MEAN -#include -#include #include -#include -#include // Enable visual styles #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") -static bool GetCommandLineArgs(int * outargc, char * * * outargv); -static void FreeCommandLineArgs(int argc, char * * argv); -static char * ConvertUTF16toUTF8(const wchar_t * src); +#define DLLIMPORT extern "C" +DLLIMPORT int LaunchOpenRCT2(int argc, wchar_t * * argv); /** * Windows entry point to OpenRCT2 with a console window using a traditional C main function. */ -int main(int argc, char * * argv) +int wmain(int argc, wchar_t * * argvW, wchar_t * envp) { - if (!GetCommandLineArgs(&argc, &argv)) - { - puts("Unable to fetch command line arguments."); - return -1; - } - - int exitCode = RunOpenRCT2(argc, argv); - - FreeCommandLineArgs(argc, argv); - return exitCode; -} - -/** - * Windows entry point to OpenRCT2 without a console window. - */ -int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -{ - int argc; - char * * argv; - if (!GetCommandLineArgs(&argc, &argv)) - { - puts("Unable to fetch command line arguments."); - return -1; - } - - int exitCode = RunOpenRCT2(argc, argv); - - FreeCommandLineArgs(argc, argv); - return exitCode; -} - -static bool GetCommandLineArgs(int * outargc, char * * * outargv) -{ - // Get UTF-16 command line arguments - int argc; - LPWSTR cmdLine = GetCommandLineW(); - LPWSTR * argvW = CommandLineToArgvW(cmdLine, &argc); - - // Allocate UTF-8 strings - char * * argv = (char * *)malloc(argc * sizeof(char *)); - if (argv == nullptr) - { - return false; - } - - // Convert to UTF-8 - for (int i = 0; i < argc; i++) - { - argv[i] = ConvertUTF16toUTF8(argvW[i]); - } - - // Free UTF-16 strings - LocalFree(argvW); - - *outargc = argc; - *outargv = argv; - return true; -} - -static void FreeCommandLineArgs(int argc, char * * argv) -{ - // Free argv - for (int i = 0; i < argc; i++) - { - free(argv[i]); - } - free(argv); -} - -static char * ConvertUTF16toUTF8(const wchar_t * src) -{ - int srcLen = lstrlenW(src); - int sizeReq = WideCharToMultiByte(CP_UTF8, 0, src, srcLen, nullptr, 0, nullptr, nullptr); - char * result = (char *)calloc(1, sizeReq + 1); - WideCharToMultiByte(CP_UTF8, 0, src, srcLen, result, sizeReq, nullptr, nullptr); - return result; + return LaunchOpenRCT2(argc, argvW); } diff --git a/src/openrct2-win/openrct2-win.vcxproj b/src/openrct2-win/openrct2-win.vcxproj index 67c40a3a0d..fd3c828888 100644 --- a/src/openrct2-win/openrct2-win.vcxproj +++ b/src/openrct2-win/openrct2-win.vcxproj @@ -45,9 +45,8 @@ $(OPENRCT2_CL_ADDITIONALOPTIONS) %(AdditionalOptions) - openrct2-lib.lib;%(AdditionalDependencies) - Console - Windows + openrct2.lib;%(AdditionalDependencies) + Console MachineX86 diff --git a/src/openrct2/OpenRCT2.h b/src/openrct2/OpenRCT2.h index e7d814f1d1..72c97060b7 100644 --- a/src/openrct2/OpenRCT2.h +++ b/src/openrct2/OpenRCT2.h @@ -88,7 +88,7 @@ extern "C" int cmdline_run(const char * * argv, int argc); #ifdef _MSC_VER - int RunOpenRCT2(int argc, char * *); + int RunOpenRCT2(int argc, char * * argv); #endif #ifdef __cplusplus