Skip to content

Commit

Permalink
Adds patches that print warnings when checking if image is null in Ge…
Browse files Browse the repository at this point in the history
…t_Image_Data() functions.
  • Loading branch information
CCHyper committed Apr 10, 2022
1 parent bed16af commit c5ab5a3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/extensions/animtype/animtypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,35 @@
******************************************************************************/
#include "animtypeext_hooks.h"
#include "animtypeext_init.h"
#include "animtype.h"
#include "animtypeext.h"
#include "supertype.h"
#include "fatal.h"
#include "debughandler.h"
#include "asserthandler.h"


/**
* Patches in an assertion check for image data.
*
* @author: CCHyper
*/
DECLARE_PATCH(_AnimTypeClass_Get_Image_Data_Assertion_Patch)
{
GET_REGISTER_STATIC(AnimTypeClass *, this_ptr, esi);
GET_REGISTER_STATIC(const ShapeFileStruct *, image, eax);

if (image == nullptr) {
DEBUG_WARNING("Anim %s has NULL image data!\n", this_ptr->Name());
}

_asm { mov eax, image } // restore eax state.
_asm { pop esi }
_asm { add esp, 0x264 }
_asm { ret }
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -43,4 +65,6 @@ void AnimTypeClassExtension_Hooks()
* Initialises the extended class.
*/
AnimTypeClassExtension_Init();

Patch_Jump(0x00419B37, &_AnimTypeClass_Get_Image_Data_Assertion_Patch);
}
23 changes: 23 additions & 0 deletions src/extensions/buildingtype/buildingtypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
#include "asserthandler.h"


/**
* Patches in an assertion check for image data.
*
* @author: CCHyper
*/
DECLARE_PATCH(_BuildingTypeClass_Get_Image_Data_Assertion_Patch)
{
GET_REGISTER_STATIC(BuildingTypeClass *, this_ptr, esi);
GET_REGISTER_STATIC(const ShapeFileStruct *, image, eax);

if (image == nullptr) {
DEBUG_WARNING("Building %s has NULL image data!\n", this_ptr->Name());
}

_asm { mov eax, image } // restore eax state.
_asm { pop esi }
_asm { add esp, 0x64 }
_asm { ret }
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -43,4 +64,6 @@ void BuildingTypeClassExtension_Hooks()
* Initialises the extended class.
*/
BuildingTypeClassExtension_Init();

Patch_Jump(0x00440365, &_BuildingTypeClass_Get_Image_Data_Assertion_Patch);
}
41 changes: 40 additions & 1 deletion src/extensions/isotiletype/isotiletypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,50 @@
******************************************************************************/
#include "isotiletypeext_hooks.h"
#include "isotiletypeext_init.h"
#include "isotiletype.h"
#include "isotiletypeext.h"
#include "supertype.h"
#include "fatal.h"
#include "debughandler.h"
#include "asserthandler.h"


/**
* A fake class for implementing new member functions which allow
* access to the "this" pointer of the intended class.
*
* @note: This must not contain a constructor or deconstructor!
* @note: All functions must be prefixed with "_" to prevent accidental virtualization.
*/
static class IsometricTileTypeClassFake final : public IsometricTileTypeClass
{
public:
const ShapeFileStruct * _Get_Image_Data();
};


/**
* Reimplementation of IsometricTileTypeClass::Get_Image_Data with added assertion.
*
* @author: CCHyper
*/
const ShapeFileStruct * IsometricTileTypeClassFake::_Get_Image_Data()
{
if (Image) {
return Image;
}

if (IsFileLoaded) {
Load_Image_Data();
}

if (Image == nullptr) {
DEBUG_WARNING("IsoTile %s has NULL image data!\n", Name());
}

return Image;
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -43,4 +80,6 @@ void IsometricTileTypeClassExtension_Hooks()
* Initialises the extended class.
*/
IsometricTileTypeClassExtension_Init();

Patch_Jump(0x004F3570, &IsometricTileTypeClassFake::_Get_Image_Data);
}
17 changes: 17 additions & 0 deletions src/extensions/objecttype/objecttypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static class ObjectTypeClassFake final : public ObjectTypeClass
{
public:
void _Assign_Theater_Name(char *buffer, TheaterType theater);
const ShapeFileStruct * _Get_Image_Data() const;
};


Expand Down Expand Up @@ -102,6 +103,21 @@ DECLARE_PATCH(_ObjectTypeClass_Load_Theater_Art_Assign_Theater_Name_Theater_Patc
}


/**
* Reimplementation of ObjectTypeClass::Get_Image_Data with added assertion.
*
* @author: CCHyper
*/
const ShapeFileStruct * ObjectTypeClassFake::_Get_Image_Data() const
{
if (Image == nullptr) {
DEBUG_WARNING("Object %s has NULL image data!\n", Name());
}

return Image;
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -112,6 +128,7 @@ void ObjectTypeClassExtension_Hooks()
*/
ObjectTypeClassExtension_Init();

Patch_Jump(0x004101A0, &ObjectTypeClassFake::_Get_Image_Data);
Patch_Jump(0x00588D00, &ObjectTypeClassFake::_Assign_Theater_Name);
Patch_Jump(0x0058891D, &_ObjectTypeClass_Load_Theater_Art_Assign_Theater_Name_Theater_Patch);
}
23 changes: 23 additions & 0 deletions src/extensions/overlaytype/overlaytypeext_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
#include "asserthandler.h"


/**
* Patches in an assertion check for image data.
*
* @author: CCHyper
*/
DECLARE_PATCH(_OverlayTypeClass_Get_Image_Data_Assertion_Patch)
{
GET_REGISTER_STATIC(OverlayTypeClass *, this_ptr, esi);
GET_REGISTER_STATIC(const ShapeFileStruct *, image, eax);

if (image == nullptr) {
DEBUG_WARNING("Overlay %s has NULL image data!\n", this_ptr->Name());
}

_asm { mov eax, image } // restore eax state.
_asm { pop esi }
_asm { add esp, 0x264 }
_asm { ret }
}


/**
* Main function for patching the hooks.
*/
Expand All @@ -43,4 +64,6 @@ void OverlayTypeClassExtension_Hooks()
* Initialises the extended class.
*/
OverlayTypeClassExtension_Init();

Patch_Jump(0x0058DC18, &_OverlayTypeClass_Get_Image_Data_Assertion_Patch);
}

0 comments on commit c5ab5a3

Please sign in to comment.