Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit d95a7a0

Browse files
committed
Correct backslashes to forward slashes when reading and setting file paths
1 parent 71b2c2c commit d95a7a0

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

Activities/GAScripted.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ int GAScripted::Create(const GAScripted &reference)
107107

108108
int GAScripted::ReadProperty(std::string propName, Reader &reader)
109109
{
110-
if (propName == "ScriptFile")
111-
reader >> m_ScriptPath;
112-
else if (propName == "LuaClassName")
110+
if (propName == "ScriptFile") {
111+
reader >> m_ScriptPath;
112+
CorrectBackslashesInPaths(m_ScriptPath);
113+
} else if (propName == "LuaClassName")
113114
reader >> m_LuaClassName;
114115
else if (propName == "AddPieSlice")
115116
{

Entities/GlobalScript.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ int GlobalScript::Create(const GlobalScript &reference)
6161

6262
int GlobalScript::ReadProperty(std::string propName, Reader &reader)
6363
{
64-
if (propName == "ScriptPath")
65-
reader >> m_ScriptPath;
66-
else if (propName == "LuaClassName")
64+
if (propName == "ScriptPath") {
65+
reader >> m_ScriptPath;
66+
CorrectBackslashesInPaths(m_ScriptPath);
67+
} else if (propName == "LuaClassName")
6768
reader >> m_LuaClassName;
6869
else if (propName == "LateUpdate")
6970
reader >> m_LateUpdate;

Entities/MovableObject.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,11 @@ int MovableObject::ReadProperty(std::string propName, Reader &reader)
331331
reader >> newSlice;
332332
PieMenuGUI::AddAvailableSlice(newSlice);
333333
}
334-
else if (propName == "ScriptPath")
335-
{
336-
std::string scriptPath = reader.ReadPropValue();
337-
if (LoadScript(scriptPath) == -2) { reader.ReportError("Duplicate script path " + scriptPath); }
338-
}
339-
else if (propName == "ScreenEffect")
340-
{
334+
else if (propName == "ScriptPath") {
335+
std::string scriptPath = reader.ReadPropValue();
336+
CorrectBackslashesInPaths(scriptPath);
337+
if (LoadScript(scriptPath) == -2) { reader.ReportError("Duplicate script path " + scriptPath); }
338+
} else if (propName == "ScreenEffect") {
341339
reader >> m_ScreenEffectFile;
342340
m_pScreenEffect = m_ScreenEffectFile.GetAsBitmap();
343341
m_ScreenEffectHash = m_ScreenEffectFile.GetHash();

Menus/PieMenuGUI.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ int PieMenuGUI::Slice::ReadProperty(std::string propName, Reader &reader)
191191
reader >> m_Icon;
192192
else if (propName == "Direction")
193193
reader >> m_Direction;
194-
else if (propName == "ScriptPath")
195-
reader >> m_ScriptPath;
196-
else if (propName == "FunctionName")
194+
else if (propName == "ScriptPath") {
195+
reader >> m_ScriptPath;
196+
CorrectBackslashesInPaths(m_ScriptPath);
197+
} else if (propName == "FunctionName")
197198
reader >> m_FunctionName;
198199
else
199200
return Serializable::ReadProperty(propName, reader);

System/ContentFile.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ namespace RTE {
8383

8484
void ContentFile::SetDataPath(const std::string &newDataPath) {
8585
m_DataPath = newDataPath;
86-
m_DataPathExtension = std::filesystem::path(newDataPath).extension().string();
86+
CorrectBackslashesInPaths(m_DataPath);
87+
88+
m_DataPathExtension = std::filesystem::path(m_DataPath).extension().string();
8789

8890
RTEAssert(!m_DataPathExtension.empty(), "Failed to find file extension when trying to find file with path and name:\n\n" + m_DataPath + "\n" + GetFormattedReaderPosition());
8991

System/Serializable.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ namespace RTE {
9494
/// <param name="writer">A Writer that the Serializable will save itself to.</param>
9595
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
9696
virtual int Save(Writer &writer) const { writer.ObjectStart(GetClassName()); return 0; }
97+
98+
/// <summary>
99+
/// Replaces backslashes with forward slashes in file paths to eliminate issues with cross-platform compatibility or invalid escape sequences.
100+
/// </summary>
101+
/// <param name="pathToCorrect">Reference to the file path string to correct slashes in.</param>
102+
void CorrectBackslashesInPaths(std::string &pathToCorrect) const {
103+
// TODO: Add a warning log entry if backslashes are found in a data path. Perhaps overwrite them in the ini file itself.
104+
while (std::find(pathToCorrect.begin(), pathToCorrect.end(), '\\') != pathToCorrect.end()) {
105+
pathToCorrect.replace(pathToCorrect.find("\\"), 1, "/");
106+
}
107+
}
97108
#pragma endregion
98109

99110
#pragma region Logging

0 commit comments

Comments
 (0)