Skip to content

Commit

Permalink
Change hoe storage is loaded/saved
Browse files Browse the repository at this point in the history
  • Loading branch information
ZivDero committed Sep 23, 2024
1 parent 1707b7d commit 098024b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 97 deletions.
2 changes: 1 addition & 1 deletion YRpp
23 changes: 6 additions & 17 deletions src/Ext/House/Body.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Body.h"

#include <Ext/SWType/Body.h>
#include "New/Entity/ExtendedStorageClass.h"
#include <New/Entity/PhobosStorageClass.h>
#include <Ext/TechnoType/Body.h>
#include <Ext/Techno/Body.h>

Expand Down Expand Up @@ -647,6 +647,8 @@ void HouseExt::ExtData::Serialize(T& Stm)
.Process(this->NumWarFactories_NonMFB)
.Process(this->NumConYards_NonMFB)
.Process(this->NumShipyards_NonMFB)
.Process(this->Tiberium)
.Process(this->Weed)
;
}

Expand All @@ -655,22 +657,15 @@ void HouseExt::ExtData::LoadFromStream(PhobosStreamReader& Stm)
Extension<HouseClass>::LoadFromStream(Stm);
this->Serialize(Stm);

auto ownedTiberium = reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->OwnedTiberium);
auto ownedWeed = reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->OwnedWeed);

*ownedTiberium = new PhobosStorageClass();
*ownedWeed = new PhobosStorageClass();

(*ownedTiberium)->Load(Stm, false);
(*ownedWeed)->Load(Stm, false);
// Restore the pointers to our new Storage class
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject()->OwnedTiberium)) PhobosStorageClass(&Tiberium);
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject()->OwnedWeed)) PhobosStorageClass(&Weed);
}

void HouseExt::ExtData::SaveToStream(PhobosStreamWriter& Stm)
{
Extension<HouseClass>::SaveToStream(Stm);
this->Serialize(Stm);
(*reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->OwnedTiberium))->Save(Stm);
(*reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->OwnedWeed))->Save(Stm);
}

bool HouseExt::LoadGlobals(PhobosStreamReader& Stm)
Expand Down Expand Up @@ -731,12 +726,6 @@ DEFINE_HOOK(0x4F6532, HouseClass_CTOR, 0x5)
if (RulesExt::Global()->EnablePowerSurplus)
pItem->PowerSurplus = RulesClass::Instance->PowerSurplus;

auto ownedTiberium = new PhobosStorageClass();
auto ownedWeed = new PhobosStorageClass();

std::memcpy(&pItem->OwnedTiberium, &ownedTiberium, sizeof(ownedTiberium));
std::memcpy(&pItem->OwnedWeed, &ownedWeed, sizeof(ownedWeed));

return 0;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Ext/House/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class HouseExt
int NumConYards_NonMFB;
int NumShipyards_NonMFB;

std::vector<float> Tiberium;
std::vector<float> Weed;

ExtData(HouseClass* OwnerObject) : Extension<HouseClass>(OwnerObject)
, PowerPlantEnhancers {}
, OwnedLimboDeliveredBuildings {}
Expand All @@ -75,7 +78,13 @@ class HouseExt
, NumWarFactories_NonMFB { 0 }
, NumConYards_NonMFB { 0 }
, NumShipyards_NonMFB { 0 }
{ }
, Tiberium(TiberiumClass::Array->Count, 0.0f)
, Weed(TiberiumClass::Array->Count, 0.0f)
{
// Initialize our new storage classes inside the owner object
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject->OwnedTiberium)) PhobosStorageClass(&Tiberium);
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject->OwnedWeed)) PhobosStorageClass(&Weed);
}

bool OwnsLimboDeliveredBuilding(BuildingClass* pBuilding);
void AddToLimboTracking(TechnoTypeClass* pTechnoType);
Expand Down
2 changes: 2 additions & 0 deletions src/Ext/House/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Ext/Building/Body.h"
#include <unordered_map>

#include "Utilities/AresHelper.h"

DEFINE_HOOK(0x508C30, HouseClass_UpdatePower_UpdateCounter, 0x5)
{
GET(HouseClass*, pThis, ECX);
Expand Down
10 changes: 3 additions & 7 deletions src/Ext/Techno/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ void TechnoExt::ExtData::Serialize(T& Stm)
.Process(this->HasRemainingWarpInDelay)
.Process(this->LastWarpInDelay)
.Process(this->IsBeingChronoSphered)
.Process(this->Tiberium)
;
}

