-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextmodule_option.cpp
More file actions
95 lines (75 loc) · 2.41 KB
/
textmodule_option.cpp
File metadata and controls
95 lines (75 loc) · 2.41 KB
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
#include "textmodule_option.hpp"
#include <nlohmann/json.hpp>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <stdexcept>
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/args.h>
#include "textmodule.hpp"
#include "textmodule_string.hpp"
#include "textmodule_lua.hpp"
#include "textmodule_exception.hpp"
#define OPTION_PATH ".\\textmodule\\config.json"
#define VERSION_CHECK_URL L"https://raw.githubusercontent.com/shumm7/textmodule/main/VERSION"
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace std;
nlohmann::json getOption() {
std::ifstream ifs(OPTION_PATH);
if (ifs.fail()) {
throw std::runtime_error(FAILED_TO_LOAD_FILE);
}
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
nlohmann::json j = nlohmann::json::parse(str);
return j;
}
int versionCheck() {
try {
std::wstring url = VERSION_CHECK_URL;
web::http::client::http_client client(url);
web::http::http_request request(methods::GET);
web::http::http_response response = client.request(request).get();
if (response.status_code() == status_codes::OK) {
std::wstring v = std::regex_replace(response.extract_string().get(), wregex(L"\n"), L"\0");
if (v == tostring_n(MODULE_VERSION))
return VERSION_CHECK_LATEST;
else
return VERSION_CHECK_OUTDATED;
}
else {
return VERSION_CHECK_ERROR;
}
}
catch (std::exception) {
return VERSION_CHECK_ERROR;
}
}
void tm_debuglog(lua_Option opt, lua_Sstring tag, lua_Sstring log) {
if (opt["debug"]) {
auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
std::tm tm;
__time64_t time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
_localtime64_s(&tm, &time_t);
store.push_back(tm);
store.push_back(tag);
store.push_back(log);
std::string c = fmt::vformat("[{0:%Y-%m-%d %H-%M-%S}] (textmodule): debug log: [{1:s}] {2:s}", store);
OutputDebugStringA(c.c_str());
//std::cout << c << std::endl;
}
}
void tm_debuglog_apiloaded(lua_Option opt, lua_Sstring apiname) {
std::string s = apiname + std::string(" was successfully loaded");
tm_debuglog(opt, "API", s);
}
void tm_debuglog_apinoloaded(lua_Option opt, lua_Sstring apiname) {
std::string s = apiname + std::string(" was not loaded");
tm_debuglog(opt, "API", s);
}