mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 09:06:54 +02:00
Further work on support for cheats
Cheats can now be enabled/disabled and games can be launched with cheats
This commit is contained in:
@@ -3,57 +3,16 @@
|
||||
#include "fat/File.h"
|
||||
#include "CheatsViewModel.h"
|
||||
|
||||
#define CRCPOLY 0xEDB88320
|
||||
|
||||
static u32 crc32(const void* buffer, u32 length)
|
||||
{
|
||||
u32 crc = ~0u;
|
||||
const u8* p = (u8*)buffer;
|
||||
while (length--)
|
||||
{
|
||||
crc ^= *p++;
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY : 0);
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
CheatsViewModel::CheatsViewModel(const FileInfo& romFileInfo, IRomBrowserController* romBrowserController)
|
||||
: _romFileInfo(romFileInfo), _romBrowserController(romBrowserController)
|
||||
{
|
||||
_categoryStack.fill(nullptr);
|
||||
_loadCheatsTask = _romBrowserController->GetIoTaskQueue()->Enqueue([this] (const vu8& cancelRequested)
|
||||
{
|
||||
LOG_DEBUG("%s\n", _romFileInfo.GetFileName());
|
||||
auto file = std::make_unique<File>();
|
||||
file->Open(_romFileInfo.GetFastFileRef(), FA_READ);
|
||||
auto headerBuffer = std::make_unique_for_overwrite<u8[]>(512);
|
||||
if (!file->ReadExact(headerBuffer.get(), 512))
|
||||
{
|
||||
LOG_ERROR("Could not read rom header\n");
|
||||
return TaskResult<void>::Failed();
|
||||
}
|
||||
file->Close();
|
||||
|
||||
if (cancelRequested)
|
||||
{
|
||||
return TaskResult<void>::Canceled();
|
||||
}
|
||||
|
||||
u32 gameCode = *(u32*)(headerBuffer.get() + 0xC);
|
||||
u32 crc = crc32(headerBuffer.get(), 512);
|
||||
headerBuffer.reset();
|
||||
|
||||
if (cancelRequested)
|
||||
{
|
||||
return TaskResult<void>::Canceled();
|
||||
}
|
||||
|
||||
_cheats = _romBrowserController->GetCheatRepository().GetCheatsForGame(gameCode, crc);
|
||||
_cheats = _romBrowserController->GetCheatRepository().GetCheatsForGame(_romFileInfo.GetFastFileRef());
|
||||
if (_cheats)
|
||||
{
|
||||
_categoryStack[0] = _cheats.get();
|
||||
_state = State::DisplayCheats;
|
||||
}
|
||||
else
|
||||
@@ -64,3 +23,64 @@ CheatsViewModel::CheatsViewModel(const FileInfo& romFileInfo, IRomBrowserControl
|
||||
return TaskResult<void>::Completed();
|
||||
});
|
||||
}
|
||||
|
||||
void CheatsViewModel::ItemActivated()
|
||||
{
|
||||
auto cheatCategory = GetCurrentCheatCategory();
|
||||
u32 numberOfCategories = 0;
|
||||
auto categories = cheatCategory->GetCategories(numberOfCategories);
|
||||
u32 numberOfCheats = 0;
|
||||
auto cheats = cheatCategory->GetCheats(numberOfCheats);
|
||||
|
||||
if (_selectedItem < (int)numberOfCategories)
|
||||
{
|
||||
// Category activated
|
||||
if (_categoryStackLevel + 1 != _categoryStack.size())
|
||||
{
|
||||
_categoryStack[++_categoryStackLevel] = &categories[_selectedItem];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Toggle cheat on/off
|
||||
auto& cheat = cheats[_selectedItem - numberOfCategories];
|
||||
bool isEnabled = !cheat.GetIsCheatActive();
|
||||
if (isEnabled && cheatCategory->GetIsMaxOneCheatActive())
|
||||
{
|
||||
for (u32 i = 0; i < numberOfCheats; i++)
|
||||
{
|
||||
cheats[i].SetIsCheatActive(false);
|
||||
}
|
||||
}
|
||||
cheat.SetIsCheatActive(isEnabled);
|
||||
_changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void CheatsViewModel::Back()
|
||||
{
|
||||
if (_categoryStackLevel == 0)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
_categoryStack[_categoryStackLevel--] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void CheatsViewModel::Close()
|
||||
{
|
||||
if (_changed)
|
||||
{
|
||||
// Save which cheats are enabled/disabled
|
||||
_romBrowserController->GetIoTaskQueue()->Enqueue(
|
||||
[romBrowserController = _romBrowserController, cheats = move(_cheats)] (const vu8& cancelRequested)
|
||||
{
|
||||
romBrowserController->GetCheatRepository().UpdateEnabledCheatsForGame(cheats);
|
||||
return TaskResult<void>::Completed();
|
||||
});
|
||||
}
|
||||
|
||||
_romBrowserController->HideGameInfo();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user