mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 09:06:54 +02:00
Change cheat implementation to show cheats in database order, fix some bugs
- AdvancedPaletteManager incorrectly handled negative y positions - FocusManager still had a pointer to a view that was destroyed in the cheats panel. After changing focus, memory got corrupted.
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/// @brief Class representing a single cheat.
|
||||
class Cheat
|
||||
{
|
||||
public:
|
||||
Cheat() { }
|
||||
|
||||
Cheat(const char* name, const char* description, u32* flagsPointer, const void* cheatData, u32 cheatDataLength)
|
||||
: _name(name), _description(description), _flagsPointer(flagsPointer)
|
||||
, _cheatData(cheatData), _cheatDataLength(cheatDataLength) { }
|
||||
|
||||
/// @brief Gets the name of this cheat.
|
||||
/// @return A pointer to the name of this cheat.
|
||||
const char* GetName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
/// @brief Gets the description of this cheat.
|
||||
/// @return A pointer to the description of this cheat.
|
||||
const char* GetDescription() const
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
/// @brief Gets a pointer to the data of this cheat.
|
||||
/// @param cheatDataLength The length of the cheat data is returned in this reference.
|
||||
/// @return A pointer to the cheat data.
|
||||
/// This pointer is only valid for the lifetime of the \see GameCheats instance this cheat belongs to.
|
||||
const void* GetCheatData(u32& cheatDataLength) const
|
||||
{
|
||||
cheatDataLength = _cheatDataLength;
|
||||
return _cheatData;
|
||||
}
|
||||
|
||||
/// @brief Gets whether this cheat is active (enabled) or not.
|
||||
/// @return \c true when this cheat is active, or \c false when not active.
|
||||
bool GetIsCheatActive() const
|
||||
{
|
||||
return ((*_flagsPointer >> 24) & 1) == 1;
|
||||
}
|
||||
|
||||
/// @brief Sets whether this cheat is active (enabled) or not.
|
||||
/// @param isCheatActive \c true to enable this cheat, or \c false to disable this cheat.
|
||||
void SetIsCheatActive(bool isCheatActive) const
|
||||
{
|
||||
u32 flags = *_flagsPointer;
|
||||
flags &= ~(1 << 24);
|
||||
if (isCheatActive)
|
||||
{
|
||||
flags |= 1 << 24;
|
||||
}
|
||||
*_flagsPointer = flags;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _name = nullptr;
|
||||
const char* _description = nullptr;
|
||||
u32* _flagsPointer = nullptr;
|
||||
const void* _cheatData = nullptr;
|
||||
u32 _cheatDataLength = 0;
|
||||
};
|
||||
@@ -1,93 +0,0 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include "ICheatCategory.h"
|
||||
|
||||
/// @brief Class representing a cheat category, containing sub-categories and cheats.
|
||||
class CheatCategory : public ICheatCategory
|
||||
{
|
||||
public:
|
||||
CheatCategory() { }
|
||||
|
||||
CheatCategory(const char* name, const char* description, bool isMaxOneCheatActive,
|
||||
CheatCategory* subCategories, u32 numberOfSubCategories, Cheat* cheats, u32 numberOfCheats)
|
||||
: _name(name), _description(description), _isMaxOneCheatActive(isMaxOneCheatActive)
|
||||
, _subCategories(subCategories), _numberOfSubCategories(numberOfSubCategories)
|
||||
, _cheats(cheats), _numberOfCheats(numberOfCheats) { }
|
||||
|
||||
CheatCategory(CheatCategory& other) = delete;
|
||||
CheatCategory& operator=(CheatCategory& other) = delete;
|
||||
|
||||
CheatCategory(CheatCategory&& other)
|
||||
{
|
||||
*this = std::move(other);
|
||||
}
|
||||
|
||||
CheatCategory& operator=(CheatCategory&& other)
|
||||
{
|
||||
_name = other._name;
|
||||
other._name = nullptr;
|
||||
_description = other._description;
|
||||
other._description = nullptr;
|
||||
_isMaxOneCheatActive = other._isMaxOneCheatActive;
|
||||
other._isMaxOneCheatActive = false;
|
||||
_subCategories = other._subCategories;
|
||||
other._subCategories = nullptr;
|
||||
_numberOfSubCategories = other._numberOfSubCategories;
|
||||
other._numberOfSubCategories = 0;
|
||||
_cheats = other._cheats;
|
||||
other._cheats = nullptr;
|
||||
_numberOfCheats = other._numberOfCheats;
|
||||
other._numberOfCheats = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~CheatCategory()
|
||||
{
|
||||
if (_subCategories != nullptr)
|
||||
{
|
||||
free(_subCategories);
|
||||
}
|
||||
if (_cheats != nullptr)
|
||||
{
|
||||
free(_cheats);
|
||||
}
|
||||
}
|
||||
|
||||
const char* GetName() const override
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
/// @brief Gets the description of this cheat category.
|
||||
/// @return A pointer to the description of this cheat category.
|
||||
const char* GetDescription() const
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
bool GetIsMaxOneCheatActive() const override
|
||||
{
|
||||
return _isMaxOneCheatActive;
|
||||
}
|
||||
|
||||
const CheatCategory* GetCategories(u32& numberOfCategories) const override
|
||||
{
|
||||
numberOfCategories = _numberOfSubCategories;
|
||||
return _subCategories;
|
||||
}
|
||||
|
||||
const Cheat* GetCheats(u32& numberOfCheats) const override
|
||||
{
|
||||
numberOfCheats = _numberOfCheats;
|
||||
return _cheats;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _name = nullptr;
|
||||
const char* _description = nullptr;
|
||||
bool _isMaxOneCheatActive = false;
|
||||
CheatCategory* _subCategories = nullptr;
|
||||
u32 _numberOfSubCategories = 0;
|
||||
Cheat* _cheats = nullptr;
|
||||
u32 _numberOfCheats = 0;
|
||||
};
|
||||
190
arm9/source/cheats/CheatEntry.h
Normal file
190
arm9/source/cheats/CheatEntry.h
Normal file
@@ -0,0 +1,190 @@
|
||||
#pragma once
|
||||
|
||||
/// @brief Class representing a cheat or a cheat category.
|
||||
class CheatEntry
|
||||
{
|
||||
public:
|
||||
/// @brief Dummy empty constructor.
|
||||
CheatEntry()
|
||||
: _isCheatCategory(false), _flagsPointer(nullptr), _cheatData(nullptr), _cheatDataLength(0) { }
|
||||
|
||||
/// @brief Constructor for a cheat.
|
||||
CheatEntry(const char* name, const char* description, u32* flagsPointer, const void* cheatData, u32 cheatDataLength)
|
||||
: _name(name), _description(description), _isCheatCategory(false), _flagsPointer(flagsPointer)
|
||||
, _cheatData(cheatData), _cheatDataLength(cheatDataLength) { }
|
||||
|
||||
/// @brief Constructor for a category.
|
||||
CheatEntry(const char* name, const char* description, bool isMaxOneCheatActive, CheatEntry* subEntries, u32 numberOfSubEntries)
|
||||
: _name(name), _description(description), _isCheatCategory(true), _isMaxOneCheatActive(isMaxOneCheatActive)
|
||||
, _subEntries(subEntries), _numberOfSubEntries(numberOfSubEntries) { }
|
||||
|
||||
CheatEntry(const CheatEntry& other) = delete;
|
||||
|
||||
CheatEntry(CheatEntry&& other)
|
||||
: _isCheatCategory(false), _flagsPointer(nullptr), _cheatData(nullptr), _cheatDataLength(0)
|
||||
{
|
||||
*this = std::move(other);
|
||||
}
|
||||
|
||||
~CheatEntry()
|
||||
{
|
||||
if (_isCheatCategory && _subEntries != nullptr)
|
||||
{
|
||||
delete[] _subEntries;
|
||||
}
|
||||
}
|
||||
|
||||
CheatEntry& operator=(const CheatEntry& other) = delete;
|
||||
|
||||
CheatEntry& operator=(CheatEntry&& other)
|
||||
{
|
||||
if (_isCheatCategory && _subEntries != nullptr)
|
||||
{
|
||||
delete[] _subEntries;
|
||||
_subEntries = nullptr;
|
||||
}
|
||||
|
||||
_name = other._name;
|
||||
other._name = nullptr;
|
||||
_description = other._description;
|
||||
other._description = nullptr;
|
||||
_isCheatCategory = other.IsCheatCategory();
|
||||
if (_isCheatCategory)
|
||||
{
|
||||
_isMaxOneCheatActive = other._isMaxOneCheatActive;
|
||||
_subEntries = other._subEntries;
|
||||
other._subEntries = nullptr;
|
||||
_numberOfSubEntries = other._numberOfSubEntries;
|
||||
other._numberOfSubEntries = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_flagsPointer = other._flagsPointer;
|
||||
other._flagsPointer = nullptr;
|
||||
_cheatData = other._cheatData;
|
||||
other._cheatData = nullptr;
|
||||
_cheatDataLength = other._cheatDataLength;
|
||||
other._cheatDataLength = 0;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// @brief Gets the name of this cheat entry.
|
||||
/// @return A pointer to the name of this cheat entry.
|
||||
const char* GetName() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
/// @brief Gets the description of this cheat entry.
|
||||
/// @return A pointer to the description of this cheat entry.
|
||||
const char* GetDescription() const
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
/// @brief When this entry is a cheat, gets a pointer to the cheat data.
|
||||
/// @param cheatDataLength The length of the cheat data is returned in this reference.
|
||||
/// @return A pointer to the cheat data.
|
||||
/// This pointer is only valid for the lifetime of the \see GameCheats instance this cheat belongs to.
|
||||
/// If this entry is not a cheat, \c nullptr is returned.
|
||||
const void* GetCheatData(u32& cheatDataLength) const
|
||||
{
|
||||
if (_isCheatCategory)
|
||||
{
|
||||
cheatDataLength = 0;
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
cheatDataLength = _cheatDataLength;
|
||||
return _cheatData;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Gets whether this entry is an active (enabled) cheat or not.
|
||||
/// @return \c true when this entry is an active cheat, or \c false otherwise.
|
||||
bool GetIsCheatActive() const
|
||||
{
|
||||
if (_isCheatCategory)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ((*_flagsPointer >> 24) & 1) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief If this entry is a cheat, sets whether this cheat is active (enabled) or not.
|
||||
/// @param isCheatActive \c true to enable this cheat, or \c false to disable this cheat.
|
||||
void SetIsCheatActive(bool isCheatActive) const
|
||||
{
|
||||
if (!_isCheatCategory)
|
||||
{
|
||||
u32 flags = *_flagsPointer;
|
||||
flags &= ~(1 << 24);
|
||||
if (isCheatActive)
|
||||
{
|
||||
flags |= 1 << 24;
|
||||
}
|
||||
*_flagsPointer = flags;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Indicates if this entry is a cheat category or not.
|
||||
/// @return \c true when this entry is a cheat category, or \c false when this entry is a cheat.
|
||||
bool IsCheatCategory() const
|
||||
{
|
||||
return _isCheatCategory;
|
||||
}
|
||||
|
||||
/// @brief Indicates if this entry is a cheat category in which only one cheat is allowed to be on at a time or not.
|
||||
/// @return \c true when this entry is a cheat category in which only one cheat is allowed
|
||||
/// to be active at a time, or \c false otherwise.
|
||||
bool GetIsMaxOneCheatActive() const
|
||||
{
|
||||
return _isCheatCategory && _isMaxOneCheatActive;
|
||||
}
|
||||
|
||||
/// @brief Gets the sub-entries of this entry.
|
||||
/// @param numberOfSubEntries The number of sub-entries is returned through this reference.
|
||||
/// @return A pointer to an array of entries. This may be \c nullptr when \p numberOfSubEntries is 0.
|
||||
const CheatEntry* GetSubEntries(u32& numberOfSubEntries) const
|
||||
{
|
||||
if (_isCheatCategory)
|
||||
{
|
||||
numberOfSubEntries = _numberOfSubEntries;
|
||||
return _subEntries;
|
||||
}
|
||||
else
|
||||
{
|
||||
numberOfSubEntries = 0;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _name = nullptr;
|
||||
const char* _description = nullptr;
|
||||
bool _isCheatCategory;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
// For cheat
|
||||
u32* _flagsPointer;
|
||||
const void* _cheatData;
|
||||
u32 _cheatDataLength;
|
||||
};
|
||||
struct
|
||||
{
|
||||
// For category
|
||||
bool _isMaxOneCheatActive;
|
||||
CheatEntry* _subEntries;
|
||||
u32 _numberOfSubEntries;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -1,48 +1,15 @@
|
||||
#pragma once
|
||||
#include "Cheat.h"
|
||||
#include "CheatCategory.h"
|
||||
#include "ICheatCategory.h"
|
||||
#include "CheatEntry.h"
|
||||
|
||||
/// @brief Class holding the cheats for a game.
|
||||
class GameCheats : public ICheatCategory
|
||||
class GameCheats : public CheatEntry
|
||||
{
|
||||
public:
|
||||
GameCheats(std::unique_ptr<u8[]> cheatData, u32 cheatDataLength, u32 fileOffset, const char* gameName,
|
||||
CheatCategory* categories, u32 numberOfCategories, Cheat* cheats, u32 numberOfCheats)
|
||||
: _cheatData(std::move(cheatData)), _cheatDataLength(cheatDataLength), _fileOffset(fileOffset), _gameName(gameName)
|
||||
, _categories(categories), _numberOfCategories(numberOfCategories)
|
||||
, _cheats(cheats), _numberOfCheats(numberOfCheats) { }
|
||||
|
||||
~GameCheats()
|
||||
{
|
||||
if (_categories != nullptr)
|
||||
{
|
||||
free(_categories);
|
||||
}
|
||||
if (_cheats != nullptr)
|
||||
{
|
||||
free(_cheats);
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Gets the name of the game.
|
||||
/// @return A pointer to the name of the game.
|
||||
const char* GetGameName() const
|
||||
{
|
||||
return _gameName;
|
||||
}
|
||||
|
||||
const CheatCategory* GetCategories(u32& numberOfCategories) const override
|
||||
{
|
||||
numberOfCategories = _numberOfCategories;
|
||||
return _categories;
|
||||
}
|
||||
|
||||
const Cheat* GetCheats(u32& numberOfCheats) const override
|
||||
{
|
||||
numberOfCheats = _numberOfCheats;
|
||||
return _cheats;
|
||||
}
|
||||
CheatEntry* subEntries, u32 numberOfSubEntries)
|
||||
: CheatEntry(gameName, "", false, subEntries, numberOfSubEntries)
|
||||
, _cheatData(std::move(cheatData)), _cheatDataLength(cheatDataLength)
|
||||
, _fileOffset(fileOffset) { }
|
||||
|
||||
/// @brief Gets a pointer to the cheat data.
|
||||
/// @param cheatDataLength The length of the cheat data is returned in this reference.
|
||||
@@ -62,20 +29,4 @@ private:
|
||||
std::unique_ptr<u8[]> _cheatData;
|
||||
u32 _cheatDataLength;
|
||||
u32 _fileOffset;
|
||||
const char* _gameName;
|
||||
CheatCategory* _categories;
|
||||
u32 _numberOfCategories;
|
||||
Cheat* _cheats;
|
||||
u32 _numberOfCheats;
|
||||
|
||||
// From ICheatCategory
|
||||
const char* GetName() const override
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
bool GetIsMaxOneCheatActive() const override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
class CheatCategory;
|
||||
class Cheat;
|
||||
|
||||
/// @brief Interface for a collection of cheats.
|
||||
class ICheatCategory
|
||||
{
|
||||
public:
|
||||
virtual ~ICheatCategory() = default;
|
||||
|
||||
/// @brief Gets the name of this category.
|
||||
/// @return The name of this category.
|
||||
virtual const char* GetName() const = 0;
|
||||
|
||||
/// @brief Indicates if only one cheat in this category is allowed to be on or not.
|
||||
/// @return \c true when only one cheat is allowed to be active in this category, or \c false otherwise.
|
||||
virtual bool GetIsMaxOneCheatActive() const = 0;
|
||||
|
||||
/// @brief Gets the sub-categories of this category.
|
||||
/// @param numberOfCategories The number of categories is returned through this reference.
|
||||
/// @return A pointer to an array of categories. This may be \c nullptr when \p numberOfCategories is 0.
|
||||
virtual const CheatCategory* GetCategories(u32& numberOfCategories) const;
|
||||
|
||||
/// @brief Gets the cheats in this category.
|
||||
/// @param numberOfCheats The number of cheats is returned through this reference.
|
||||
/// @return A pointer to an array of cheats. This may be \c nullptr when \p numberOfCheats is 0.
|
||||
virtual const Cheat* GetCheats(u32& numberOfCheats) const;
|
||||
|
||||
protected:
|
||||
ICheatCategory() = default;
|
||||
};
|
||||
@@ -8,7 +8,7 @@ pload_cheats_t* PicoLoaderCheatDataFactory::CreateCheatData(const std::unique_pt
|
||||
if (gameCheats)
|
||||
{
|
||||
u32 totalNumberOfCheats = 0;
|
||||
u32 requiredSize = GetCheatCategoryRequiredSize(gameCheats.get(), totalNumberOfCheats);
|
||||
u32 requiredSize = GetCheatEntryRequiredSize(gameCheats.get(), totalNumberOfCheats);
|
||||
if (totalNumberOfCheats != 0)
|
||||
{
|
||||
requiredSize += sizeof(u32) * 2;
|
||||
@@ -16,32 +16,32 @@ pload_cheats_t* PicoLoaderCheatDataFactory::CreateCheatData(const std::unique_pt
|
||||
cheatData->length = requiredSize;
|
||||
cheatData->numberOfCheats = totalNumberOfCheats;
|
||||
u8* buffer = (u8*)&cheatData->firstCheat;
|
||||
GetCheatCategoryData(gameCheats.get(), buffer);
|
||||
GetCheatEntryData(gameCheats.get(), buffer);
|
||||
}
|
||||
}
|
||||
|
||||
return cheatData;
|
||||
}
|
||||
|
||||
u32 PicoLoaderCheatDataFactory::GetCheatCategoryRequiredSize(const ICheatCategory* cheatCategory, u32& totalNumberOfCheats) const
|
||||
u32 PicoLoaderCheatDataFactory::GetCheatEntryRequiredSize(const CheatEntry* cheatEntry, u32& totalNumberOfCheats) const
|
||||
{
|
||||
u32 requiredSize = 0;
|
||||
|
||||
u32 numberOfCategories = 0;
|
||||
auto subCategories = cheatCategory->GetCategories(numberOfCategories);
|
||||
for (u32 i = 0; i < numberOfCategories; i++)
|
||||
if (cheatEntry->IsCheatCategory())
|
||||
{
|
||||
requiredSize += GetCheatCategoryRequiredSize(&subCategories[i], totalNumberOfCheats);
|
||||
}
|
||||
|
||||
u32 numberOfCheats = 0;
|
||||
auto cheats = cheatCategory->GetCheats(numberOfCheats);
|
||||
for (u32 i = 0; i < numberOfCheats; i++)
|
||||
{
|
||||
u32 cheatRequiredSize = GetCheatRequiredSize(&cheats[i]);
|
||||
if (cheatRequiredSize != 0)
|
||||
u32 numberOfSubEntries = 0;
|
||||
auto subEntries = cheatEntry->GetSubEntries(numberOfSubEntries);
|
||||
for (u32 i = 0; i < numberOfSubEntries; i++)
|
||||
{
|
||||
requiredSize += cheatRequiredSize;
|
||||
requiredSize += GetCheatEntryRequiredSize(&subEntries[i], totalNumberOfCheats);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cheatEntry->GetIsCheatActive())
|
||||
{
|
||||
u32 cheatDataLength = 0;
|
||||
cheatEntry->GetCheatData(cheatDataLength);
|
||||
requiredSize += sizeof(u32) + ((cheatDataLength + 7) & ~7);
|
||||
totalNumberOfCheats++;
|
||||
}
|
||||
}
|
||||
@@ -49,51 +49,32 @@ u32 PicoLoaderCheatDataFactory::GetCheatCategoryRequiredSize(const ICheatCategor
|
||||
return requiredSize;
|
||||
}
|
||||
|
||||
u32 PicoLoaderCheatDataFactory::GetCheatRequiredSize(const Cheat* cheat) const
|
||||
void PicoLoaderCheatDataFactory::GetCheatEntryData(const CheatEntry* cheatEntry, u8*& buffer) const
|
||||
{
|
||||
if (cheat->GetIsCheatActive())
|
||||
if (cheatEntry->IsCheatCategory())
|
||||
{
|
||||
u32 cheatDataLength = 0;
|
||||
cheat->GetCheatData(cheatDataLength);
|
||||
return sizeof(u32) + ((cheatDataLength + 7) & ~7);
|
||||
u32 numberOfSubEntries = 0;
|
||||
auto subEntries = cheatEntry->GetSubEntries(numberOfSubEntries);
|
||||
for (u32 i = 0; i < numberOfSubEntries; i++)
|
||||
{
|
||||
GetCheatEntryData(&subEntries[i], buffer);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void PicoLoaderCheatDataFactory::GetCheatCategoryData(const ICheatCategory* cheatCategory, u8*& buffer) const
|
||||
{
|
||||
u32 numberOfCategories = 0;
|
||||
auto subCategories = cheatCategory->GetCategories(numberOfCategories);
|
||||
for (u32 i = 0; i < numberOfCategories; i++)
|
||||
{
|
||||
GetCheatCategoryData(&subCategories[i], buffer);
|
||||
}
|
||||
|
||||
u32 numberOfCheats = 0;
|
||||
auto cheats = cheatCategory->GetCheats(numberOfCheats);
|
||||
for (u32 i = 0; i < numberOfCheats; i++)
|
||||
{
|
||||
GetCheatData(&cheats[i], buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void PicoLoaderCheatDataFactory::GetCheatData(const Cheat* cheat, u8*& buffer) const
|
||||
{
|
||||
if (cheat->GetIsCheatActive())
|
||||
{
|
||||
u32 cheatDataLength = 0;
|
||||
auto cheatData = cheat->GetCheatData(cheatDataLength);
|
||||
u32 paddedCheatDataLength = (cheatDataLength + 7) & ~7;
|
||||
*(u32*)buffer = paddedCheatDataLength;
|
||||
buffer += sizeof(u32);
|
||||
memcpy(buffer, cheatData, cheatDataLength);
|
||||
if (cheatDataLength != paddedCheatDataLength)
|
||||
if (cheatEntry->GetIsCheatActive())
|
||||
{
|
||||
memset(buffer + cheatDataLength, 0, paddedCheatDataLength - cheatDataLength);
|
||||
u32 cheatDataLength = 0;
|
||||
auto cheatData = cheatEntry->GetCheatData(cheatDataLength);
|
||||
u32 paddedCheatDataLength = (cheatDataLength + 7) & ~7;
|
||||
*(u32*)buffer = paddedCheatDataLength;
|
||||
buffer += sizeof(u32);
|
||||
memcpy(buffer, cheatData, cheatDataLength);
|
||||
if (cheatDataLength != paddedCheatDataLength)
|
||||
{
|
||||
memset(buffer + cheatDataLength, 0, paddedCheatDataLength - cheatDataLength);
|
||||
}
|
||||
buffer += paddedCheatDataLength;
|
||||
}
|
||||
buffer += paddedCheatDataLength;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@ public:
|
||||
pload_cheats_t* CreateCheatData(const std::unique_ptr<GameCheats>& gameCheats) const;
|
||||
|
||||
private:
|
||||
u32 GetCheatCategoryRequiredSize(const ICheatCategory* cheatCategory, u32& totalNumberOfCheats) const;
|
||||
u32 GetCheatRequiredSize(const Cheat* cheat) const;
|
||||
void GetCheatCategoryData(const ICheatCategory* cheatCategory, u8*& buffer) const;
|
||||
void GetCheatData(const Cheat* cheat, u8*& buffer) const;
|
||||
u32 GetCheatEntryRequiredSize(const CheatEntry* cheatEntry, u32& totalNumberOfCheats) const;
|
||||
void GetCheatEntryData(const CheatEntry* cheatEntry, u8*& buffer) const;
|
||||
};
|
||||
|
||||
@@ -107,11 +107,8 @@ std::unique_ptr<GameCheats> UsrCheatRepository::GetCheatsForGame(u32 gameCode, u
|
||||
// master codes
|
||||
ptr += 8 * 4;
|
||||
|
||||
auto categories = (CheatCategory*)malloc(totalNumberOfItems * sizeof(CheatCategory));
|
||||
u32 categoryCount = 0;
|
||||
|
||||
auto cheats = (Cheat*)malloc(totalNumberOfItems * sizeof(Cheat));
|
||||
u32 cheatCount = 0;
|
||||
auto entries = new CheatEntry[totalNumberOfItems];
|
||||
u32 entryCount = 0;
|
||||
|
||||
while (ptr < cheatData.get() + cheatDataLength)
|
||||
{
|
||||
@@ -119,21 +116,20 @@ std::unique_ptr<GameCheats> UsrCheatRepository::GetCheatsForGame(u32 gameCode, u
|
||||
bool isCategory = ((itemFlags >> 28) & 1) == 1;
|
||||
if (isCategory)
|
||||
{
|
||||
ParseCategory(categories[categoryCount], ptr);
|
||||
categoryCount++;
|
||||
entries[entryCount++] = ParseCategory(ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
ParseCheat(cheats[cheatCount], ptr);
|
||||
cheatCount++;
|
||||
entries[entryCount++] = ParseCheat(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
categories = (CheatCategory*)realloc(categories, categoryCount * sizeof(CheatCategory));
|
||||
cheats = (Cheat*)realloc(cheats, cheatCount * sizeof(Cheat));
|
||||
auto actualEntries = new CheatEntry[entryCount];
|
||||
std::move(entries, entries + entryCount, actualEntries);
|
||||
delete[] entries;
|
||||
|
||||
return std::make_unique<GameCheats>(
|
||||
std::move(cheatData), cheatDataLength, index->offset, gameName, categories, categoryCount, cheats, cheatCount);
|
||||
std::move(cheatData), cheatDataLength, index->offset, gameName, actualEntries, entryCount);
|
||||
}
|
||||
|
||||
const usr_cheat_index_entry_t* UsrCheatRepository::FindIndex(u32 gameCode, u32 headerCrc32) const
|
||||
@@ -162,7 +158,7 @@ const usr_cheat_index_entry_t* UsrCheatRepository::FindIndex(u32 gameCode, u32 h
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void UsrCheatRepository::ParseCategory(CheatCategory& category, u8*& ptr) const
|
||||
CheatEntry UsrCheatRepository::ParseCategory(u8*& ptr) const
|
||||
{
|
||||
// flags
|
||||
u32 itemFlags = *(u32*)ptr;
|
||||
@@ -181,35 +177,25 @@ void UsrCheatRepository::ParseCategory(CheatCategory& category, u8*& ptr) const
|
||||
// padding
|
||||
ptr = (u8*)(((u32)ptr + 3) & ~3); // 32-bit align
|
||||
|
||||
auto categories = (CheatCategory*)malloc(numberOfItems * sizeof(CheatCategory));
|
||||
u32 categoryCount = 0;
|
||||
|
||||
auto cheats = (Cheat*)malloc(numberOfItems * sizeof(Cheat));
|
||||
u32 cheatCount = 0;
|
||||
|
||||
auto entries = new CheatEntry[numberOfItems];
|
||||
for (u32 i = 0; i < numberOfItems; i++)
|
||||
{
|
||||
u32 itemFlags = *(u32*)ptr;
|
||||
bool isCategory = ((itemFlags >> 28) & 1) == 1;
|
||||
if (isCategory)
|
||||
{
|
||||
ParseCategory(categories[categoryCount], ptr);
|
||||
categoryCount++;
|
||||
entries[i] = ParseCategory(ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
ParseCheat(cheats[cheatCount], ptr);
|
||||
cheatCount++;
|
||||
entries[i] = ParseCheat(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
categories = (CheatCategory*)realloc(categories, categoryCount * sizeof(CheatCategory));
|
||||
cheats = (Cheat*)realloc(cheats, cheatCount * sizeof(Cheat));
|
||||
|
||||
new (&category) CheatCategory (itemName, itemDescription, isMaxOneCheatActive, categories, categoryCount, cheats, cheatCount);
|
||||
return CheatEntry(itemName, itemDescription, isMaxOneCheatActive, entries, numberOfItems);
|
||||
}
|
||||
|
||||
void UsrCheatRepository::ParseCheat(Cheat& cheat, u8*& ptr) const
|
||||
CheatEntry UsrCheatRepository::ParseCheat(u8*& ptr) const
|
||||
{
|
||||
// flags
|
||||
u32* flagsPtr = (u32*)ptr;
|
||||
@@ -230,8 +216,10 @@ void UsrCheatRepository::ParseCheat(Cheat& cheat, u8*& ptr) const
|
||||
u32 numberOfCodeWords = *(u32*)ptr;
|
||||
ptr += 4;
|
||||
|
||||
new (&cheat) Cheat(itemName, itemDescription, flagsPtr, ptr, numberOfCodeWords * 4);
|
||||
const void* cheatData = ptr;
|
||||
|
||||
// code
|
||||
ptr += numberOfCodeWords * 4;
|
||||
|
||||
return CheatEntry(itemName, itemDescription, flagsPtr, cheatData, numberOfCodeWords * 4);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,6 @@ private:
|
||||
|
||||
std::unique_ptr<GameCheats> GetCheatsForGame(u32 gameCode, u32 headerCrc32) const;
|
||||
const usr_cheat_index_entry_t* FindIndex(u32 gameCode, u32 headerCrc32) const;
|
||||
void ParseCategory(CheatCategory& category, u8*& ptr) const;
|
||||
void ParseCheat(Cheat& cheat, u8*& ptr) const;
|
||||
CheatEntry ParseCategory(u8*& ptr) const;
|
||||
CheatEntry ParseCheat(u8*& ptr) const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user