CResourceFactory: Make use of unique_ptr

Makes the API more memory safe
This commit is contained in:
Lioncash
2020-06-11 18:57:09 -04:00
parent ce315280c3
commit 907f1270bd
44 changed files with 314 additions and 276 deletions

View File

@@ -2,7 +2,7 @@
#include "Core/Resource/Factory/CScriptLoader.h"
#include "Core/Resource/Script/NGameList.h"
CTweakData* CTweakLoader::LoadCTWK(IInputStream& CTWK, CResourceEntry* pEntry)
std::unique_ptr<CTweakData> CTweakLoader::LoadCTWK(IInputStream& CTWK, CResourceEntry* pEntry)
{
// Find the correct template based on the asset ID.
static const std::unordered_map<uint, const char*> skIdToTemplateName =
@@ -24,26 +24,25 @@ CTweakData* CTweakLoader::LoadCTWK(IInputStream& CTWK, CResourceEntry* pEntry)
{ 0xF1ED8FD7, "TweakPlayerControls", }
};
auto Find = skIdToTemplateName.find( pEntry->ID().ToLong() );
ASSERT( Find != skIdToTemplateName.end() );
auto Find = skIdToTemplateName.find(pEntry->ID().ToLong());
ASSERT(Find != skIdToTemplateName.end());
const char* pkTemplateName = Find->second;
// Fetch template
CGameTemplate* pGameTemplate = NGameList::GetGameTemplate( pEntry->Game() );
ASSERT( pGameTemplate != nullptr );
CGameTemplate* pGameTemplate = NGameList::GetGameTemplate(pEntry->Game());
ASSERT(pGameTemplate != nullptr);
CScriptTemplate* pTweakTemplate = pGameTemplate->FindMiscTemplate(pkTemplateName);
ASSERT( pTweakTemplate != nullptr );
ASSERT(pTweakTemplate != nullptr);
// Load tweak data
CTweakData* pTweakData = new CTweakData(pTweakTemplate, pEntry->ID().ToLong(), pEntry);
CScriptLoader::LoadStructData( CTWK, pTweakData->TweakData() );
auto pTweakData = std::make_unique<CTweakData>(pTweakTemplate, pEntry->ID().ToLong(), pEntry);
CScriptLoader::LoadStructData(CTWK, pTweakData->TweakData());
// Verify
if (!CTWK.EoF() && CTWK.PeekShort() != -1)
{
errorf("%s: unread property data, tweak template may be malformed (%d bytes left)", *CTWK.GetSourceString(), CTWK.Size() - CTWK.Tell());
delete pTweakData;
return nullptr;
}

View File

@@ -2,6 +2,7 @@
#define CTWEAKLOADER_H
#include "CTweakData.h"
#include <memory>
/** Class responsible for loading tweak data */
class CTweakLoader
@@ -11,7 +12,7 @@ class CTweakLoader
public:
/** Loader entry point */
static CTweakData* LoadCTWK(IInputStream& CTWK, CResourceEntry* pEntry);
static std::unique_ptr<CTweakData> LoadCTWK(IInputStream& CTWK, CResourceEntry* pEntry);
static void LoadNTWK(IInputStream& NTWK, EGame Game, std::vector<CTweakData*>& OutTweaks);
};