Add httpGet function

This commit is contained in:
Federico Cecchetto
2021-06-20 20:04:36 +02:00
parent e5083cd228
commit b1a29ded7d
11 changed files with 253 additions and 34 deletions

View File

@ -161,6 +161,26 @@ namespace io
return scripting::script_value{};
});
gsc::function::add("httpget", [](const gsc::function_args& args) -> scripting::script_value
{
const auto url = args[0].as<std::string>();
const auto object = scripting::entity(scripting::make_object());
scheduler::once([object, url]()
{
const auto result = utils::http::get_data(url.data());
scheduler::once([object, result]()
{
const auto value = result.has_value()
? result.value().substr(0, 0x5000)
: "";
scripting::notify(object, "done", {value});
});
}, scheduler::pipeline::async);
return object;
});
}
};
}

View File

@ -1,73 +1,128 @@
#include <stdinc.hpp>
#include "loader/component_loader.hpp"
#include "scheduler.hpp"
#include "game/game.hpp"
namespace scheduler
{
namespace
{
std::queue<std::function<void()>> tasks;
struct task
{
std::function<bool()> handler;
std::function<bool()> handler{};
std::chrono::milliseconds interval{};
std::chrono::high_resolution_clock::time_point last_call{};
};
utils::concurrent_list<task> callbacks;
using task_list = std::vector<task>;
void execute()
class task_pipeline
{
for (auto callback : callbacks)
public:
void add(task&& task)
{
const auto now = std::chrono::high_resolution_clock::now();
const auto diff = now - callback->last_call;
if (diff < callback->interval) continue;
callback->last_call = now;
const auto res = callback->handler();
if (res)
new_callbacks_.access([&task](task_list& tasks)
{
callbacks.remove(callback);
}
tasks.emplace_back(std::move(task));
});
}
void execute()
{
callbacks_.access([&](task_list& tasks)
{
this->merge_callbacks();
for (auto i = tasks.begin(); i != tasks.end();)
{
const auto now = std::chrono::high_resolution_clock::now();
const auto diff = now - i->last_call;
if (diff < i->interval)
{
++i;
continue;
}
i->last_call = now;
const auto res = i->handler();
if (res == cond_end)
{
i = tasks.erase(i);
}
else
{
++i;
}
}
});
}
private:
utils::concurrency::container<task_list> new_callbacks_;
utils::concurrency::container<task_list, std::recursive_mutex> callbacks_;
void merge_callbacks()
{
callbacks_.access([&](task_list& tasks)
{
new_callbacks_.access([&](task_list& new_tasks)
{
tasks.insert(tasks.end(), std::move_iterator<task_list::iterator>(new_tasks.begin()), std::move_iterator<task_list::iterator>(new_tasks.end()));
new_tasks = {};
});
});
}
};
std::thread thread;
task_pipeline pipelines[pipeline::count];
void execute(const pipeline type)
{
assert(type >= 0 && type < pipeline::count);
pipelines[type].execute();
}
void server_frame()
void server_frame_stub()
{
execute();
reinterpret_cast<void (*)()>(0x50C1E0)();
execute(pipeline::server);
}
}
void schedule(const std::function<bool()>& callback, const std::chrono::milliseconds delay)
void schedule(const std::function<bool()>& callback, const pipeline type,
const std::chrono::milliseconds delay)
{
assert(type >= 0 && type < pipeline::count);
task task;
task.handler = callback;
task.interval = delay;
task.last_call = std::chrono::high_resolution_clock::now();
callbacks.add(task);
pipelines[type].add(std::move(task));
}
void loop(const std::function<void()>& callback, const std::chrono::milliseconds delay)
void loop(const std::function<void()>& callback, const pipeline type,
const std::chrono::milliseconds delay)
{
schedule([callback]()
{
callback();
return false;
}, delay);
return cond_continue;
}, type, delay);
}
void once(const std::function<void()>& callback, const std::chrono::milliseconds delay)
void once(const std::function<void()>& callback, const pipeline type,
const std::chrono::milliseconds delay)
{
schedule([callback]()
{
callback();
return true;
}, delay);
return cond_end;
}, type, delay);
}
class component final : public component_interface
@ -75,9 +130,18 @@ namespace scheduler
public:
void post_unpack() override
{
utils::hook::call(0x50CEDC, server_frame);
thread = std::thread([]()
{
while (true)
{
execute(pipeline::async);
std::this_thread::sleep_for(10ms);
}
});
utils::hook::call(0x50CEDC, server_frame_stub);
}
};
}
REGISTER_COMPONENT(scheduler::component)
REGISTER_COMPONENT(scheduler::component)

View File

@ -2,7 +2,20 @@
namespace scheduler
{
void schedule(const std::function<bool()>& callback, std::chrono::milliseconds delay = 0ms);
void loop(const std::function<void()>& callback, std::chrono::milliseconds delay = 0ms);
void once(const std::function<void()>& callback, std::chrono::milliseconds delay = 0ms);
enum pipeline
{
server,
async,
count,
};
static const bool cond_continue = false;
static const bool cond_end = true;
void schedule(const std::function<bool()>& callback, pipeline type = pipeline::server,
std::chrono::milliseconds delay = 0ms);
void loop(const std::function<void()>& callback, pipeline type = pipeline::server,
std::chrono::milliseconds delay = 0ms);
void once(const std::function<void()>& callback, pipeline type = pipeline::server,
std::chrono::milliseconds delay = 0ms);
}

View File

@ -85,6 +85,8 @@ namespace scripting
{
current_file = filename;
printf("%s\n", filename);
const auto file_id = atoi(filename);
if (file_id)
{