Merge pull request #147 from fedddddd/develop

v2.0.2
This commit is contained in:
fed 2022-02-05 00:16:01 +01:00 committed by GitHub
commit d28a375d25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 1626 additions and 101 deletions

View File

@ -19,7 +19,7 @@ jobs:
- Release
steps:
- name: Wait for previous workflows
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
uses: softprops/turnstyle@v1
with:
poll-interval-seconds: 10

3
.gitmodules vendored
View File

@ -43,3 +43,6 @@
[submodule "deps/imgui"]
path = deps/imgui
url = https://github.com/fedddddd/imgui.git
[submodule "deps/curl"]
path = deps/curl
url = https://github.com/curl/curl.git

View File

@ -1,5 +1,6 @@
![license](https://img.shields.io/github/license/fedddddd/h2-mod.svg)
[![open bugs](https://img.shields.io/github/issues/fedddddd/h2-mod/bug?label=bugs)](https://github.com/fedddddd/h2-mod/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
[![Build](https://github.com/fedddddd/h2-mod/workflows/Build/badge.svg)](https://github.com/fedddddd/h2-mod/actions)
[![Build status](https://ci.appveyor.com/api/projects/status/0sh80kdnsvm53rno?svg=true)](https://ci.appveyor.com/project/fedddddd/h2-mod)
@ -14,7 +15,7 @@
**NOTE**: Cracked/Pirated versions of the game are NOT compatible with this mod, if you run such a version and have issues/crashes launching the client refer to [this issue](https://github.com/fedddddd/h2-mod/issues/111).
- **[Click here to get the latest release](https://ci.appveyor.com/api/projects/fedddddd/h2-mod/artifacts/build%2Fbin%2Fx64%2FRelease%2Fh2-mod.exe?branch=main&job=Environment%3A%20APPVEYOR_BUILD_WORKER_IMAGE%3DVisual%20Studio%202019%2C%20PREMAKE_ACTION%3Dvs2019%2C%20CI%3D1%3B%20Configuration%3A%20Release)**
- **[Click here to get the latest release](https://ci.appveyor.com/api/projects/fedddddd/h2-mod/artifacts/build%2Fbin%2Fx64%2FRelease%2Fh2-mod.exe?branch=develop&job=Environment%3A%20APPVEYOR_BUILD_WORKER_IMAGE%3DVisual%20Studio%202019%2C%20PREMAKE_ACTION%3Dvs2019%2C%20CI%3D1%3B%20Configuration%3A%20Release)**
- **You will need to drop this in your Call of Duty: Modern Warfare 2 Campaign Remastered installation folder. If you don't have Call of Duty: Modern Warfare 2 Campaign Remastered, get those game files first.**
## Compile from source

View File

@ -10,6 +10,7 @@ environment:
branches:
only:
- develop
- main
skip_branch_with_pr: true

View File

@ -0,0 +1,92 @@
local spacing = 10
local topoffset = 15
local extrawidth = 50
local extraheight = 40
LUI.MenuBuilder.m_types_build["SystemInfo"] = function (f6_arg0, f6_arg1)
local f6_local0 = LUI.MenuTemplate.spMenuOffset
local title = "LUA_MENU_SYSTEM_INFO_CAPS"
local f6_local2 = false
local f6_local3 = 0
local menu = LUI.MenuTemplate.new(f6_arg0, {
menu_title = title,
menu_top_indent = f6_local0 + f6_local3,
showSelectButton = false,
skipAnim = f6_local2
})
menu:setClass(LUI.SystemInfo)
menu:PopulateMissingProps(f6_arg1)
menu:ValidateProps(f6_arg1)
menu.id = "systemInfo_id"
local f6_local5 = 300
local f6_local6 = LUI.MenuTemplate.ListTop + f6_local0
local f6_local7 = f6_arg1.menu_height
if not f6_local7 then
f6_local7 = f6_local5
end
f6_local7 = f6_local7 + f6_local6 - extraheight
local f6_local9 = luiglobals.GenericMenuDims.OptionMenuWidth + 100
local f6_local10 = menu.properties
local topoffset2 = LUI.MenuTemplate.ListTop + LUI.MenuTemplate.spMenuOffset
local decobox = LUI.MenuBuilder.BuildRegisteredType("h1_box_deco", {
decoTopOffset = topoffset2 - topoffset + 15,
decoBottomOffset = -f6_local7,
decoRightOffset = -665 + extrawidth
})
menu:addElement(decobox)
local decoleft = CoD.CreateState(0, 0.5, 8, 0.5, CoD.AnchorTypes.TopLeft)
decoleft.color = luiglobals.Colors.h1.light_grey
decobox:addElement(LUI.UILine.new(decoleft))
local decoright = CoD.CreateState(0, 0.5, -8, 0.5, CoD.AnchorTypes.TopRight)
decoright.color = luiglobals.Colors.h1.light_grey
decobox:addElement(LUI.UILine.new(decoright))
local element = LUI.UIVerticalList.new({
leftAnchor = true,
rightAnchor = true,
topAnchor = true,
bottomAnchor = true,
left = spacing,
right = 100,
top = topoffset2 + 15,
bottom = 0,
spacing = spacing * 0.8
})
element.id = "systemInfoList_id"
menu.vlist = element
menu:addElement(element)
local optionmenuwidth = luiglobals.GenericMenuDims.OptionMenuWidth
luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth + extrawidth
menu:AddInfo(Engine.Localize("MENU_SYSINFO_VERSION"), function()
return Engine.GetBuildNumber()
end)
menu:AddInfo(Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_LINK"), function()
return Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_URL")
end)
menu:AddInfo(Engine.Localize("MENU_SYSINFO_DONATION_LINK"), function()
return Engine.Localize("MENU_SYSINFO_DONATION_URL")
end)
luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth
menu:AddBackButton()
menu:registerEventHandler("menu_close", LUI.SystemInfo.LeaveMenu)
return menu
end

View File

@ -0,0 +1 @@
require("loading")

View File

@ -18,41 +18,41 @@ function createdivider(menu, text)
menu.list:addElement(element)
end
local maincampaign = LUI.MenuBuilder.m_types_build["main_campaign"]
LUI.MenuBuilder.m_types_build["main_campaign"] = function(a1, a2)
local menu = maincampaign(a1, a2)
local buttonlist = menu:getChildById("main_campaign_list")
local button = menu:AddButton("$_MODS", function()
LUI.FlowManager.RequestAddMenu(nil, "mods_menu")
end, nil, true, nil, {
desc_text = "Open mods menu"
})
function string:truncate(length)
if (#self <= length) then
return self
end
buttonlist:removeElement(button)
buttonlist:insertElement(button, 6)
button.id = "mods_menu-button"
local hintbox = menu.optionTextInfo
local firstbutton = buttonlist:getFirstChild()
hintbox:dispatchEventToRoot({
name = "set_button_info_text",
text = firstbutton.properties.desc_text,
immediate = true
})
menu:CreateBottomDivider()
menu:AddBottomDividerToList(buttonlist:getLastChild())
menu:removeElement(menu.optionTextInfo)
LUI.Options.InitScrollingList(menu.list, nil)
menu:CreateBottomDivider()
menu.optionTextInfo = LUI.Options.AddOptionTextInfo(menu)
return menu
return self:sub(1, length - 3) .. "..."
end
function modsmenu(a1)
LUI.addmenubutton("main_campaign", {
index = 6,
text = "$_MODS",
description = "Load installed mods.",
callback = function()
LUI.FlowManager.RequestAddMenu(nil, "mods_menu")
end
})
function getmodname(path)
local name = path
local desc = "Load " .. name
local infofile = path .. "/info.json"
if (io.fileexists(infofile)) then
pcall(function()
local data = json.decode(io.readfile(infofile))
desc = string.format("%s\nAuthor: %s\nVersion: %s",
data.description, data.author, data.version)
name = data.name
end)
end
return name, desc
end
LUI.MenuBuilder.m_types_build["mods_menu"] = function(a1)
local menu = LUI.MenuTemplate.new(a1, {
menu_title = "$_MODS",
exclusiveController = 0,
@ -61,14 +61,22 @@ function modsmenu(a1)
showTopRightSmallBar = true
})
menu:AddButton("$_OPEN STORE", function()
if (LUI.MenuBuilder.m_types_build["mod_store_menu"]) then
LUI.FlowManager.RequestAddMenu(nil, "mod_store_menu")
end
end, nil, true, nil, {
desc_text = "Download and install mods."
})
local modfolder = game:getloadedmod()
if (modfolder ~= "") then
createdivider(menu, "$_Loaded mod: " .. modfolder)
createdivider(menu, "$_Loaded mod: ^3" .. getmodname(modfolder):truncate(20))
menu:AddButton("$_UNLOAD", function()
game:executecommand("unloadmod")
end, nil, true, nil, {
desc_text = "Unload the currently loaded mod"
desc_text = "Unload the currently loaded mod."
})
end
@ -77,16 +85,10 @@ function modsmenu(a1)
if (io.directoryexists("mods")) then
local mods = io.listfiles("mods/")
for i = 1, #mods do
local desc = "Load " .. mods[i]
local infofile = mods[i] .. "/mod.txt"
local exists = io.fileexists(infofile)
if (exists) then
desc = io.readfile(infofile)
end
local name, desc = getmodname(mods[i])
if (mods[i] ~= modfolder) then
menu:AddButton("$_" .. mods[i], function()
menu:AddButton("$_" .. name, function()
game:executecommand("loadmod " .. mods[i])
end, nil, true, nil, {
desc_text = desc
@ -94,7 +96,7 @@ function modsmenu(a1)
end
end
end
menu:AddBackButton(function(a1)
Engine.PlaySound(CoD.SFX.MenuBack)
LUI.FlowManager.RequestLeaveMenu(a1)
@ -107,8 +109,6 @@ function modsmenu(a1)
return menu
end
LUI.MenuBuilder.m_types_build["mods_menu"] = modsmenu
local localize = Engine.Localize
Engine.Localize = function(...)
local args = {...}

View File

View File

@ -0,0 +1,168 @@
function createdivider(menu, text)
local element = LUI.UIElement.new({
leftAnchor = true,
rightAnchor = true,
left = 0,
right = 0,
topAnchor = true,
bottomAnchor = false,
top = 0,
bottom = 33.33
})
element.scrollingToNext = true
element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", {
title_bar_text = Engine.ToUpperCase(Engine.Localize(text))
}))
menu.list:addElement(element)
end
LUI.addmenubutton("pc_controls", {
index = 4,
text = "$_GENERAL",
description = "Set the client's settings.",
callback = function()
LUI.FlowManager.RequestAddMenu(nil, "settings_menu")
end
})
LUI.MenuBuilder.m_types_build["settings_menu"] = function(a1)
local menu = LUI.MenuTemplate.new(a1, {
menu_title = "$_GENERAL",
menu_list_divider_top_offset = -(LUI.H1MenuTab.tabChangeHoldingElementHeight + luiglobals.H1MenuDims.spacing),
menu_width = luiglobals.GenericMenuDims.OptionMenuWidth
})
Engine.SetDvarFromString("ui_cg_autoUpdate", Engine.GetDvarBool("cg_autoUpdate") and "1" or "0")
Engine.SetDvarFromString("ui_cg_drawFps", Engine.GetDvarInt("cg_drawFps") .. "")
Engine.SetDvarFromString("ui_cg_speedGraph", Engine.GetDvarBool("cg_speedGraph") and "1" or "0")
Engine.SetDvarFromString("ui_cg_drawSpeed", Engine.GetDvarBool("cg_drawSpeed") and "1" or "0")
Engine.SetDvarFromString("ui_r_fullbright", Engine.GetDvarInt("r_fullbright") .. "")
createdivider(menu, "$_UPDATES")
LUI.Options.CreateOptionButton(
menu,
"ui_cg_autoUpdate",
"$_AUTOMATIC UPDATES",
"Enable or disable automatic updates on startup.",
{
{
text = "$_ENABLED",
value = "1"
},
{
text = "$_DISABLED",
value = "0"
}
}, nil, nil, function(value)
Engine.SetDvarBool("cg_autoUpdate", Engine.GetDvarString("ui_cg_autoUpdate") == "1")
end
)
menu:AddButton("$_CHECK FOR UPDATES", function()
LUI.tryupdating(false)
end, nil, true, nil, {
desc_text = "Check for updates."
})
createdivider(menu, "$_DRAWING")
LUI.Options.CreateOptionButton(
menu,
"ui_cg_drawFps",
"$_DRAW FPS",
"Enable or disable drawing fps or viewpos on screen.",
{
{
text = "$_DISABLED",
value = "0"
},
{
text = "$_FPS ONLY",
value = "1"
},
{
text = "$_FPS AND VIEWPOS",
value = "2"
}
}, nil, nil, function(value)
Engine.SetDvarInt("cg_drawFps", tonumber(Engine.GetDvarString("ui_cg_drawFps")))
end
)
LUI.Options.CreateOptionButton(
menu,
"ui_cg_drawSpeed",
"$_DRAW SPEED",
"Enable or disable drawing the player speed on screen.",
{
{
text = "$_DISABLED",
value = "0"
},
{
text = "$_ENABLED",
value = "1"
}
}, nil, nil, function(value)
Engine.SetDvarBool("cg_drawSpeed", Engine.GetDvarString("ui_cg_drawSpeed") == "1")
end
)
LUI.Options.CreateOptionButton(
menu,
"ui_cg_speedGraph",
"$_DRAW SPEED GRAPH",
"Enable or disable the speed graph.",
{
{
text = "$_DISABLED",
value = "0"
},
{
text = "$_ENABLED",
value = "1"
}
}, nil, nil, function(value)
Engine.SetDvarBool("cg_speedGraph", Engine.GetDvarString("ui_cg_speedGraph") == "1")
end
)
createdivider(menu, "$_RENDERING")
LUI.Options.CreateOptionButton(
menu,
"ui_r_fullbright",
"$_FULLBRIGHT",
"Change the fullbright mode.",
{
{
text = "$_DISABLED",
value = "0"
},
{
text = "$_ENABLED",
value = "1"
},
{
text = "$_MODE 2",
value = "2"
},
{
text = "$_MODE 3",
value = "3"
}
}, nil, nil, function(value)
Engine.SetDvarInt("r_fullbright", tonumber(Engine.GetDvarString("ui_r_fullbright")))
end
)
LUI.Options.InitScrollingList(menu.list, nil)
LUI.Options.AddOptionTextInfo(menu)
menu:AddBackButton()
return menu
end

2
deps/GSL vendored

@ -1 +1 @@
Subproject commit 99a29ce797c8337b8923f2688ba1489be6f65bc4
Subproject commit 4377f6e603c64a86c934f1546aa9db482f2e1a4e

1
deps/curl vendored Submodule

@ -0,0 +1 @@
Subproject commit 9f1d29ecacffe3e94349bcef6e9fafa62b1cc431

73
deps/premake/curl.lua vendored Normal file
View File

@ -0,0 +1,73 @@
curl = {
source = path.join(dependencies.basePath, "curl"),
}
function curl.import()
links { "curl" }
filter "toolset:msc*"
links { "Crypt32.lib" }
filter {}
curl.includes()
end
function curl.includes()
filter "toolset:msc*"
includedirs {
path.join(curl.source, "include"),
}
defines {
"CURL_STRICTER",
"CURL_STATICLIB",
"CURL_DISABLE_LDAP",
}
filter {}
end
function curl.project()
if not os.istarget("windows") then
return
end
project "curl"
language "C"
curl.includes()
includedirs {
path.join(curl.source, "lib"),
}
files {
path.join(curl.source, "lib/**.c"),
path.join(curl.source, "lib/**.h"),
}
defines {
"BUILDING_LIBCURL",
}
filter "toolset:msc*"
defines {
"USE_SCHANNEL",
"USE_WINDOWS_SSPI",
"USE_THREADS_WIN32",
}
filter "toolset:not msc*"
defines {
"USE_GNUTLS",
"USE_THREADS_POSIX",
}
filter {}
warnings "Off"
kind "StaticLib"
end
table.insert(dependencies, curl)

2
deps/protobuf vendored

@ -1 +1 @@
Subproject commit b48ba578dd01adfebeb4fac0887db1eeb163e00f
Subproject commit 3ea30d80847cd9561db570ae7f673afc15523545

View File

@ -5,12 +5,24 @@
#include "command.hpp"
#include "game/game.hpp"
#include "game/ui_scripting/execution.hpp"
#include <utils/hook.hpp>
#include <utils/string.hpp>
#include <version.h>
namespace branding
{
float color[4] = {0.9f, 0.9f, 0.5f, 1.f};
namespace
{
float color[4] = {0.9f, 0.9f, 0.5f, 1.f};
int get_build_number_stub(game::hks::lua_State* s)
{
ui_scripting::push_value(VERSION);
return 1;
}
}
void draw()
{
@ -26,6 +38,12 @@ namespace branding
void post_unpack() override
{
localized_strings::override("MENU_SP_CAMPAIGN", "H2-MOD");
localized_strings::override("MENU_SYSINFO_CUSTOMER_SUPPORT_LINK", "Github Page:");
localized_strings::override("MENU_SYSINFO_CUSTOMER_SUPPORT_URL", "https://github.com/fedddddd/h2-mod");
localized_strings::override("MENU_SYSINFO_DONATION_LINK", "Donation Link:");
localized_strings::override("MENU_SYSINFO_DONATION_URL", "https://paypal.me/fedecek");
utils::hook::jump(0x33D550_b, get_build_number_stub, true);
}
};
}

View File

@ -0,0 +1,24 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "game/dvars.hpp"
#include "command.hpp"
#include "game_console.hpp"
#include <utils/hook.hpp>
namespace config
{
class component final : public component_interface
{
public:
void post_unpack() override
{
dvars::register_bool("cg_autoUpdate", true, game::DvarFlags::DVAR_FLAG_SAVED);
}
};
}
REGISTER_COMPONENT(config::component)

View File

@ -378,7 +378,7 @@ namespace game_console
void print(const int type, const char* fmt, ...)
{
char va_buffer[0x200] = { 0 };
char va_buffer[2048] = {0};
va_list ap;
va_start(ap, fmt);

View File

@ -0,0 +1,153 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "game_console.hpp"
#include "game/dvars.hpp"
#include <utils/hook.hpp>
namespace logger
{
namespace
{
utils::hook::detour com_error_hook;
utils::hook::detour nullsub_48_hook;
utils::hook::detour sub_32AEF0;
void print_error(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_error, buffer);
}
void print_com_error(int, const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_error, buffer);
}
void com_error_stub(const int error, const char* msg, ...)
{
char buffer[2048];
{
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_error, "Error: %s\n", buffer);
}
com_error_hook.invoke<void>(error, "%s", buffer);
}
void print_warning(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_warning, buffer);
}
void print(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_info, buffer);
}
void print_dev(const char* msg, ...)
{
static auto* enabled = dvars::register_bool("logger_dev", false, game::DVAR_FLAG_SAVED);
if (!enabled->current.enabled)
{
return;
}
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
game_console::print(game_console::con_type_info, buffer);
}
void lui_print(const char* msg, ...)
{
char buffer[2048];
va_list ap;
va_start(ap, msg);
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, msg, ap);
va_end(ap);
if (strstr(msg, "LUI WARNING:"))
{
game_console::print(game_console::con_type_warning, buffer);
}
else
{
static auto* enabled = dvars::register_bool("logger_dev", false, game::DVAR_FLAG_SAVED);
if (!enabled->current.enabled)
{
return;
}
game_console::print(game_console::con_type_info, buffer);
}
}
}
class component final : public component_interface
{
public:
void post_unpack() override
{
utils::hook::jump(0x32C620_b, print_warning, true);
utils::hook::jump(0x32C630_b, print_warning, true);
utils::hook::jump(0x32AEF0_b, lui_print, true);
com_error_hook.create(0x5A2D80_b, com_error_stub);
}
};
}
REGISTER_COMPONENT(logger::component)

View File

@ -60,7 +60,7 @@ namespace ui_scripting
void hksi_lual_error_stub(game::hks::lua_State* s, const char* fmt, ...)
{
char va_buffer[0x200] = { 0 };
char va_buffer[2048] = {0};
va_list ap;
va_start(ap, fmt);
@ -200,7 +200,11 @@ namespace ui_scripting
{
scheduler::loop([]()
{
ui_scripting::lua::engine::run_frame();
if (game::Sys_IsMainThread())
{
ui_scripting::lua::engine::run_frame();
}
fps::draw();
branding::draw();
game_console::draw_console();

View File

@ -131,6 +131,7 @@ namespace game
WEAK symbol<void()> Sys_ShowConsole{0x633080};
WEAK symbol<bool()> Sys_IsDatabaseReady2{0x5A9FE0};
WEAK symbol<int()> Sys_Milliseconds{0x650720};
WEAK symbol<bool()> Sys_IsMainThread{0x5AA020};
WEAK symbol<const char*(const char* string)> UI_SafeTranslateString{0x5A2930};
WEAK symbol<int(int localClientNum, const char* sound)> UI_PlayLocalSoundAlias{0x606080};

View File

@ -17,6 +17,9 @@
#include <utils/string.hpp>
#include <utils/nt.hpp>
#include <utils/io.hpp>
#include <utils/http.hpp>
#include <utils/cryptography.hpp>
#include <version.h>
namespace ui_scripting::lua
{
@ -28,6 +31,7 @@ namespace ui_scripting::lua
namespace
{
const auto animation_script = utils::nt::load_resource(LUA_ANIMATION_SCRIPT);
const auto json_script = utils::nt::load_resource(LUA_JSON_SCRIPT);
scripting::script_value script_convert(const sol::lua_value& value)
{
@ -85,15 +89,25 @@ namespace ui_scripting::lua
{
state["io"]["fileexists"] = utils::io::file_exists;
state["io"]["writefile"] = utils::io::write_file;
state["io"]["movefile"] = utils::io::move_file;
state["io"]["filesize"] = utils::io::file_size;
state["io"]["createdirectory"] = utils::io::create_directory;
state["io"]["directoryexists"] = utils::io::directory_exists;
state["io"]["directoryisempty"] = utils::io::directory_is_empty;
state["io"]["listfiles"] = utils::io::list_files;
state["io"]["copyfolder"] = utils::io::copy_folder;
state["io"]["removefile"] = utils::io::remove_file;
state["io"]["removedirectory"] = utils::io::remove_directory;
state["io"]["readfile"] = static_cast<std::string(*)(const std::string&)>(utils::io::read_file);
}
void setup_json(sol::state& state)
{
const auto json = state.safe_script(json_script, &sol::script_pass_on_error);
handle_error(json);
state["json"] = json;
}
void setup_vector_type(sol::state& state)
{
auto vector_type = state.new_usertype<scripting::vector>("vector", sol::constructors<scripting::vector(float, float, float)>());
@ -1081,7 +1095,7 @@ namespace ui_scripting::lua
const auto alternate = name.starts_with("alt_");
const auto weapon = ::game::G_GetWeaponForName(name.data());
char buffer[0x400];
char buffer[0x400] = {0};
::game::CG_GetWeaponDisplayName(weapon, alternate, buffer, 0x400);
return std::string(buffer);
@ -1092,6 +1106,94 @@ namespace ui_scripting::lua
return ::game::mod_folder;
};
static int request_id{};
game_type["httpget"] = [](const game&, const std::string& url)
{
const auto id = request_id++;
::scheduler::once([url, id]()
{
const auto result = utils::http::get_data(url);
::scheduler::once([result, id]
{
event event;
event.element = &ui_element;
event.name = "http_request_done";
if (result.has_value())
{
event.arguments = {id, true, result.value()};
}
else
{
event.arguments = {id, false};
}
notify(event);
}, ::scheduler::pipeline::renderer);
}, ::scheduler::pipeline::async);
return id;
};
game_type["httpgettofile"] = [](const game&, const std::string& url,
const std::string& dest)
{
const auto id = request_id++;
::scheduler::once([url, id, dest]()
{
const auto result = utils::http::get_data(url);
::scheduler::once([result, id, dest]
{
event event;
event.element = &ui_element;
event.name = "http_request_done";
if (result.has_value())
{
const auto write = utils::io::write_file(dest, result.value(), false);
event.arguments = {id, true, write};
}
else
{
event.arguments = {id, false};
}
notify(event);
}, ::scheduler::pipeline::renderer);
}, ::scheduler::pipeline::async);
return id;
};
game_type["sha"] = [](const game&, const std::string& data)
{
return utils::string::to_upper(utils::cryptography::sha1::compute(data, true));
};
game_type["environment"] = [](const game&)
{
return GIT_BRANCH;
};
game_type["binaryname"] = [](const game&)
{
utils::nt::library self;
return self.get_name();
};
game_type["relaunch"] = [](const game&)
{
utils::nt::relaunch_self("-singleplayer");
utils::nt::terminate();
};
game_type["isdebugbuild"] = [](const game&)
{
#ifdef DEBUG
return true;
#else
return false;
#endif
};
struct player
{
};
@ -1215,12 +1317,6 @@ namespace ui_scripting::lua
table.set(name, convert({s, value}));
};
state["luiglobals"] = table((*::game::hks::lua_state)->globals.v.table);
state["CoD"] = state["luiglobals"]["CoD"];
state["LUI"] = state["luiglobals"]["LUI"];
state["Engine"] = state["luiglobals"]["Engine"];
state["Game"] = state["luiglobals"]["Game"];
auto function_type = state.new_usertype<function>("function_");
function_type[sol::meta_function::call] = [](const function& function, const sol::this_state s, sol::variadic_args va)
@ -1243,6 +1339,12 @@ namespace ui_scripting::lua
return sol::as_returns(returns);
};
state["luiglobals"] = table((*::game::hks::lua_state)->globals.v.table);
state["CoD"] = state["luiglobals"]["CoD"];
state["LUI"] = state["luiglobals"]["LUI"];
state["Engine"] = state["luiglobals"]["Engine"];
state["Game"] = state["luiglobals"]["Game"];
state.script(animation_script);
}
}
@ -1261,6 +1363,7 @@ namespace ui_scripting::lua
sol::lib::table);
setup_io(this->state_);
setup_json(this->state_);
setup_vector_type(this->state_);
setup_element_type(this->state_, this->event_handler_, this->scheduler_);
setup_menu_type(this->state_, this->event_handler_, this->scheduler_);

View File

@ -29,7 +29,7 @@ namespace ui_scripting::lua
class context
{
public:
context(std::string data, script_type);
context(std::string data, script_type type);
~context();
context(context&&) noexcept = delete;

View File

@ -13,7 +13,7 @@ namespace ui_scripting::lua::engine
{
namespace
{
const auto mods_menu_script = utils::nt::load_resource(LUI_MODS_MENU);
const auto updater_script = utils::nt::load_resource(LUI_UPDATER_MENU);
float screen_max[2];
@ -413,7 +413,7 @@ namespace ui_scripting::lua::engine
get_scripts().clear();
clear_menus();
load_code(mods_menu_script);
load_code(updater_script);
load_scripts("ui_scripts/");
load_scripts("h2-mod/ui_scripts/");

View File

@ -62,14 +62,27 @@ namespace ui_scripting
const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
game::hks::hksi_lua_pushlstring(state, value, (unsigned int)strlen(value));
game::hks::hksi_lua_pushlstring(state, value, static_cast<unsigned int>(strlen(value)));
obj = state->m_apistack.top[-1];
this->value_ = obj;
}
script_value::script_value(const char* value, unsigned int len)
{
game::hks::HksObject obj{};
const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
game::hks::hksi_lua_pushlstring(state, value, len);
obj = state->m_apistack.top[-1];
this->value_ = obj;
}
script_value::script_value(const std::string& value)
: script_value(value.data())
: script_value(value.data(), static_cast<unsigned int>(value.size()))
{
}

View File

@ -22,6 +22,7 @@ namespace ui_scripting
script_value(double value);
script_value(const char* value);
script_value(const char* value, unsigned int len);
script_value(const std::string& value);
script_value(const lightuserdata& value);

View File

@ -22,4 +22,5 @@
#define ICON_IMAGE 313
#define LUA_ANIMATION_SCRIPT 314
#define LUI_MODS_MENU 315
#define LUA_JSON_SCRIPT 315
#define LUI_UPDATER_MENU 316

View File

@ -98,7 +98,9 @@ ID_ICON ICON "resources/icon.ico"
MENU_MAIN RCDATA "resources/main.html"
LUA_ANIMATION_SCRIPT RCDATA "resources/animation.lua"
LUI_MODS_MENU RCDATA "resources/mods_menu.lua"
LUA_JSON_SCRIPT RCDATA "resources/json.lua"
LUI_UPDATER_MENU RCDATA "resources/updater.lua"
#ifdef _DEBUG
TLS_DLL RCDATA "../../build/bin/x64/Debug/tlsdll.dll"

View File

@ -0,0 +1,388 @@
--
-- json.lua
--
-- Copyright (c) 2020 rxi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
local json = { _version = "0.1.2" }
-------------------------------------------------------------------------------
-- Encode
-------------------------------------------------------------------------------
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
-- Circular reference?
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
-- Empty / end of array?
if str:sub(i, i) == "]" then
i = i + 1
break
end
-- Read token
x, i = parse(str, i)
res[n] = x
n = n + 1
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
-- Empty / end of object?
if str:sub(i, i) == "}" then
i = i + 1
break
end
-- Read key
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
-- Read ':' delimiter
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
-- Read value
val, i = parse(str, i)
-- Set
res[key] = val
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json

View File

@ -0,0 +1,411 @@
menucallbacks = {}
originalmenus = {}
LUI.onmenuopen = function(name, callback)
if (not LUI.MenuBuilder.m_types_build[name]) then
return
end
if (not menucallbacks[name]) then
menucallbacks[name] = {}
end
table.insert(menucallbacks[name], callback)
if (not originalmenus[name]) then
originalmenus[name] = LUI.MenuBuilder.m_types_build[name]
LUI.MenuBuilder.m_types_build[name] = function(...)
local args = {...}
local menu = originalmenus[name](table.unpack(args))
for k, v in luiglobals.next, menucallbacks[name] do
v(menu, table.unpack(args))
end
return menu
end
end
end
local addoptionstextinfo = LUI.Options.AddOptionTextInfo
LUI.Options.AddOptionTextInfo = function(menu)
local result = addoptionstextinfo(menu)
menu.optionTextInfo = result
return result
end
LUI.addmenubutton = function(name, data)
LUI.onmenuopen(name, function(menu)
if (not menu.list) then
return
end
local button = menu:AddButton(data.text, data.callback, nil, true, nil, {
desc_text = data.description
})
local buttonlist = menu:getChildById(menu.type .. "_list")
if (data.id) then
button.id = data.id
end
if (data.index) then
buttonlist:removeElement(button)
buttonlist:insertElement(button, data.index)
end
local hintbox = menu.optionTextInfo
menu:removeElement(hintbox)
LUI.Options.InitScrollingList(menu.list, nil)
menu.optionTextInfo = LUI.Options.AddOptionTextInfo(menu)
end)
end
stack = {}
LUI.openmenu = function(menu, args)
stack = args
LUI.FlowManager.RequestAddMenu(nil, menu)
return stack.ret
end
LUI.openpopupmenu = function(menu, args)
stack = args
LUI.FlowManager.RequestPopupMenu(nil, menu)
return stack.ret
end
LUI.onmenuopen("main_lockout", function()
if (game:isdebugbuild() or not Engine.GetDvarBool("cg_autoUpdate")) then
return
end
if (game:sharedget("has_tried_updating") == "") then
game:ontimeout(function()
game:sharedset("has_tried_updating", "1")
tryupdate(true)
end, 0)
end
end)
stack = {}
LUI.MenuBuilder.m_types_build["generic_waiting_popup_"] = function (menu, event)
local oncancel = stack.oncancel
local popup = LUI.MenuBuilder.BuildRegisteredType("waiting_popup", {
message_text = stack.text,
isLiveWithCancel = true,
cancel_func = function(...)
local args = {...}
oncancel()
LUI.common_menus.CommonPopups.CancelCSSDownload(table.unpack(args))
end
})
stack = {
ret = popup
}
return popup
end
LUI.MenuBuilder.m_types_build["generic_yes_no_popup_"] = function()
local callback = stack.callback
local popup = LUI.MenuBuilder.BuildRegisteredType("generic_yesno_popup", {
popup_title = stack.title,
message_text = stack.text,
yes_action = function()
callback(true)
end,
no_action = function()
callback(false)
end
})
stack = {
ret = popup
}
return popup
end
LUI.MenuBuilder.m_types_build["generic_confirmation_popup_"] = function()
local popup = LUI.MenuBuilder.BuildRegisteredType( "generic_confirmation_popup", {
cancel_will_close = false,
popup_title = stack.title,
message_text = stack.text,
button_text = stack.buttontext,
confirmation_action = stack.callback
})
stack = {
ret = popup
}
return stack.ret
end
LUI.yesnopopup = function(data)
for k, v in luiglobals.next, data do
stack[k] = v
end
LUI.FlowManager.RequestPopupMenu(nil, "generic_yes_no_popup_")
return stack.ret
end
LUI.confirmationpopup = function(data)
for k, v in luiglobals.next, data do
stack[k] = v
end
LUI.FlowManager.RequestPopupMenu(nil, "generic_confirmation_popup_")
return stack.ret
end
function updaterpopup(oncancel)
return LUI.openpopupmenu("generic_waiting_popup_", {
oncancel = oncancel,
withcancel = true,
text = "Checking for updates..."
})
end
function deleteoldfile()
io.removefile(game:binaryname() .. ".old")
end
deleteoldfile()
function verifyfiles(files)
local needed = {}
local updatebinary = false
if (game:isdebugbuild()) then
return needed, updatebinary
end
local binaryname = game:binaryname()
for i = 1, #files do
local name = files[i][1]
if (io.fileexists(name) and game:sha(io.readfile(name)) == files[i][3]) then
goto continue
end
if (name == binaryname) then
updatebinary = true
end
table.insert(needed, files[i])
::continue::
end
return needed, updatebinary
end
local canceled = false
function downloadfiles(popup, files, callback)
deleteoldfile()
local text = popup:getchildren()[7]
local folder = game:environment() == "develop" and "data-dev" or "data"
if (#files == 0) then
callback(true)
return
end
local total = 0
local filedownloaded = function()
total = total + 1
if (total == #files) then
callback(true)
end
end
local download = nil
local stop = false
download = function(index)
if (canceled or stop) then
return
end
local filename = files[index][1]
local url = "https://master.fed0001.xyz/" .. folder .. "/" .. filename .. "?" .. os.time()
text:setText(string.format("Downloading file [%i/%i]\n%s", index, #files, filename))
if (filename == game:binaryname()) then
io.movefile(filename, filename .. ".old")
end
httprequesttofile(url, filename, function(valid, success)
if (not valid) then
callback(false, "Invalid server response")
stop = true
return
end
if (not success) then
callback(false, "Failed to write file " .. filename)
stop = true
return
end
filedownloaded()
if (files[index + 1]) then
download(index + 1)
else
callback(true)
end
end)
end
download(1)
end
function userdata_:getchildren()
local children = {}
local first = self:getFirstChild()
while (first) do
table.insert(children, first)
first = first:getNextSibling()
end
return children
end
function tryupdate(autoclose)
canceled = false
local popup = updaterpopup(function()
canceled = true
end)
local text = popup:getchildren()[7]
local file = game:environment() == "develop" and "files-dev.json" or "files.json"
local url = "https://master.fed0001.xyz/" .. file .. "?" .. os.time()
httprequest(url, function(valid, data)
if (not valid) then
text:setText("Update check failed: Invalid server response")
return
end
local valid = pcall(function()
local files = json.decode(data)
local needed, updatebinary = verifyfiles(files)
if (#needed == 0) then
text:setText("No updates available")
if (autoclose) then
LUI.common_menus.CommonPopups.CancelCSSDownload(popup, {})
end
return
end
local download = function()
local gotresult = false
downloadfiles(popup, needed, function(result, error)
if (gotresult or canceled) then
return
end
gotresult = true
if (not result) then
text:setText("Update failed: " .. error)
return
end
if (updatebinary) then
LUI.confirmationpopup({
title = "RESTART REQUIRED",
text = "Update requires restart",
buttontext = "RESTART",
callback = function()
game:relaunch()
end
})
return
end
if (result and #needed > 0) then
game:executecommand("lui_restart")
return
end
text:setText("Update successful!")
if (autoclose) then
LUI.common_menus.CommonPopups.CancelCSSDownload(popup, {})
end
end)
end
if (autoclose) then
download()
else
LUI.yesnopopup({
title = "NOTICE",
text = "An update is available, proceed with installation?",
callback = function(result)
if (not result) then
LUI.common_menus.CommonPopups.CancelCSSDownload(popup, {})
else
download()
end
end
})
end
end)
if (not valid) then
text:setText("Update failed: unknown error")
end
end)
end
LUI.tryupdating = function(autoclose)
tryupdate(autoclose)
end
function httprequest(url, callback)
local request = game:httpget(url)
local listener = nil
listener = game:onnotify("http_request_done", function(id, valid, data)
if (id ~= request) then
return
end
listener:clear()
callback(valid, data)
end)
end
function httprequesttofile(url, dest, callback)
local request = game:httpgettofile(url, dest)
local listener = nil
listener = game:onnotify("http_request_done", function(id, valid, success)
if (id ~= request) then
return
end
listener:clear()
callback(valid, success)
end)
end
local localize = Engine.Localize
Engine.Localize = function(...)
local args = {...}
if (type(args[1]) == "string" and args[1]:sub(1, 2) == "$_") then
return args[1]:sub(3, -1)
end
return localize(table.unpack(args))
end

View File

@ -1,48 +1,100 @@
#include "http.hpp"
#include "nt.hpp"
#include <atlcomcli.h>
#include <curl/curl.h>
#include <gsl/gsl>
#pragma comment(lib, "ws2_32.lib")
namespace utils::http
{
std::optional<std::string> get_data(const std::string& url)
namespace
{
CComPtr<IStream> stream;
if (FAILED(URLOpenBlockingStreamA(nullptr, url.data(), &stream, 0, nullptr)))
struct progress_helper
{
return {};
}
const std::function<void(size_t)>* callback{};
std::exception_ptr exception{};
};
char buffer[0x1000];
std::string result;
HRESULT status{};
do
int progress_callback(void *clientp, const curl_off_t /*dltotal*/, const curl_off_t dlnow, const curl_off_t /*ultotal*/, const curl_off_t /*ulnow*/)
{
DWORD bytes_read = 0;
status = stream->Read(buffer, sizeof(buffer), &bytes_read);
auto* helper = static_cast<progress_helper*>(clientp);
if (bytes_read > 0)
try
{
result.append(buffer, bytes_read);
if (*helper->callback)
{
(*helper->callback)(dlnow);
}
}
catch(...)
{
helper->exception = std::current_exception();
return -1;
}
}
while (SUCCEEDED(status) && status != S_FALSE);
if (FAILED(status))
return 0;
}
size_t write_callback(void* contents, const size_t size, const size_t nmemb, void* userp)
{
return {};
}
auto* buffer = static_cast<std::string*>(userp);
return {result};
const auto total_size = size * nmemb;
buffer->append(static_cast<char*>(contents), total_size);
return total_size;
}
}
std::future<std::optional<std::string>> get_data_async(const std::string& url)
std::optional<std::string> get_data(const std::string& url, const headers& headers, const std::function<void(size_t)>& callback)
{
return std::async(std::launch::async, [url]()
curl_slist* header_list = nullptr;
auto* curl = curl_easy_init();
if (!curl)
{
return get_data(url);
return {};
}
auto _ = gsl::finally([&]()
{
curl_slist_free_all(header_list);
curl_easy_cleanup(curl);
});
for(const auto& header : headers)
{
auto data = header.first + ": " + header.second;
header_list = curl_slist_append(header_list, data.data());
}
std::string buffer{};
progress_helper helper{};
helper.callback = &callback;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_URL, url.data());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &helper);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
if (curl_easy_perform(curl) == CURLE_OK)
{
return {std::move(buffer)};
}
if(helper.exception)
{
std::rethrow_exception(helper.exception);
}
return {};
}
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers)
{
return std::async(std::launch::async, [url, headers]()
{
return get_data(url, headers);
});
}
}

View File

@ -6,6 +6,8 @@
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);
using headers = std::unordered_map<std::string, std::string>;
std::optional<std::string> get_data(const std::string& url, const headers& headers = {}, const std::function<void(size_t)>& callback = {});
std::future<std::optional<std::string>> get_data_async(const std::string& url, const headers& headers = {});
}

View File

@ -104,6 +104,11 @@ namespace utils::io
return std::filesystem::is_empty(directory);
}
bool remove_directory(const std::string& directory)
{
return std::filesystem::remove_all(directory);
}
std::vector<std::string> list_files(const std::string& directory)
{
std::vector<std::string> files;

View File

@ -16,6 +16,7 @@ namespace utils::io
bool create_directory(const std::string& directory);
bool directory_exists(const std::string& directory);
bool directory_is_empty(const std::string& directory);
bool remove_directory(const std::string& directory);
std::vector<std::string> list_files(const std::string& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);
}

View File

@ -1,4 +1,5 @@
#include "nt.hpp"
#include "string.hpp"
namespace utils::nt
{
@ -225,7 +226,7 @@ namespace utils::nt
return std::string(LPSTR(LockResource(handle)), SizeofResource(nullptr, res));
}
void relaunch_self()
void relaunch_self(const std::string& extra_command_line)
{
const utils::nt::library self;
@ -238,9 +239,14 @@ namespace utils::nt
char current_dir[MAX_PATH];
GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir,
std::string command_line = GetCommandLineA();
if (!extra_command_line.empty())
{
command_line += " " + extra_command_line;
}
CreateProcessA(self.get_path().data(), command_line.data(), nullptr, nullptr, false, NULL, nullptr, current_dir,
&startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread);

View File

@ -105,6 +105,6 @@ namespace utils::nt
__declspec(noreturn) void raise_hard_exception();
std::string load_resource(int id);
void relaunch_self();
void relaunch_self(const std::string& extra_command_line = "");
__declspec(noreturn) void terminate(uint32_t code = 0);
}