Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5576: Create a separate AseModelLoader to be able to refactor the AS…
…E importer code.
- Loading branch information
1 parent
f2996fc
commit 8b669ca
Showing
10 changed files
with
208 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "AseModelLoader.h" | ||
|
||
#include "lib/picomodel.h" | ||
#include "os/path.h" | ||
#include "string/case_conv.h" | ||
#include "StaticModel.h" | ||
|
||
extern "C" | ||
{ | ||
extern const picoModule_t picoModuleASE; | ||
} | ||
|
||
namespace model | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
size_t picoInputStreamReam(void* inputStream, unsigned char* buffer, size_t length) | ||
{ | ||
return reinterpret_cast<InputStream*>(inputStream)->read(buffer, length); | ||
} | ||
|
||
} | ||
|
||
AseModelLoader::AseModelLoader() : | ||
ModelImporterBase("ASE") | ||
{} | ||
|
||
IModelPtr AseModelLoader::loadModelFromPath(const std::string& path) | ||
{ | ||
// Open an ArchiveFile to load | ||
auto file = path_is_absolute(path.c_str()) ? | ||
GlobalFileSystem().openFileInAbsolutePath(path) : | ||
GlobalFileSystem().openFile(path); | ||
|
||
if (!file) | ||
{ | ||
rError() << "Failed to load model " << path << std::endl; | ||
return IModelPtr(); | ||
} | ||
|
||
auto* model = PicoModuleLoadModelStream( | ||
&picoModuleASE, | ||
&file->getInputStream(), | ||
picoInputStreamReam, | ||
file->size(), | ||
0 | ||
); | ||
|
||
// greebo: Check if the model load was successful | ||
if (!model || model->numSurfaces == 0) | ||
{ | ||
// Model is either NULL or has no surfaces, this must've failed | ||
return IModelPtr(); | ||
} | ||
|
||
auto modelObj = std::make_shared<StaticModel>(model, string::to_lower_copy(getExtension())); | ||
|
||
// Set the filename | ||
modelObj->setFilename(os::getFilename(file->getName())); | ||
modelObj->setModelPath(path); | ||
|
||
PicoFreeModel(model); | ||
|
||
return modelObj; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include "ModelImporterBase.h" | ||
|
||
namespace model | ||
{ | ||
|
||
class AseModelLoader : | ||
public ModelImporterBase | ||
{ | ||
public: | ||
AseModelLoader(); | ||
|
||
// Load the given model from the path, VFS or absolute | ||
IModelPtr loadModelFromPath(const std::string& name) override; | ||
}; | ||
|
||
} // namespace model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "ModelImporterBase.h" | ||
|
||
#include "ifilesystem.h" | ||
#include "itextstream.h" | ||
#include "imodelcache.h" | ||
#include "string/case_conv.h" | ||
#include "os/path.h" | ||
#include "StaticModelNode.h" | ||
#include "StaticModel.h" | ||
|
||
namespace model | ||
{ | ||
|
||
namespace | ||
{ | ||
// name may be absolute or relative | ||
inline std::string rootPath(const std::string& name) | ||
{ | ||
return GlobalFileSystem().findRoot( | ||
path_is_absolute(name.c_str()) ? name : GlobalFileSystem().findFile(name) | ||
); | ||
} | ||
} | ||
|
||
ModelImporterBase::ModelImporterBase(const std::string& extension) : | ||
_extension(string::to_upper_copy(extension)) | ||
{ | ||
|
||
} | ||
|
||
const std::string& ModelImporterBase::getExtension() const | ||
{ | ||
return _extension; | ||
} | ||
|
||
scene::INodePtr ModelImporterBase::loadModel(const std::string& modelName) | ||
{ | ||
// Initialise the paths, this is all needed for realisation | ||
std::string path = rootPath(modelName); | ||
std::string name = os::getRelativePath(modelName, path); | ||
|
||
// greebo: Path is empty for models in PK4 files, don't check this | ||
|
||
// Try to load the model from the given VFS path | ||
IModelPtr model = GlobalModelCache().getModel(name); | ||
|
||
if (!model) | ||
{ | ||
rError() << "ModelImporterBase: Could not load model << " << modelName << std::endl; | ||
return scene::INodePtr(); | ||
} | ||
|
||
// The cached model should be a StaticModel, otherwise we're in the wrong movie | ||
auto picoModel = std::dynamic_pointer_cast<StaticModel>(model); | ||
|
||
if (picoModel) | ||
{ | ||
// Load was successful, construct a modelnode using this resource | ||
return std::make_shared<StaticModelNode>(picoModel); | ||
} | ||
else | ||
{ | ||
rError() << "ModelImporterBase: Cached model is not a PicoModel?" << std::endl; | ||
} | ||
|
||
return scene::INodePtr(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "imodel.h" | ||
|
||
namespace model | ||
{ | ||
|
||
class ModelImporterBase : | ||
public IModelImporter | ||
{ | ||
private: | ||
// Supported file extension in UPPERCASE (ASE, LWO, whatever) | ||
std::string _extension; | ||
|
||
public: | ||
ModelImporterBase(const std::string& extension); | ||
|
||
const std::string& getExtension() const override; | ||
|
||
// Returns a new ModelNode for the given model name | ||
scene::INodePtr loadModel(const std::string& modelName) override; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,23 @@ | ||
#pragma once | ||
|
||
#include "imodel.h" | ||
#include "ModelImporterBase.h" | ||
|
||
typedef struct picoModule_s picoModule_t; | ||
|
||
namespace model | ||
{ | ||
|
||
class PicoModelLoader : | ||
public IModelImporter | ||
public ModelImporterBase | ||
{ | ||
private: | ||
const picoModule_t* _module; | ||
|
||
// Supported file extension in UPPERCASE (ASE, LWO, whatever) | ||
std::string _extension; | ||
|
||
public: | ||
PicoModelLoader(const picoModule_t* module, const std::string& extension); | ||
|
||
const std::string& getExtension() const override; | ||
|
||
// Returns a new ModelNode for the given model name | ||
scene::INodePtr loadModel(const std::string& modelName) override; | ||
|
||
// Load the given model from the path, VFS or absolute | ||
IModelPtr loadModelFromPath(const std::string& name) override; | ||
}; | ||
typedef std::shared_ptr<PicoModelLoader> PicoModelLoaderPtr; | ||
|
||
} // namespace model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters