RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
2#include "PathManager.h"
3#include "UPnPManager.h"
4#include "DebugConsole.h"
5#include "Logger.h"
6#include "FirewallUtils.h"
7
8namespace ShutdownUtils
9{
10 inline void ArmDeadManKill(unsigned timeoutMs)
11 {
12 std::thread([timeoutMs]()
13 {
14 std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMs));
15 TerminateProcess(GetCurrentProcess(), 0); // hard kill (OS reclaims everything)
16 })
17 .detach();
18 }
19} // namespace ShutdownUtils
20
22{
23 if (!glfwInit())
24 {
25 std::cerr << "Falha ao inicializar o GLFW!" << std::endl;
26 return nullptr;
27 }
28
29 // Cria a janela e inicializa o contexto OpenGL
30 GLFWwindow* window = glfwCreateWindow(1280, 720, "Runic VTT", nullptr, nullptr);
31 if (!window)
32 {
33 std::cerr << "Falha ao criar a janela GLFW!" << std::endl;
34 glfwTerminate();
35 return nullptr;
36 }
37
38 // Faz o contexto OpenGL atual para a janela
39 glfwMakeContextCurrent(window);
40
41 // Inicializa o GLEW (opcional, se estiver usando)
42 if (glewInit() != GLEW_OK)
43 {
44 std::cerr << "Falha ao inicializar o GLEW!" << std::endl;
45 return nullptr;
46 }
47
48 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
49 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
50 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
51 glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
52 glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
53
54 // Retorna o ponteiro da janela
55 return window;
56}
57
58GLFWimage loadImage(const char* iconPath)
59{
60 int width, height, channels;
61 unsigned char* image = stbi_load(iconPath, &width, &height, &channels, 4); // Força 4 canais (RGBA)
62 if (!image)
63 {
64 std::cerr << "Erro: Não foi possível carregar o ícone: " << iconPath << std::endl;
65 }
66
67 // Cria o objeto GLFWimage e configura o ícone
68 GLFWimage icon;
69 icon.width = width;
70 icon.height = height;
71 icon.pixels = image;
72
73 return icon;
74}
75
76// Função para definir o ícone da janela
77void setWindowIcon(GLFWwindow* window, std::filesystem::path iconFolderPath)
78{
79 // Carrega a imagem do ícone usando stb_image
80 auto icon16 = iconFolderPath / "RunicVTTIcon_16.png";
81 auto icon32 = iconFolderPath / "RunicVTTIcon_32.png";
82 auto icon64 = iconFolderPath / "RunicVTTIcon_64.png";
83 auto icon256 = iconFolderPath / "RunicVTTIcon.png";
84
85 GLFWimage icons[4];
86 icons[0] = loadImage(icon16.string().c_str());
87 icons[1] = loadImage(icon32.string().c_str());
88 icons[2] = loadImage(icon64.string().c_str());
89 icons[3] = loadImage(icon256.string().c_str());
90
91 glfwSetWindowIcon(window, 4, icons); // Define o ícone
92
93 // Libera a memória usada pela imagem
94 stbi_image_free(icons[0].pixels);
95 stbi_image_free(icons[1].pixels);
96 stbi_image_free(icons[2].pixels);
97 stbi_image_free(icons[3].pixels);
98}
99
100static std::string getSelfPath()
101{
102 wchar_t buf[MAX_PATH];
103 GetModuleFileNameW(nullptr, buf, MAX_PATH);
104 std::wstring ws(buf);
105 return std::string(ws.begin(), ws.end());
106}
107
108int main()
109{
111 Logger::instance().setChannelCapacity(4000);
112 auto runic_exe = getSelfPath();
113 auto node_exe = PathManager::getNodeExePath().string();
114 auto runic_firewall_rule_name = "RunicVTT Inbound TCP (Any)";
115 auto node_firewall_rule_name = "RunicVTT LocalTunnel(Any TCP)";
116
118
119 GLFWwindow* window = initializeGLFWContext();
120 if (!window)
121 {
122 return -1; // Falha na inicialização
123 }
124 auto iconFolderPath = PathManager::getResPath();
125 setWindowIcon(window, iconFolderPath);
126
127 glfwPollEvents();
128
129
130 //FirewallUtils::addInboundAnyTcpForExe(runic_firewall_rule_name, runic_exe, /*Private*/ false);
131 //FirewallUtils::addInboundAnyTcpForExe(node_firewall_rule_name, node_exe, false);
132 //FirewallUtils::addInboundAnyUdpForExe("RunicVTT Inbound UDP (Any)", runic_exe, /*Private*/ false);
133 {
134 const std::string rule1 = runic_firewall_rule_name;
135 const std::string exe1 = runic_exe;
136 const std::string rule2 = node_firewall_rule_name;
137 const std::string exe2 = node_exe;
138
139 std::thread([rule1, exe1, rule2, exe2]() {
140 try {
141 FirewallUtils::addInboundAnyTcpForExe(rule1, exe1, /*privateOnly*/ false);
142 } catch (...) {
143 // swallow or log
144 }
145 try {
146 FirewallUtils::addInboundAnyTcpForExe(rule2, exe2, /*privateOnly*/ false);
147 } catch (...) {
148 // swallow or log
149 }
150 // thread exits automatically
151 }).detach();
152 }
153 // before CreateProcessA
154 auto nodeModules = (PathManager::getExternalPath() / "node" / "node_modules").string();
155 _putenv_s("NODE_PATH", nodeModules.c_str());
156 if (const char* np = std::getenv("NODE_PATH"))
157 {
158 Logger::instance().log("localtunnel", Logger::Level::Success, std::string("Parent NODE_PATH=") + np);
159 }
160 else
161 {
162 Logger::instance().log("localtunnel", Logger::Level::Error, "Parent NODE_PATH is not set");
163 }
164
165 std::shared_ptr<DirectoryWindow> map_directory = std::make_shared<DirectoryWindow>(PathManager::getMapsPath().string(), "MapsDiretory", DirectoryKind::MAP);
166 std::shared_ptr<DirectoryWindow> marker_directory = std::make_shared<DirectoryWindow>(PathManager::getMarkersPath().string(), "MarkersDirectory", DirectoryKind::MARKER);
167 ApplicationHandler app(window, map_directory, marker_directory);
168 app.run();
169
170 FirewallUtils::removeRuleElevated(runic_firewall_rule_name);
171 FirewallUtils::removeRuleElevated(node_firewall_rule_name);
172
173 ShutdownUtils::ArmDeadManKill(2000); // 2s grace
174 return 0;
175}
static void bootstrapStdCapture()
static Logger & instance()
Definition Logger.h:39
static fs::path getMarkersPath()
Definition PathManager.h:66
static fs::path getNodeExePath()
Definition PathManager.h:97
static void ensureDirectories()
static fs::path getExternalPath()
Definition PathManager.h:93
static fs::path getResPath()
static fs::path getMapsPath()
Definition PathManager.h:61
static std::string getSelfPath()
Definition main.cpp:100
GLFWimage loadImage(const char *iconPath)
Definition main.cpp:58
GLFWwindow * initializeGLFWContext()
Definition main.cpp:21
void setWindowIcon(GLFWwindow *window, std::filesystem::path iconFolderPath)
Definition main.cpp:77
int main()
Definition main.cpp:108
void addInboundAnyTcpForExe(const std::string &displayName, const std::string &programPath, bool privateOnly=true)
void removeRuleElevated(const std::string &displayName)
void ArmDeadManKill(unsigned timeoutMs)
Definition main.cpp:10