Enhance Usecode handling with new structures and caching mechanisms
- Introduced UsecodeSlotOwnerBuffers for managing descriptors and chunk states. - Added UsecodeSlotChunkCache class for efficient loading and unloading of event bodies. - Updated UsecodeClass to include extraSlotCount and adjusted related functions. - Implemented tests for new functionality, ensuring correct behavior of slot management and event loading.
This commit is contained in:
parent
08a907f322
commit
8c23bb02c9
5 changed files with 187 additions and 14 deletions
|
|
@ -16,6 +16,29 @@ u32 readDword(const std::vector<u8>& bytes, std::size_t offset)
|
|||
(static_cast<u32>(bytes[offset + 2]) << 16) | (static_cast<u32>(bytes[offset + 3]) << 24);
|
||||
}
|
||||
|
||||
std::optional<UsecodeEventBody> readEventBodyFromDescriptors(const UsecodeClass& usecodeClass,
|
||||
const std::vector<u8>& descriptors, std::size_t descriptorOffset, u32 eventId)
|
||||
{
|
||||
if (usecodeClass.codeBase < 20 || usecodeClass.codeBase > usecodeClass.bytes.size() ||
|
||||
eventId >= (usecodeClass.codeBase - 20) / 6 ||
|
||||
descriptorOffset > descriptors.size() || descriptors.size() - descriptorOffset <
|
||||
static_cast<std::size_t>(eventId) * 6 + 6) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::size_t row = descriptorOffset + static_cast<std::size_t>(eventId) * 6;
|
||||
const u16 length = readWord(descriptors, row);
|
||||
const u32 offset = readDword(descriptors, row + 2);
|
||||
if (length == 0 || offset == 0 || offset > usecodeClass.bytes.size() - usecodeClass.codeBase + 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::size_t start = usecodeClass.codeBase + static_cast<std::size_t>(offset) - 1;
|
||||
if (length > usecodeClass.bytes.size() - start) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return UsecodeEventBody{length, offset,
|
||||
std::vector<u8>(usecodeClass.bytes.begin() + start, usecodeClass.bytes.begin() + start + length)};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
u16 selectUsecodeClassId(const UsecodeItemIdentity& item, const UsecodeClassBases& bases)
|
||||
|
|
@ -59,6 +82,7 @@ std::optional<UsecodeClass> loadUsecodeClass(const FlexArchive& archive, u32 cla
|
|||
return std::nullopt;
|
||||
}
|
||||
const std::size_t nameOffset = 4 + static_cast<std::size_t>(classId) * 13;
|
||||
const u32 extraSlotCount = readDword(*bytes, 0);
|
||||
const u32 codeBase = readDword(*bytes, 8);
|
||||
if (codeBase < 20 || codeBase > bytes->size() || (codeBase - 20) % 6 != 0) {
|
||||
return std::nullopt;
|
||||
|
|
@ -67,27 +91,103 @@ std::optional<UsecodeClass> loadUsecodeClass(const FlexArchive& archive, u32 cla
|
|||
for (std::size_t index = 0; index < 9 && (*names)[nameOffset + index] != 0; ++index) {
|
||||
name.push_back(static_cast<char>((*names)[nameOffset + index]));
|
||||
}
|
||||
return UsecodeClass{classId, std::move(name), codeBase, std::move(*bytes)};
|
||||
return UsecodeClass{classId, std::move(name), extraSlotCount, codeBase, std::move(*bytes)};
|
||||
}
|
||||
|
||||
std::optional<UsecodeSlotOwnerBuffers> initializeUsecodeSlotOwnerBuffers(const UsecodeClass& usecodeClass)
|
||||
{
|
||||
constexpr std::size_t baseChunkCount = 0x20;
|
||||
constexpr std::size_t baseDescriptorBytes = 0xc0;
|
||||
constexpr std::size_t baseStateBytes = 0x40;
|
||||
if (usecodeClass.codeBase < 20 || usecodeClass.codeBase > usecodeClass.bytes.size()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const std::size_t chunkCount = baseChunkCount + usecodeClass.extraSlotCount;
|
||||
const std::size_t descriptorBytes = baseDescriptorBytes +
|
||||
static_cast<std::size_t>(usecodeClass.extraSlotCount) * 6;
|
||||
const std::size_t stateBytes = baseStateBytes +
|
||||
static_cast<std::size_t>(usecodeClass.extraSlotCount) * 2;
|
||||
if (chunkCount != (usecodeClass.codeBase - 20) / 6 ||
|
||||
descriptorBytes != chunkCount * 6 || stateBytes != chunkCount * 2 ||
|
||||
descriptorBytes > usecodeClass.bytes.size() - 20) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
UsecodeSlotOwnerBuffers buffers;
|
||||
buffers.descriptors.assign(usecodeClass.bytes.begin() + 20,
|
||||
usecodeClass.bytes.begin() + 20 + descriptorBytes);
|
||||
buffers.chunkStates.assign(stateBytes / 2, 0xffff);
|
||||
return buffers;
|
||||
}
|
||||
|
||||
std::optional<UsecodeEventBody> readUsecodeEventBody(const UsecodeClass& usecodeClass, u32 eventId)
|
||||
{
|
||||
if (usecodeClass.codeBase < 20 || usecodeClass.codeBase > usecodeClass.bytes.size() ||
|
||||
eventId >= (usecodeClass.codeBase - 20) / 6) {
|
||||
return std::nullopt;
|
||||
return readEventBodyFromDescriptors(usecodeClass, usecodeClass.bytes, 20, eventId);
|
||||
}
|
||||
|
||||
UsecodeSlotChunkCache::UsecodeSlotChunkCache(UsecodeClass usecodeClass)
|
||||
: usecodeClass_(std::move(usecodeClass))
|
||||
{
|
||||
auto buffers = initializeUsecodeSlotOwnerBuffers(usecodeClass_);
|
||||
if (buffers) {
|
||||
ownerBuffers_ = std::move(*buffers);
|
||||
chunks_.resize(ownerBuffers_.chunkStates.size());
|
||||
}
|
||||
const std::size_t row = 20 + static_cast<std::size_t>(eventId) * 6;
|
||||
const u16 length = readWord(usecodeClass.bytes, row);
|
||||
const u32 offset = readDword(usecodeClass.bytes, row + 2);
|
||||
if (length == 0 || offset == 0 || offset > usecodeClass.bytes.size() - usecodeClass.codeBase + 1) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const UsecodeEventBody* UsecodeSlotChunkCache::ensureLoaded(u16 chunkIndex)
|
||||
{
|
||||
if (chunkIndex >= chunks_.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
const std::size_t start = usecodeClass.codeBase + static_cast<std::size_t>(offset) - 1;
|
||||
if (length > usecodeClass.bytes.size() - start) {
|
||||
return std::nullopt;
|
||||
auto& body = chunks_[chunkIndex];
|
||||
auto& state = ownerBuffers_.chunkStates[chunkIndex];
|
||||
if (!body) {
|
||||
if (state != 0xffff) {
|
||||
return nullptr;
|
||||
}
|
||||
auto loadedBody = readEventBodyFromDescriptors(usecodeClass_, ownerBuffers_.descriptors, 0, chunkIndex);
|
||||
if (!loadedBody) {
|
||||
return nullptr;
|
||||
}
|
||||
chunks_[chunkIndex] = std::move(*loadedBody);
|
||||
state = 1;
|
||||
} else {
|
||||
if (state >= 29999) {
|
||||
return nullptr;
|
||||
}
|
||||
++state;
|
||||
}
|
||||
return UsecodeEventBody{length, offset,
|
||||
std::vector<u8>(usecodeClass.bytes.begin() + start, usecodeClass.bytes.begin() + start + length)};
|
||||
return &*chunks_[chunkIndex];
|
||||
}
|
||||
|
||||
bool UsecodeSlotChunkCache::release(u16 chunkIndex) noexcept
|
||||
{
|
||||
if (chunkIndex >= chunks_.size() || ownerBuffers_.chunkStates[chunkIndex] == 0xffff ||
|
||||
ownerBuffers_.chunkStates[chunkIndex] == 0) {
|
||||
return false;
|
||||
}
|
||||
--ownerBuffers_.chunkStates[chunkIndex];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UsecodeSlotChunkCache::tryUnload(u16 chunkIndex) noexcept
|
||||
{
|
||||
if (chunkIndex >= chunks_.size() || ownerBuffers_.chunkStates[chunkIndex] != 0) {
|
||||
return false;
|
||||
}
|
||||
chunks_[chunkIndex].reset();
|
||||
ownerBuffers_.chunkStates[chunkIndex] = 0xffff;
|
||||
return true;
|
||||
}
|
||||
|
||||
u16 UsecodeSlotChunkCache::inUseCount(u16 chunkIndex) const noexcept
|
||||
{
|
||||
if (chunkIndex >= chunks_.size() || ownerBuffers_.chunkStates[chunkIndex] == 0xffff) {
|
||||
return 0;
|
||||
}
|
||||
return ownerBuffers_.chunkStates[chunkIndex];
|
||||
}
|
||||
|
||||
std::optional<EligibleUsecodeEvent> selectEligibleUsecodeEvent(const FlexArchive& archive,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue