summaryrefslogtreecommitdiff
path: root/src/config/ConfigManager.cpp
blob: 297e9c04f309d5950cffbb3a87b95742b0213d78 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
#include "ConfigManager.hpp"
#include "supplementary/jeremy/Jeremy.hpp"
#include "legacy/ConfigManager.hpp"
#include "lua/ConfigManager.hpp"
#include "../debug/log/Logger.hpp"

#include <hyprutils/path/Path.hpp>
#include <filesystem>

using namespace Config;

static UP<IConfigManager> g_mgr;

//
bool Config::initConfigManager() {
    if (mgr())
        return true;

    // run this bitch
    const auto CFG_PATH = Supplementary::Jeremy::getMainConfigPath();

    if (!CFG_PATH) {
        Log::logger->log(Log::CRIT, "[cfg] Couldn't load config: {}", CFG_PATH.error());
        return false;
    }

    std::filesystem::path filePath = CFG_PATH->path;

    if (CFG_PATH->type == Supplementary::Jeremy::CONFIG_TYPE_REGULAR) {
        Log::logger->log(Log::DEBUG, "[cfg] Regular config at {}", filePath.string());

        std::error_code ec;
        if (filePath.extension() == ".lua") {
            // we have lua!
            Log::logger->log(Log::DEBUG, "[cfg] Using lua config found at {}", filePath.string());
            g_mgr = makeUnique<Lua::CConfigManager>();
        } else {
            filePath.replace_extension(".conf");
            Log::logger->log(Log::DEBUG, "[cfg] Lua config not found, using legacy config at {}", filePath.string());
            g_mgr = makeUnique<Legacy::CConfigManager>();
        }
    } else {
        Log::logger->log(Log::DEBUG, "[cfg] Config is either explicit or special.");

        if (filePath.extension() == ".lua") {
            Log::logger->log(Log::DEBUG, "[cfg] Config is lua, loading lua mgr");
            g_mgr = makeUnique<Lua::CConfigManager>();
        } else {
            Log::logger->log(Log::DEBUG, "[cfg] Config is NOT lua, loading regular mgr");
            g_mgr = makeUnique<Legacy::CConfigManager>();
        }
    }

    RASSERT(g_mgr, "failed to create a suitable config manager");

    std::error_code ec;
    if (!std::filesystem::exists(filePath, ec) || ec) {
        if (ec) {
            Log::logger->log(Log::CRIT, "[cfg] Couldn't load config: {}", ec.message());
            return false;
        }

        // generate default
        if (const auto v = g_mgr->generateDefaultConfig(filePath); !v) {
            Log::logger->log(Log::CRIT, "[cfg] Couldn't generate default config: {}", v.error());
            return false;
        }
    }

    return true;
}

UP<IConfigManager>& Config::mgr() {
    return g_mgr;
}

const char* Config::typeToString(eConfigManagerType t) {
    switch (t) {
        case CONFIG_LUA: return "lua";
        case CONFIG_LEGACY: return "hyprlang";
        default: return "error";
    }
}