Store individual multiblock saves into a separate in-mem db by value

This commit is contained in:
King_DuckZ 2025-02-15 02:45:02 +00:00
parent b0cc4426bd
commit 0b94626549
7 changed files with 181 additions and 6 deletions

82
src/qt/block_db.cpp Normal file
View file

@ -0,0 +1,82 @@
/* Copyright 2020-2025, Michele Santullo
* This file is part of memoserv.
*
* Memoserv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Memoserv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Memoserv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "block_db.hpp"
#include "memcard/block.hpp"
#include <algorithm>
#include <ciso646>
#include <iterator>
#include <utility>
#include <tuple>
namespace duck {
BlockEntry::BlockEntry (
std::vector<mc::psx::BasicBlock<false>>&& blocks,
std::unique_ptr<uint8_t[]>&& data
) :
blocks(std::move(blocks)),
data(std::move(data))
{
}
std::string_view BlockEntry::identifier() const {
return blocks.front().identifier();
}
std::string BlockEntry::title() const {
return blocks.front().title();
}
BlockDb::BlockDb() = default;
BlockDb::~BlockDb() noexcept = default;
template <bool Const>
unsigned int BlockDb::add (const std::vector<mc::psx::BasicBlock<Const> >& blocks) {
constexpr auto block_size = mc::psx::BasicBlock<Const>::BlockByteSize;
constexpr auto frame_size = mc::psx::BasicFrame<Const>::Size;
assert(not blocks.empty());
const auto block_count = blocks.size();
assert(block_count == blocks.front().block_count());
auto mem = std::make_unique<uint8_t[]>(frame_size + block_size * block_count);
uint8_t* dest = std::copy(blocks.front().toc().cbegin(), blocks.front().toc().cend(), mem.get());
assert(std::distance(mem.get(), dest) == frame_size);
mc::psx::Frame toc(mem.get(), 0);
std::vector<mc::psx::BasicBlock<false>> new_blocks;
new_blocks.reserve(block_count);
std::size_t index = 0;
for (const auto& block : blocks) {
assert(block_count == block.block_count() or block.block_count() == 0);
uint8_t* const curr_block_ptr = dest;
dest = std::copy(block.data(), block.data() + block_size, dest);
new_blocks.emplace_back(curr_block_ptr, toc, index++);
}
const std::size_t new_id = m_next_id++;
m_saves.emplace(
std::piecewise_construct,
std::forward_as_tuple(new_id),
std::forward_as_tuple(std::move(new_blocks), std::move(mem))
);
return static_cast<unsigned int>(m_saves.size() - 1u);
}
template unsigned int BlockDb::add<true> (const std::vector<mc::psx::BasicBlock<true> >&);
template unsigned int BlockDb::add<false> (const std::vector<mc::psx::BasicBlock<false> >&);
} //namespace duck

57
src/qt/block_db.hpp Normal file
View file

@ -0,0 +1,57 @@
/* Copyright 2020-2025, Michele Santullo
* This file is part of memoserv.
*
* Memoserv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Memoserv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Memoserv. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include <vector>
#include <memory>
#include <string_view>
#include <string>
#include <map>
namespace mc::psx {
template <bool C> class BasicBlock;
} //namespace mc::psx
namespace duck {
struct BlockEntry {
BlockEntry (
std::vector<mc::psx::BasicBlock<false> >&& blocks,
std::unique_ptr<uint8_t[]>&& data
);
std::string_view identifier() const;
std::string title() const;
std::vector<mc::psx::BasicBlock<false> > blocks;
std::unique_ptr<uint8_t[]> data;
};
class BlockDb {
public:
BlockDb();
~BlockDb() noexcept;
template <bool Const>
unsigned int add (const std::vector<mc::psx::BasicBlock<Const> >& blocks);
private:
std::map<unsigned int, BlockEntry> m_saves;
std::size_t m_next_id{0};
};
} //namespace duck

View file

@ -30,6 +30,7 @@
#include <QLabel>
#include <memory>
#include <filesystem>
#include <cassert>
class QApplication;
@ -105,7 +106,16 @@ void MemoservWin::load_memory_cards (const QStringList& paths) {
auto& mc = m_memcards.back();
auto grid = std::make_unique<widget::BlockGrid>(this, 5, 3, g_icon_fps);
grid->set_icon_size(g_icon_size);
std::vector<mc::psx::Block> blocks_for_db;
for (const auto& block : mc) {
blocks_for_db.reserve(block.block_count());
blocks_for_db.push_back(block);
if (blocks_for_db.size() == blocks_for_db.front().block_count()) {
m_block_db.add(blocks_for_db);
blocks_for_db.clear();
}
for (std::size_t n = 0; block.has_magic() and n < block.block_count(); ++n) {
grid->push_back(make_qt_animation(block, g_icon_size, g_icon_size));
}

View file

@ -17,6 +17,7 @@
#pragma once
#include "block_db.hpp"
#include <QMainWindow>
#include <QStringList>
#include <list>
@ -45,6 +46,7 @@ private:
void open_file_from_dialog();
void load_memory_cards (const QStringList& paths);
duck::BlockDb m_block_db;
std::list<mc::psx::MemoryCard> m_memcards;
QGridLayout* m_main_lay;
unsigned int m_grid_count;

View file

@ -38,6 +38,7 @@ executable(app_name,
'widgets/detail/block_drag_and_drop.cpp',
'make_qt_animation.cpp',
'animated_pixmap.cpp',
'block_db.cpp',
include_directories: inc,
dependencies: [
qt6_dep,

View file

@ -48,12 +48,14 @@ public:
static const constexpr std::size_t FrameCount = 64;
static const constexpr std::size_t TOCBlockIndex = 0xFF;
static const constexpr uint16_t LastLink = 0xFFFF;
static constexpr std::size_t BlockByteSize = BasicFrame<Const>::Size * FrameCount;
BasicBlock() = default;
constexpr BasicBlock (const BasicBlock& other);
BasicBlock(BasicBlock&&) = default;
BasicBlock (data_type* blk, std::size_t index);
BasicBlock (data_type* blk, BasicFrame<Const> toc_entry, std::size_t index);
template <bool Const2> BasicBlock (const BasicBlock<Const2>& other);
BasicBlock(BasicBlock&&) = default;
template <bool Const2> BasicBlock(const BasicBlock<Const2>& other);
~BasicBlock();
iterator begin();
@ -68,6 +70,8 @@ public:
BasicFrame<Const> frame(unsigned int idx);
ConstFrame frame(unsigned int idx) const;
BasicFrame<Const> toc();
ConstFrame toc() const;
static constexpr std::size_t size() { return FrameCount; }
@ -115,12 +119,21 @@ inline int32_t calc_icon_count (IconDisplayFlag idf) {
}
}
template <bool Const>
constexpr BasicBlock<Const>::BasicBlock (const BasicBlock& other) :
m_toc_entry(other.m_toc_entry),
m_index(other.m_index),
m_begin(other.m_begin)
{
}
template <bool Const>
template <bool Const2>
inline BasicBlock<Const>::BasicBlock (const BasicBlock<Const2>& other) {
m_index = other.m_index;
m_toc_entry = other.m_toc_entry;
m_begin = other.m_begin;
inline BasicBlock<Const>::BasicBlock(const BasicBlock<Const2>& other) :
m_toc_entry(other.m_toc_entry),
m_index(other.m_index),
m_begin(other.m_begin)
{
}
using Block = BasicBlock<false>;

View file

@ -145,6 +145,16 @@ ConstFrame BasicBlock<Const>::frame(unsigned int idx) const {
return {this->data() + Frame::Size * idx, idx};
}
template <bool Const>
BasicFrame<Const> BasicBlock<Const>::toc() {
return m_toc_entry;
}
template <bool Const>
ConstFrame BasicBlock<Const>::toc() const {
return m_toc_entry;
}
template <bool Const>
std::vector<uint8_t> BasicBlock<Const>::icon_palette() const {
return extract_palette(frame(0));