Poke_Transporter_GB/include/script_var.h
Philippe Symons eef173b0d2 Fix crash + unrelated buffer overflow + some optimizations
There was a crash happening with ptgb::vector when you'd press A on the CONFIRM button of the box screen. It only occurred on actual gba hardware and
was a real heisenbug: as soon as you'd add code to display logs on screen, the problem would disappear. So it was very difficult to figure this one
out. We're not even entirely sure why, but it looks like the malloc/realloc/free use in ptgb::vector would cause issues.

Maybe it was alignment, but after messing with the code we also saw a warning appear in the terminal telling us that realloc wouldn't properly
deal with non-POD types. It complained about this very thing while referring to the add_track() function, which stores ptgb::vectors inside another
ptgb::vector. We also didn't have a custom copy constructor yet to actually copy the buffer instead of its pointer.
All of these could potentially have led to the crash. But debugging during the link cable flow was difficult, so we were never able to confirm it in
a debugger, log or dump.

Because I suspected the high IWRAM consumption (especially now with ZX0 decompression) for a while, I also did an optimization in mystery_gift_builder
to pass global_memory_buffer as its section_30_data buffer instead. This reduces IWRAM consumption by 4 KB.

There was another problem I discovered during my crash hunt: the out_array (now payload_buffer) was allocated as a 672 byte array, but the payloads
were actually 707 bytes. Therefore writing this to the buffer caused a buffer overflow, thereby corrupting the global variables appearing after it in
IWRAM. It turned out eventually that none of these variables were really critical, but it could explain some minor bugs GearsProgress has seen.

I also did a few performance optimizations:

- At various stages in the code, for loops were used to copy data from one buffer into another byte-by-byte. This was far from optimal because the gba
cpu can load/copy 4 bytes at a time if you ask it to. So I replaced those with memcpy(), which is a hand-optimized assembly function to copy data
using this principle.

- generate_payload was being called twice: once at start_link and once at continue_link, giving the exact same result, even though it was already
being stored in a global buffer allocated in IWRAM. This was also a fairly heavy function. So I optimized the code to only initialize it once in
the script chain and then just retrieve the buffer.

- generate_payload was constructing the eventual payload twice even within the same call. That's because it first merged z80_rng_seed, z80_payload
and z80_patchlist into a full_data ptgb::vector, after which it then copied the data again to out_array (now called payload_buffer). I eliminated the
full_data vector now.
2025-06-18 10:23:03 +02:00

87 lines
3.2 KiB
C++

#include <tonc.h>
#include "libstd_replacements.h"
#include "rom_data.h"
class script_var
{
public:
script_var(u32 nValue, ptgb::vector<script_var *> &var_list_ref, int *nCurr_loc_ptr);
script_var(ptgb::vector<script_var *> &var_list_ref, int *nCurr_loc_ptr);
virtual void fill_refrences(u8 mg_array_ptr[]); // goes through all the script locations and updates them to point to the start
virtual void set_start(); // Add a pointer to where the start is
u32 place_word(); // Place the value in memory
int *curr_loc_ptr;
u32 value;
ptgb::vector<u32> location_list; // stores all the locations in the script that need to be updated
u32 start_location_in_script;
};
class asm_var : public script_var
{
public:
using script_var::script_var;
void set_start(); // Add a pointer to where the start is
void set_start(bool nIsDirect); // Add a pointer to where the start is
u8 add_reference(); // Add a location for the variable to be refrenced in the script
u8 add_reference(int nCommand_offset); // Add a location for the variable to be refrenced in the script
void fill_refrences(u8 mg_array_ptr[]); // goes through all the script locations and updates them to point to the start
u32 get_loc_in_sec30();
bool isDirect;
};
class xse_var : public script_var
{
public:
using script_var::script_var;
int command_offset; // The amount of bytes in the command before the variable
xse_var *refrence_var; // The offset from a different location in the script. Used to point to functions through addition/subtraction
bool has_reference_var;
void set_start(); // Add a pointer to where the start is
u8 add_reference(int nCommand_offset); // Add a location for the variable to be refrenced in the script
u8 add_reference(int nCommand_offset, xse_var *offset_from);
void fill_refrences(u8 mg_array_ptr[]); // goes through all the script locations and updates them to point to the start
u32 get_loc_in_sec30();
};
class textbox_var : public xse_var
{
public:
using xse_var::xse_var;
void set_text(const byte nText[]);
void insert_text(const u16 *charset, u8 mg_array[], bool should_set_virtual_start = false);
void set_start();
void set_virtual_start();
const byte *text;
int text_length;
};
class movement_var : public xse_var
{
public:
using xse_var::xse_var;
void set_movement(const int movement[], unsigned int nSize);
void insert_movement(u8 mg_array[]);
void set_start();
const int *movement;
unsigned int size;
};
class sprite_var : public xse_var
{
public:
using xse_var::xse_var;
void insert_sprite_data(u8 mg_array[], const unsigned int sprite_array[], unsigned int size, const unsigned short palette_array[]);
void set_start();
};
class music_var : public xse_var
{
public:
using xse_var::xse_var;
void insert_music_data(u8 mg_array[], u8 blockCount, u8 priority, u8 reverb, u32 toneDataPointer);
void set_start();
void add_track(const byte* trackBytes, size_t trackSize);
int numTracks;
ptgb::vector<u32> trackPointers;
ptgb::vector<ptgb::vector<byte>> trackArrays;
};