Skip to content

Commit

Permalink
Updated restricted_memory
Browse files Browse the repository at this point in the history
  • Loading branch information
twist84 committed Dec 19, 2024
1 parent ff68262 commit 009a787
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
79 changes: 51 additions & 28 deletions game/source/cache/restricted_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
#include "cseries/cseries.hpp"
#include "multithreading/synchronized_value.hpp"

const long k_max_section_mirrors = 4;
const long k_max_registered_members = 128;

enum e_restricted_memory_region
{
k_game_state_header_region = 0,
Expand All @@ -18,75 +15,101 @@ enum e_restricted_memory_region

k_game_state_shared_region_mirror0 = k_game_state_shared_region + 1,
k_game_state_shared_region_mirror1 = k_game_state_shared_region + 2,
};

enum
{
k_game_state_header_region_size = 0x30000,
k_game_state_update_region_size = 0x200000,
k_game_state_render_region_size = 0x2D0000,
k_game_state_shared_region_size = 0x480000,

k_game_state_size = k_game_state_header_region_size + k_game_state_update_region_size + k_game_state_render_region_size + k_game_state_shared_region_size,
k_game_state_with_mirrors_size = k_game_state_size + (k_game_state_shared_region_size * 2),
k_game_state_allocation_size = k_game_state_header_region_size + k_game_state_update_region_size + k_game_state_render_region_size + k_game_state_shared_region_size,
k_game_state_file_size = k_game_state_allocation_size + (k_game_state_shared_region_size * 2),
};
static_assert(0x0980000 == k_game_state_allocation_size);
static_assert(0x1280000 == k_game_state_file_size);

enum e_restricted_section_constants
{
k_max_section_mirrors = 4
};
static_assert(0x0980000 == k_game_state_size);
static_assert(0x1280000 == k_game_state_with_mirrors_size);

enum e_memory_protection;
enum e_memory_protection
{
_memory_protection_noaccess = 0,
_memory_protection_readonly,
_memory_protection_readwrite
};

struct c_restricted_section
{
enum e_internal_flags
{
_initialized = 0
};

dword_flags m_internal_flags;
long m_protection; // e_memory_protection
long m_protection_scheme;
long m_external_lock;
byte* m_address;
dword m_size;
dword m_owner_thread;
long m_owner_thread;
};
static_assert(sizeof(c_restricted_section) == 0x18);

struct c_restricted_memory_callbacks;
struct c_restricted_memory
{
enum e_internal_flags
{
_initialized = 0
};

enum e_constants
{
k_max_registered_members = 128
};

struct s_mirror_slot
{
c_interlocked_long valid;
c_synchronized_long readable_flag;
c_synchronized_long __unknown8;
c_synchronized_long writable_flag;
c_restricted_section* restricted_section;
};
static_assert(sizeof(s_mirror_slot) == 0x10);

struct s_registered_member
{
// same as `offset`
dword __unknown0;

dword offset;
dword original_offset;
dword member_offset;
dword size;
void(__cdecl* tls_update_callback)(void*);
void(__cdecl* tls_post_copy_fixup_callback)(void*);
void(__cdecl* tls_pre_overwrite_fixup_callback)(void*);
void(__cdecl* update_function)(void*);
void(__cdecl* post_copy_function)(void*);
void(__cdecl* overwrite_function)(void*);
};
static_assert(sizeof(s_registered_member) == 0x18);

c_interlocked_long m_mirror_read_in_progress;
c_interlocked_long m_mirror_write_in_progress;
c_static_array<s_mirror_slot, k_max_section_mirrors> m_mirrors;
c_interlocked_long m_new_write_mirror;
c_interlocked_long m_available_mirror;
s_mirror_slot m_mirrors[k_max_section_mirrors];
c_interlocked_long m_mirror_write_position;
c_interlocked_long m_mirror_read_position;
c_synchronized_long m_alias_allowed;
c_synchronized_long m_alias_count;
c_restricted_section* m_primary_section;
dword_flags m_internal_flags; // 0: _initialized
long __unknown60;
dword_flags m_internal_flags;
dword m_tls_handle;
dword m_subsection_size;
dword m_reserved_size;
long m_mirror_count;
dword m_mirror_section;
long m_registered_member_count;
c_static_array<s_registered_member, k_max_registered_members> m_registered_member;
long m_internal_lock;
dword m_registered_member_count;
s_registered_member m_registered_member[k_max_registered_members];
c_restricted_memory_callbacks* m_callbacks;
void* m_subsectionC7C;
void* m_subsectionC80;
void* m_primary_backup_memory;
void* m_mirror_backup_memory;
long m_region_index;
};
static_assert(sizeof(c_restricted_memory) == 0xC88);
Expand Down
8 changes: 4 additions & 4 deletions game/source/saved_games/game_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ void __cdecl game_state_reset_mapping(long a1)
{
//INVOKE(0x00510330, game_state_reset_mapping, a1);

game_state_set_buffer_protection(game_state_globals.base_address, k_game_state_with_mirrors_size, game_state_globals.guard_page_size);
game_state_set_buffer_protection(game_state_globals.base_address, k_game_state_file_size, game_state_globals.guard_page_size);
}

//.text:00510350
Expand Down Expand Up @@ -397,7 +397,7 @@ void __cdecl game_state_save_core(char const* core_name)

game_state_call_before_save_procs(game_state_proc_flags);

bool success = game_state_write_core(core_name, game_state_globals.base_address, k_game_state_size);
bool success = game_state_write_core(core_name, game_state_globals.base_address, k_game_state_allocation_size);
console_printf(success ? "saved '%s'" : "error writing '%s'", core_name);

game_state_call_after_save_procs(game_state_proc_flags);
Expand Down Expand Up @@ -510,7 +510,7 @@ void __cdecl game_state_shell_initialize()
game_state_globals.runtime_saved_game_storage_count = game_state_get_storage_count();
ASSERT(IN_RANGE_INCLUSIVE(game_state_globals.runtime_saved_game_storage_count, 1, k_saved_game_storage_max_count));

dword available_memory = k_game_state_with_mirrors_size;
dword available_memory = k_game_state_file_size;
byte* starting_address = static_cast<byte*>(game_state_globals.base_address);

initialize_game_state_section(k_game_state_header_region, k_game_state_header_region_size, &starting_address, &available_memory, k_crit_section_header_subsection);
Expand Down Expand Up @@ -541,7 +541,7 @@ void __cdecl game_state_shell_initialize()
if (file_create(&game_state_allocation_record_file) && file_open(&game_state_allocation_record_file, FLAG(_file_open_flag_desired_access_write), &error))
{
file_printf(&game_state_allocation_record_file, "game state allocations from: %s\r\n", version_get_full_string());
file_printf(&game_state_allocation_record_file, "memory total, %u\r\n", k_game_state_with_mirrors_size);
file_printf(&game_state_allocation_record_file, "memory total, %u\r\n", k_game_state_file_size);
file_printf(&game_state_allocation_record_file, "memory available, %u\r\n", available_memory);
file_printf(&game_state_allocation_record_file, "% 44s,% 24s", "name", "type");

Expand Down

0 comments on commit 009a787

Please sign in to comment.