Skip to content

Commit

Permalink
Merge pull request #775 from Berthalamew/development
Browse files Browse the repository at this point in the history
cseries changes
  • Loading branch information
Berthalamew authored Jan 12, 2025
2 parents bdc76d0 + ed24a29 commit b2fc29b
Show file tree
Hide file tree
Showing 38 changed files with 239 additions and 110 deletions.
6 changes: 3 additions & 3 deletions xlive/Blam/Engine/cartographer/discord/discord_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void discord_interface_set_difficulty(int16 difficulty)

// Convert difficulty to string
char number_string[2];
snprintf(number_string, sizeof(number_string), "%hd", difficulty);
csnprintf(number_string, 1, sizeof(number_string), "%hd", difficulty);

// Create image name we select for the difficulty
c_static_string<16> difficulty_image_name;
Expand Down Expand Up @@ -343,7 +343,7 @@ void discord_rich_presence_update(s_discord_data* discord)
XUID host;
XUserGetXUID(0, &host);

snprintf(g_discord_globals.activity.party.id, sizeof(g_discord_globals.activity.party.id), "%016llx", host);
csprintf(g_discord_globals.activity.party.id, sizeof(g_discord_globals.activity.party.id), "%016llx", host);
g_discord_globals.activity.party.privacy = DiscordActivityPartyPrivacy_Public;
discord_interface_encode_xsession_info(&session);
}
Expand Down Expand Up @@ -379,7 +379,7 @@ void discord_interface_encode_xsession_info(XSESSION_INFO* session_info)
// Encode the data into hex string
for (uint32 i = 0; i < sizeof(XSESSION_INFO); i++)
{
sprintf(&g_discord_globals.activity.secrets.join[2 * i], "%02hhX", session_bytes[i]);
csprintf(&g_discord_globals.activity.secrets.join[2 * i], NUMBEROF(g_discord_globals.activity.secrets.join), "%02hhX", session_bytes[i]);
}

error(0, "Encoded join secret: %s", g_discord_globals.activity.secrets.join);
Expand Down
126 changes: 119 additions & 7 deletions xlive/Blam/Engine/cseries/cseries.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,136 @@
#include "stdafx.h"
#include "cseries.h"

#ifdef ASSERTS_ENABLED
#include "shell/shell.h"
#endif

/* constants */

enum
{
MAXIMUM_MEMCPY_SIZE = 0x20000000,
MAXIMUM_MEMMOVE_SIZE = 0x20000000,
MAXIMUM_MEMSET_SIZE = 0x20000000
};

/* globals */

// TODO: figure out a decent way to strip this from release builds
char g_temporary[256] = {};

#ifdef ASSERTS_ENABLED
bool g_catch_exceptions = true;
#endif

/* macros */

