Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make game runnable on posix #14

Merged
merged 18 commits into from Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -141,6 +141,7 @@ endif()

if (APPLE)
target_link_libraries(${PROJECT} "-framework Cocoa")
target_compile_definitions(${PROJECT} PRIVATE COMPAT_STD_BYTE=1)
endif ()

if(EXISTS /Users/Marijn/Downloads/SDL2-2.0.7/build/libSDL2.dylib)
Expand Down
2 changes: 1 addition & 1 deletion openloco.common.props
Expand Up @@ -51,7 +51,7 @@
C4549: 'operator': operator before comma has no effect; did you intend 'operator'?
C4555: expression has no effect; expected expression with side-effect
-->
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_USE_MATH_DEFINES;SDL_MAIN_HANDLED;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_USE_MATH_DEFINES;SDL_MAIN_HANDLED;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<TreatWarningAsError>true</TreatWarningAsError>
Expand Down
57 changes: 57 additions & 0 deletions src/openloco/console.cpp
@@ -0,0 +1,57 @@
#include "console.h"
#include "utility/string.hpp"
#include <stdarg.h>

namespace openloco::console
{
static int _group = 0;

static void vwrite(FILE* buffer, const char* format, va_list args)
{
for (int i = 0; i < _group; i++)
{
fprintf(buffer, " ");
}

vprintf(format, args);
fprintf(buffer, "\n");
}

void log(const char* format, ...)
{
va_list args;
va_start(args, format);
vwrite(stdout, format, args);
va_end(args);
}

void error(const char* format, ...)
{
va_list args;
va_start(args, format);
vwrite(stderr, format, args);
va_end(args);
}

void group(const char* format, ...)
{
for (int i = 0; i < _group; i++)
{
printf(" ");
}

printf("> ");
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");

_group++;
}

void group_end()
{
_group--;
}
}
10 changes: 10 additions & 0 deletions src/openloco/console.h
@@ -0,0 +1,10 @@
#pragma once

namespace openloco::console
{
void log(const char* format, ...);
void error(const char* format, ...);

void group(const char* format, ...);
void group_end();
}
8 changes: 7 additions & 1 deletion src/openloco/envionment.cpp
Expand Up @@ -87,6 +87,12 @@ namespace openloco::environment
path = platform::prompt_directory("Select your Locomotion install path.");
if (validate_loco_install_path(path))
{
#ifdef _OPENLOCO_USE_BOOST_FS_
cfg.loco_install_path = path.make_preferred().string();
#else
cfg.loco_install_path = path.make_preferred().u8string();
#endif
config::write_new_config();
return path;
}

Expand Down Expand Up @@ -139,7 +145,7 @@ namespace openloco::environment
static fs::path get_sub_path(path_id id)
{
static constexpr const char* paths[] = {
"Data/G1.DAT",
"Data/g1.DAT",
"Data/PLUGIN.DAT",
"Data/PLUGIN2.DAT",
"Data/CSS1.DAT",
Expand Down
72 changes: 71 additions & 1 deletion src/openloco/interop/hook.cpp
@@ -1,3 +1,4 @@
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -190,7 +191,7 @@ namespace openloco::interop
register_hook(
address,
[](registers& regs) -> uint8_t {
std::printf(" fn %u\n", passAddress);
std::printf(" fn %" PRIuPTR "\n", passAddress);
return 0;
});
}
Expand All @@ -212,6 +213,75 @@ namespace openloco::interop
write_memory(address, data, sizeof(data));
}

static void* _smallHooks;
static uint8_t* _offset;

static void* make_jump(uint32_t address, void* fn)
{

if (!_smallHooks)
{
size_t size = 20 * 500;
#ifdef _WIN32
_hookTableAddress = VirtualAllocEx(GetCurrentProcess(), NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
#else
_smallHooks = mmap(NULL, size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (_smallHooks == MAP_FAILED)
{
perror("mmap");
exit(1);
}
_offset = static_cast<uint8_t*>(_smallHooks);
#endif // _WIN32
}

int i = 0;

_offset[i++] = 0x58; // POP EAX

_offset[i++] = 0x68; // PUSH
write_address_strictalias(&_offset[i], address);
i += 4;

_offset[i++] = 0x50; // PUSH EAX

uintptr_t base = reinterpret_cast<uintptr_t>(_offset);
uintptr_t addr = reinterpret_cast<uintptr_t>(fn);

_offset[i++] = 0xE9; // JMP
write_address_strictalias(&_offset[i], addr - base - 12);
i += 4;

uint8_t* ptr = _offset;

_offset += i;
return ptr;
}

void hook_dump(uint32_t address, void* fn)
{
uint8_t data[4] = { 0 };

void* hook = make_jump(address, fn);

uintptr_t addr = reinterpret_cast<uintptr_t>(hook);

write_address_strictalias(&data[0], addr);

write_memory(address, data, sizeof(data));
}

void hook_lib(uint32_t address, void* fn)
{
uint8_t data[4] = { 0 };

uintptr_t addr = reinterpret_cast<uintptr_t>(fn);

write_address_strictalias(&data[0], addr);

write_memory(address, data, sizeof(data));
}

void write_nop(uint32_t address, size_t count)
{
std::vector<uint8_t> buffer(count, 0x90);
Expand Down