Skip to content

Commit 1f04830

Browse files
authored
Handle Scripts migration when Assets/Scripts already exists (#737)
1 parent c1682a5 commit 1f04830

1 file changed

Lines changed: 62 additions & 11 deletions

File tree

Sources/OvEditor/src/OvEditor/Core/EditorActions.cpp

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,9 +1207,66 @@ void OvEditor::Core::EditorActions::MigrateScripts()
12071207
}
12081208

12091209
const auto targetPath = m_context.projectAssetsPath / "Scripts";
1210+
std::vector<std::filesystem::path> migratedScriptNames;
1211+
1212+
for (const auto& entry : std::filesystem::recursive_directory_iterator(legacyScriptsPath))
1213+
{
1214+
if (!entry.is_directory())
1215+
{
1216+
if (OvTools::Utils::PathParser::GetFileType(entry.path().string()) == OvTools::Utils::PathParser::EFileType::SCRIPT)
1217+
{
1218+
migratedScriptNames.push_back(entry.path().filename());
1219+
}
1220+
}
1221+
}
12101222

12111223
std::error_code err;
1212-
std::filesystem::rename(legacyScriptsPath, targetPath, err);
1224+
1225+
if (!std::filesystem::exists(targetPath))
1226+
{
1227+
std::filesystem::rename(legacyScriptsPath, targetPath, err);
1228+
}
1229+
else if (std::filesystem::is_directory(targetPath))
1230+
{
1231+
std::filesystem::create_directories(targetPath, err);
1232+
1233+
if (!err)
1234+
{
1235+
for (const auto& entry : std::filesystem::recursive_directory_iterator(legacyScriptsPath))
1236+
{
1237+
const auto destination = targetPath / entry.path().lexically_relative(legacyScriptsPath);
1238+
1239+
if (entry.is_directory())
1240+
{
1241+
std::filesystem::create_directories(destination, err);
1242+
}
1243+
else
1244+
{
1245+
std::filesystem::create_directories(destination.parent_path(), err);
1246+
1247+
if (!err)
1248+
{
1249+
std::filesystem::copy_file(entry.path(), destination, std::filesystem::copy_options::overwrite_existing, err);
1250+
}
1251+
}
1252+
1253+
if (err)
1254+
{
1255+
break;
1256+
}
1257+
}
1258+
}
1259+
1260+
if (!err)
1261+
{
1262+
std::filesystem::remove_all(legacyScriptsPath, err);
1263+
}
1264+
}
1265+
else
1266+
{
1267+
OVLOG_ERROR("Failed to migrate Scripts/ folder: Assets/Scripts exists but is not a directory.");
1268+
return;
1269+
}
12131270

12141271
if (err)
12151272
{
@@ -1220,18 +1277,12 @@ void OvEditor::Core::EditorActions::MigrateScripts()
12201277
OVLOG_INFO("Scripts/ folder migrated to Assets/Scripts/");
12211278

12221279
// Update all scene files: replace old behaviour type (just the stem) with the new relative path
1223-
for (const auto& entry : std::filesystem::recursive_directory_iterator(targetPath))
1280+
for (const auto& scriptName : migratedScriptNames)
12241281
{
1225-
if (!entry.is_directory())
1226-
{
1227-
if (OvTools::Utils::PathParser::GetFileType(entry.path().string()) == OvTools::Utils::PathParser::EFileType::SCRIPT)
1228-
{
1229-
const auto stem = entry.path().stem().string();
1230-
const auto newRelPath = (std::filesystem::path("Scripts") / entry.path().filename()).generic_string();
1282+
const auto stem = scriptName.stem().string();
1283+
const auto newRelPath = (std::filesystem::path("Scripts") / scriptName).generic_string();
12311284

1232-
PropagateFileRenameThroughSavedFilesOfType(stem, newRelPath, OvTools::Utils::PathParser::EFileType::SCENE);
1233-
}
1234-
}
1285+
PropagateFileRenameThroughSavedFilesOfType(stem, newRelPath, OvTools::Utils::PathParser::EFileType::SCENE);
12351286
}
12361287

12371288
OVLOG_INFO("Scene files updated with new script paths");

0 commit comments

Comments
 (0)