Expand All @@ -508,16 +509,14 @@ void TechnoExt::ExtData::LoadFromStream(PhobosStreamReader& Stm)
Extension<TechnoClass>::LoadFromStream(Stm);
this->Serialize(Stm);

auto storage = reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->Tiberium);
*storage = new PhobosStorageClass();
(*storage)->Load(Stm, false);
// Restore the pointer to our new Storage class
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject()->Tiberium)) PhobosStorageClass(&Tiberium);
}

void TechnoExt::ExtData::SaveToStream(PhobosStreamWriter& Stm)
{
Extension<TechnoClass>::SaveToStream(Stm);
this->Serialize(Stm);
(*reinterpret_cast<PhobosStorageClass**>(&this->OwnerObject()->Tiberium))->Save(Stm);
}

bool TechnoExt::LoadGlobals(PhobosStreamReader& Stm)
Expand Down Expand Up @@ -548,8 +547,6 @@ DEFINE_HOOK(0x6F3260, TechnoClass_CTOR, 0x5)
GET(TechnoClass*, pItem, ESI);

TechnoExt::ExtMap.TryAllocate(pItem);
auto storageClass = new PhobosStorageClass();
std::memcpy(&pItem->Tiberium, &storageClass, sizeof(storageClass));

return 0;
}
Expand All @@ -559,7 +556,6 @@ DEFINE_HOOK(0x6F4500, TechnoClass_DTOR, 0x5)
GET(TechnoClass*, pItem, ECX);

TechnoExt::ExtMap.Remove(pItem);
delete *reinterpret_cast<PhobosStorageClass**>(&pItem->Tiberium);

return 0;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Ext/Techno/Body.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <InfantryClass.h>
#include <AnimClass.h>
#include <TiberiumClass.h>

#include <Helpers/Macro.h>
#include <Utilities/Container.h>
Expand All @@ -9,6 +10,7 @@
#include <New/Entity/ShieldClass.h>
#include <New/Entity/LaserTrailClass.h>
#include <New/Entity/AttachEffectClass.h>
#include <New/Entity/PhobosStorageClass.h>

class BulletClass;

Expand Down Expand Up @@ -55,6 +57,7 @@ class TechnoExt
bool HasRemainingWarpInDelay; // Converted from object with Teleport Locomotor to one with a different Locomotor while still phasing in OR set if ChronoSphereDelay > 0.
int LastWarpInDelay; // Last-warp in delay for this unit, used by HasCarryoverWarpInDelay.
bool IsBeingChronoSphered; // Set to true on units currently being ChronoSphered, does not apply to Ares-ChronoSphere'd buildings or Chrono reinforcements.
std::vector<float> Tiberium;

ExtData(TechnoClass* OwnerObject) : Extension<TechnoClass>(OwnerObject)
, TypeExtData { nullptr }
Expand Down Expand Up @@ -85,8 +88,12 @@ class TechnoExt
, OriginalPassengerOwner {}
, HasRemainingWarpInDelay { false }
, LastWarpInDelay { 0 }
, IsBeingChronoSphered { false}
{ }
, IsBeingChronoSphered { false }
, Tiberium(TiberiumClass::Array->Count, 0.0f)
{
// Initialize our new storage class inside the owner object
new (reinterpret_cast<PhobosStorageClass*>(&OwnerObject->Tiberium)) PhobosStorageClass(&Tiberium);
}

void OnEarlyUpdate();

Expand Down
77 changes: 8 additions & 69 deletions src/New/Entity/Hooks.Storage.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,11 @@
#include <Utilities/Macro.h>
#include "New/Entity/PhobosStorageClass.h"

