admin
2025-07-17 6cd92a169cbc0db35042f243a09d976fd3e1445c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "common/pch.h"
 
#include "ConfigUtil.h"
#include <iostream>
#include <fstream>
#include "../common/JsonUtil.h"
#include "../common/TimeUtil.h"
 
 
bool fileExists(const std::string& filename) {
    std::ifstream file(filename);
    return file.good(); // Èç¹ûÎļþ³É¹¦´ò¿ª£¬Ôò·µ»Ø true
}
 
string ConfigUtil::getConfigPath()
{
    wchar_t* appDataPath = nullptr;
    CString path;
    // »ñÈ¡APPDataĿ¼·¾¶
    if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appDataPath))) {
        std::wcout << L"APPDataĿ¼·¾¶: " << appDataPath << std::endl;
        path.Append(appDataPath);
        CoTaskMemFree(appDataPath);  // ÊÍ·ÅÄÚ´æ
    }
    else {
        std::wcerr << L"ÎÞ·¨»ñÈ¡APPDataĿ¼·¾¶" << std::endl;
        return "config.cfg";
    }
    path.Append(L"\\¼ÝÊ»²Õ");
    // ÅжÏÎļþ¼ÐÊÇ·ñ´æÔÚ
    DWORD fileAttributes = GetFileAttributesW(path);
    if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
        CreateDirectoryW(path, NULL);
    }
    string fpath = StringUtil::cstring2String(path);
    fpath.append("\\config.cfg");
    return  fpath.c_str();
}
 
void ConfigUtil::readConfig(libconfig::Config& config)
{
 
    string strConfPath = getConfigPath();
    if (!fileExists(strConfPath)) {
        std::ofstream file(strConfPath);
        file.is_open();
    }
 
    //½â¶ÁÅäÖÃÎļþ
    try {
        config.readFile(strConfPath.c_str());
    }
    catch (const libconfig::FileIOException& fioex) {
        std::cerr << "I/O exception while reading the file." << std::endl;
    }
    catch (const libconfig::ParseException& pex) {
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
            << " - " << pex.getError() << std::endl;
    }
}
 
void ConfigUtil::writeConfig(libconfig::Config& config)
{
    config.writeFile(getConfigPath().c_str());
}
 
int ConfigUtil::readIntConfig(const char* key)
{
 
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        int val;
        root.lookupValue(key, val);
        return val;
    }
    throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ");
}
 
string ConfigUtil::readStringConfig(const char* key)
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        //string val;
        const char* val;
        root.lookupValue(key, val);
        return string(val);
    }
    throw string("ÉÐδ»ñÈ¡µ½ÄÚÈÝ");
}
 
 
 
void ConfigUtil::setIntConfig(const char* key, int val) {
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        root[key] = val;
    }
    else {
        root.add(key, libconfig::Setting::TypeInt) = val;
    }
    writeConfig(mConfig);
}
void ConfigUtil::setStringConfig(const char* key, string val) {
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    if (root.exists(key)) {
        root[key] = val.c_str();
    }
    else {
        root.add(key, libconfig::Setting::TypeString) = val.c_str();
    }
    writeConfig(mConfig);
}
 
list<string> ConfigUtil::getKeys()
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    list<string> keys;
    for (auto iter = root.begin(); iter != root.end(); ++iter) {
        keys.push_back(iter->getName());
    }
    return keys;
}
 
void ConfigUtil::delKey(string key)
{
    libconfig::Config mConfig;
    readConfig(mConfig);
    libconfig::Setting& root = mConfig.getRoot();
    root.remove(key.c_str());
    writeConfig(mConfig);
}
 
void ConfigUtil::setBuildServer(ServerInfo* server)
{
    rapidjson::StringBuffer buf;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
    writer.StartObject();
    writer.Key("ip");
    writer.String(server->ip.c_str());
    writer.Key("port");
    writer.Int(server->port);
    writer.Key("userName");
    writer.String(server->userName.c_str());
    writer.Key("pwd");
    writer.String(server->pwd.c_str());
    writer.EndObject();
    const char* json_content = buf.GetString();
    setStringConfig("build_server_info", json_content);
}
 
