Skip to content

Commit

Permalink
Add launcher command line parameter to run legacy binaries from bin_l…
Browse files Browse the repository at this point in the history
…egacy
  • Loading branch information
kb-1000 committed Jan 10, 2025
1 parent b0f0afc commit 3329e8e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 60 deletions.
Binary file modified Star Ruler 2.exe
Binary file not shown.
28 changes: 20 additions & 8 deletions StarRuler2.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
set -eo pipefail

#Figure out where the script is located
if [[ -L "$0" ]]; then
script_loc="$(readlink "$0")"
Expand All @@ -7,15 +9,25 @@ else
fi
cd "$(dirname "$script_loc")"

bin_folder="bin"
args=()
for arg in "$@"; do
if [ "$arg" = "--legacy" ]; then
bin_folder="bin_legacy"
else
args+=("$arg")
fi
done

#Execute the right binary for this architecture
if [ `uname` == "Darwin" ]; then
chmod +x ./bin/osx64/StarRuler2.bin
DYLD_LIBRARY_PATH="./bin/osx64/:$DYLD_LIBRARY_PATH" exec ./bin/osx64/StarRuler2.bin $@
elif [ `uname -m` = "x86_64" ]; then
chmod +x ./bin/lin64/StarRuler2.bin
LD_LIBRARY_PATH="./bin/lin64/:$LD_LIBRARY_PATH" exec ./bin/lin64/StarRuler2.bin $@
if [ "$(uname)" = "Darwin" ]; then
chmod +x ./$bin_folder/osx64/StarRuler2.bin
DYLD_LIBRARY_PATH="./$bin_folder/osx64/:$DYLD_LIBRARY_PATH" exec ./$bin_folder/osx64/StarRuler2.bin "${args[@]}"
elif [ "$(uname -m)" = "x86_64" ]; then
chmod +x ./$bin_folder/lin64/StarRuler2.bin
LD_LIBRARY_PATH="./$bin_folder/lin64/:$LD_LIBRARY_PATH" exec ./$bin_folder/lin64/StarRuler2.bin "${args[@]}"
else
chmod +x ./bin/lin32/StarRuler2.bin
LD_LIBRARY_PATH="./bin/lin32/:$LD_LIBRARY_PATH" exec ./bin/lin32/StarRuler2.bin $@
chmod +x ./$bin_folder/lin32/StarRuler2.bin
LD_LIBRARY_PATH="./$bin_folder/lin32/:$LD_LIBRARY_PATH" exec ./$bin_folder/lin32/StarRuler2.bin "${args[@]}"
fi;
# vim: set ff=unix:
38 changes: 38 additions & 0 deletions source/loader/SR2Loader/HybridCRT.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup Condition="'$(HybridCrtConfiguration)'==''">
<HybridCrtConfiguration>$(Configuration)</HybridCrtConfiguration>
</PropertyGroup>

<ItemDefinitionGroup Condition="'$(HybridCrtConfiguration)'=='Debug'">
<ClCompile>
<!-- We use MultiThreadedDebug, rather than MultiThreadedDebugDLL, to avoid DLL dependencies on VCRUNTIME140d.dll and MSVCP140d.dll. -->
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<!-- Link statically against the runtime and STL, but link dynamically against the CRT by ignoring the static CRT
lib and instead linking against the Universal CRT DLL import library. This "hybrid" linking mechanism is
supported according to the CRT maintainer. Dynamic linking against the CRT makes the binaries a bit smaller
than they would otherwise be if the CRT, runtime, and STL were all statically linked in. -->
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries);libucrtd.lib</IgnoreSpecificDefaultLibraries>
<AdditionalOptions>%(AdditionalOptions) /defaultlib:ucrtd.lib</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(HybridCrtConfiguration)'=='Release'">
<ClCompile>
<!-- We use MultiThreaded, rather than MultiThreadedDLL, to avoid DLL dependencies on VCRUNTIME140.dll and MSVCP140.dll. -->
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<!-- Link statically against the runtime and STL, but link dynamically against the CRT by ignoring the static CRT
lib and instead linking against the Universal CRT DLL import library. This "hybrid" linking mechanism is
supported according to the CRT maintainer. Dynamic linking against the CRT makes the binaries a bit smaller
than they would otherwise be if the CRT, runtime, and STL were all statically linked in. -->
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries);libucrt.lib</IgnoreSpecificDefaultLibraries>
<AdditionalOptions>%(AdditionalOptions) /defaultlib:ucrt.lib</AdditionalOptions>
</Link>
</ItemDefinitionGroup>

</Project>
41 changes: 23 additions & 18 deletions source/loader/SR2Loader/SR2Loader.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
Expand All @@ -11,15 +11,17 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ConfigurationType>Application</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{93B31A9C-7169-47CA-80DB-CF3BB398291E}</ProjectGuid>
<RootNamespace>SR2Loader</RootNamespace>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
Expand All @@ -28,6 +30,9 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
<Import Project="$(MSBuildThisFileDirectory)\HybridCRT.props" Label="HybridCRT" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
Expand All @@ -37,14 +42,15 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)\..\..\..\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TargetName>Star Ruler 2</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -53,23 +59,19 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MinSpace</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<Optimization>MinSpace</Optimization>
<FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<ExceptionHandling>false</ExceptionHandling>
<BufferSecurityCheck>false</BufferSecurityCheck>
<CompileAsManaged>false</CompileAsManaged>
<DebugInformationFormat>None</DebugInformationFormat>
</ClCompile>
<Link>
<GenerateDebugInformation>false</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Windows</SubSystem>
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand All @@ -84,6 +86,9 @@
<ItemGroup>
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
<None Include="HybridCRT.props" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
1 change: 1 addition & 0 deletions source/loader/SR2Loader/SR2Loader.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\sr2.ico" />
<None Include="HybridCRT.props" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
Expand Down
101 changes: 69 additions & 32 deletions source/loader/main.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@
#include <Windows.h>

