| 2050 | } |
| 2051 | |
| 2052 | void FileAccessHelper::WriteIndex(const std::string& baseFolderName, MemoryCardFileEntry* const entry, MemoryCardFileMetadataReference* const parent) |
| 2053 | { |
| 2054 | // Not called for directories atm. |
| 2055 | pxAssert(entry->IsFile()); |
| 2056 | |
| 2057 | std::string folderName(baseFolderName); |
| 2058 | if (parent != nullptr) |
| 2059 | { |
| 2060 | parent->GetPath(&folderName); |
| 2061 | } |
| 2062 | else |
| 2063 | { |
| 2064 | Console.Warning(fmt::format("(FileAccesHelper::WriteIndex()) '{}' has null parent", Path::Combine(baseFolderName, (const char*)entry->entry.data.name))); |
| 2065 | } |
| 2066 | |
| 2067 | char cleanName[sizeof(entry->entry.data.name)]; |
| 2068 | memcpy(cleanName, (const char*)entry->entry.data.name, sizeof(cleanName)); |
| 2069 | FileAccessHelper::CleanMemcardFilename(cleanName); |
| 2070 | |
| 2071 | const std::string indexFileName(Path::Combine(folderName, "_pcsx2_index")); |
| 2072 | |
| 2073 | // When length isn't passed explicitly, ryml::to_csubstr spans the entire array, incl. the trailing \0's |
| 2074 | // due to a possible rapidyaml bug: https://github.com/biojppm/rapidyaml/issues/531 |
| 2075 | const ryml::csubstr key = ryml::csubstr(cleanName, std::strlen(cleanName)); |
| 2076 | std::optional<ryml::Tree> yaml = loadYamlFile(indexFileName.c_str()); |
| 2077 | |
| 2078 | if (yaml.has_value() && !yaml.value().empty()) |
| 2079 | { |
| 2080 | ryml::NodeRef index = yaml.value().rootref(); |
| 2081 | |
| 2082 | if (!index.has_child(key)) |
| 2083 | { |
| 2084 | // Newly added file - figure out the sort order as the entry should be added to the end of the list |
| 2085 | ryml::NodeRef newNode = index[key]; |
| 2086 | newNode |= ryml::MAP; |
| 2087 | unsigned int maxOrder = 0; |
| 2088 | for (const auto& n : index.children()) |
| 2089 | { |
| 2090 | unsigned int currOrder = 0; // NOTE - this limits the usefulness of making the order an int64 |
| 2091 | if (n.is_map() && n.has_child("order")) |
| 2092 | { |
| 2093 | n["order"] >> currOrder; |
| 2094 | } |
| 2095 | maxOrder = std::max(maxOrder, currOrder); |
| 2096 | } |
| 2097 | newNode["order"] << maxOrder + 1; |
| 2098 | } |
| 2099 | ryml::NodeRef entryNode = index[key]; |
| 2100 | |
| 2101 | // Update timestamps basing on internal data |
| 2102 | const auto* e = &entry->entry.data; |
| 2103 | entryNode["timeCreated"] << e->timeCreated.ToTime(); |
| 2104 | entryNode["timeModified"] << e->timeModified.ToTime(); |
| 2105 | |
| 2106 | // Write out the changes |
| 2107 | SaveYAMLToFile(indexFileName.c_str(), index); |
| 2108 | } |
| 2109 | } |
nothing calls this directly
no test coverage detected