Open source Star Ruler 2 source code!
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
#ifdef _MSC_VER
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include "files.h"
|
||||
#include "threads.h"
|
||||
#ifdef __GNUC__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int move(const char* oldname, const char* newname) {
|
||||
if(rename(oldname, newname) == 0)
|
||||
return 0;
|
||||
|
||||
auto* source = fopen(oldname, "rb");
|
||||
if(source == nullptr)
|
||||
return 1;
|
||||
|
||||
auto* dest = fopen(newname, "wb");
|
||||
if(dest == nullptr)
|
||||
return 1;
|
||||
|
||||
char buffer[1024];
|
||||
size_t num;
|
||||
while((num = fread(buffer, 1, 1024, source)) > 0) {
|
||||
if(fwrite(buffer, 1, num, dest) != num)
|
||||
return 1;
|
||||
}
|
||||
|
||||
fclose(source);
|
||||
fclose(dest);
|
||||
|
||||
remove(oldname);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "Patching..." << std::endl;
|
||||
//Fix old mistakes
|
||||
remove("patcher.exe.tmp");
|
||||
|
||||
threads::sleep(100);
|
||||
|
||||
const std::string profile = path_join(getProfileRoot(), "patch/");
|
||||
|
||||
int errors = 0;
|
||||
|
||||
std::vector<std::string> deletions;
|
||||
{
|
||||
auto delFilename = path_join(profile, ".delete.txt");
|
||||
std::ifstream delList(delFilename, std::ios_base::in);
|
||||
if(delList.is_open()) {
|
||||
std::string line;
|
||||
while(delList.good()) {
|
||||
std::getline(delList, line);
|
||||
if(!line.empty())
|
||||
deletions.push_back(line);
|
||||
}
|
||||
}
|
||||
delList.close();
|
||||
remove(delFilename.c_str());
|
||||
}
|
||||
|
||||
std::function<void(const std::string&)> transferFolder;
|
||||
transferFolder = [&](const std::string& relPath) {
|
||||
std::vector<std::string> listing;
|
||||
if(listDirectory(profile + relPath, listing)) {
|
||||
for(auto f = listing.begin(), fend = listing.end(); f != fend; ++f) {
|
||||
auto path = path_join(path_join(profile, relPath), *f);
|
||||
if(isDirectory(path)) {
|
||||
makeDirectory(relPath + *f + "/");
|
||||
transferFolder(relPath + *f + "/");
|
||||
}
|
||||
else {
|
||||
auto patchName = path_join(profile, relPath) + "/" + *f;
|
||||
auto finalName = relPath + *f;
|
||||
|
||||
if(fileExists(finalName)) {
|
||||
auto tempName = finalName + ".tmp";
|
||||
|
||||
bool writable = false;
|
||||
for(unsigned i = 0; i < 20; ++i) {
|
||||
if(fileWritable(finalName)) {
|
||||
writable = true;
|
||||
break;
|
||||
}
|
||||
|
||||
threads::sleep(100);
|
||||
}
|
||||
|
||||
if(move(finalName.c_str(), tempName.c_str()) == 0) {
|
||||
if(move(patchName.c_str(), finalName.c_str()) == 0) {
|
||||
remove(tempName.c_str());
|
||||
std::cout << "Patched " << finalName << std::endl;
|
||||
}
|
||||
else {
|
||||
move(tempName.c_str(), finalName.c_str());
|
||||
std::cout << "Failed to patch " << finalName << std::endl;
|
||||
++errors;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Failed to patch " << finalName << std::endl;
|
||||
++errors;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(move(patchName.c_str(), finalName.c_str()) == 0) {
|
||||
std::cout << "Patched " << finalName << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "Failed to patch " << finalName << std::endl;
|
||||
++errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!relPath.empty()) {
|
||||
auto folder = path_join(profile, relPath);
|
||||
remove(folder.c_str());
|
||||
}
|
||||
};
|
||||
transferFolder("");
|
||||
|
||||
for(auto f = deletions.begin(), fend = deletions.end(); f != fend; ++f) {
|
||||
if(remove(f->c_str()) != 0) {
|
||||
std::cout << "Failed to delete " << *f << std::endl;
|
||||
++errors;
|
||||
}
|
||||
}
|
||||
|
||||
if(errors > 0) {
|
||||
std::cout << "Unabled to update " << errors << " files." << std::endl;
|
||||
std::cout << "Exiting..." << std::endl;
|
||||
threads::sleep(8000);
|
||||
}
|
||||
else {
|
||||
#ifdef _MSC_VER
|
||||
STARTUPINFOA startup;
|
||||
memset(&startup, 0, sizeof(startup));
|
||||
startup.cb = sizeof(startup);
|
||||
|
||||
PROCESS_INFORMATION process;
|
||||
memset(&process, 0, sizeof(process));
|
||||
|
||||
if(CreateProcessA(NULL, "\"Star Ruler 2.exe\"", NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &startup, &process) != FALSE) {
|
||||
CloseHandle(process.hProcess);
|
||||
CloseHandle(process.hThread);
|
||||
}
|
||||
#else
|
||||
if(fork() == 0) {
|
||||
execlp("./StarRuler2.sh", "./StarRuler2.sh", (char*)nullptr);
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return errors != 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{0ADCAA9F-2998-49CA-A58C-28D11046963F}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>patcher</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)..\..\..\</OutDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)..\..\..\</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>..\..\os\include\</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>..\..\lib\win32\</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Shlwapi.lib;osd.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>..\..\os\include\</AdditionalIncludeDirectories>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalLibraryDirectories>..\..\lib\win32\</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>Shlwapi.lib;os.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<IgnoreSpecificDefaultLibraries>
|
||||
</IgnoreSpecificDefaultLibraries>
|
||||
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
|
||||
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="patcher.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="patcher.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user