Merge pull request #420 from h1-mod/confirmation-v2

This commit is contained in:
fed 2022-12-22 14:49:09 +00:00 committed by GitHub
commit f2fdc3f8ca
10 changed files with 148 additions and 19 deletions

View File

@ -15,7 +15,6 @@ Engine.GetLuiRoot():registerEventHandler("mod_download_start", function(element,
end)
popup:registerEventHandler("mod_download_progress", function(element, event)
print(event.fraction * 100)
popup.text:setText(string.format("Downloading %s (%i%%)...", file, math.floor(event.fraction * 100)))
end)

View File

@ -4,3 +4,4 @@ end
require("lobby")
require("serverlist")
require("confirm")

View File

@ -0,0 +1,12 @@
LUI.MenuBuilder.m_types_build["popup_confirmdownload"] = function()
return LUI.MenuBuilder.BuildRegisteredType("generic_yesno_popup", {
popup_title = Engine.Localize("@MENU_NOTICE"),
message_text = Engine.Localize("@LUA_MENU_3RD_PARTY_CONTENT_DESC", download.getwwwurl()),
yes_action = function()
download.userdownloadresponse(true)
end,
no_action = function()
download.userdownloadresponse(false)
end
})
end

View File

@ -62,6 +62,8 @@
"MPHUD_LATENCY_MS": " ms",
"LUA_MENU_TELEMETRY": "TELEMETRY",
"LUA_MENU_3RD_PARTY_CONTENT_DESC": "Would you like to install required 3rd-party content for this server? (from &&1)",
"MENU_ENGLISH": "English",
"MENU_ENGLISH_SAFE": "English (Safe)",
"MENU_FRENCH": "Français",

View File

@ -11,9 +11,11 @@
#include "fastfiles.hpp"
#include "game/game.hpp"
#include "game/ui_scripting/execution.hpp"
#include "steam/steam.hpp"
#include <utils/properties.hpp>
#include <utils/string.hpp>
#include <utils/info_string.hpp>
#include <utils/cryptography.hpp>
@ -48,6 +50,12 @@ namespace party
{".arena", "usermaparenahash", true},
};
struct
{
game::netadr_s host{};
utils::info_string info_string{};
} saved_info_response;
void perform_game_initialization()
{
command::execute("onlinegame 1", true);
@ -297,9 +305,80 @@ namespace party
return false;
}
void close_joining_popups()
{
if (game::Menu_IsMenuOpenAndVisible(0, "popup_acceptinginvite"))
{
command::execute("lui_close popup_acceptinginvite", false);
}
if (game::Menu_IsMenuOpenAndVisible(0, "generic_waiting_popup_"))
{
command::execute("lui_close generic_waiting_popup_", false);
}
}
std::string get_whitelist_json_path()
{
return (utils::properties::get_appdata_path() / "whitelist.json").generic_string();
}
nlohmann::json get_whitelist_json_object()
{
std::string data;
if (!utils::io::read_file(get_whitelist_json_path(), &data))
{
return nullptr;
}
nlohmann::json obj;
try
{
obj = nlohmann::json::parse(data.data());
}
catch (const nlohmann::json::parse_error& ex)
{
menu_error(utils::string::va("%s\n", ex.what()));
return nullptr;
}
return obj;
}
std::string target_ip_to_string(const game::netadr_s& target)
{
return utils::string::va("%i.%i.%i.%i",
static_cast<int>(saved_info_response.host.ip[0]),
static_cast<int>(saved_info_response.host.ip[1]),
static_cast<int>(saved_info_response.host.ip[2]),
static_cast<int>(saved_info_response.host.ip[3]));
}
bool download_files(const game::netadr_s& target, const utils::info_string& info, bool allow_download);
bool should_user_confirm(const game::netadr_s& target)
{
nlohmann::json obj = get_whitelist_json_object();
if (obj != nullptr)
{
const auto target_ip = target_ip_to_string(target);
for (const auto& [key, value] : obj.items())
{
if (value.is_string() && value.get<std::string>() == target_ip)
{
return false;
}
}
}
close_joining_popups();
command::execute("lui_open_popup popup_confirmdownload", false);
return true;
}
bool needs_vid_restart = false;
bool download_files(const game::netadr_s& target, const utils::info_string& info)
bool download_files(const game::netadr_s& target, const utils::info_string& info, bool allow_download)
{
try
{
@ -311,6 +390,11 @@ namespace party
if (files.size() > 0)
{
if (!allow_download && should_user_confirm(target))
{
return true;
}
download::stop_download();
download::start_download(target, info, files);
return true;
@ -437,18 +521,36 @@ namespace party
}
}
std::string get_www_url()
{
return saved_info_response.info_string.get("sv_wwwBaseUrl");
}
void user_download_response(bool response)
{
if (!response)
{
return;
}
nlohmann::json obj = get_whitelist_json_object();
if (obj == nullptr)
{
obj = {};
}
obj.push_back(target_ip_to_string(saved_info_response.host));
utils::io::write_file(get_whitelist_json_path(), obj.dump(4));
download_files(saved_info_response.host, saved_info_response.info_string, true);
}
void menu_error(const std::string& error)
{
console::error("%s\n", error.data());
if (game::Menu_IsMenuOpenAndVisible(0, "popup_acceptinginvite"))
{
command::execute("lui_close popup_acceptinginvite", false);
}
if (game::Menu_IsMenuOpenAndVisible(0, "generic_waiting_popup_"))
{
command::execute("lui_close generic_waiting_popup_", false);
}
close_joining_popups();
utils::hook::invoke<void>(0x17D770_b, error.data(), "MENU_NOTICE"); // Com_SetLocalizedErrorMessage
*reinterpret_cast<int*>(0x2ED2F78_b) = 1;
@ -930,6 +1032,10 @@ namespace party
return;
}
saved_info_response = {};
saved_info_response.host = target;
saved_info_response.info_string = info;
if (info.get("challenge") != connect_state.challenge)
{
menu_error("Connection failed: Invalid challenge.");
@ -971,7 +1077,7 @@ namespace party
return;
}
if (download_files(target, info))
if (download_files(target, info, false))
{
return;
}

View File

@ -3,6 +3,9 @@
namespace party
{
std::string get_www_url();
void user_download_response(bool response);
void menu_error(const std::string& error);
void reset_connect_state();

View File

@ -19,6 +19,7 @@
#include "scripting.hpp"
#include "updater.hpp"
#include "server_list.hpp"
#include "party.hpp"
#include "game/ui_scripting/execution.hpp"
#include "game/scripting/execution.hpp"
@ -69,12 +70,6 @@ namespace ui_scripting
return itr == globals.loaded_scripts.end() ? std::string() : itr->second;
}
table get_globals()
{
const auto state = *game::hks::lua_state;
return state->globals.v.table;
}
void print_error(const std::string& error)
{
console::error("************** LUI script execution error **************\n");
@ -369,6 +364,9 @@ namespace ui_scripting
lua["download"] = download_table;
download_table["abort"] = download::stop_download;
download_table["userdownloadresponse"] = party::user_download_response;
download_table["getwwwurl"] = party::get_www_url;
}
void start()
@ -523,6 +521,12 @@ namespace ui_scripting
}
}
table get_globals()
{
const auto state = *game::hks::lua_state;
return state->globals.v.table;
}
template <typename F>
game::hks::cclosure* convert_function(F f)
{

View File

@ -42,6 +42,8 @@ namespace ui_scripting
return wrap_function(std::function(f));
}
table get_globals();
template <typename F>
game::hks::cclosure* convert_function(F f);

View File

@ -111,8 +111,7 @@ namespace ui_scripting
try
{
const auto globals = table((*::game::hks::lua_state)->globals.v.table);
const auto engine = globals.get("Engine").as<table>();
const auto engine = get_globals().get("Engine").as<table>();
const auto root = engine.get("GetLuiRoot")()[0].as<userdata>();
const auto process_event = root.get("processEvent");

View File

@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <filesystem>
#include <json.hpp>
namespace utils::io
{