static inline __forceinline __declspec(allocator) wchar_t* wstrdup(const wchar_t *_String) {
size_t i = 0;
while (_String[i] != 0)
i++;
wchar_t* ret = (wchar_t *)HeapAlloc(GetProcessHeap(), 0, sizeof(wchar_t) * (i + 1));
wsprintfW(ret, L"%s", _String);
return ret;
}
#include <windows.h>
#include <stdint.h>
#include <string.h>

DWORD __stdcall mainCRTStartup(LPVOID p) {
//Create command, passing all of our arguments
LPWSTR cmdline = GetCommandLineW();
wchar_t search = ' ';
if (cmdline[0] == '"') {
search = '"';
cmdline++;
}
while (cmdline[0] != 0) {
cmdline++;
if (cmdline[0] == search) {
cmdline++;
break;
int wmain(int argc, wchar_t* argv[]) {

STARTUPINFOW startup = {};
startup.cb = sizeof(startup);

PROCESS_INFORMATION process = {};

wchar_t* path = L"bin\\win64\\Star Ruler 2.exe";

size_t size = 0;
for (int i = 0; i < argc; i++) {
if (wcscmp(argv[i], L"--legacy") == 0) {
path = L"bin_legacy\\win64\\Star Ruler 2.exe";
}
else {
// worst-case estimate of expected escaped argument size, two output characters for every input character
// (to handle quotes and backslashes), one separator space and a leading and trailing quote
// first argument has no space, use it for the null terminator
size += wcslen(argv[i]) * 2 + 3;
}
}
while (cmdline[0] == ' ') {
cmdline++;
}

STARTUPINFOW startup;
SecureZeroMemory(&startup, sizeof(startup));
startup.cb = sizeof(startup);
//Create command, passing all of our arguments

wchar_t* cmdline = malloc(size * sizeof(wchar_t));
wchar_t* cur = cmdline;

PROCESS_INFORMATION process;
SecureZeroMemory(&process, sizeof(process));
for (int i = 0; i < argc; i++) {
if (wcscmp(argv[i], L"--legacy") == 0) {
continue;
}

for (wchar_t* arg = argv[i]; *arg != L'\0'; arg++) {
if (i != 0) {
*cur++ = L' ';
}
*cur++ = L'"';
switch (*arg) {
case L'"':
*cur++ = L'\\';
*cur++ = L'"';
arg++;
break;
case L'\\':
int backslashes = 0;
do {
backslashes++;
} while (arg[backslashes] == L'\\');
if (arg[backslashes] != 0 && arg[backslashes] != L'"') {
for (int j = 0; j < backslashes; j++) {
*cur++ = L'\\';
}
}
else {
for (int j = 0; j < backslashes; j++) {
*cur++ = L'\\';
*cur++ = L'\\';
}
}
arg += backslashes;
break;
default:
*cur++ = *arg++;
}
*cur++ = L'"';
}
}
*cur = L'\0';

if(CreateProcessW(L"bin\\win64\\Star Ruler 2.exe", wstrdup(cmdline), NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &startup, &process) != FALSE) {
if (CreateProcessW(path, cmdline, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &startup, &process) != FALSE) {
CloseHandle(process.hProcess);
CloseHandle(process.hThread);
}
free(cmdline);
return 0;
}
6 changes: 4 additions & 2 deletions source/msvc/Star Ruler 2/Star Ruler 2.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32407.337
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Star Ruler 2", "Star Ruler 2.vcxproj", "{50CA85BE-6640-4824-8CB6-9D65F77518F6}"
ProjectSection(ProjectDependencies) = postProject
Expand Down Expand Up @@ -89,7 +89,9 @@ Global
{A1E1942C-36D2-4824-9ECB-B4727543E958}.Non-Steam Release|x64.ActiveCfg = Release|x64
{A1E1942C-36D2-4824-9ECB-B4727543E958}.Non-Steam Release|x64.Build.0 = Release|x64
{93B31A9C-7169-47CA-80DB-CF3BB398291E}.Debug|x64.ActiveCfg = Debug|x64
{93B31A9C-7169-47CA-80DB-CF3BB398291E}.Debug|x64.Build.0 = Debug|x64
{93B31A9C-7169-47CA-80DB-CF3BB398291E}.Non-Steam Release|x64.ActiveCfg = Release|x64
{93B31A9C-7169-47CA-80DB-CF3BB398291E}.Non-Steam Release|x64.Build.0 = Release|x64
{ABEA2C04-40F0-4D98-9191-A026E1D0DF07}.Debug|x64.ActiveCfg = Debug|x64
{ABEA2C04-40F0-4D98-9191-A026E1D0DF07}.Debug|x64.Build.0 = Debug|x64
{ABEA2C04-40F0-4D98-9191-A026E1D0DF07}.Non-Steam Release|x64.ActiveCfg = Release|x64
Expand Down

0 comments on commit 3329e8e

Please sign in to comment.