feat: expand editor scripting asset and viewport flow
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
#include "ProjectManager.h"
|
||||
#include <XCEngine/Core/Asset/ResourceManager.h>
|
||||
#include <XCEngine/Debug/Logger.h>
|
||||
#include <XCEngine/Scene/Scene.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cwctype>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <initializer_list>
|
||||
#include <string_view>
|
||||
#include <windows.h>
|
||||
@@ -212,6 +217,27 @@ bool CanPreviewImageAssetExtension(std::wstring_view extension) {
|
||||
});
|
||||
}
|
||||
|
||||
bool IsSceneAssetFile(const fs::path& path) {
|
||||
if (!fs::is_regular_file(path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::wstring extension = path.extension().wstring();
|
||||
std::transform(extension.begin(), extension.end(), extension.begin(), ::towlower);
|
||||
return extension == L".xc";
|
||||
}
|
||||
|
||||
std::string ReadFileText(const fs::path& path) {
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::ostringstream stream;
|
||||
stream << input.rdbuf();
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const std::vector<AssetItemPtr>& ProjectManager::GetCurrentItems() const {
|
||||
@@ -496,6 +522,89 @@ bool ProjectManager::RenameItem(const std::string& sourceFullPath, const std::st
|
||||
}
|
||||
}
|
||||
|
||||
IProjectManager::SceneAssetReferenceMigrationReport ProjectManager::MigrateSceneAssetReferences() {
|
||||
IProjectManager::SceneAssetReferenceMigrationReport report;
|
||||
if (m_projectPath.empty()) {
|
||||
return report;
|
||||
}
|
||||
|
||||
const fs::path assetsPath = fs::path(Utf8PathToWstring(m_projectPath)) / L"Assets";
|
||||
if (!fs::exists(assetsPath) || !fs::is_directory(assetsPath)) {
|
||||
return report;
|
||||
}
|
||||
|
||||
auto& logger = ::XCEngine::Debug::Logger::Get();
|
||||
::XCEngine::Resources::ResourceManager& resourceManager = ::XCEngine::Resources::ResourceManager::Get();
|
||||
resourceManager.Initialize();
|
||||
|
||||
const std::string previousRoot = resourceManager.GetResourceRoot().CStr();
|
||||
const bool restoreResourceRoot = previousRoot != m_projectPath;
|
||||
if (restoreResourceRoot) {
|
||||
resourceManager.SetResourceRoot(m_projectPath.c_str());
|
||||
}
|
||||
|
||||
try {
|
||||
for (const fs::directory_entry& entry : fs::recursive_directory_iterator(assetsPath)) {
|
||||
if (!IsSceneAssetFile(entry.path())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++report.scannedSceneCount;
|
||||
const fs::path scenePath = entry.path();
|
||||
const std::string scenePathUtf8 = WstringPathToUtf8(scenePath.wstring());
|
||||
|
||||
try {
|
||||
const std::string before = ReadFileText(scenePath);
|
||||
::XCEngine::Components::Scene scene;
|
||||
{
|
||||
::XCEngine::Resources::ResourceManager::ScopedDeferredSceneLoad deferredLoadScope(resourceManager);
|
||||
scene.Load(scenePathUtf8);
|
||||
}
|
||||
|
||||
scene.Save(scenePathUtf8);
|
||||
const std::string after = ReadFileText(scenePath);
|
||||
if (after != before) {
|
||||
++report.migratedSceneCount;
|
||||
} else {
|
||||
++report.unchangedSceneCount;
|
||||
}
|
||||
} catch (const std::exception& exception) {
|
||||
++report.failedSceneCount;
|
||||
logger.Error(
|
||||
::XCEngine::Debug::LogCategory::FileSystem,
|
||||
("Failed to migrate scene asset references: " + scenePathUtf8 + " - " + exception.what()).c_str());
|
||||
} catch (...) {
|
||||
++report.failedSceneCount;
|
||||
logger.Error(
|
||||
::XCEngine::Debug::LogCategory::FileSystem,
|
||||
("Failed to migrate scene asset references: " + scenePathUtf8 + " - unknown error").c_str());
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& exception) {
|
||||
logger.Error(
|
||||
::XCEngine::Debug::LogCategory::FileSystem,
|
||||
("Scene asset reference migration aborted: " + std::string(exception.what())).c_str());
|
||||
} catch (...) {
|
||||
logger.Error(
|
||||
::XCEngine::Debug::LogCategory::FileSystem,
|
||||
"Scene asset reference migration aborted: unknown error");
|
||||
}
|
||||
|
||||
if (restoreResourceRoot) {
|
||||
resourceManager.SetResourceRoot(previousRoot.c_str());
|
||||
}
|
||||
|
||||
logger.Info(
|
||||
::XCEngine::Debug::LogCategory::FileSystem,
|
||||
("Scene asset reference migration finished. scanned=" + std::to_string(report.scannedSceneCount) +
|
||||
" migrated=" + std::to_string(report.migratedSceneCount) +
|
||||
" unchanged=" + std::to_string(report.unchangedSceneCount) +
|
||||
" failed=" + std::to_string(report.failedSceneCount)).c_str());
|
||||
|
||||
RefreshCurrentFolder();
|
||||
return report;
|
||||
}
|
||||
|
||||
AssetItemPtr ProjectManager::FindCurrentItemByPath(const std::string& fullPath) const {
|
||||
const int index = FindCurrentItemIndex(fullPath);
|
||||
if (index < 0) {
|
||||
|
||||
@@ -38,6 +38,7 @@ public:
|
||||
bool DeleteItem(const std::string& fullPath) override;
|
||||
bool MoveItem(const std::string& sourceFullPath, const std::string& destFolderFullPath) override;
|
||||
bool RenameItem(const std::string& sourceFullPath, const std::string& newName) override;
|
||||
SceneAssetReferenceMigrationReport MigrateSceneAssetReferences() override;
|
||||
|
||||
const std::string& GetProjectPath() const override { return m_projectPath; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user