2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CFactoryMgr.hpp"
|
|
|
|
|
2020-03-09 16:17:15 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <cctype>
|
|
|
|
#include <iterator>
|
2021-04-03 16:48:39 +00:00
|
|
|
#include "optick.h"
|
2020-03-09 16:17:15 +00:00
|
|
|
|
2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CStopwatch.hpp"
|
|
|
|
#include "Runtime/IObj.hpp"
|
2016-02-16 05:50:41 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2020-03-09 16:17:15 +00:00
|
|
|
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'),
|
|
|
|
};
|
2016-02-16 05:50:41 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
CFactoryFnReturn CFactoryMgr::MakeObject(const SObjectTag& tag, metaforce::CInputStream& in,
|
2018-12-08 05:30:43 +00:00
|
|
|
const CVParamTransfer& paramXfer, CObjectReference* selfRef) {
|
2022-02-21 00:03:38 +00:00
|
|
|
auto search = x10_factories.find(tag.type);
|
|
|
|
if (search == x10_factories.end())
|
2018-12-08 05:30:43 +00:00
|
|
|
return {};
|
2016-02-16 05:50:41 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
return search->second(tag, in, paramXfer, selfRef);
|
2016-02-16 05:50:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
bool CFactoryMgr::CanMakeMemory(const metaforce::SObjectTag& tag) const {
|
2022-02-21 00:03:38 +00:00
|
|
|
auto search = x24_memFactories.find(tag.type);
|
|
|
|
return search != x24_memFactories.cend();
|
2016-03-29 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFactoryFnReturn CFactoryMgr::MakeObjectFromMemory(const SObjectTag& tag, std::unique_ptr<u8[]>&& buf, int size,
|
2016-09-02 19:32:57 +00:00
|
|
|
bool compressed, const CVParamTransfer& paramXfer,
|
2018-12-08 05:30:43 +00:00
|
|
|
CObjectReference* selfRef) {
|
2021-04-03 16:48:39 +00:00
|
|
|
OPTICK_EVENT();
|
2018-12-08 05:30:43 +00:00
|
|
|
std::unique_ptr<u8[]> localBuf = std::move(buf);
|
2016-02-16 05:50:41 +00:00
|
|
|
|
2022-02-21 00:03:38 +00:00
|
|
|
const auto memFactoryIter = x24_memFactories.find(tag.type);
|
|
|
|
if (memFactoryIter != x24_memFactories.cend()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
if (compressed) {
|
2022-02-18 07:37:54 +00:00
|
|
|
std::unique_ptr<CInputStream> compRead =
|
|
|
|
std::make_unique<CMemoryInStream>(localBuf.get(), size, CMemoryInStream::EOwnerShip::NotOwned);
|
|
|
|
const u32 decompLen = compRead->ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
CZipInputStream r(std::move(compRead));
|
2022-02-18 07:37:54 +00:00
|
|
|
std::unique_ptr<u8[]> decompBuf(new u8[decompLen]);
|
|
|
|
r.Get(decompBuf.get(), decompLen);
|
2020-03-09 16:19:55 +00:00
|
|
|
return memFactoryIter->second(tag, std::move(decompBuf), decompLen, paramXfer, selfRef);
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-03-09 16:19:55 +00:00
|
|
|
return memFactoryIter->second(tag, std::move(localBuf), size, paramXfer, selfRef);
|
2016-02-16 05:50:41 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2022-02-21 00:03:38 +00:00
|
|
|
const auto factoryIter = x10_factories.find(tag.type);
|
|
|
|
if (factoryIter == x10_factories.end()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return {};
|
2020-03-09 16:19:55 +00:00
|
|
|
}
|
2016-03-29 23:14:14 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (compressed) {
|
2022-02-18 07:37:54 +00:00
|
|
|
std::unique_ptr<CInputStream> compRead =
|
|
|
|
std::make_unique<CMemoryInStream>(localBuf.get(), size, CMemoryInStream::EOwnerShip::NotOwned);
|
|
|
|
|
|
|
|
compRead->ReadLong();
|
2018-12-08 05:30:43 +00:00
|
|
|
CZipInputStream r(std::move(compRead));
|
2020-03-09 16:19:55 +00:00
|
|
|
return factoryIter->second(tag, r, paramXfer, selfRef);
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2022-02-18 07:37:54 +00:00
|
|
|
CMemoryInStream r(localBuf.get(), size, CMemoryInStream::EOwnerShip::NotOwned);
|
2020-03-09 16:19:55 +00:00
|
|
|
return factoryIter->second(tag, r, paramXfer, selfRef);
|
2016-02-16 05:50:41 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2016-02-16 05:50:41 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CFactoryMgr::ETypeTable CFactoryMgr::FourCCToTypeIdx(FourCC fcc) {
|
2020-03-09 16:17:15 +00:00
|
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
fcc.getChars()[i] = char(std::toupper(fcc.getChars()[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto search =
|
|
|
|
std::find_if(TypeTable.cbegin(), TypeTable.cend(), [fcc](const FourCC& test) { return test == fcc; });
|
|
|
|
if (search == TypeTable.cend()) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return ETypeTable::Invalid;
|
2020-03-09 16:17:15 +00:00
|
|
|
}
|
|
|
|
return ETypeTable(std::distance(TypeTable.cbegin(), search));
|
2017-10-26 05:37:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 16:17:15 +00:00
|
|
|
FourCC CFactoryMgr::TypeIdxToFourCC(ETypeTable fcc) { return TypeTable[size_t(fcc)]; }
|
2017-10-26 05:37:46 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|