* Verifies and extracts the relative save path from the the given full path to a decl file. * E.g. The full path "c:/games/darkmod/skins/test.skin" will be converted to "test.skin" when declFolder is "skins/". * * The declFolder is the folder relative to the mod path, like: "materials/" * The given path is verified to end with the given extension (with or without the dot, e.g. "mtr") * * Thr
| 18 | * Throws a std::invalid_argument exception if the given full path is not located in the given mod decl folder. |
| 19 | */ |
| 20 | inline std::string geRelativeDeclSavePath(const std::string& fullPath, const std::string& declFolder, std::string extension) |
| 21 | { |
| 22 | std::string path = fullPath; |
| 23 | |
| 24 | if (path_is_absolute(path.c_str())) |
| 25 | { |
| 26 | auto rootPath = GlobalFileSystem().findRoot(path); |
| 27 | |
| 28 | if (rootPath.empty()) |
| 29 | { |
| 30 | throw std::invalid_argument("The path " + path + " is not located in the current mod file structure"); |
| 31 | } |
| 32 | |
| 33 | path = os::getRelativePath(path, rootPath); |
| 34 | } |
| 35 | |
| 36 | auto pathRelativeToDeclFolder = os::getRelativePath(path, os::standardPathWithSlash(declFolder)); |
| 37 | |
| 38 | // Check if the path starts with a "decl/" folder |
| 39 | // getRelativePath will return the unchanged path if this is not the case |
| 40 | if (pathRelativeToDeclFolder == path) |
| 41 | { |
| 42 | throw std::invalid_argument("The path " + path + " does not point within " + os::standardPathWithSlash(declFolder) + " folder"); |
| 43 | } |
| 44 | |
| 45 | if (string::starts_with(extension, ".")) |
| 46 | { |
| 47 | extension = extension.substr(1); |
| 48 | } |
| 49 | |
| 50 | if (os::getExtension(pathRelativeToDeclFolder) != extension) |
| 51 | { |
| 52 | throw std::invalid_argument("The file extension must be " + extension); |
| 53 | } |
| 54 | |
| 55 | return pathRelativeToDeclFolder; |
| 56 | } |
| 57 | |
| 58 | // Returns a name that doesn't exist yet for this type |
| 59 | inline std::string generateNonConflictingName(Type type, const std::string& name) |
no test coverage detected