mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-21 02:39:17 +00:00
Implemented filesystem handling functions in Common
This commit is contained in:
@@ -12,15 +12,14 @@
|
||||
#include "Core/Resource/Factory/CStringLoader.h"
|
||||
#include "Core/Resource/Factory/CTextureDecoder.h"
|
||||
#include "Core/Resource/Factory/CWorldLoader.h"
|
||||
#include <Common/Log.h>
|
||||
|
||||
#include <Common/TString.h>
|
||||
#include <FileIO/FileIO.h>
|
||||
#include <Common/FileUtil.h>
|
||||
#include <Common/Log.h>
|
||||
#include <Common/TString.h>
|
||||
#include <iostream>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
CResCache::CResCache()
|
||||
: mpPak(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -60,127 +59,33 @@ void CResCache::Clean()
|
||||
void CResCache::SetFolder(TString Path)
|
||||
{
|
||||
Path.EnsureEndsWith("/");
|
||||
mResSource.Path = Path;
|
||||
mResSource.Source = SResSource::eFolder;
|
||||
mResDir = Path;
|
||||
Log::Write("Set resource folder: " + Path);
|
||||
}
|
||||
|
||||
void CResCache::SetPak(const TString& rkPath)
|
||||
{
|
||||
CFileInStream *pPakFile = new CFileInStream(rkPath.ToStdString(), IOUtil::eBigEndian);
|
||||
|
||||
if (!pPakFile->IsValid())
|
||||
{
|
||||
Log::Error("Couldn't load pak file: " + rkPath);
|
||||
delete pPakFile;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mpPak) delete mpPak;
|
||||
mpPak = new CPakFile(pPakFile);
|
||||
mResSource.Path = rkPath;
|
||||
mResSource.Source = SResSource::ePakFile;
|
||||
Log::Write("Loaded pak file: " + rkPath);
|
||||
}
|
||||
|
||||
void CResCache::SetResSource(SResSource& rResSource)
|
||||
{
|
||||
mResSource = rResSource;
|
||||
}
|
||||
|
||||
SResSource CResCache::GetResSource()
|
||||
{
|
||||
return mResSource;
|
||||
}
|
||||
|
||||
TString CResCache::GetSourcePath()
|
||||
{
|
||||
return mResSource.Path;
|
||||
return mResDir;
|
||||
}
|
||||
|
||||
CResource* CResCache::GetResource(CUniqueID ResID, CFourCC Type)
|
||||
{
|
||||
if (!ResID.IsValid()) return nullptr;
|
||||
|
||||
auto got = mResourceCache.find(ResID.ToLongLong());
|
||||
|
||||
if (got != mResourceCache.end())
|
||||
return got->second;
|
||||
|
||||
std::vector<u8> *pBuffer = nullptr;
|
||||
TString Source;
|
||||
|
||||
// Load from pak
|
||||
if (mResSource.Source == SResSource::ePakFile)
|
||||
{
|
||||
pBuffer = mpPak->Resource(ResID.ToLongLong(), Type);
|
||||
Source = ResID.ToString() + "." + Type.ToString();
|
||||
}
|
||||
|
||||
// Load from folder
|
||||
else
|
||||
{
|
||||
Source = mResSource.Path + ResID.ToString() + "." + Type.ToString();
|
||||
CFileInStream File(Source.ToStdString(), IOUtil::eBigEndian);
|
||||
|
||||
if (!File.IsValid())
|
||||
{
|
||||
Log::Error("Couldn't open resource: " + ResID.ToString() + "." + Type.ToString());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pBuffer = new std::vector<u8>;
|
||||
pBuffer->resize(File.Size());
|
||||
File.ReadBytes(pBuffer->data(), pBuffer->size());
|
||||
}
|
||||
if (!pBuffer) return nullptr;
|
||||
|
||||
// Load resource
|
||||
CMemoryInStream Mem(pBuffer->data(), pBuffer->size(), IOUtil::eBigEndian);
|
||||
Mem.SetSourceString(*Source.GetFileName());
|
||||
CResource *pRes = nullptr;
|
||||
bool SupportedFormat = true;
|
||||
|
||||
if (Type == "CMDL") pRes = CModelLoader::LoadCMDL(Mem);
|
||||
else if (Type == "TXTR") pRes = CTextureDecoder::LoadTXTR(Mem);
|
||||
else if (Type == "ANCS") pRes = CAnimSetLoader::LoadANCS(Mem);
|
||||
else if (Type == "CHAR") pRes = CAnimSetLoader::LoadCHAR(Mem);
|
||||
else if (Type == "MREA") pRes = CAreaLoader::LoadMREA(Mem);
|
||||
else if (Type == "MLVL") pRes = CWorldLoader::LoadMLVL(Mem);
|
||||
else if (Type == "STRG") pRes = CStringLoader::LoadSTRG(Mem);
|
||||
else if (Type == "FONT") pRes = CFontLoader::LoadFONT(Mem);
|
||||
else if (Type == "SCAN") pRes = CScanLoader::LoadSCAN(Mem);
|
||||
else if (Type == "DCLN") pRes = CCollisionLoader::LoadDCLN(Mem);
|
||||
else if (Type == "EGMC") pRes = CPoiToWorldLoader::LoadEGMC(Mem);
|
||||
else if (Type == "CINF") pRes = CSkeletonLoader::LoadCINF(Mem);
|
||||
else if (Type == "ANIM") pRes = CAnimationLoader::LoadANIM(Mem);
|
||||
else if (Type == "CSKR") pRes = CSkinLoader::LoadCSKR(Mem);
|
||||
else SupportedFormat = false;
|
||||
|
||||
// Log errors
|
||||
if (!SupportedFormat)
|
||||
Log::Write("Unsupported format; unable to load " + Type.ToString() + " " + ResID.ToString());
|
||||
|
||||
if (!pRes) pRes = new CResource(); // Default for invalid resource or unsupported format
|
||||
|
||||
// Add to cache and cleanup
|
||||
pRes->mID = ResID;
|
||||
pRes->mResSource = Source;
|
||||
mResourceCache[ResID.ToLongLong()] = pRes;
|
||||
delete pBuffer;
|
||||
return pRes;
|
||||
TString Source = mResDir + ResID.ToString() + "." + Type.ToString();
|
||||
return GetResource(Source);
|
||||
}
|
||||
|
||||
CResource* CResCache::GetResource(const TString& rkResPath)
|
||||
{
|
||||
// Since this function takes a string argument it always loads directly from a file - no pak
|
||||
CUniqueID ResID = rkResPath.Hash64();
|
||||
|
||||
// Check if resource already exists
|
||||
auto Got = mResourceCache.find(ResID.ToLongLong());
|
||||
|
||||
if (Got != mResourceCache.end())
|
||||
return Got->second;
|
||||
|
||||
// Open file
|
||||
CFileInStream File(rkResPath.ToStdString(), IOUtil::eBigEndian);
|
||||
if (!File.IsValid())
|
||||
{
|
||||
@@ -188,10 +93,9 @@ CResource* CResCache::GetResource(const TString& rkResPath)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Save old ResSource to restore later
|
||||
const SResSource OldSource = mResSource;
|
||||
mResSource.Source = SResSource::eFolder;
|
||||
mResSource.Path = rkResPath.GetFileDirectory();
|
||||
// Save old ResDir to restore later
|
||||
TString OldResDir = mResDir;
|
||||
mResDir = rkResPath.GetFileDirectory();
|
||||
|
||||
// Load resource
|
||||
CResource *pRes = nullptr;
|
||||
@@ -220,7 +124,7 @@ CResource* CResCache::GetResource(const TString& rkResPath)
|
||||
pRes->mID = *rkResPath;
|
||||
pRes->mResSource = rkResPath;
|
||||
mResourceCache[ResID.ToLongLong()] = pRes;
|
||||
mResSource = OldSource;
|
||||
mResDir = OldResDir;
|
||||
return pRes;
|
||||
}
|
||||
|
||||
@@ -230,30 +134,15 @@ CFourCC CResCache::FindResourceType(CUniqueID ResID, const TStringList& rkPossib
|
||||
if (rkPossibleTypes.size() == 1)
|
||||
return CFourCC(rkPossibleTypes.front());
|
||||
|
||||
// Determine extension from pak
|
||||
if (mResSource.Source == SResSource::ePakFile)
|
||||
{
|
||||
for (auto it = rkPossibleTypes.begin(); it != rkPossibleTypes.end(); it++)
|
||||
{
|
||||
SResInfo ResInfo = mpPak->ResourceInfo(ResID.ToLongLong(), CFourCC(*it));
|
||||
|
||||
if (ResInfo.Type != "NULL")
|
||||
return CFourCC(*it);
|
||||
}
|
||||
}
|
||||
|
||||
// Determine extension from filesystem - try every extension until we find one that works
|
||||
else
|
||||
TString PathBase = mResDir + ResID.ToString() + ".";
|
||||
|
||||
for (auto it = rkPossibleTypes.begin(); it != rkPossibleTypes.end(); it++)
|
||||
{
|
||||
TString PathBase = mResSource.Path + ResID.ToString() + ".";
|
||||
TString NewPath = PathBase + *it;
|
||||
|
||||
for (auto it = rkPossibleTypes.begin(); it != rkPossibleTypes.end(); it++)
|
||||
{
|
||||
TString NewPath = PathBase + *it;
|
||||
|
||||
if (boost::filesystem::exists(NewPath.ToStdString()))
|
||||
return CFourCC(*it);
|
||||
}
|
||||
if (FileUtil::Exists(NewPath))
|
||||
return CFourCC(*it);
|
||||
}
|
||||
|
||||
return "UNKN";
|
||||
|
||||
@@ -1,34 +1,21 @@
|
||||
#ifndef CRESCACHE_H
|
||||
#define CRESCACHE_H
|
||||
|
||||
#include "CPakFile.h"
|
||||
#include "CResource.h"
|
||||
#include <Common/types.h>
|
||||
#include <Common/TString.h>
|
||||
#include <unordered_map>
|
||||
|
||||
struct SResSource
|
||||
{
|
||||
TString Path;
|
||||
enum {
|
||||
eFolder, ePakFile
|
||||
} Source;
|
||||
};
|
||||
|
||||
class CResCache
|
||||
{
|
||||
std::unordered_map<u64, CResource*> mResourceCache;
|
||||
CPakFile *mpPak;
|
||||
SResSource mResSource;
|
||||
TString mResDir;
|
||||
|
||||
public:
|
||||
CResCache();
|
||||
~CResCache();
|
||||
void Clean();
|
||||
void SetFolder(TString Path);
|
||||
void SetPak(const TString& rkPath);
|
||||
void SetResSource(SResSource& rResSource);
|
||||
SResSource GetResSource();
|
||||
TString GetSourcePath();
|
||||
CResource* GetResource(CUniqueID ResID, CFourCC Type);
|
||||
CResource* GetResource(const TString& rkResPath);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include "CResCache.h"
|
||||
#include <Common/CUniqueID.h>
|
||||
#include <Common/CFourCC.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <Common/FileUtil.h>
|
||||
|
||||
class CResourceInfo
|
||||
{
|
||||
@@ -20,7 +20,7 @@ public:
|
||||
CResourceInfo(const TString& rkPath)
|
||||
: mPath(rkPath), mIsPath(true)
|
||||
{
|
||||
mIsValidPath = boost::filesystem::exists(rkPath.ToStdString());
|
||||
mIsValidPath = FileUtil::Exists(rkPath);
|
||||
}
|
||||
|
||||
CResourceInfo(const CUniqueID& rkID, CFourCC Type)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Core/Render/CBoneTransformData.h"
|
||||
#include "Core/Render/CDrawUtil.h"
|
||||
#include "Core/Render/CGraphics.h"
|
||||
#include <Common/Assert.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
#include <Math/MathUtil.h>
|
||||
|
||||
// ************ CBone ************
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "CTemplateWriter.h"
|
||||
#include "CAreaCooker.h"
|
||||
#include <Common/FileUtil.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <tinyxml2.h>
|
||||
|
||||
using namespace tinyxml2;
|
||||
@@ -42,7 +42,7 @@ void CTemplateWriter::SaveAllTemplates()
|
||||
{
|
||||
// Create directory
|
||||
std::list<CMasterTemplate*> MasterList = CMasterTemplate::MasterList();
|
||||
boost::filesystem::create_directory(smTemplatesDir.ToStdString());
|
||||
FileUtil::CreateDirectory(smTemplatesDir);
|
||||
|
||||
// Resave property list
|
||||
SavePropertyList();
|
||||
@@ -95,7 +95,7 @@ void CTemplateWriter::SaveGameTemplates(CMasterTemplate *pMaster)
|
||||
// Create directory
|
||||
TString OutFile = smTemplatesDir + pMaster->mSourceFile;
|
||||
TString OutDir = OutFile.GetFileDirectory();
|
||||
boost::filesystem::create_directory(OutDir.ToStdString());
|
||||
FileUtil::CreateDirectory(OutDir);
|
||||
|
||||
// Resave script templates
|
||||
for (auto it = pMaster->mTemplates.begin(); it != pMaster->mTemplates.end(); it++)
|
||||
@@ -226,7 +226,7 @@ void CTemplateWriter::SaveScriptTemplate(CScriptTemplate *pTemp)
|
||||
// Create directory
|
||||
TString OutFile = smTemplatesDir + pMaster->GetDirectory() + pTemp->mSourceFile;
|
||||
TString OutDir = OutFile.GetFileDirectory();
|
||||
boost::filesystem::create_directory(*OutDir);
|
||||
FileUtil::CreateDirectory(*OutDir);
|
||||
|
||||
// Create new document
|
||||
XMLDocument ScriptXML;
|
||||
@@ -418,7 +418,7 @@ void CTemplateWriter::SaveStructTemplate(CStructTemplate *pTemp)
|
||||
TString OutFile = smTemplatesDir + pMaster->GetDirectory() + pTemp->mSourceFile;
|
||||
TString OutDir = OutFile.GetFileDirectory();
|
||||
TString Name = OutFile.GetFileName(false);
|
||||
boost::filesystem::create_directory(OutDir.ToStdString());
|
||||
FileUtil::CreateDirectory(OutDir);
|
||||
|
||||
// Create new document and write struct properties to it
|
||||
XMLDocument StructXML;
|
||||
@@ -442,7 +442,7 @@ void CTemplateWriter::SaveEnumTemplate(CEnumTemplate *pTemp)
|
||||
TString OutFile = smTemplatesDir + pMaster->GetDirectory() + pTemp->mSourceFile;
|
||||
TString OutDir = OutFile.GetFileDirectory();
|
||||
TString Name = OutFile.GetFileName(false);
|
||||
boost::filesystem::create_directory(*OutDir);
|
||||
FileUtil::CreateDirectory(OutDir);
|
||||
|
||||
// Create new document and write enumerators to it
|
||||
XMLDocument EnumXML;
|
||||
@@ -465,7 +465,7 @@ void CTemplateWriter::SaveBitfieldTemplate(CBitfieldTemplate *pTemp)
|
||||
TString OutFile = smTemplatesDir + pMaster->GetDirectory() + pTemp->mSourceFile;
|
||||
TString OutDir = OutFile.GetFileDirectory();
|
||||
TString Name = pTemp->mSourceFile.GetFileName(false);
|
||||
boost::filesystem::create_directory(*OutDir);
|
||||
FileUtil::CreateDirectory(OutDir);
|
||||
|
||||
// Create new document and write enumerators to it
|
||||
XMLDocument BitfieldXML;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "CAnimationLoader.h"
|
||||
#include <Common/Assert.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
#include <Common/Log.h>
|
||||
#include <Math/MathUtil.h>
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "CSkeletonLoader.h"
|
||||
#include <Common/Assert.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
#include <Common/Log.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "CSkinLoader.h"
|
||||
#include <Common/Assert.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
|
||||
// ************ STATIC ************
|
||||
CSkin* CSkinLoader::LoadCSKR(IInputStream& rCSKR)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "CTemplateLoader.h"
|
||||
#include "CAreaLoader.h"
|
||||
#include "Core/Resource/Script/IPropertyTemplate.h"
|
||||
#include <Common/FileUtil.h>
|
||||
#include <Common/Log.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
const TString CTemplateLoader::mskTemplatesDir = "../templates/";
|
||||
const TString CTemplateLoader::mskGameListPath = CTemplateLoader::mskTemplatesDir + "GameList.xml";
|
||||
@@ -533,7 +533,7 @@ CScriptTemplate* CTemplateLoader::LoadScriptTemplate(XMLDocument *pDoc, const TS
|
||||
else
|
||||
{
|
||||
TString Path = "../resources/" + ID;
|
||||
if (!boost::filesystem::exists(*Path))
|
||||
if (!FileUtil::Exists(Path))
|
||||
{
|
||||
Log::Error(rkTemplateName + ": Invalid file for " + Type + " asset: " + ID);
|
||||
pAsset = pAsset->NextSiblingElement();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "Core/Render/CDrawUtil.h"
|
||||
#include "Core/Render/CRenderer.h"
|
||||
#include "Core/OpenGL/GLCommon.h"
|
||||
#include <Common/Assert.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
|
||||
CModel::CModel()
|
||||
: CBasicModel()
|
||||
|
||||
Reference in New Issue
Block a user