Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions UE4SS/proxy_generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <ImageHlp.h>
#include <tchar.h>
#include <Helpers/String.hpp>
#include <Helpers/Win32Error.hpp>
#include <Helpers/SysError.hpp>

using namespace RC;
namespace fs = std::filesystem;
Expand Down Expand Up @@ -51,7 +51,7 @@ std::vector<ExportFunction> DumpExports(const fs::path& dll_path)

if (export_directory == nullptr)
{
auto err_msg = to_string(Win32Error(GetLastError()));
auto err_msg = to_string(SysError(GetLastError()));
cerr << std::format("Failed to get export directory, reason: {}", err_msg) << '\n';
return {};
}
Expand Down
6 changes: 3 additions & 3 deletions UE4SS/src/CrashDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <polyhook2/PE/IatHook.hpp>
#include <dbghelp.h>
#include <Helpers/Win32Error.hpp>
#include <Helpers/SysError.hpp>
#include <String/StringType.hpp>

namespace fs = std::filesystem;
Expand All @@ -33,7 +33,7 @@ namespace RC

if (file == INVALID_HANDLE_VALUE)
{
const StringType message = fmt::format(STR("Failed to create crashdump file, reason: {}"), Win32Error(GetLastError()).c_str());
const StringType message = fmt::format(STR("Failed to create crashdump file, reason: {}"), SysError(GetLastError()).c_str());
MessageBoxW(NULL, FromCharTypePtr<wchar_t>(message.c_str()), L"Fatal Error!", MB_OK);
return EXCEPTION_CONTINUE_SEARCH;
}
Expand All @@ -55,7 +55,7 @@ namespace RC

if (!ok)
{
const StringType message = fmt::format(STR("Failed to write crashdump file, reason: {}"), Win32Error(GetLastError()).c_str());
const StringType message = fmt::format(STR("Failed to write crashdump file, reason: {}"), SysError(GetLastError()).c_str());
MessageBoxW(NULL, FromCharTypePtr<wchar_t>(message.c_str()), L"Fatal Error!", MB_OK);
return EXCEPTION_CONTINUE_SEARCH;
}
Expand Down
4 changes: 2 additions & 2 deletions UE4SS/src/Mod/CppMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <Windows.h>

#include <DynamicOutput/DynamicOutput.hpp>
#include <Helpers/Win32Error.hpp>
#include <Helpers/SysError.hpp>
#include <Helpers/String.hpp>
#include <Mod/CppMod.hpp>

Expand All @@ -29,7 +29,7 @@ namespace RC
if (!m_main_dll_module)
{
Output::send<LogLevel::Warning>(STR("Failed to load dll <{}> for mod {}, error: {}\n"),
ensure_str(dll_path), m_mod_name, Win32Error(GetLastError()).c_str());
ensure_str(dll_path), m_mod_name, SysError(GetLastError()).c_str());
set_installable(false);
return;
}
Expand Down
30 changes: 15 additions & 15 deletions deps/first/File/src/FileType/WinFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define NOMINMAX
#include <Windows.h>
#include <Helpers/String.hpp>
#include <Helpers/Win32Error.hpp>
#include <Helpers/SysError.hpp>
#ifdef TEXT
#undef TEXT
#endif
Expand All @@ -34,15 +34,15 @@ namespace RC::File
if (DeleteFileW(file_path_and_name.wstring().c_str()) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}
}
else
{
if (DeleteFileA(file_path_and_name.string().c_str()) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ namespace RC::File
if (!WriteFile(file.get_file(), data, num_bytes_to_write, &bytes_written, nullptr))
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_to_file] Tried writing to file but was unable to complete operation. error: {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}
}

Expand Down Expand Up @@ -296,7 +296,7 @@ namespace RC::File
if (res == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::get_serialized_item] Tried deserializing file but was unable to complete operation. error: {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}

cache_file.close();
Expand Down Expand Up @@ -344,7 +344,7 @@ namespace RC::File
{
if (UnmapViewOfFile(m_memory_map) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to unmap file, error: {}", to_string(Win32Error(GetLastError())).c_str()))
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to unmap file, error: {}", to_string(SysError(GetLastError())).c_str()))
}
else
{
Expand All @@ -356,7 +356,7 @@ namespace RC::File
{
if (CloseHandle(m_map_handle) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close map handle, {}", to_string(Win32Error(GetLastError())).c_str()))
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close map handle, {}", to_string(SysError(GetLastError())).c_str()))
}
else
{
Expand All @@ -371,7 +371,7 @@ namespace RC::File

if (CloseHandle(m_file) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close file, {}", to_string(Win32Error(GetLastError())).c_str()))
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close file, {}", to_string(SysError(GetLastError())).c_str()))
}
else
{
Expand All @@ -391,7 +391,7 @@ namespace RC::File
if (string_size == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_string_to_file] Tried writing string to file but string_size was 0. {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}

std::string string_converted_to_utf8(string_size, 0);
Expand All @@ -406,7 +406,7 @@ namespace RC::File
{
THROW_INTERNAL_FILE_ERROR(
fmt::format("[WinFile::write_string_to_file] Tried writing string to file but could not convert to utf-8. {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}

write_to_file(*this, string_converted_to_utf8.c_str(), string_size);
Expand All @@ -417,13 +417,13 @@ namespace RC::File
BY_HANDLE_FILE_INFORMATION file_info{};
if (GetFileInformationByHandle(m_file, &file_info) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(Win32Error(GetLastError())).c_str()))
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(SysError(GetLastError())).c_str()))
}

BY_HANDLE_FILE_INFORMATION other_file_info{};
if (GetFileInformationByHandle(other_file.get_file(), &other_file_info) == 0)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(Win32Error(GetLastError())).c_str()))
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(SysError(GetLastError())).c_str()))
}

