timeline refactor, bit broken rn
This commit is contained in:
133
src/loader.cpp
133
src/loader.cpp
@@ -1,5 +1,7 @@
|
||||
#include "loader.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <imgui/backends/imgui_impl_opengl3.h>
|
||||
#include <imgui/backends/imgui_impl_sdl3.h>
|
||||
|
||||
@@ -8,11 +10,57 @@
|
||||
#include "filesystem_.h"
|
||||
#include "log.h"
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
using namespace anm2ed::types;
|
||||
using namespace anm2ed::util;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
constexpr auto SOCKET_ADDRESS = "127.0.0.1";
|
||||
constexpr auto SOCKET_PORT = 11414;
|
||||
|
||||
namespace
|
||||
{
|
||||
bool socket_paths_send(Socket& socket, const std::vector<std::string>& paths)
|
||||
{
|
||||
uint32_t count = htonl(static_cast<uint32_t>(paths.size()));
|
||||
if (!socket.send(&count, sizeof(count))) return false;
|
||||
|
||||
for (const auto& path : paths)
|
||||
{
|
||||
uint32_t length = htonl(static_cast<uint32_t>(path.size()));
|
||||
if (!socket.send(&length, sizeof(length))) return false;
|
||||
if (!path.empty() && !socket.send(path.data(), path.size())) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::string> socket_paths_receive(Socket& socket)
|
||||
{
|
||||
uint32_t count{};
|
||||
if (!socket.receive(&count, sizeof(count))) return {};
|
||||
count = ntohl(count);
|
||||
|
||||
std::vector<std::string> paths;
|
||||
paths.reserve(count);
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i)
|
||||
{
|
||||
uint32_t length{};
|
||||
if (!socket.receive(&length, sizeof(length))) return {};
|
||||
length = ntohl(length);
|
||||
|
||||
std::string path(length, '\0');
|
||||
if (length > 0 && !socket.receive(path.data(), length)) return {};
|
||||
paths.emplace_back(std::move(path));
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Loader::settings_path()
|
||||
{
|
||||
return filesystem::path_preferences_get() + "settings.ini";
|
||||
@@ -23,6 +71,45 @@ namespace anm2ed
|
||||
for (int i = 1; i < argc; i++)
|
||||
arguments.emplace_back(argv[i]);
|
||||
|
||||
Socket testSocket;
|
||||
if (!testSocket.open(SERVER))
|
||||
logger.warning(std::format("Failed to open socket; single instancing will not work."));
|
||||
|
||||
bool isPrimaryInstance = false;
|
||||
|
||||
if (testSocket.bind({SOCKET_ADDRESS, SOCKET_PORT}))
|
||||
{
|
||||
socket = std::move(testSocket);
|
||||
|
||||
if (!socket.listen())
|
||||
logger.warning("Could not listen on socket; single instancing disabled.");
|
||||
else
|
||||
{
|
||||
isPrimaryInstance = true;
|
||||
isSocketThread = true;
|
||||
logger.info(std::format("Opened socket at {}:{}", SOCKET_ADDRESS, SOCKET_PORT));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.info(std::format("Existing instance of program exists; passing arguments..."));
|
||||
Socket clientSocket;
|
||||
if (!clientSocket.open(CLIENT))
|
||||
logger.warning("Could not open client socket to forward arguments.");
|
||||
else if (!clientSocket.connect({SOCKET_ADDRESS, SOCKET_PORT}))
|
||||
logger.warning("Could not connect to existing instance.");
|
||||
else if (!socket_paths_send(clientSocket, arguments))
|
||||
logger.warning("Failed to transfer arguments to existing instance.");
|
||||
else
|
||||
logger.info("Sent arguments to existing instance. Exiting.");
|
||||
|
||||
if (!isPrimaryInstance)
|
||||
{
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
settings = Settings(settings_path());
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
@@ -93,10 +180,54 @@ namespace anm2ed
|
||||
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
||||
|
||||
ImGui::LoadIniSettingsFromDisk(settings_path().c_str());
|
||||
|
||||
if (isSocketThread)
|
||||
{
|
||||
isSocketRunning = true;
|
||||
socketThread = std::thread(
|
||||
[this]()
|
||||
{
|
||||
while (isSocketRunning)
|
||||
{
|
||||
auto client = socket.accept();
|
||||
if (!client.is_valid())
|
||||
{
|
||||
if (!isSocketRunning) break;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto paths = socket_paths_receive(client);
|
||||
for (auto& path : paths)
|
||||
{
|
||||
if (path.empty()) continue;
|
||||
SDL_Event event{};
|
||||
event.type = SDL_EVENT_DROP_FILE;
|
||||
event.drop.data = SDL_strdup(path.c_str());
|
||||
event.drop.windowID = window ? SDL_GetWindowID(window) : 0;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Loader::~Loader()
|
||||
{
|
||||
if (isSocketThread)
|
||||
{
|
||||
isSocketRunning = false;
|
||||
|
||||
if (socket.is_valid())
|
||||
{
|
||||
Socket wakeSocket;
|
||||
if (wakeSocket.open(CLIENT) && wakeSocket.connect({SOCKET_ADDRESS, SOCKET_PORT}))
|
||||
socket_paths_send(wakeSocket, {});
|
||||
}
|
||||
|
||||
socket.close();
|
||||
if (socketThread.joinable()) socketThread.join();
|
||||
}
|
||||
|
||||
if (ImGui::GetCurrentContext())
|
||||
{
|
||||
settings.save(settings_path(), ImGui::SaveIniSettingsToMemory(nullptr));
|
||||
@@ -115,4 +246,4 @@ namespace anm2ed
|
||||
SDL_Quit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user