Update Prefab.cpp
This commit is contained in:
parent
ea372550fe
commit
ffeb2e5e02
@ -12,24 +12,21 @@
|
|||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
std::string Prefab::CreatePrefab(const std::shared_ptr<Object>& obj)
|
std::string Prefab::CreatePrefab(const std::shared_ptr<Object> &obj)
|
||||||
{
|
{
|
||||||
if (!obj)
|
if (!obj)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
// sanitize the prefab filename
|
// sanitize the prefab filename
|
||||||
std::string filename = obj->GetName().empty() ? "Unnamed" : obj->GetName();
|
std::string filename = obj->GetName().empty() ? "Unnamed" : obj->GetName();
|
||||||
for (auto& ch : filename)
|
for (auto &ch : filename)
|
||||||
if (ch == '/' || ch == '\\' || ch == ':' || ch == '*' || ch == '?' ||
|
if (ch == '/' || ch == '\\' || ch == ':' || ch == '*' || ch == '?' ||
|
||||||
ch == '\"' || ch == '<' || ch == '>' || ch == '|')
|
ch == '\"' || ch == '<' || ch == '>' || ch == '|')
|
||||||
ch = '_';
|
ch = '_';
|
||||||
|
|
||||||
// build path: <projectRoot>/<projectName>/asset/Prefabs
|
fs::path prefabDir = fs::path(ProjectManager::GetCurrentAssetsPath()) / "Prefabs";
|
||||||
fs::path prefabDir =
|
|
||||||
fs::path(ProjectManager::GetCurrentAssetsPath()) / "Prefabs";
|
|
||||||
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
fs::create_directories(prefabDir, ec);
|
fs::create_directories(prefabDir, ec);
|
||||||
@ -44,9 +41,12 @@ std::string Prefab::CreatePrefab(const std::shared_ptr<Object>& obj)
|
|||||||
obj->Save(objectOut);
|
obj->Save(objectOut);
|
||||||
|
|
||||||
YAML::Node objectNode;
|
YAML::Node objectNode;
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
objectNode = YAML::Load(objectOut.c_str());
|
objectNode = YAML::Load(objectOut.c_str());
|
||||||
} catch (const YAML::Exception& e) {
|
}
|
||||||
|
catch (const YAML::Exception &e)
|
||||||
|
{
|
||||||
Logger::LogError("[Prefab] Failed to parse object YAML: %s", e.what());
|
Logger::LogError("[Prefab] Failed to parse object YAML: %s", e.what());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -66,7 +66,8 @@ std::string Prefab::CreatePrefab(const std::shared_ptr<Object>& obj)
|
|||||||
|
|
||||||
// write out the file
|
// write out the file
|
||||||
std::ofstream file(fullPath);
|
std::ofstream file(fullPath);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open())
|
||||||
|
{
|
||||||
Logger::LogError("[Prefab] Failed to write prefab file: %s",
|
Logger::LogError("[Prefab] Failed to write prefab file: %s",
|
||||||
fullPath.string().c_str());
|
fullPath.string().c_str());
|
||||||
return "";
|
return "";
|
||||||
|
Loading…
Reference in New Issue
Block a user