feat(editor): support opening scene files via command line arguments

This commit is contained in:
ayuan9957 2026-05-21 16:20:59 +08:00
parent 5f0690580b
commit d195294b17
3 changed files with 31 additions and 5 deletions

View File

@ -148,7 +148,7 @@ int main(int argc, char* argv[]) {
MetaCore::MetaCoreEditorApp editorApp;
MetaCoreWriteCrashLogRaw("metacore.app: construct app done");
MetaCoreWriteCrashLogRaw("metacore.app: initialize begin");
if (!editorApp.Initialize()) {
if (!editorApp.Initialize(argc, argv)) {
MetaCoreWriteCrashLogRaw("metacore.app: initialize failed");
std::cerr << "MetaCoreEditorApp initialize failed\n";
return 1;

View File

@ -1,4 +1,4 @@
#include "MetaCoreEditor/MetaCoreEditorApp.h"
#include "MetaCoreEditor/MetaCoreEditorApp.h"
#include "MetaCoreEditor/MetaCoreBuiltinModules.h"
#include "MetaCoreEditor/MetaCoreEditorServices.h"
@ -511,7 +511,7 @@ MetaCoreEditorApp::~MetaCoreEditorApp() {
Shutdown();
}
bool MetaCoreEditorApp::Initialize() {
bool MetaCoreEditorApp::Initialize(int argc, char* argv[]) {
MetaCoreTraceStartup("metacore.app: initialize begin");
if (Initialized_) {
MetaCoreTraceStartup("metacore.app: already initialized");
@ -621,7 +621,33 @@ bool MetaCoreEditorApp::Initialize() {
bool loadedStartupScene = false;
if (const auto scenePersistenceService = ModuleRegistry_.ResolveService<MetaCoreIScenePersistenceService>();
scenePersistenceService != nullptr) {
loadedStartupScene = scenePersistenceService->LoadStartupScene(*EditorContext_);
// 尝试从命令行参数直接加载场景(支持双击打开关联的场景文件)
if (argc > 1 && argv[1] != nullptr) {
const std::filesystem::path inputPath(argv[1]);
const auto assetDatabaseService = ModuleRegistry_.ResolveService<MetaCoreIAssetDatabaseService>();
if (assetDatabaseService != nullptr && assetDatabaseService->HasProject() && std::filesystem::is_regular_file(inputPath)) {
const std::filesystem::path projectRoot = assetDatabaseService->GetProjectDescriptor().RootPath;
// 计算相对路径
std::filesystem::path relativePath = std::filesystem::relative(inputPath, projectRoot);
// 检查相对路径是否合法,不应该以 ".." 开头,也不应该是绝对路径
std::string relStr = relativePath.generic_string();
if (!relStr.empty() && relStr.find("..") == std::string::npos && !relativePath.is_absolute()) {
if (scenePersistenceService->LoadScene(*EditorContext_, relativePath)) {
loadedStartupScene = true;
EditorContext_->AddConsoleMessage(
MetaCoreLogLevel::Info,
"Project",
"通过命令行参数成功打开场景: " + relStr
);
}
}
}
}
// 如果没有通过命令行打开成功,则加载默认启动场景
if (!loadedStartupScene) {
loadedStartupScene = scenePersistenceService->LoadStartupScene(*EditorContext_);
}
if (!loadedStartupScene) {
if (const auto assetDatabaseService = ModuleRegistry_.ResolveService<MetaCoreIAssetDatabaseService>();

View File

@ -22,7 +22,7 @@ public:
MetaCoreEditorApp() = default;
~MetaCoreEditorApp();
bool Initialize();
bool Initialize(int argc = 0, char* argv[] = nullptr);
int Run();
void Shutdown();