Skip to content

Commit

Permalink
Merge branch 'rafalh:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
is-this-c authored Nov 19, 2024
2 parents dd2f780 + 80be5a7 commit 83bcd15
Show file tree
Hide file tree
Showing 25 changed files with 102 additions and 48 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: CI

on: [push]
on:
push:
pull_request:

env:
BUILD_TYPE: Release
Expand Down
20 changes: 12 additions & 8 deletions common/include/common/error/error-utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <windows.h>
#include <ostream>
#include <sstream>
#include <string>
#include <exception>

inline std::string get_win32_error_description(DWORD error)
{
Expand All @@ -29,19 +29,23 @@ inline std::string get_win32_error_description(DWORD error)
return message;
}

inline void print_exception(const std::exception& e, std::ostream& stream, int level = 0)
inline void print_exception(const std::exception& e, std::string& buf, int level = 0)
{
stream << std::string(level, ' ') << e.what() << '\n';
for (int i = 0; i < level; ++i) {
buf += ' ';
}
buf += e.what();
buf += '\n';
try {
std::rethrow_if_nested(e);
} catch(const std::exception& e) {
print_exception(e, stream, level + 1);
print_exception(e, buf, level + 1);
} catch(...) {}
}

inline std::string generate_message_for_exception(const std::exception& e)
{
std::stringstream ss;
print_exception(e, ss);
return ss.str();
std::string buf;
print_exception(e, buf);
return buf;
}
1 change: 0 additions & 1 deletion common/include/common/utils/mem-pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <vector>
#include <memory>
#include <xlog/xlog.h>

