Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "Logger/Logger.h"
2#include "..\Private\IBaseApi.h"
3#include "..\Private\Ark\ArkBaseApi.h"
4
5#include "Tools.h"
6#include <filesystem>
7#include <tlhelp32.h>
8#include <fstream>
9#include <json.hpp>
10
12{
13 const DWORD InvalidParentProcessId = 0;
14
15 HANDLE Snapshot = 0;
16 PROCESSENTRY32 ProcessEntry;
17 DWORD PID = GetCurrentProcessId();
18
19 Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
20 if (Snapshot == INVALID_HANDLE_VALUE)
21 return InvalidParentProcessId;
22
23 ZeroMemory(&ProcessEntry, sizeof(ProcessEntry));
24 ProcessEntry.dwSize = sizeof(ProcessEntry);
25
26 if (!Process32First(Snapshot, &ProcessEntry))
27 return InvalidParentProcessId;
28
29 do
30 {
31 if (ProcessEntry.th32ProcessID == PID)
32 return ProcessEntry.th32ParentProcessID;
33 } while (Process32Next(Snapshot, &ProcessEntry));
34
35 return InvalidParentProcessId;
36}
37
39{
40 const std::string config_path = AsaApi::Tools::GetCurrentDir() + "/config.json";
41 std::ifstream file{ config_path };
42 if (!file.is_open())
43 return false;
44
45 nlohmann::json config;
46 file >> config;
47 file.close();
48
49 return config["settings"].value("AttachToParent", true);
50}
51
53{
54 DWORD parentProcessId = GetParentProcessId();
55 bool attachToParent = AttachToParent();
56 if (GetConsoleWindow())
57 return;
58 if (attachToParent && parentProcessId && AttachConsole(parentProcessId))
59 return;
60 AllocConsole();
61 FILE* p_cout;
62 freopen_s(&p_cout, "conout$", "w", stdout);
63 SetConsoleOutputCP(CP_UTF8);
64}
65
67{
68 const std::string config_path = AsaApi::Tools::GetCurrentDir() + "/config.json";
69 std::ifstream file{ config_path };
70 if (!file.is_open())
71 return;
72
73 nlohmann::json config;
74 file >> config;
75 file.close();
76
77 if (config.value("DeleteOldLogs", nlohmann::json::object()).value("Enabled", false) == false)
78 return;
79
80 const std::string folderPath = API::Tools::GetCurrentDir() + "\\logs";
81 const int maxAgeInSeconds = config.value("DeleteOldLogs", nlohmann::json::object()).value("MaxAge", 24) * 3600;
82
83 for (const auto& entry : std::filesystem::directory_iterator(folderPath)) {
84 if (entry.is_regular_file()) {
85 try
86 {
87 std::filesystem::file_time_type file_time = std::filesystem::last_write_time(entry);
88 auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(file_time - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
89 std::time_t tt = std::chrono::system_clock::to_time_t(sctp);
90
91 if (std::difftime(std::time(nullptr), tt) > maxAgeInSeconds) {
92 Log::GetLog()->info("Deleted log file: {}", entry.path().filename().string());
93
94 std::filesystem::remove(entry.path());
95 }
96 }
97 catch (std::exception& error)
98 {
99 Log::GetLog()->error("Error: {}", error.what());
100 break;
101 }
102 }
103 }
104}
105
106void Init()
107{
108 namespace fs = std::filesystem;
109
111
112 const std::string current_dir = API::Tools::GetCurrentDir();
113
114 if (!fs::exists(current_dir + "/logs"))
115 {
116 fs::create_directory(current_dir + "/logs");
117 }
118
119 Log::Get().Init("API");
120
122
123 API::game_api = std::make_unique<API::ArkBaseApi>();
124 API::game_api->Init();
125}
126
127extern "C" ARK_API void InitApi()
128{
129 Init();
130}
#define ARK_API
Definition Ark.h:16
Definition Logger.h:9
static Log & Get()
Definition Logger.h:16
bool AttachToParent()
Definition main.cpp:38
void OpenConsole()
Definition main.cpp:52
void Init()
Definition main.cpp:106
ARK_API void InitApi()
Definition main.cpp:127
void DeleteOldLogFiles()
Definition main.cpp:66
DWORD GetParentProcessId()
Definition main.cpp:11