Merge pull request #205 from lioncash/fn

CFactoryMgr: Minor cleanup
This commit is contained in:
Phillip Stephens 2020-03-12 15:09:11 -07:00 committed by GitHub
commit 5f2db40528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 26 deletions

View File

@ -1,9 +1,22 @@
#include "Runtime/CFactoryMgr.hpp"
#include <algorithm>
#include <array>
#include <cctype>
#include <iterator>
#include "Runtime/CStopwatch.hpp"
#include "Runtime/IObj.hpp"
namespace urde {
constexpr std::array TypeTable{
FOURCC('CLSN'), FOURCC('CMDL'), FOURCC('CSKR'), FOURCC('ANIM'), FOURCC('CINF'), FOURCC('TXTR'), FOURCC('PLTT'),
FOURCC('FONT'), FOURCC('ANCS'), FOURCC('EVNT'), FOURCC('MADF'), FOURCC('MLVL'), FOURCC('MREA'), FOURCC('MAPW'),
FOURCC('MAPA'), FOURCC('SAVW'), FOURCC('SAVA'), FOURCC('PART'), FOURCC('WPSC'), FOURCC('SWHC'), FOURCC('DPSC'),
FOURCC('ELSC'), FOURCC('CRSC'), FOURCC('AFSM'), FOURCC('DCLN'), FOURCC('AGSC'), FOURCC('ATBL'), FOURCC('CSNG'),
FOURCC('STRG'), FOURCC('SCAN'), FOURCC('PATH'), FOURCC('DGRP'), FOURCC('HMAP'), FOURCC('CTWK'), FOURCC('FRME'),
FOURCC('HINT'), FOURCC('MAPU'), FOURCC('DUMB'), FOURCC('OIDS'),
};
CFactoryFnReturn CFactoryMgr::MakeObject(const SObjectTag& tag, urde::CInputStream& in,
const CVParamTransfer& paramXfer, CObjectReference* selfRef) {
@ -24,52 +37,48 @@ CFactoryFnReturn CFactoryMgr::MakeObjectFromMemory(const SObjectTag& tag, std::u
CObjectReference* selfRef) {
std::unique_ptr<u8[]> localBuf = std::move(buf);
auto search = m_memFactories.find(tag.type);
if (search != m_memFactories.cend()) {
const auto memFactoryIter = m_memFactories.find(tag.type);
if (memFactoryIter != m_memFactories.cend()) {
if (compressed) {
std::unique_ptr<CInputStream> compRead = std::make_unique<athena::io::MemoryReader>(localBuf.get(), size);
u32 decompLen = compRead->readUint32Big();
const u32 decompLen = compRead->readUint32Big();
CZipInputStream r(std::move(compRead));
std::unique_ptr<u8[]> decompBuf = r.readUBytes(decompLen);
return search->second(tag, std::move(decompBuf), decompLen, paramXfer, selfRef);
return memFactoryIter->second(tag, std::move(decompBuf), decompLen, paramXfer, selfRef);
} else {
return search->second(tag, std::move(localBuf), size, paramXfer, selfRef);
return memFactoryIter->second(tag, std::move(localBuf), size, paramXfer, selfRef);
}
} else {
auto search = m_factories.find(tag.type);
if (search == m_factories.end())
const auto factoryIter = m_factories.find(tag.type);
if (factoryIter == m_factories.end()) {
return {};
}
if (compressed) {
std::unique_ptr<CInputStream> compRead = std::make_unique<athena::io::MemoryReader>(localBuf.get(), size);
compRead->readUint32Big();
CZipInputStream r(std::move(compRead));
return search->second(tag, r, paramXfer, selfRef);
return factoryIter->second(tag, r, paramXfer, selfRef);
} else {
CMemoryInStream r(localBuf.get(), size);
return search->second(tag, r, paramXfer, selfRef);
return factoryIter->second(tag, r, paramXfer, selfRef);
}
}
}
static const FourCC TypeTable[] = {
FOURCC('CLSN'), FOURCC('CMDL'), FOURCC('CSKR'), FOURCC('ANIM'), FOURCC('CINF'), FOURCC('TXTR'), FOURCC('PLTT'),
FOURCC('FONT'), FOURCC('ANCS'), FOURCC('EVNT'), FOURCC('MADF'), FOURCC('MLVL'), FOURCC('MREA'), FOURCC('MAPW'),
FOURCC('MAPA'), FOURCC('SAVW'), FOURCC('SAVA'), FOURCC('PART'), FOURCC('WPSC'), FOURCC('SWHC'), FOURCC('DPSC'),
FOURCC('ELSC'), FOURCC('CRSC'), FOURCC('AFSM'), FOURCC('DCLN'), FOURCC('AGSC'), FOURCC('ATBL'), FOURCC('CSNG'),
FOURCC('STRG'), FOURCC('SCAN'), FOURCC('PATH'), FOURCC('DGRP'), FOURCC('HMAP'), FOURCC('CTWK'), FOURCC('FRME'),
FOURCC('HINT'), FOURCC('MAPU'), FOURCC('DUMB'), FOURCC('OIDS')};
CFactoryMgr::ETypeTable CFactoryMgr::FourCCToTypeIdx(FourCC fcc) {
for (int i = 0; i < 4; ++i)
fcc.getChars()[i] = char(toupper(fcc.getChars()[i]));
auto search =
std::find_if(std::begin(TypeTable), std::end(TypeTable), [fcc](const FourCC& test) { return test == fcc; });
if (search == std::end(TypeTable))
return ETypeTable::Invalid;
return ETypeTable(search - std::begin(TypeTable));
for (size_t i = 0; i < 4; ++i) {
fcc.getChars()[i] = char(std::toupper(fcc.getChars()[i]));
}
FourCC CFactoryMgr::TypeIdxToFourCC(ETypeTable fcc) { return TypeTable[int(fcc)]; }
const auto search =
std::find_if(TypeTable.cbegin(), TypeTable.cend(), [fcc](const FourCC& test) { return test == fcc; });
if (search == TypeTable.cend()) {
return ETypeTable::Invalid;
}
return ETypeTable(std::distance(TypeTable.cbegin(), search));
}
FourCC CFactoryMgr::TypeIdxToFourCC(ETypeTable fcc) { return TypeTable[size_t(fcc)]; }
} // namespace urde

View File

@ -21,8 +21,8 @@ public:
bool CanMakeMemory(const urde::SObjectTag& tag) const;
CFactoryFnReturn MakeObjectFromMemory(const SObjectTag& tag, std::unique_ptr<u8[]>&& buf, int size, bool compressed,
const CVParamTransfer& paramXfer, CObjectReference* selfRef);
void AddFactory(FourCC key, FFactoryFunc func) { m_factories[key] = func; }
void AddFactory(FourCC key, FMemFactoryFunc func) { m_memFactories[key] = func; }
void AddFactory(FourCC key, FFactoryFunc func) { m_factories.insert_or_assign(key, std::move(func)); }
void AddFactory(FourCC key, FMemFactoryFunc func) { m_memFactories.insert_or_assign(key, std::move(func)); }
enum class ETypeTable : u8 {
CLSN,