Ark Server API 3.54
Serverside plugin support for Ark Survival Evolved.
Loading...
Searching...
No Matches
version.cpp
Go to the documentation of this file.
1#include <Windows.h>
2#include <cstdio>
3#include <filesystem>
4
8#include "Core/Public/Tools.h"
9
10#pragma comment(lib, "Crypt32.lib")
11#pragma comment(lib, "Iphlpapi.lib")
12
13HINSTANCE m_hinst_dll = nullptr;
14extern "C" UINT_PTR mProcs[17]{ 0 };
15
16LPCSTR import_names[] = {
17 "GetFileVersionInfoA", "GetFileVersionInfoByHandle", "GetFileVersionInfoExA", "GetFileVersionInfoExW",
18 "GetFileVersionInfoSizeA", "GetFileVersionInfoSizeExA", "GetFileVersionInfoSizeExW", "GetFileVersionInfoSizeW",
19 "GetFileVersionInfoW", "VerFindFileA", "VerFindFileW", "VerInstallFileA", "VerInstallFileW", "VerLanguageNameA",
20 "VerLanguageNameW", "VerQueryValueA", "VerQueryValueW"
21};
22
24{
25 AllocConsole();
26 FILE* p_cout;
27 freopen_s(&p_cout, "conout$", "w", stdout);
28 SetConsoleOutputCP(CP_UTF8);
29}
30
31std::time_t GetFileWriteTime(const std::filesystem::path& filename)
32{
33 struct _stat64 fileInfo {};
34 if (_wstati64(filename.wstring().c_str(), &fileInfo) != 0)
35 {
36 throw std::runtime_error("Failed to get last write time.");
37 }
38 return fileInfo.st_mtime;
39}
40
42{
43 namespace fs = std::filesystem;
44
45 const auto now = std::chrono::system_clock::now();
46
47 const std::string current_dir = API::Tools::GetCurrentDir();
48
49 for (const auto& file : fs::directory_iterator(current_dir + "/logs"))
50 {
51 const std::time_t ftime = GetFileWriteTime(file);
52
53 if (file.path().filename() == "ArkApi.log")
54 {
55 tm tm{};
56 localtime_s(&tm, &ftime);
57
58 char time_str[64];
59 strftime(time_str, 64, "%Y-%m-%d-%H-%M", &tm);
60
61 const std::string new_name = "ArkApi_" + std::string(time_str) + ".log";
62 std::rename(file.path().generic_string().data(), (current_dir + "/logs/" + new_name).data());
63 }
64
65 const auto time_point = std::chrono::system_clock::from_time_t(ftime);
66
67 auto diff = std::chrono::duration_cast<std::chrono::hours>(now - time_point);
68 if (diff.count() >= 24 * 14) // 14 days
69 {
70 fs::remove(file);
71 }
72 }
73}
74
75std::string DetectGame()
76{
77 namespace fs = std::filesystem;
78
79 const std::string current_dir = API::Tools::GetCurrentDir();
80
81 for (const auto& directory_entry : fs::directory_iterator(current_dir))
82 {
83 const auto& path = directory_entry.path();
84 if (is_directory(path))
85 {
86 const auto name = path.filename().stem().generic_string();
87 if (name == "ArkApi")
88 {
89 return "Ark";
90 }
91 if (name == "AtlasApi")
92 {
93 return "Atlas";
94 }
95 }
96 }
97
98 return "";
99}
100
101void Init()
102{
103 namespace fs = std::filesystem;
104
105 OpenConsole();
106
107 const std::string current_dir = API::Tools::GetCurrentDir();
108
109 if (!fs::exists(current_dir + "/logs"))
110 {
111 fs::create_directory(current_dir + "/logs");
112 }
113
114 PruneOldLogs();
115
116 Log::Get().Init("API");
117
118 const std::string game_name = DetectGame();
119 if (game_name == "Ark")
120 API::game_api = std::make_unique<API::ArkBaseApi>();
121 else if (game_name == "Atlas")
122 API::game_api = std::make_unique<API::AtlasBaseApi>();
123 else
124 Log::GetLog()->critical("Failed to detect game");
125
126 API::game_api->Init();
127}
128
129BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD fdw_reason, LPVOID /*unused*/)
130{
131 if (fdw_reason == DLL_PROCESS_ATTACH)
132 {
133 DisableThreadLibraryCalls(hinst_dll);
134
135 CHAR sys_dir[MAX_PATH];
136 GetSystemDirectoryA(sys_dir, MAX_PATH);
137
138 char buffer[MAX_PATH];
139 sprintf_s(buffer, "%s\\version.dll", sys_dir);
140
141 m_hinst_dll = LoadLibraryA(buffer);
142 if (m_hinst_dll == nullptr)
143 {
144 return FALSE;
145 }
146
147 for (int i = 0; i < 17; i++)
148 {
149 mProcs[i] = reinterpret_cast<UINT_PTR>(GetProcAddress(m_hinst_dll, import_names[i]));
150 }
151
152 Init();
153 }
154 else if (fdw_reason == DLL_PROCESS_DETACH)
155 {
156 FreeLibrary(m_hinst_dll);
157 }
158
159 return TRUE;
160}
161
171extern "C" void VerFindFileA_wrapper();
172extern "C" void VerFindFileW_wrapper();
173extern "C" void VerInstallFileA_wrapper();
174extern "C" void VerInstallFileW_wrapper();
175extern "C" void VerLanguageNameA_wrapper();
176extern "C" void VerLanguageNameW_wrapper();
177extern "C" void VerQueryValueA_wrapper();
178extern "C" void VerQueryValueW_wrapper();
void Init(const std::string &plugin_name)
Definition: Logger.h:27
static std::shared_ptr< spdlog::logger > & GetLog()
Definition: Logger.h:22
static Log & Get()
Definition: Logger.h:16
std::unique_ptr< IBaseApi > game_api
Definition: IBaseApi.h:25
void OpenConsole()
Definition: version.cpp:23
void GetFileVersionInfoSizeW_wrapper()
void Init()
Definition: version.cpp:101
LPCSTR import_names[]
Definition: version.cpp:16
HINSTANCE m_hinst_dll
Definition: version.cpp:13
void VerQueryValueA_wrapper()
void GetFileVersionInfoExW_wrapper()
void VerLanguageNameA_wrapper()
BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD fdw_reason, LPVOID)
Definition: version.cpp:129
void GetFileVersionInfoSizeExW_wrapper()
std::string DetectGame()
Definition: version.cpp:75
void GetFileVersionInfoA_wrapper()
void VerInstallFileW_wrapper()
void VerLanguageNameW_wrapper()
void VerFindFileW_wrapper()
void GetFileVersionInfoByHandle_wrapper()
UINT_PTR mProcs[17]
Definition: version.cpp:14
void GetFileVersionInfoW_wrapper()
std::time_t GetFileWriteTime(const std::filesystem::path &filename)
Definition: version.cpp:31
void PruneOldLogs()
Definition: version.cpp:41
void VerInstallFileA_wrapper()
void GetFileVersionInfoExA_wrapper()
void VerQueryValueW_wrapper()
void GetFileVersionInfoSizeExA_wrapper()
void VerFindFileA_wrapper()
void GetFileVersionInfoSizeA_wrapper()