namespace StorageClass_Wrappers
{
int __fastcall GetTotalValue(PhobosStorageClass* pThis)
{
return pThis->GetTotalValue();
}

double __fastcall GetTotalAmount(PhobosStorageClass* pThis)
{
return pThis->GetTotalAmount();
}

double __fastcall GetAmount(PhobosStorageClass* pThis, void*, int index)
{
return pThis->GetAmount(index);
}

float __fastcall IncreaseAmount(PhobosStorageClass* pThis, void*, float amount, int index)
{
return pThis->IncreaseAmount(amount, index);
}

float __fastcall DecreaseAmount(PhobosStorageClass* pThis, void*, float amount, int index)
{
return pThis->DecreaseAmount(amount, index);
}

PhobosStorageClass* __fastcall Operator_Plus(PhobosStorageClass* ppThis, void*, PhobosStorageClass* retstr, PhobosStorageClass* ppThat)
{
*retstr = *ppThis + *ppThat;
return retstr;
}

PhobosStorageClass* __fastcall Operator_PlusEquals(PhobosStorageClass* ppThis, void*, PhobosStorageClass* retstr, PhobosStorageClass* ppThat)
{
*ppThis += *ppThat;
*retstr = *ppThis;
return ppThis;
}

PhobosStorageClass* __fastcall Operator_Minus(PhobosStorageClass* ppThis, void*, PhobosStorageClass* retstr, PhobosStorageClass* ppThat)
{
*retstr = *ppThis - *ppThat;
return retstr;
}

PhobosStorageClass* __fastcall Operator_MinusEquals(PhobosStorageClass* ppThis, void*, PhobosStorageClass* retstr, PhobosStorageClass* ppThat)
{
*ppThis -= *ppThat;
*retstr = *ppThis;
return ppThis;
}

int __fastcall FirstUsedSlot(PhobosStorageClass* pThis)
{
return pThis->FirstUsedSlot();
}
}

DEFINE_JUMP(LJMP, 0x6C9600, GET_OFFSET(StorageClass_Wrappers::GetTotalValue))
DEFINE_JUMP(LJMP, 0x6C9650, GET_OFFSET(StorageClass_Wrappers::GetTotalAmount))
DEFINE_JUMP(LJMP, 0x6C9680, GET_OFFSET(StorageClass_Wrappers::GetAmount))
DEFINE_JUMP(LJMP, 0x6C9690, GET_OFFSET(StorageClass_Wrappers::IncreaseAmount))
DEFINE_JUMP(LJMP, 0x6C96B0, GET_OFFSET(StorageClass_Wrappers::DecreaseAmount))
DEFINE_JUMP(LJMP, 0x6C96E0, GET_OFFSET(StorageClass_Wrappers::Operator_Plus))
DEFINE_JUMP(LJMP, 0x6C9740, GET_OFFSET(StorageClass_Wrappers::Operator_PlusEquals))
DEFINE_JUMP(LJMP, 0x6C9780, GET_OFFSET(StorageClass_Wrappers::Operator_Minus))
DEFINE_JUMP(LJMP, 0x6C97E0, GET_OFFSET(StorageClass_Wrappers::Operator_MinusEquals))
DEFINE_JUMP(LJMP, 0x6C9820, GET_OFFSET(StorageClass_Wrappers::FirstUsedSlot))
DEFINE_JUMP(LJMP, 0x6C9600, GET_PTR(&PhobosStorageClass::GetTotalValue))
DEFINE_JUMP(LJMP, 0x6C9650, GET_PTR(&PhobosStorageClass::GetTotalAmount))
DEFINE_JUMP(LJMP, 0x6C9680, GET_PTR(&PhobosStorageClass::GetAmount))
DEFINE_JUMP(LJMP, 0x6C9690, GET_PTR(&PhobosStorageClass::IncreaseAmount))
DEFINE_JUMP(LJMP, 0x6C96B0, GET_PTR(&PhobosStorageClass::DecreaseAmount))
DEFINE_JUMP(LJMP, 0x6C9740, GET_PTR(&PhobosStorageClass::operator+=))
DEFINE_JUMP(LJMP, 0x6C97E0, GET_PTR(&PhobosStorageClass::operator-=))
DEFINE_JUMP(LJMP, 0x6C9820, GET_PTR(&PhobosStorageClass::FirstUsedSlot))
13 changes: 13 additions & 0 deletions src/Utilities/Macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ typedef _VTABLE _OFFSET;
#pragma endregion Patch Structs

#pragma region Macros
template<typename T>
int GetPtr(T func)
{
union
{
T Func;
int Ptr;
} addr;

addr.Func = func;
return addr.Ptr;
}
#define GET_PTR(func) GetPtr(func)
#define GET_OFFSET(pointer) reinterpret_cast<DWORD>(pointer)

#pragma region Static Patch
Expand Down

0 comments on commit 098024b

Please sign in to comment.