template <typename T, size_t N>
class MemPool
Expand Down
5 changes: 3 additions & 2 deletions crash_handler/MainDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ LRESULT MainDlg::OnNotify([[ maybe_unused ]] WPARAM wparam, LPARAM lparam)
void MainDlg::OpenArchivedReport()
{
auto archived_report_path = GetCrashReportApp()->GetArchivedReportFilePath();
auto* ret = ShellExecuteA(GetHwnd(), nullptr, archived_report_path.c_str(), nullptr, nullptr, SW_SHOW);
if (reinterpret_cast<int>(ret) <= 32) {
auto result = ShellExecuteA(GetHwnd(), nullptr, archived_report_path.c_str(), nullptr, nullptr, SW_SHOW);
auto result_int = reinterpret_cast<INT_PTR>(result);
if (result_int <= 32) {
// ShellExecuteA failed - fallback to opening the directory
CString args;
args = "/select,";
Expand Down
4 changes: 2 additions & 2 deletions crash_handler/TextDumpHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ class TextDumpHelper {
{
MEMORY_BASIC_INFORMATION mbi;
std::vector<MEMORY_BASIC_INFORMATION> memory_map;
const char* addr = reinterpret_cast<const char*>(m_sys_info.lpMinimumApplicationAddress);
const void* addr = m_sys_info.lpMinimumApplicationAddress;
while (VirtualQueryEx(process, addr, &mbi, sizeof(mbi)) && addr <= m_sys_info.lpMaximumApplicationAddress) {
memory_map.push_back(mbi);
addr = reinterpret_cast<const char*>(mbi.BaseAddress) + mbi.RegionSize;
addr = reinterpret_cast<std::byte*>(mbi.BaseAddress) + mbi.RegionSize;
}
return memory_map;
}
Expand Down
6 changes: 2 additions & 4 deletions crash_handler/ZipHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ZipHelper

void add_file(const char* path, const char* name_in_zip)
{
zip_fileinfo zfi;
memset(&zfi, 0, sizeof(zfi));
zip_fileinfo zfi{};
get_file_time(path, &zfi.tmz_date, &zfi.dosDate);

int err = zipOpenNewFileInZip(m_zf, name_in_zip, &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
Expand All @@ -56,8 +55,7 @@ class ZipHelper

void add_file(const char* name_in_zip, std::string_view content)
{
zip_fileinfo zfi;
memset(&zfi, 0, sizeof(zfi));
zip_fileinfo zfi{};

int err = zipOpenNewFileInZip(m_zf, name_in_zip, &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
if (err != ZIP_OK)
Expand Down
8 changes: 8 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ Version 1.9.0 (not released yet)
- Do not load unnecessary VPPs in dedicated server mode
- Add level filename to "Level Initializing" console message
- Properly handle WM_PAINT in dedicated server, may improve performance (DF bug)
- Fix crash when `verify_level` command is run without a level being loaded
- Fix cockpit not rendering for any jeeps after the first one entered each level load
- Add `server_password` command

Contributions:

- [@is-this-c](https://github.com/is-this-c)
- Support `©` in TrueType fonts

Version 1.8.0 (released 2022-09-17)
-----------------------------------
Expand Down
1 change: 1 addition & 0 deletions editor_patch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ extern "C" DWORD DF_DLL_EXPORT Init([[maybe_unused]] void* unused)
"maps3.txt",
"maps4.txt",
"maps_df.txt",
nullptr,
};
write_mem_ptr(0x0041B813 + 1, maps_files_names);
write_mem_ptr(0x0041B824 + 1, maps_files_names);
Expand Down
7 changes: 3 additions & 4 deletions game_patch/graphics/d3d11/gr_d3d11_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace df::gr::d3d11

void PerFrameBuffer::update(ID3D11DeviceContext* device_context)
{
PerFrameBufferData data;
PerFrameBufferData data{};
data.time = rf::frametime_total_milliseconds / 1000.0f;

D3D11_MAPPED_SUBRESOURCE mapped_subres;
Expand Down Expand Up @@ -209,8 +209,7 @@ namespace df::gr::d3d11
device_context->Map(buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subres)
);

LightsBufferData data;
std::memset(&data, 0, sizeof(data));
LightsBufferData data{};
for (int i = 0; i < std::min(rf::gr::num_relevant_lights, LightsBufferData::max_point_lights); ++i) {
LightsBufferData::PointLight& gpu_light = data.point_lights[i];
// Make sure radius is never 0 because we divide by it
Expand Down Expand Up @@ -259,7 +258,7 @@ namespace df::gr::d3d11

void RenderModeBuffer::update_buffer(ID3D11DeviceContext* device_context)
{
RenderModeBufferData data;
RenderModeBufferData data{};
data.current_color = {
current_color_.red / 255.0f,
current_color_.green / 255.0f,
Expand Down
7 changes: 7 additions & 0 deletions game_patch/graphics/d3d11/gr_d3d11_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <patch_common/CodeInjection.h>
#include <patch_common/FunHook.h>
#include <common/utils/list-utils.h>
#include <float.h>
#include "../../rf/gr/gr.h"
#include "../../rf/os/os.h"
#include "../../rf/v3d.h"
Expand Down Expand Up @@ -47,6 +48,12 @@ namespace df::gr::d3d11
xlog::info("Initializing D3D11");
renderer.emplace(hwnd);
rf::os_add_msg_handler(msg_handler);

// Switch FPU to single-precision mode for backward compatibility
// Direct3D 8/9 does it automatically unless D3DCREATE_FPU_PRESERVE flag is used,
// but Direct3D 11 no longer does it.
// It is needed to keep old checksums in AC
_controlfp(_PC_24, _MCW_PC);
}

rf::bm::Format read_back_buffer(int x, int y, int w, int h, rf::ubyte *data)
Expand Down
4 changes: 2 additions & 2 deletions game_patch/graphics/d3d11/gr_d3d11_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ namespace df::gr::d3d11

void BoneTransformsBuffer::update(const CharacterInstance* ci, ID3D11DeviceContext* device_context)
{
BoneTransformsBufferData data;
// Note: if some matrices that are unused by skeleton are referenced by vertices and not get initialized
// bad things can happen even if weight is zero (e.g. in case of NaNs)
std::memset(&data, 0, sizeof(data));
BoneTransformsBufferData data{};

for (int i = 0; i < ci->base_character->num_bones; ++i) {
const Matrix43& bone_mat = ci->bone_transforms_final[i];
data.matrices[i] = convert_bone_matrix(bone_mat);
Expand Down
2 changes: 1 addition & 1 deletion game_patch/graphics/gr_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ GrNewFont::GrNewFont(std::string_view name) :
{0x9C, 0x9C},
{0x9F, 0x9F},
{0xA6, 0xA7},
{0xAA, 0xAB},
{0xA9, 0xAB},
{0xAE, 0xAE},
{0xB0, 0xB0},
{0xC0, 0xC2},
Expand Down
1 change: 0 additions & 1 deletion game_patch/misc/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <patch_common/CodeInjection.h>
#include <common/version/version.h>
#include <xlog/xlog.h>
#include <cstring>
#include "../rf/ui.h"
#include "../rf/gr/gr.h"
#include "../rf/gr/gr_font.h"
Expand Down
3 changes: 3 additions & 0 deletions game_patch/misc/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ void player_do_patch()
// Fix hud msg never disappearing in spectate mode
players_do_frame_hook.install();

// Fix jeep cockpit not rendering for any jeeps entered after the first
AsmWriter(0x004A77C3).jmp(0x004A77FB);

// Make sure scanner bitmap is a render target in player_allocate
write_mem<u8>(0x004A34BF + 1, rf::bm::FORMAT_RENDER_TARGET);

Expand Down
6 changes: 2 additions & 4 deletions game_patch/multi/level_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ static std::vector<std::string> unrar(const char* path, const char* output_dir,
{
char cmt_buf[16384];

RAROpenArchiveDataEx open_archive_data;
memset(&open_archive_data, 0, sizeof(open_archive_data));
RAROpenArchiveDataEx open_archive_data{};
open_archive_data.ArcName = const_cast<char*>(path);
open_archive_data.CmtBuf = cmt_buf;
open_archive_data.CmtBufSize = sizeof(cmt_buf);
Expand All @@ -125,9 +124,8 @@ static std::vector<std::string> unrar(const char* path, const char* output_dir,
}

std::vector<std::string> extracted_files;
struct RARHeaderData header_data;
struct RARHeaderData header_data{};
header_data.CmtBuf = nullptr;
memset(&open_archive_data.Reserved, 0, sizeof(open_archive_data.Reserved));

while (true) {
int code = RARReadHeader(archive_handle, &header_data);
Expand Down
1 change: 1 addition & 0 deletions game_patch/multi/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
const char* g_rcon_cmd_whitelist[] = {
"kick",
"level",
"server_password",
"map",
"ban",
"ban_ip",
Expand Down
1 change: 0 additions & 1 deletion game_patch/object/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <patch_common/AsmWriter.h>
#include <xlog/xlog.h>
#include <cassert>
#include <cstring>
#include "../rf/object.h"
#include "../rf/event.h"
#include "../rf/entity.h"
Expand Down
2 changes: 1 addition & 1 deletion game_patch/object/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void send_trigger_activate_packet(rf::Player* player, int trigger_uid, int32_t e
packet.header.size = sizeof(packet) - sizeof(packet.header);
packet.uid = trigger_uid;
packet.entity_handle = entity_handle;
rf::multi_io_send_reliable(player, reinterpret_cast<uint8_t*>(&packet), sizeof(packet), 0);
rf::multi_io_send_reliable(player, &packet, sizeof(packet), 0);
}

FunHook<void(int, int)> send_trigger_activate_packet_to_all_players_hook{
Expand Down
36 changes: 36 additions & 0 deletions game_patch/os/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <common/utils/list-utils.h>
#include <algorithm>
#include <patch_common/CallHook.h>
#include <patch_common/FunHook.h>
#include <patch_common/AsmWriter.h>

ConsoleCommand2 dot_cmd{
Expand Down Expand Up @@ -91,6 +92,39 @@ DcCommandAlias map_info_cmd{
level_info_cmd,
};

ConsoleCommand2 server_password_cmd{
"server_password",
[](std::optional<std::string> new_password) {
if (!rf::is_multi || !rf::is_server) {
rf::console::print("This command can only be run as a server!");
return;
}

if (new_password) {
rf::netgame.password = new_password.value().c_str();
rf::console::print("Server password set to: {}", rf::netgame.password);
}
else {
rf::netgame.password = "";
rf::console::print("Server password removed.");
}
},
"Set or remove the server password.",
"server_password <password>",
};

// only allow verify_level if a level is loaded (avoid a crash if command is run in menu)
FunHook<void()> verify_level_cmd_hook{
0x0045E1F0,
[]() {
if (rf::level.flags & rf::LEVEL_LOADED) {
verify_level_cmd_hook.call_target();
} else {
rf::console::print("No level loaded!");
}
}
};

static void register_builtin_command(const char* name, const char* description, uintptr_t addr)
{
static std::vector<std::unique_ptr<rf::console::Command>> builtin_commands;
Expand Down Expand Up @@ -192,4 +226,6 @@ void console_commands_init()
map_cmd.register_cmd();
level_info_cmd.register_cmd();
map_info_cmd.register_cmd();
server_password_cmd.register_cmd();
verify_level_cmd_hook.install();
}
1 change: 0 additions & 1 deletion game_patch/os/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#pragma once

#include <patch_common/Traits.h>
#include <functional>
#include <optional>
#include "../rf/os/console.h"
#include "../rf/localize.h"
Expand Down
3 changes: 1 addition & 2 deletions game_patch/purefaction/pf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ static void send_pf_announce_player_packet(rf::Player* player, pf_pure_status pu
// Send: server -> client
assert(rf::is_server);

pf_player_announce_packet announce_packet;
pf_player_announce_packet announce_packet{};
announce_packet.hdr.type = static_cast<uint8_t>(pf_packet_type::announce_player);
announce_packet.hdr.size = sizeof(announce_packet) - sizeof(announce_packet.hdr);
announce_packet.version = pf_announce_player_packet_version;
announce_packet.player_id = player->net_data->player_id;
announce_packet.is_pure = static_cast<uint8_t>(pure_status);
std::memset(&announce_packet.reserved, 0, sizeof(announce_packet.reserved));

auto player_list = SinglyLinkedList(rf::player_list);
for (auto& other_player : player_list) {
Expand Down
7 changes: 4 additions & 3 deletions launcher/AboutDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ void AboutDlg::OpenLicensingInfo()
}

std::wstring licensing_info_path = get_dir_from_path(buf) + L"\\licensing-info.txt";
HINSTANCE exec_result = ShellExecuteW(*this, L"open", licensing_info_path.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
if (reinterpret_cast<int>(exec_result) <= 32) {
xlog::error("ShellExecuteA failed {}", static_cast<void*>(exec_result));
HINSTANCE exec_res = ShellExecuteW(*this, L"open", licensing_info_path.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
auto exec_res_int = reinterpret_cast<INT_PTR>(exec_res);
if (exec_res_int <= 32) {
xlog::error("ShellExecute failed: {}", exec_res_int);
}
}
14 changes: 8 additions & 6 deletions launcher/MainDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ LRESULT MainDlg::OnUpdateCheck(WPARAM wparam, LPARAM lparam)
int result = MessageBoxA(chk_result.message.c_str(), "Dash Faction update is available!",
MB_OKCANCEL | MB_ICONEXCLAMATION);
if (result == IDOK) {
HINSTANCE exec_ret = ShellExecuteA(*this, "open", chk_result.url.c_str(), nullptr, nullptr, SW_SHOW);
if (reinterpret_cast<int>(exec_ret) <= 32) {
xlog::error("ShellExecuteA failed {}", static_cast<void*>(exec_ret));
HINSTANCE exec_res = ShellExecuteA(*this, "open", chk_result.url.c_str(), nullptr, nullptr, SW_SHOW);
auto exec_res_int = reinterpret_cast<INT_PTR>(exec_res);
if (exec_res_int <= 32) {
xlog::error("ShellExecuteA failed {}", exec_res_int);
}
EndDialog(0);
}
Expand Down Expand Up @@ -197,9 +198,10 @@ void MainDlg::OnBnClickedEditorBtn()
void MainDlg::OnSupportLinkClick()
{
xlog::info("Opening support channel");
HINSTANCE ret = ShellExecuteA(*this, "open", "https://discord.gg/bC2WzvJ", nullptr, nullptr, SW_SHOW);
if (reinterpret_cast<int>(ret) <= 32) {
xlog::error("ShellExecuteA failed {}", static_cast<void*>(ret));
HINSTANCE result = ShellExecuteA(*this, "open", "https://discord.gg/bC2WzvJ", nullptr, nullptr, SW_SHOW);
auto result_int = reinterpret_cast<INT_PTR>(result);
if (result_int <= 32) {
xlog::error("ShellExecuteA failed {}", result_int);
}
}

Expand Down
4 changes: 1 addition & 3 deletions patch_common/include/patch_common/StaticBufferResizePatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <vector>
#include <memory>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <patch_common/MemUtils.h>
Expand Down Expand Up @@ -54,9 +53,8 @@ class StaticBufferResizePatch : public Installable
if (!new_buf_) {
// Alloc owned buffer
// Note: raw memory is used here because it is expected the patched program will call constructors
// Note: std::make_unique uses value-initialization so buffer is zero-initialized (just like global variables)
new_buf_owned_ = std::make_unique<std::byte[]>(new_buf_size);
// zero new buffer (as if it was global variable and part of BSS section)
std::memset(new_buf_owned_.get(), 0, new_buf_size);
new_buf_ = reinterpret_cast<T*>(new_buf_owned_.get());
}

Expand Down
Loading

0 comments on commit 83bcd15

Please sign in to comment.