Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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
  •  
  •  
  •  
29 changes: 11 additions & 18 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,18 +46,17 @@ else()
add_compile_options(-msse2)
endif()

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

# Macros

macro(enable_warnings target)
if(NOT MSVC)
target_compile_options(${target} PRIVATE -Wall -Wextra -Wundef)
else()
target_compile_options(${target} PRIVATE /W3)
target_compile_options(${target} PRIVATE
/W3
/w34100 # Unused parameters.
/w34189 # Unused local variables.
)
endif()
endmacro()

Expand Down
64 changes: 32 additions & 32 deletions common/include/common/utils/mem-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
#include <memory>

template <typename T, size_t N>
class MemPool
{
using Slot = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
class MemPool {
struct Slot {
alignas(T) std::byte buf[sizeof(T)];
};

class Delete
{
MemPool* pool;
class Delete {
MemPool* pool{};

public:
Delete(MemPool* pool) : pool(pool) {}
explicit Delete(MemPool* pool)
: pool(pool) {
}

void operator()(T* ptr) const
{
pool->release(ptr);
void operator()(this const Delete& self, T* const ptr) {
self.pool->release(ptr);
}
};

std::vector<Slot*> free_slots;
std::vector<std::unique_ptr<Slot[]>> pages;
std::vector<Slot*> free_slots{};
std::vector<std::unique_ptr<Slot[]>> pages{};

public:
using Pointer = std::unique_ptr<T, Delete>;
Expand All @@ -31,36 +32,35 @@ class MemPool
MemPool(const MemPool&) = delete;
MemPool& operator=(const MemPool&) = delete;

Pointer alloc()
{
return {acquire(), {this}};
Pointer alloc(this MemPool& self) {
return Pointer{self.acquire(), Delete{&self}};
}

private:
void alloc_page()
{
pages.push_back(std::make_unique<Slot[]>(N));
auto& page = pages.back();
free_slots.reserve(free_slots.size() + N);
void alloc_page(this MemPool& self) {
self.pages.push_back(std::make_unique<Slot[]>(N));
const std::unique_ptr<Slot[]>& page = self.pages.back();

self.free_slots.reserve(self.free_slots.size() + N);
for (std::size_t i = 0; i < N; ++i) {
Slot& slot = page[i];
free_slots.push_back(&slot);
self.free_slots.push_back(&slot);
}
}

T* acquire()
{
if (free_slots.empty()) {
alloc_page();
T* acquire(this MemPool& self) {
if (self.free_slots.empty()) {
self.alloc_page();
}
Slot* p = free_slots.back();
free_slots.pop_back();
return ::new (p) T;
Slot* const slot = self.free_slots.back();
self.free_slots.pop_back();
T* const ptr = reinterpret_cast<T*>(slot->buf);
return std::construct_at(ptr);
}

void release(T* p)
{
p->~T();
free_slots.push_back(std::launder(reinterpret_cast<Slot*>(p)));
void release(this MemPool& self, T* const ptr) {
std::destroy_at(ptr);
Slot* const slot = reinterpret_cast<Slot*>(ptr);
self.free_slots.push_back(slot);
}
};
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{};
const 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

check_hr(hr, [] { xlog::error("`D3D11CreateDevice` failed"); });

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
Loading
Loading