mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-04-20 21:05:44 +00:00
Add httpGet function
This commit is contained in:
parent
e5083cd228
commit
b1a29ded7d
@ -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;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -85,6 +85,8 @@ namespace scripting
|
||||
{
|
||||
current_file = filename;
|
||||
|
||||
printf("%s\n", filename);
|
||||
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
|
@ -150,7 +150,7 @@ namespace scripting
|
||||
script_value array::get(const std::string& key) const
|
||||
{
|
||||
const auto string_value = game::SL_GetString(key.data(), 0);
|
||||
const auto variable_id = game::GetVariable(this->id_, string_value);
|
||||
const auto variable_id = game::FindVariable(this->id_, string_value);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
@ -167,7 +167,7 @@ namespace scripting
|
||||
|
||||
script_value array::get(const unsigned int index) const
|
||||
{
|
||||
const auto variable_id = game::GetVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF);
|
||||
|
||||
if (!variable_id)
|
||||
{
|
||||
|
@ -283,4 +283,14 @@ namespace scripting
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
unsigned int make_object()
|
||||
{
|
||||
unsigned int index = 0;
|
||||
const auto variable = game::AllocVariable(&index);
|
||||
variable->w.type = game::SCRIPT_STRUCT;
|
||||
variable->u.f.prev = 0;
|
||||
|
||||
return index;
|
||||
}
|
||||
}
|
@ -36,4 +36,5 @@ namespace scripting
|
||||
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
|
||||
|
||||
unsigned int make_array();
|
||||
unsigned int make_object();
|
||||
}
|
||||
|
@ -25,6 +25,9 @@
|
||||
#include <filesystem>
|
||||
#include <map>
|
||||
#include <csetjmp>
|
||||
#include <atlcomcli.h>
|
||||
|
||||
#pragma comment(lib, "urlmon.lib")
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
@ -36,6 +39,8 @@ using namespace std::literals;
|
||||
#include "utils/hook.hpp"
|
||||
#include "utils/concurrent_list.hpp"
|
||||
#include "utils/io.hpp"
|
||||
#include "utils/concurrency.hpp"
|
||||
#include "utils/http.hpp"
|
||||
|
||||
#include "game/structs.hpp"
|
||||
#include "game/game.hpp"
|
46
src/utils/concurrency.hpp
Normal file
46
src/utils/concurrency.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
|
||||
namespace utils::concurrency
|
||||
{
|
||||
template <typename T, typename MutexType = std::mutex>
|
||||
class container
|
||||
{
|
||||
public:
|
||||
template <typename R = void, typename F>
|
||||
R access(F&& accessor) const
|
||||
{
|
||||
std::lock_guard<MutexType> _{mutex_};
|
||||
return accessor(object_);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access(F&& accessor)
|
||||
{
|
||||
std::lock_guard<MutexType> _{mutex_};
|
||||
return accessor(object_);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access_with_lock(F&& accessor) const
|
||||
{
|
||||
std::unique_lock<MutexType> lock{mutex_};
|
||||
return accessor(object_, lock);
|
||||
}
|
||||
|
||||
template <typename R = void, typename F>
|
||||
R access_with_lock(F&& accessor)
|
||||
{
|
||||
std::unique_lock<MutexType> lock{mutex_};
|
||||
return accessor(object_, lock);
|
||||
}
|
||||
|
||||
T& get_raw() { return object_; }
|
||||
const T& get_raw() const { return object_; }
|
||||
|
||||
private:
|
||||
mutable MutexType mutex_{};
|
||||
T object_{};
|
||||
};
|
||||
}
|
47
src/utils/http.cpp
Normal file
47
src/utils/http.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "http.hpp"
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url)
|
||||
{
|
||||
CComPtr<IStream> stream;
|
||||
|
||||
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
char buffer[0x1000];
|
||||
std::string result;
|
||||
|
||||
HRESULT status{};
|
||||
|
||||
do
|
||||
{
|
||||
DWORD bytes_read = 0;
|
||||
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
|
||||
|
||||
if (bytes_read > 0)
|
||||
{
|
||||
result.append(buffer, bytes_read);
|
||||
}
|
||||
}
|
||||
while (SUCCEEDED(status) && status != S_FALSE);
|
||||
|
||||
if (FAILED(status))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
return {result};
|
||||
}
|
||||
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url)
|
||||
{
|
||||
return std::async(std::launch::async, [url]()
|
||||
{
|
||||
return get_data(url);
|
||||
});
|
||||
}
|
||||
}
|
11
src/utils/http.hpp
Normal file
11
src/utils/http.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <future>
|
||||
|
||||
namespace utils::http
|
||||
{
|
||||
std::optional<std::string> get_data(const std::string& url);
|
||||
std::future<std::optional<std::string>> get_data_async(const std::string& url);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user