#ifdef ASSERTS_ENABLED
#define CSERIES_ASSERT(STATEMENT) \
if (!(STATEMENT)) \
{ \
if (!is_debugger_present()) \
{ \
error(3, ""); \
/* TODO: stalk walk call here */ \
} \
error(3, ""); \
if (is_debugger_present()) \
{ \
error(3, "%s(%d): %s: %s", __FILE__, __LINE__, "ASSERT", #STATEMENT); \
} \
else \
{ \
error(3, "%s", shell_get_version()); \
error(3, "%s at %s,#%d", "### ASSERTION FAILED: ", __FILE__, __LINE__); \
error(3, " %s", ##STATEMENT); \
} \
/* TODO: error callback call here */ \
if (!is_debugger_present()) \
{ \
RaiseException(0x73746Bu, 0, 0, 0); \
/* halt_and_catch_fire(); */ \
/* unk_cseries_windows_call(-1, ##STATEMENT); */ \
} \
ASSERT_TRIGGER_EXCEPTION(); \
} \
(void)0
#else
#define CSERIES_ASSERT(STATEMENT) (void)0
#endif

/* public code */

void display_assert(char const* condition, char const* file, int32 line, bool assertion_failed)
{
return;
}

void* csmemmove(void* dst, void* src, size_t size)
void* csmemmove(void* destination, void* source, size_t size)
{
return memmove(dst, src, size);
CSERIES_ASSERT(size == 0 || (destination && source));
CSERIES_ASSERT(size >= 0 && size <= MAXIMUM_MEMMOVE_SIZE);
return memmove(destination, source, size);
}

void* csmemset(void* dst, int32 val, size_t size)
void* csmemset(void* destination, int32 val, size_t size)
{
return memset(dst, val, size);
CSERIES_ASSERT(size == 0 || destination);
CSERIES_ASSERT(size >= 0 && size <= MAXIMUM_MEMSET_SIZE);
return memset(destination, val, size);
}

void* csmemcpy(void* dst, const void* src, size_t size)
void* csmemcpy(void* destination, const void* source, size_t size)
{
return memcpy(dst, src, size);
}
CSERIES_ASSERT(size == 0 || (destination && source));
CSERIES_ASSERT(size >= 0 && size < MAXIMUM_MEMCPY_SIZE);
ASSERT((byte*)source + size <= (byte*)destination || (byte*)destination + size <= (byte*)source);

return memcpy(destination, source, size);
}

int32 vsprintf(char* buffer, size_t size, const char* format, ...)
{
va_list va_args;
va_start(va_args, format);

CSERIES_ASSERT(buffer);
CSERIES_ASSERT(format);
CSERIES_ASSERT(size > 0);

const int32 result = (int32)_vsnprintf_s(buffer, size, UINT_MAX, format, va_args);
va_end(va_args);
return result;
}

int32 vsnprintf(char* buffer, size_t size, size_t max_count, const char* format, ...)
{
va_list va_args;
va_start(va_args, format);

CSERIES_ASSERT(buffer);
CSERIES_ASSERT(format);
CSERIES_ASSERT(size > 0);

const int32 result = (int32)_vsnprintf_s(buffer, size, max_count, format, va_args);
va_end(va_args);
return result;
}

const char* csprintf(char* buffer, size_t size, const char* format, ...)
{
va_list va_args;
va_start(va_args, format);
(void)vsprintf(buffer, size, format, &va_args);
va_end(va_args);
return buffer;
}

const char* csnprintf(char* buffer, size_t size, size_t max_count, const char* format, ...)
{
va_list va_args;
va_start(va_args, format);
(void)vsnprintf(buffer, size, max_count, format, &va_args);
va_end(va_args);
return buffer;
}
65 changes: 48 additions & 17 deletions xlive/Blam/Engine/cseries/cseries.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ static_assert(sizeof(datum) == 4);
#define IS_MSVC_COMPILER
#endif

/* globals */

extern char g_temporary[256];

/* macros */

// Invokes a function
// ADDR_CLIENT: file offset in halo2.exe
// ADDR_SERVER: file offset in h2server.exe
Expand All @@ -70,7 +76,7 @@ static_assert(sizeof(datum) == 4);
// optimizes out a cmov or related instruction when building release whenever we call a function that doesn't have a dedi address specified
#ifdef NDEBUG
#define INVOKE_BY_TYPE(_addr_client, _addr_server, _type, ...) \
Memory::GetAddress<_type>(_addr_client, _addr_server != 0 ? _addr_server : _addr_client)(__VA_ARGS__)
Memory::GetAddress<_type>(_addr_client, (_addr_server) != 0 ? _addr_server : _addr_client)(__VA_ARGS__)
#else
#define INVOKE_BY_TYPE(_addr_client, _addr_server, _type, ...) \
Memory::GetAddress<_type>(_addr_client, _addr_server)(__VA_ARGS__)
Expand Down Expand Up @@ -144,24 +150,36 @@ static_assert (sizeof(STRUCT) == (_SIZE), "Invalid size for struct ("#STRUCT") e
static_assert (offsetof(STRUCT, FIELD) == (OFFSET), #STRUCT " Offset(" #OFFSET ") for " #FIELD " is invalid");

#ifdef ASSERTS_ENABLED
#define ASSERT_TRIGGER_EXCEPTION() \
if (!is_debugger_present() && g_catch_exceptions) \
exit(-1); \
else \
__debugbreak(); \
(void)0


#define DISPLAY_ASSERT_EXCEPTION(STATEMENT, IS_EXCEPTION) \
display_assert(#STATEMENT, __FILE__, __LINE__, IS_EXCEPTION); \
if (!is_debugger_present() && g_catch_exceptions) \
exit(-1); \
else \
__debugbreak(); \
display_assert(STATEMENT, __FILE__, __LINE__, IS_EXCEPTION); \
ASSERT_TRIGGER_EXCEPTION(); \
(void)0

#define DISPLAY_ASSERT(STATEMENT) DISPLAY_ASSERT_EXCEPTION(STATEMENT, true)
#define DISPLAY_ASSERT(STATEMENT) \
DISPLAY_ASSERT_EXCEPTION(STATEMENT, true); \
(void)0

#define ASSERT_EXCEPTION(STATEMENT, IS_EXCEPTION) \
if (!(STATEMENT)) \
{ \
DISPLAY_ASSERT_EXCEPTION(STATEMENT, IS_EXCEPTION) \
{ \
DISPLAY_ASSERT_EXCEPTION(#STATEMENT, IS_EXCEPTION); \
} \
(void)0

#define ASSERT(STATEMENT) ASSERT_EXCEPTION(STATEMENT, true)
#define ASSERT(STATEMENT) \
ASSERT_EXCEPTION(STATEMENT, true); \
(void)0

#else
#define ASSERT_EXCEPTION() (void)(0)
#define DISPLAY_ASSERT_EXCEPTION(STATEMENT, IS_EXCEPTION) (void)(#STATEMENT)
#define DISPLAY_ASSERT(STATEMENT) (void)(#STATEMENT)
#define ASSERT_EXCEPTION(STATEMENT, IS_EXCEPTION) (void)(#STATEMENT)
Expand All @@ -170,17 +188,30 @@ if (!(STATEMENT)) \

#endif // _DEBUG


extern bool g_catch_exceptions;

// TODO implement
void display_assert(char const* condition, char const* file, int32 line, bool assertion_failed);

// TODO reimplement this properly
void* csmemmove(void* dst, void* src, size_t size);
void* csmemmove(void* destination, void* source, size_t size);

void* csmemset(void* destination, int32 val, size_t size);

void* csmemcpy(void* destination, const void* source, size_t size);

int32 vsprintf(char* buffer, size_t size, const char* format, ...);

/*
* NOTES:
* size_t max_count was added in vista to this function call
*/
int32 vsnprintf(char* buffer, size_t size, size_t max_count, const char* format, ...);


// TODO reimplement this properly
void* csmemset(void* dst, int32 val, size_t size);
const char* csprintf(char* buffer, size_t size, const char* format, ...);

// TODO reimplement this properly
void* csmemcpy(void* dst, const void* src, size_t size);
/*
* NOTES:
* size_t max_count was added in vista to this function call
*/
const char* csnprintf(char* buffer, size_t size, size_t max_count, const char* format, ...);
4 changes: 2 additions & 2 deletions xlive/Blam/Engine/cseries/cseries_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void error(int32 priority, const char* format, ...)
va_list va_args;
va_start(va_args, format);
char string[1024];
vsnprintf(string, NUMBEROF(string), format, va_args);
csnprintf(string, NUMBEROF(string), NUMBEROF(string), format, va_args);
error_internal(0, priority, string/*, va_args*/);
va_end(va_args);
#endif
Expand All @@ -26,7 +26,7 @@ void error(int32 category, int32 priority, const char* format, ...)
va_list va_args;
va_start(va_args, format);
char string[1024];
vsnprintf(string, NUMBEROF(string), format, va_args);
csnprintf(string, NUMBEROF(string), NUMBEROF(string), format, va_args);
error_internal(category, priority, string/*, va_args*/);
va_end(va_args);
#endif
Expand Down
15 changes: 0 additions & 15 deletions xlive/Blam/Engine/cseries/cseries_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,3 @@ int32 csstrncmp(const char* s1, const char* s2, size_t size)
// TODO: add asserts and logging
return strncmp(s1, s2, size);
}

int32 cvsnzprintf(char* buffer, size_t size, char const* format, ...)
{
ASSERT(buffer);
ASSERT(format);
ASSERT(size > 0);

va_list va_args;
va_start(va_args, format);
int32 result = vsnprintf(buffer, size, format, va_args);
buffer[size - 1] = '\0';

va_end(va_args);
return result;
}
1 change: 0 additions & 1 deletion xlive/Blam/Engine/cseries/cseries_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,3 @@ int32 csstricmp(const char* s1, const char* s2);

int32 csstrncmp(const char* s1, const char* s2, size_t size);

int32 cvsnzprintf(char* buffer, size_t size, char const* format, ...);
4 changes: 2 additions & 2 deletions xlive/Blam/Engine/effects/particle_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ void __cdecl particle_update_points_interpolate_hook(const real_point3d* previou
// there for instead of rendering the particle at the root origin do it at the target (initial) position instead
// this is only really noticeable on 30 tick gameplay
if (memcmp(previous_point, &global_zero_vector3d, sizeof(real_point3d)) == 0)
memcpy(out, target_point, sizeof(real_point3d));
csmemcpy(out, target_point, sizeof(real_point3d));
else
memcpy(out, previous_point, sizeof(real_point3d));
csmemcpy(out, previous_point, sizeof(real_point3d));
}

void particle_update(real32 delta)
Expand Down
6 changes: 3 additions & 3 deletions xlive/Blam/Engine/game/game_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static void game_globals_prepare_lmao_representation(s_game_globals_custom_repre
new_variant->name = 0xABABABA;
new_variant->dialogue.group = base_variant->dialogue.group;
new_variant->dialogue.index = base_variant->dialogue.index;
memcpy(new_variant->runtime_model_region_index, base_variant->runtime_model_region_index, sizeof(new_variant->runtime_model_region_index));
csmemcpy(new_variant->runtime_model_region_index, base_variant->runtime_model_region_index, sizeof(new_variant->runtime_model_region_index));

s_model_variant_region* region_blocks = (s_model_variant_region*)tag_injection_extend_block(&new_variant->regions, new_variant->regions.type_size(), base_variant->regions.count);
for (auto i = 0; i < base_variant->regions.count; i++)
Expand All @@ -214,8 +214,8 @@ static void game_globals_prepare_lmao_representation(s_game_globals_custom_repre
new_permutation->runtime_model_permutation_index = permutation->runtime_model_permutation_index;
new_permutation->flags = permutation->flags;
new_permutation->probability = permutation->probability;
memcpy(new_permutation->runtime_state_permutation_index, permutation->runtime_state_permutation_index, sizeof(new_permutation->runtime_state_permutation_index));
memcpy(new_permutation->pad1, permutation->pad1, sizeof(new_permutation->pad1));
csmemcpy(new_permutation->runtime_state_permutation_index, permutation->runtime_state_permutation_index, sizeof(new_permutation->runtime_state_permutation_index));
csmemcpy(new_permutation->pad1, permutation->pad1, sizeof(new_permutation->pad1));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void __cdecl create_account_xbox_task_progress_cb(c_screen_xbox_live_task_progre
SecureZeroMemory(g_account_create_data.user_name, sizeof(g_account_create_data.user_name));
SecureZeroMemory(g_account_create_data.email, sizeof(g_account_create_data.email));
SecureZeroMemory(g_account_create_data.password, sizeof(g_account_create_data.password));
memset(&g_account_create_data, 0, sizeof(g_account_create_data));
csmemset(&g_account_create_data, 0, sizeof(g_account_create_data));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void c_player_profile_list::update_displayed_profiles()

c_list_item_widget* current_child = (c_list_item_widget*)this->m_child_widget;

memset(profile_indices, NONE, sizeof(profile_indices));
csmemset(profile_indices, NONE, sizeof(profile_indices));
for (int32 current_child_widget_index = 0; current_child_widget_index < 16; ++current_child_widget_index)
{
if (current_child->get_last_data_index() != NONE)
Expand Down
3 changes: 2 additions & 1 deletion xlive/Blam/Engine/interface/user_interface_networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ s_game_variant* __cdecl user_interface_session_get_game_variant(void)
void user_interface_networking_set_globals(bool a1, XSESSION_INFO* session, int32 unused, bool from_game_invite)
{
*byte_D6840E_get() = a1;
memcpy(global_session_info_get(), session, sizeof(XSESSION_INFO));
*global_session_info_get() = *session;
*dword_86EEE0_get() = unused;
*from_game_invite_global_get() = from_game_invite;
return;
}

void __cdecl user_interface_networking_reset_player_counts(void)
Expand Down
Loading

0 comments on commit b2fc29b

Please sign in to comment.