Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ps2xRecomp/include/ps2recomp/ps2_recompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace ps2recomp
std::vector<Function> &functions,
std::unordered_map<uint32_t, std::vector<Instruction>> &decodedFunctions);

static std::string ClampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength);

private:
ConfigManager m_configManager;
std::unique_ptr<ElfParser> m_elfParser;
Expand Down Expand Up @@ -70,6 +72,7 @@ namespace ps2recomp
bool generateStubHeader();
bool writeToFile(const std::string &path, const std::string &content);
std::filesystem::path getOutputPath(const Function &function) const;
static std::string clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength);
std::string sanitizeFunctionName(const std::string &name) const;
};

Expand Down
42 changes: 41 additions & 1 deletion ps2xRecomp/src/lib/ps2_recompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,11 +1451,46 @@ namespace ps2recomp
}

std::filesystem::path outputPath = m_config.outputPath;
outputPath /= safeName + ".cpp";
outputPath /= clampFilenameLength(safeName, ".cpp", 100);

return outputPath;
}

std::string PS2Recompiler::clampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength)
{
if (maxLength == 0)
{
std::cerr << "clampFilenameLength::maxLength must be greater than 0" << std::endl;
//Better go over the limit than create files with an empty path
return baseName + extension;
}

if (baseName.size() + extension.size() <= maxLength)
return baseName + extension;

std::string namePart = baseName;
std::string preservedSuffix;

auto suffixPos = namePart.rfind("_0x");
if (suffixPos != std::string::npos)
{
preservedSuffix = namePart.substr(suffixPos);
namePart = namePart.substr(0, suffixPos);
}

std::size_t available = maxLength - extension.size() - preservedSuffix.size();

if (available == 0)
{
return preservedSuffix + extension;
}

if (namePart.size() > available)
namePart = namePart.substr(0, available);

return namePart + preservedSuffix + extension;
}

std::string PS2Recompiler::sanitizeFunctionName(const std::string &name) const
{
std::string sanitized = sanitizeIdentifierBody(name);
Expand Down Expand Up @@ -1510,4 +1545,9 @@ namespace ps2recomp
}
return StubTarget::Unknown;
}

std::string PS2Recompiler::ClampFilenameLength(const std::string& baseName, const std::string& extension, std::size_t maxLength)
{
return clampFilenameLength(baseName, extension, maxLength);
}
}
11 changes: 10 additions & 1 deletion ps2xTest/src/ps2_recompiler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,14 @@ void register_ps2_recompiler_tests()

std::error_code removeError;
std::filesystem::remove(elfPath, removeError);
}); });
});

tc.Run("respect max length for .cpp filenames", [](TestCase& t) {

t.IsTrue(PS2Recompiler::ClampFilenameLength("ReallyLongFunctionNameReallyLongFunctionNameReallyLongFunctionName_0x12345678",".cpp",50).length() <= 50,"Function name must be max 50 characters");

t.IsTrue(PS2Recompiler::ClampFilenameLength("ReallyLongFunctionNameReallyLongFunctionNameReallyLongFunctionName_0x12345678", ".cpp", 50).rfind("0x12345678") != std::string::npos, "Function name must mantain the function address at the end, if present");

});
});
}