ServerInfo* ConfigUtil::getBuildServer()
{
    try {
        string result = readStringConfig("build_server_info");
        auto doc = JsonUtil::parseUTF8(result);
        ServerInfo* server = new ServerInfo();
        server->ip = doc["ip"].GetString();
        server->port = doc["port"].GetInt();
        server->userName = doc["userName"].GetString();
        server->pwd = doc["pwd"].GetString();
        return server;
    }
    catch (...) {
        return nullptr;
    }
    
}
 
void ConfigUtil::setProjectBuildInfo(ProjectBuildInfo* server, int index)
{
    string key = string("project_build_info-").append(to_string(index));
    rapidjson::StringBuffer buf;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
    writer.StartObject();
    writer.Key("name");
    writer.String(server->name.c_str());
    writer.Key("dir");
    writer.String(server->dir.c_str());
    writer.Key("scriptFile");
    writer.String(server->scriptFile.c_str());
    writer.Key("outputDir");
    writer.String(server->outputDir.c_str());
    writer.Key("outputFile");
    writer.String(server->outputFile.c_str());
    writer.Key("targetServer");
    writer.StartObject();
    writer.Key("ip");
    writer.String(server->targetServer.ip.c_str());
    writer.Key("port");
    writer.Int(server->targetServer.port);
    writer.Key("userName");
    writer.String(server->targetServer.userName.c_str());
    writer.Key("pwd");
    writer.String(server->targetServer.pwd.c_str());
 
    writer.EndObject();
    writer.Key("targetDir");
    writer.String(server->targetDir.c_str());
 
    writer.Key("targetAppFile");
    writer.String(server->targetAppFile.c_str());
 
    writer.Key("targetAppRestartFile");
    writer.String(server->targetAppRestartFile.c_str());
    
 
    writer.EndObject();
    const char* json_content = buf.GetString();
    setStringConfig(key.c_str(), json_content);
}
 
ProjectBuildInfo* ConfigUtil::getProjectBuildInfo(int index)
{
 
    try {
        string key = string("project_build_info-").append(to_string(index));
        string result = readStringConfig(key.c_str());
        auto doc = JsonUtil::parseUTF8(result);
        ProjectBuildInfo* server = new ProjectBuildInfo();
        if (doc.HasMember("name")) {
            server->name = doc["name"].GetString();
        }
 
        server->dir = doc["dir"].GetString();
        server->scriptFile = doc["scriptFile"].GetString();
        server->outputDir = doc["outputDir"].GetString();
        server->outputFile = doc["outputFile"].GetString();
        auto targetServer =  doc["targetServer"].GetObjectW();
        server->targetServer.ip = targetServer["ip"].GetString();
        server->targetServer.port = targetServer["port"].GetInt();
        server->targetServer.userName = targetServer["userName"].GetString();
        server->targetServer.pwd = targetServer["pwd"].GetString();
        server->targetDir = doc["targetDir"].GetString();
        if (doc.HasMember("targetAppFile")) {
            server->targetAppFile = doc["targetAppFile"].GetString();
        }
        if (doc.HasMember("targetAppRestartFile")) {
            server->targetAppRestartFile = doc["targetAppRestartFile"].GetString();
        }
 
        return server;
    }
    catch (...) {
        return nullptr;
    }
 
    return nullptr;
}
 
void ConfigUtil::setLocalCacheDir(string dir)
{
    setStringConfig("local_cache_dir", dir);
 
}
 
string ConfigUtil::getLocalCacheDir()
{
    try {
        string result = readStringConfig("local_cache_dir");
        return result;
    }
    catch (...) {
        return "";
    }
 
}