2024-12-17 11:30:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Luanti authors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
|
|
|
#include "sscsm_environment.h"
|
|
|
|
#include "sscsm_requests.h"
|
|
|
|
#include "sscsm_events.h"
|
|
|
|
#include "sscsm_stupid_channel.h"
|
2025-02-21 12:19:04 +01:00
|
|
|
#include "client/mod_vfs.h"
|
2024-12-17 11:30:13 +01:00
|
|
|
|
|
|
|
|
2025-01-27 14:12:40 +01:00
|
|
|
SSCSMEnvironment::SSCSMEnvironment(std::shared_ptr<StupidChannel> channel) :
|
|
|
|
Thread("SSCSMEnvironment-thread"),
|
|
|
|
m_channel(std::move(channel)),
|
2025-02-21 12:19:04 +01:00
|
|
|
m_script(std::make_unique<SSCSMScripting>(this)),
|
|
|
|
m_vfs(std::make_unique<ModVFS>())
|
2025-01-27 14:12:40 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-02-21 12:19:04 +01:00
|
|
|
SSCSMEnvironment::~SSCSMEnvironment() = default;
|
|
|
|
|
2024-12-17 11:30:13 +01:00
|
|
|
void *SSCSMEnvironment::run()
|
|
|
|
{
|
|
|
|
while (true) {
|
2025-03-24 12:35:53 +01:00
|
|
|
auto next_event = [&]{
|
|
|
|
auto request = SSCSMRequestPollNextEvent{};
|
|
|
|
auto answer = doRequest(std::move(request));
|
|
|
|
return std::move(answer.next_event);
|
|
|
|
}();
|
2024-12-17 11:30:13 +01:00
|
|
|
|
|
|
|
if (dynamic_cast<SSCSMEventTearDown *>(next_event.get())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:11:51 +01:00
|
|
|
try {
|
|
|
|
next_event->exec(this);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
setFatalError(std::string("Lua error: ") + e.what());
|
|
|
|
} catch (ModError &e) {
|
|
|
|
setFatalError(std::string("Mod error: ") + e.what());
|
|
|
|
}
|
2024-12-17 11:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializedSSCSMAnswer SSCSMEnvironment::exchange(SerializedSSCSMRequest req)
|
|
|
|
{
|
|
|
|
return m_channel->exchangeA(std::move(req));
|
|
|
|
}
|
|
|
|
|
2025-01-27 14:12:40 +01:00
|
|
|
void SSCSMEnvironment::updateVFSFiles(std::vector<std::pair<std::string, std::string>> &&files)
|
|
|
|
{
|
|
|
|
for (auto &&p : files) {
|
2025-02-21 12:19:04 +01:00
|
|
|
m_vfs->m_vfs.emplace(std::move(p.first), std::move(p.second));
|
2025-01-27 14:12:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-14 14:57:22 +01:00
|
|
|
std::optional<std::string_view> SSCSMEnvironment::readVFSFile(const std::string &path)
|
|
|
|
{
|
2025-02-21 12:19:04 +01:00
|
|
|
auto it = m_vfs->m_vfs.find(path);
|
|
|
|
if (it == m_vfs->m_vfs.end())
|
2025-02-14 14:57:22 +01:00
|
|
|
return std::nullopt;
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2025-01-27 14:12:40 +01:00
|
|
|
void SSCSMEnvironment::setFatalError(const std::string &reason)
|
|
|
|
{
|
2025-02-14 14:57:22 +01:00
|
|
|
auto request = SSCSMRequestSetFatalError{};
|
|
|
|
request.reason = reason;
|
|
|
|
doRequest(std::move(request));
|
2025-01-27 14:12:40 +01:00
|
|
|
}
|