Files
josh 1121b44857 Organize scripts and add TWS plugins with HTTP timeouts
Vendor pinned T5 and T6 plugin sources under the new plugins layout. Add native HTTP deadlines, status and timing diagnostics, telemetry integration, and build/test tooling.
2026-10-09 11:02:20 -04:00

122 lines
4.0 KiB
C++

// Minimal host interfaces for compiling the real component without the game VM.
#pragma once
#include "http.hpp"
#include <atomic>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
#include <thread>
#include <variant>
#include <vector>
using namespace std::chrono_literals;
namespace game { constexpr int SCRIPT_NONE = 0; }
namespace plugin { class plugin {}; }
class component_interface
{
public:
virtual ~component_interface() = default;
virtual void on_startup(plugin::plugin*) {}
virtual void on_shutdown(plugin::plugin*) {}
};
#define REGISTER_COMPONENT(...)
namespace scripting
{
class array;
class script_value;
using fields = std::map<std::string, script_value>;
class script_value
{
public:
std::variant<std::monostate, int, bool, double, std::string, std::shared_ptr<fields>> data;
script_value() = default;
script_value(int v) : data(v) {}
script_value(bool v) : data(v) {}
script_value(double v) : data(v) {}
script_value(const char* v) : data(std::string(v)) {}
script_value(std::string v) : data(std::move(v)) {}
script_value(const array& v);
template <typename T> bool is() const { return std::holds_alternative<T>(data); }
template <typename T> T as() const { return std::get<T>(data); }
struct raw { int type; };
raw get_raw() const { return {static_cast<int>(data.index())}; }
std::string to_string() const { return as<std::string>(); }
};
class array
{
public:
std::shared_ptr<fields> data = std::make_shared<fields>();
script_value& operator[](const script_value& key) const { return (*data)[key.as<std::string>()]; }
std::vector<script_value> get_keys() const
{
std::vector<script_value> keys;
for (const auto& [key, value] : *data) keys.emplace_back(key);
return keys;
}
};
inline script_value::script_value(const array& v) : data(v.data) {}
template <> inline bool script_value::is<array>() const { return std::holds_alternative<std::shared_ptr<fields>>(data); }
template <> inline array script_value::as<array>() const
{
array result;
result.data = std::get<std::shared_ptr<fields>>(data);
return result;
}
using variadic_args = std::vector<script_value>;
class object
{
inline static unsigned next_id = 1;
unsigned id = next_id++;
public:
unsigned get_entity_id() const { return id; }
};
inline std::vector<std::vector<script_value>> notifications;
inline std::function<void()> after_notify;
inline void notify(unsigned, const std::string& event, const std::vector<script_value>& args)
{
assert(event == "done");
notifications.push_back(args);
if (after_notify) after_notify();
}
inline void on_shutdown(const std::function<void()>&) {}
}
namespace scheduler
{
struct pool
{
std::vector<std::function<void()>> tasks;
void push(std::function<void()> f) { tasks.push_back(std::move(f)); }
void run()
{
auto work = std::move(tasks);
tasks.clear();
for (auto& task : work) task();
}
};
inline pool thread_pool;
constexpr int server = 0;
inline void loop(const std::function<void()>&, int) {}
}
namespace gsc::function
{
template <typename F> void add(const char*, F) {}
template <typename F, typename... Names> void add_multiple(F, Names...) {}
}
namespace utils::http
{
inline bool throw_from_transfer = false;
inline std::optional<result> next_result = result{};
inline timeouts observed_limits;
inline std::optional<result> get_data(const std::string&, const std::string&, const headers&, const std::string&, const timeouts& limits)
{
observed_limits = limits;
if (throw_from_transfer) throw std::runtime_error("simulated worker error");
return next_result;
}
}