Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
23 changes: 6 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
project(AlpineFaction)

if(NOT "${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
message(FATAL_ERROR "Only x86 (32 bit) platform is supported!")
message(FATAL_ERROR "Only x86 (32-bit) platforms are supported!")
endif()

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
Expand All @@ -13,27 +13,21 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(MSVC)
add_compile_definitions(
_CRT_SECURE_NO_WARNINGS
_CRT_SECURE_NO_DEPRECATE
_CRT_NONSTDC_NO_DEPRECATE
_SCL_SECURE_NO_WARNINGS
_USE_MATH_DEFINES
)
endif()

# Set target Windows version to XP SP3
# Set our target version to Windows 7.
add_compile_definitions(
WINVER=0x0501
# needed for PROCESS_DEP_ENABLE on MinGW
_WIN32_WINNT=0x0601
_WIN32_IE=0x0501
# needed for GetModuleFileNameEx resolution before Windows 7
PSAPI_VERSION=1
NTDDI_VERSION=0x06010000
WINVER=0x0601
_WIN32_IE=0x0800
ALPINE_FACTION
"$<$<CONFIG:Debug>:DEBUG>"
)

if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO")
else()
# Use static linking
Expand All @@ -52,11 +46,6 @@ else()
add_compile_options(-msse2)
endif()

# FIXME
# if(MSVC)
# set(CMAKE_GENERATOR_TOOLSET v141_xp)
# endif()

# Macros

macro(enable_warnings target)
Expand Down
4 changes: 3 additions & 1 deletion common/include/common/utils/mem-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
template <typename T, size_t N>
class MemPool
{
using Slot = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
struct Slot {
alignas(T) std::byte buffer[sizeof(T)];
};

class Delete
{
Expand Down
23 changes: 17 additions & 6 deletions common/src/utils/os-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@
#include <intrin.h>
#endif

std::string get_os_version()
{
OSVERSIONINFO ver_info;
std::string get_os_version() {
OSVERSIONINFOA ver_info{};
ver_info.dwOSVersionInfoSize = sizeof(ver_info);
if (!GetVersionEx(&ver_info))
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
if (!GetVersionExA(&ver_info)) {
THROW_WIN32_ERROR("GetVersionEx failed");

return std::format("{}.{}.{}", ver_info.dwMajorVersion, ver_info.dwMinorVersion, ver_info.dwBuildNumber);
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
return std::format(
"{}.{}.{}",
ver_info.dwMajorVersion,
ver_info.dwMinorVersion,
ver_info.dwBuildNumber
);
}

std::string get_real_os_version()
Expand Down
2 changes: 1 addition & 1 deletion crash_handler/MiniDumpHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool MiniDumpHelper::is_data_section_needed(const WCHAR* module_name)

// Compare the name with the list of known names and decide
for (const auto& name : m_known_modules) {
if (wcsicmp(file_name, name.c_str()) == 0) {
if (_wcsicmp(file_name, name.c_str()) == 0) {
return true;
}
}
Expand Down
1 change: 0 additions & 1 deletion editor_patch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static int g_current_level_version = -1;

static const auto g_editor_app = reinterpret_cast<std::byte*>(0x006F9DA0);


void *GetMainFrame()
{
return struct_field_ref<void*>(g_editor_app, 0xC8);
Expand Down
55 changes: 32 additions & 23 deletions game_patch/graphics/d3d11/gr_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace df::gr::d3d11
Renderer::Renderer(HWND hwnd) : hwnd_{hwnd}, d3d11_lib_{L"d3d11.dll"}
{
if (!d3d11_lib_) {
RF_DEBUG_ERROR("Failed to load d3d11.dll");
rf::fatal_error("Failed to load d3d11.dll");
}
init_device();
init_swap_chain(hwnd);
Expand Down Expand Up @@ -104,7 +104,7 @@ namespace df::gr::d3d11
{
auto pD3D11CreateDevice = d3d11_lib_.get_proc_address<PFN_D3D11_CREATE_DEVICE>("D3D11CreateDevice");
if (!pD3D11CreateDevice) {
RF_DEBUG_ERROR("Cannot find D3D11CreateDevice procedure");
rf::fatal_error("Cannot find D3D11CreateDevice procedure");
}

// D3D_FEATURE_LEVEL feature_levels[] = {
Expand All @@ -118,29 +118,38 @@ namespace df::gr::d3d11
// };

DWORD flags = 0;
//#ifndef NDEBUG
// Requires Windows 10 SDK
//flags |= D3D11_CREATE_DEVICE_DEBUG;
//#endif
D3D_FEATURE_LEVEL feature_level_supported;
DF_GR_D3D11_CHECK_HR(
pD3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
// feature_levels,
// std::size(feature_levels),
nullptr,
0,
D3D11_SDK_VERSION,
&device_,
&feature_level_supported,
&context_
)
#ifndef NDEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
CREATE_DEVICE:
#endif
D3D_FEATURE_LEVEL feature_level_supported{};
HRESULT hr = pD3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
flags,
// feature_levels,
// std::size(feature_levels),
nullptr,
0,
D3D11_SDK_VERSION,
&device_,
&feature_level_supported,
&context_
);

init_error(device_);
#ifndef NDEBUG
if (hr == DXGI_ERROR_SDK_COMPONENT_MISSING &&
flags & D3D11_CREATE_DEVICE_DEBUG) {
xlog::warn( "D3D11 debug layer not available");
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
goto CREATE_DEVICE;
}
#endif

DF_GR_D3D11_CHECK_HR(hr);

set_dbg_breaks(device_);

xlog::info("D3D11 feature level: 0x{:x}", static_cast<int>(feature_level_supported));
}
Expand Down
42 changes: 29 additions & 13 deletions game_patch/graphics/d3d11/gr_d3d11.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <source_location>
#include <concepts>
#include <d3d11.h>
#include <common/ComPtr.h>
#include <common/DynamicLinkLibrary.h>
Expand Down Expand Up @@ -115,24 +117,38 @@ namespace df::gr::d3d11
int render_target_bm_handle_ = -1;
};

void init_error(ID3D11Device* device);
void fatal_error(HRESULT hr);
void set_dbg_breaks(ID3D11Device* device);
void fatal_gr_error(
HRESULT hr,
const std::source_location& loc = std::source_location::current()
);

template<typename T>
static inline void check_hr(HRESULT hr, T what)
{
template <std::invocable F>
inline void check_hr(
const HRESULT hr,
F&& what,
const std::source_location& loc = std::source_location::current()
) {
if (FAILED(hr)) {
if constexpr (std::is_pointer_v<T>) {
xlog::error("D3D11 API returned error: {}", what);
}
else {
what();
}
fatal_error(hr);
what();
fatal_gr_error(hr, loc);
}
}

#define DF_GR_D3D11_CHECK_HR(code) { auto func_name = __func__; check_hr(code, [=]() { xlog::error("D3D11 call failed: {} (function {} in line {})", #code, func_name, __LINE__); }); }
#define DF_GR_D3D11_CHECK_HR(code) { \
const char* const func_name = __func__; \
::df::gr::d3d11::check_hr( \
code, \
[=] { \
::xlog::error( \
"D3D11 call failed: {} (function {} in line {})", \
#code, \
func_name, \
__LINE__ \
); \
} \
); \
}

static inline int pack_color(const rf::Color& color)
{
Expand Down
41 changes: 24 additions & 17 deletions game_patch/graphics/d3d11/gr_d3d11_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,39 @@ namespace df::gr::d3d11
}
}

void init_error(ID3D11Device* device)
{
void set_dbg_breaks(ID3D11Device* const device) {
g_device = device;
ComPtr<ID3D11Debug> debug;
if (SUCCEEDED(device->QueryInterface(__uuidof(ID3D11Debug), (void**)&debug))) {

ComPtr<ID3D11InfoQueue> info_queue;
if (SUCCEEDED(debug->QueryInterface(__uuidof(ID3D11InfoQueue), (void**)&info_queue))) {
#ifndef NDEBUG
ComPtr<ID3D11Debug> debug{};
if (SUCCEEDED(device->QueryInterface(__uuidof(ID3D11Debug), debug.put_void()))) {
ComPtr<ID3D11InfoQueue> info_queue{};
if (SUCCEEDED(
debug->QueryInterface(__uuidof(ID3D11InfoQueue), info_queue.put_void())
)) {
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
#endif
}
}
#endif
}

void fatal_error(HRESULT hr)
{
const char* code_name = get_hresult_code_name(hr);
std::string error_desc = get_win32_error_description(hr);
xlog::error("D3D11 result: {:x} ({})\n{}", static_cast<unsigned>(hr), code_name, error_desc);
void fatal_gr_error(const HRESULT hr, const std::source_location& loc) {
const char* const code_name = get_hresult_code_name(hr);
const std::string error_desc = get_win32_error_description(hr);
xlog::error(
"D3D11 result: 0x{:X} ({})\n{}",
static_cast<uint32_t>(hr),
code_name,
error_desc
);
if (hr == DXGI_ERROR_DEVICE_REMOVED) {
hr = g_device->GetDeviceRemovedReason();
code_name = get_hresult_code_name(hr);
xlog::error("Device removed reason: {:x} {}", hr, code_name);
const HRESULT hr = g_device->GetDeviceRemovedReason();
xlog::error(
"Device removed reason: 0x{:X} ({})",
static_cast<uint32_t>(hr),
get_hresult_code_name(hr)
);
}
RF_DEBUG_ERROR("D3D11 subsystem fatal error");
rf::fatal_error("D3D11 subsystem fatal error", loc);
}
}
16 changes: 8 additions & 8 deletions game_patch/misc/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ void __fastcall UiButton_create(rf::ui::Button& this_, int, const char *normal_b
rf::bm::get_dimensions(this_.selected_bitmap, &this_.w, &this_.h);
}
}
this_.text = strdup(text);
this_.text = _strdup(text);
this_.font = font;
}
FunHook UiButton_create_hook{0x004574D0, UiButton_create};

void __fastcall UiButton_set_text(rf::ui::Button& this_, int, const char *text, int font)
{
delete[] this_.text;
this_.text = strdup(text);
free(this_.text);
this_.text = _strdup(text);
this_.font = font;
}
FunHook UiButton_set_text_hook{0x00457710, UiButton_set_text};
Expand Down Expand Up @@ -287,7 +287,7 @@ void __fastcall UiLabel_create(rf::ui::Label& this_, int, rf::ui::Gadget *parent
const auto [text_w, text_h] = rf::gr::get_string_size(text, font);
this_.w = static_cast<int>(text_w / rf::ui::scale_x);
this_.h = static_cast<int>(text_h / rf::ui::scale_y);
this_.text = strdup(text);
this_.text = _strdup(text);
this_.font = font;
this_.align = rf::gr::ALIGN_LEFT;
this_.clr.set(0, 0, 0, 255);
Expand All @@ -310,16 +310,16 @@ void __fastcall UiLabel_create2(rf::ui::Label& this_, int, rf::ui::Gadget *paren
else {
this_.align = rf::gr::ALIGN_LEFT;
}
this_.text = strdup(text);
this_.text = _strdup(text);
this_.font = font;
this_.clr.set(0, 0, 0, 255);
}
FunHook UiLabel_create2_hook{0x00456C20, UiLabel_create2};

void __fastcall UiLabel_set_text(rf::ui::Label& this_, int, const char *text, int font)
{
delete[] this_.text;
this_.text = strdup(text);
free(this_.text);
this_.text = _strdup(text);
this_.font = font;
}
FunHook UiLabel_set_text_hook{0x00456DC0, UiLabel_set_text};
Expand Down Expand Up @@ -397,7 +397,7 @@ FunHook UiInputBox_render_hook{0x004570E0, UiInputBox_render};
void __fastcall UiCycler_add_item(rf::ui::Cycler& this_, int, const char *text, int font)
{
if (this_.num_items < rf::ui::Cycler::max_items) {
this_.items_text[this_.num_items] = strdup(text);
this_.items_text[this_.num_items] = _strdup(text);
this_.items_font[this_.num_items] = font;
++this_.num_items;
}
Expand Down
Loading
Loading