Skip to content

Commit 012c349

Browse files
authored
Fixed file rename caused crash in protected folder (#392)
* Fixed file rename caused crash in protected folder * Updated implementation to be faster and use no temp file
1 parent 32dd2aa commit 012c349

1 file changed

Lines changed: 70 additions & 20 deletions

File tree

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

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -944,34 +944,84 @@ void OvEditor::Core::EditorActions::PropagateFileRename(std::string p_previousNa
944944
EDITOR_PANEL(Panels::MaterialEditor, "Material Editor").Refresh();
945945
}
946946

947-
void OvEditor::Core::EditorActions::PropagateFileRenameThroughSavedFilesOfType(const std::string& p_previousName, const std::string& p_newName, OvTools::Utils::PathParser::EFileType p_fileType)
947+
uint64_t ReplaceStringInFile(const std::filesystem::path& p_filePath,
948+
const std::string_view p_searchStr,
949+
const std::string_view p_replaceStr
950+
)
948951
{
949-
for (auto& entry : std::filesystem::recursive_directory_iterator(m_context.projectAssetsPath))
952+
uint64_t occurences = 0;
953+
954+
if (!std::filesystem::exists(p_filePath))
955+
{
956+
throw std::runtime_error("File does not exist: " + p_filePath.string());
957+
}
958+
959+
std::string content;
960+
961+
if (auto inFile = std::ifstream{ p_filePath, std::ios::in })
962+
{
963+
std::stringstream buffer;
964+
buffer << inFile.rdbuf();
965+
content = buffer.str();
966+
}
967+
else
950968
{
951-
if (OvTools::Utils::PathParser::GetFileType(entry.path().string()) == p_fileType)
969+
throw std::runtime_error("Cannot open file for reading: " + p_filePath.string());
970+
}
971+
972+
size_t pos = 0;
973+
974+
while ((pos = content.find(p_searchStr, pos)) != std::string::npos)
975+
{
976+
content.replace(pos, p_searchStr.length(), p_replaceStr);
977+
pos += p_replaceStr.length();
978+
++occurences;
979+
}
980+
981+
if (occurences > 0)
982+
{
983+
if (auto outFile = std::ofstream{ p_filePath, std::ios::out | std::ios::trunc })
952984
{
953-
using namespace std;
985+
outFile << content;
986+
}
987+
else
988+
{
989+
throw std::runtime_error("Cannot open file for writing: " + p_filePath.string());
990+
}
991+
}
992+
993+
return occurences;
994+
}
995+
996+
void OvEditor::Core::EditorActions::PropagateFileRenameThroughSavedFilesOfType(
997+
const std::string& p_previousName,
998+
const std::string& p_newName,
999+
OvTools::Utils::PathParser::EFileType p_fileType
1000+
)
1001+
{
1002+
const auto replaceFrom = std::string{ ">" + p_previousName + "<" };
1003+
const auto replaceTo = std::string{ ">" + p_newName + "<" };
9541004

1005+
for (const auto& entry : std::filesystem::recursive_directory_iterator(m_context.projectAssetsPath))
1006+
{
1007+
const std::filesystem::path entryPath = entry.path();
1008+
1009+
if (OvTools::Utils::PathParser::GetFileType(entryPath.string()) == p_fileType)
1010+
{
1011+
try
9551012
{
956-
ifstream in(entry.path().string().c_str());
957-
ofstream out("TEMP");
958-
string wordToReplace(">" + p_previousName + "<");
959-
string wordToReplaceWith(">" + p_newName + "<");
960-
961-
string line;
962-
size_t len = wordToReplace.length();
963-
while (getline(in, line))
1013+
const uint64_t occurences = ReplaceStringInFile(entryPath, replaceFrom, replaceTo);
1014+
1015+
if (occurences > 0)
9641016
{
965-
if (OvTools::Utils::String::Replace(line, wordToReplace, wordToReplaceWith))
966-
OVLOG_INFO("Asset retargeting: \"" + p_previousName + "\" to \"" + p_newName + "\" in \"" + entry.path().string() + "\"");
967-
out << line << '\n';
1017+
OVLOG_INFO("Asset retargeting: \"" + p_previousName + "\" to \"" + p_newName + "\" in \"" + entryPath.string() + "\"");
9681018
}
969-
970-
out.close(); in.close();
9711019
}
972-
973-
std::filesystem::copy_file("TEMP", entry.path(), std::filesystem::copy_options::overwrite_existing);
974-
std::filesystem::remove("TEMP");
1020+
catch (const std::exception& e)
1021+
{
1022+
const auto errorMessage = std::string{ e.what() };
1023+
OVLOG_ERROR("Asset retargeting failed: " + errorMessage);
1024+
}
9751025
}
9761026
}
9771027
}

0 commit comments

Comments
 (0)