if (file_info.dwVolumeSerialNumber != other_file_info.dwVolumeSerialNumber)
Expand Down Expand Up @@ -523,14 +523,14 @@ namespace RC::File
if (!m_map_handle)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'CreateFileMapping' returned {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}

m_memory_map = static_cast<uint8_t*>(MapViewOfFile(m_map_handle, mapping_desired_access, 0, 0, 0));
if (!m_memory_map)
{
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'MapViewOfFile' returned {}",
to_string(Win32Error(GetLastError())).c_str()))
to_string(SysError(GetLastError())).c_str()))
}

MEMORY_BASIC_INFORMATION buffer{};
Expand Down Expand Up @@ -613,7 +613,7 @@ namespace RC::File
std::string_view open_type = open_properties.open_for == OpenFor::Writing || open_properties.open_for == OpenFor::Appending ? "writing" : "reading";

THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::open_file] Tried opening file for {} but encountered an error. Path & File: {} | error: {}\n",
open_type, file_name_and_path.string(), to_string(Win32Error(GetLastError())).c_str()))
open_type, file_name_and_path.string(), to_string(SysError(GetLastError())).c_str()))
}

file.m_file_path_and_name = file_name_and_path;
Expand Down
95 changes: 95 additions & 0 deletions deps/first/Helpers/include/Helpers/SysError.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once

#include <system_error>

namespace RC
{
inline const std::error_category& default_error_category() noexcept
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
inline const std::error_category& default_error_category() noexcept
inline auto default_error_category() noexcept -> const std::error_category&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My Windows installation is borked at the moment. I'll try to rerun the unit tests asap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test suite is all OK.

{
#ifdef _WIN32
return std::system_category();
#else
return std::generic_category();
#endif
}

#ifdef _WIN32
using PlatformResultCode = HRESULT;
#else
using PlatformResultCode = int;
#endif
}

namespace RC
{
/**
* Wrapper for error messages from system error code to be used as a string object
*/
class SysError
{
public:
/**
* Constructor for error code
* @param error_code : a system error code
* @param category : the error category (std::system_category() for OS specific codes or std::generic_category() for POSIX codes)
*/
explicit SysError(int error_code, const std::error_category& category = default_error_category());
/**
* Constructor for error code
* @param error_code : a system error code
* @param category : the error category (std::system_category() for OS specific codes or std::generic_category() for POSIX codes)
*/
explicit SysError(unsigned long error_code, const std::error_category& category = default_error_category());
/**
* Assign an error code and update the object with the corresponding error message
* @param error_code : a system error code
*/
auto assign(unsigned long error_code) -> void;
#ifdef _WIN32
/**
* Constructor for COM HRESULT codes -- only works for FACILITY_WIN32
* @param hresult : a COM operation result code
*/
explicit SysError(HRESULT hresult);

/**
* Assign an error code and update the object with the corresponding error message
* @param hresult : a COM operation result code
*/
auto assign(HRESULT hresult) -> void;
#endif
/**
* Returns a pointer to an array that contains a null-terminated sequence of characters representing the current value of the string object
* @return a pointer to the c-string representation of the string object's value
*/
[[nodiscard]] auto c_str() const noexcept -> const CharType* { return m_error_text.c_str(); }
/**
* Returns the name of the error category
* @return the name of the error category
*/
[[nodiscard]] auto category() const -> StringType;
/**
* Explicit cast operator to const CharType*
*/
explicit operator const CharType*() const noexcept { return c_str(); } // must be explicit, or it will cause an ambiguous call to overloaded function error when passed to RC::to_string
/**
* Implicit cast operator to const StringType& so that the wrapper can be passed to RC::to_string, for instance
*/
operator const StringType&() const noexcept { return m_error_text; }
private:
/**
* Formats the error message corresponding to the specified error code.
* @param error_code : a system error code or COM error code
* @return a string containing the formatted message, if successful
*/
[[nodiscard]] auto format_error(int error_code) const -> StringType;
/**
* The error category (std::system_category() for OS specific codes or std::generic_category() for POSIX codes)
*/
const std::error_category* m_error_category = nullptr;
/**
* The string object backing the wrapper
*/
StringType m_error_text;
};
}
66 changes: 0 additions & 66 deletions deps/first/Helpers/include/Helpers/Win32Error.hpp

This file was deleted.

Loading
Loading