File: DeclLib.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (72 lines) | stat: -rw-r--r-- 2,205 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once

#include "ideclmanager.h"
#include "ifilesystem.h"
#include "os/path.h"
#include "fmt/format.h"

namespace decl
{

/**
 * 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")
 *
 * Throws a std::invalid_argument exception if the given full path is not located in the given mod decl folder.
 */
inline std::string geRelativeDeclSavePath(const std::string& fullPath, const std::string& declFolder, std::string extension)
{
    std::string path = fullPath;

    if (path_is_absolute(path.c_str()))
    {
        auto rootPath = GlobalFileSystem().findRoot(path);

        if (rootPath.empty())
        {
            throw std::invalid_argument("The path " + path + " is not located in the current mod file structure");
        }

        path = os::getRelativePath(path, rootPath);
    }

    auto pathRelativeToDeclFolder = os::getRelativePath(path, os::standardPathWithSlash(declFolder));

    // Check if the path starts with a "decl/" folder
    // getRelativePath will return the unchanged path if this is not the case
    if (pathRelativeToDeclFolder == path)
    {
        throw std::invalid_argument("The path " + path + " does not point within " + os::standardPathWithSlash(declFolder) + " folder");
    }

    if (string::starts_with(extension, "."))
    {
        extension = extension.substr(1);
    }

    if (os::getExtension(pathRelativeToDeclFolder) != extension)
    {
        throw std::invalid_argument("The file extension must be " + extension);
    }

    return pathRelativeToDeclFolder;
}

// Returns a name that doesn't exist yet for this type
inline std::string generateNonConflictingName(Type type, const std::string& name)
{
    auto candidate = name;
    auto i = 0;

    while (GlobalDeclarationManager().findDeclaration(type, candidate))
    {
        candidate += fmt::format("{0:02d}", ++i);
    }

    return candidate;
}

}