From db742f84454a8a963ca41812b3f4ffaffc91d0eb Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Thu, 10 Mar 2022 01:30:03 +0100 Subject: [PATCH] Add stats menu --- data/ui_scripts/stats/__init__.lua | 226 ++++++++++++++++++++++++++ src/client/component/patches.cpp | 10 -- src/client/component/stats.cpp | 75 +++++++++ src/client/component/ui_scripting.cpp | 37 +++++ 4 files changed, 338 insertions(+), 10 deletions(-) create mode 100644 data/ui_scripts/stats/__init__.lua create mode 100644 src/client/component/stats.cpp diff --git a/data/ui_scripts/stats/__init__.lua b/data/ui_scripts/stats/__init__.lua new file mode 100644 index 00000000..0aedc62a --- /dev/null +++ b/data/ui_scripts/stats/__init__.lua @@ -0,0 +1,226 @@ +if (game:issingleplayer()) then + return +end + +game:addlocalizedstring("LUA_MENU_STATS", "Stats") +game:addlocalizedstring("LUA_MENU_STATS_DESC", "Edit player stats settings.") + +game:addlocalizedstring("LUA_MENU_UNLOCKALL_ITEMS", "Unlock all items") +game:addlocalizedstring("LUA_MENU_UNLOCKALL_ITEMS_DESC", "Whether items should be locked based on the player's stats or always unlocked.") + +game:addlocalizedstring("LUA_MENU_UNLOCKALL_CLASSES", "Unlock all classes") +game:addlocalizedstring("LUA_MENU_UNLOCKALL_CLASSES_DESC", "Whether classes should be locked based on the player's stats or always unlocked.") + +game:addlocalizedstring("LUA_MENU_PRESTIGE", "Prestige") +game:addlocalizedstring("LUA_MENU_PRESTIGE_DESC", "Edit prestige level.") +game:addlocalizedstring("LUA_MENU_RANK", "Rank") +game:addlocalizedstring("LUA_MENU_RANK_DESC", "Edit rank.") + +game:addlocalizedstring("LUA_MENU_UNSAVED_CHANGES", "You have unsaved changes, are you sure you want to exit?") +game:addlocalizedstring("LUA_MENU_SAVE", "Save changes") +game:addlocalizedstring("LUA_MENU_SAVE_DESC", "Save changes.") +game:addlocalizedstring("LUA_MENU_SETTINGS", "Settings") +game:addlocalizedstring("LUA_MENU_EDIT_STATS", "Edit Stats") + +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 + +local personalizationbutton = LUI.MPLobbyBase.AddPersonalizationButton +LUI.MPLobbyBase.AddPersonalizationButton = function(menu) + personalizationbutton(menu) + menu:AddButton("@LUA_MENU_STATS", function() + LUI.FlowManager.RequestAddMenu(nil, "stats_menu") + end) +end + +LUI.MenuBuilder.registerType("stats_menu", function(a1) + local menu = LUI.MenuTemplate.new(a1, { + menu_title = Engine.ToUpperCase(Engine.Localize("@LUA_MENU_STATS")), + menu_list_divider_top_offset = -(LUI.H1MenuTab.tabChangeHoldingElementHeight + luiglobals.H1MenuDims.spacing), + menu_width = luiglobals.GenericMenuDims.OptionMenuWidth, + skipAnim = 0 ~= LUI.PCGraphicOptions.FindTypeIndex(LUI.PreviousMenuName) + }) + + createdivider(menu, "@LUA_MENU_SETTINGS") + + LUI.Options.CreateOptionButton( + menu, + "cg_unlockall_items", + "@LUA_MENU_UNLOCKALL_ITEMS", + "@LUA_MENU_UNLOCKALL_ITEMS_DESC", + { + { + text = "@LUA_MENU_ENABLED", + value = true + }, + { + text = "@LUA_MENU_DISABLED", + value = false + } + }, + nil, + nil + ) + + LUI.Options.CreateOptionButton( + menu, + "cg_unlockall_classes", + "@LUA_MENU_UNLOCKALL_CLASSES", + "@LUA_MENU_UNLOCKALL_CLASSES_DESC", + { + { + text = "@LUA_MENU_ENABLED", + value = true + }, + { + text = "@LUA_MENU_DISABLED", + value = false + } + }, + nil, + nil + ) + + createdivider(menu, "@LUA_MENU_EDIT_STATS") + + local prestige = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "prestige") or 0 + local experience = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "experience") or 0 + local rank = luiglobals.Lobby.GetRankForXP(experience, prestige) + + local saved = true + local prestigevalue = prestige + local rankvalue = rank + local rankbutton = nil + + prestigeeditbutton(menu, function(value) + prestigevalue = value + saved = false + end) + + rankbutton = rankeditbutton(menu, function(value) + rankvalue = value + saved = false + end) + + local savebutton = menu:AddButton("@LUA_MENU_SAVE", function() + Engine.SetPlayerData(0, CoD.StatsGroup.Ranked, "prestige", tonumber(prestigevalue)) + + local rank = tonumber(rankvalue) + local prestige = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "prestige") or 0 + local experience = rank == 0 and 0 or luiglobals.Rank.GetRankMaxXP(tonumber(rankvalue) - 1, prestige) + + Engine.SetPlayerData(0, CoD.StatsGroup.Ranked, "experience", experience) + + saved = true + end, nil, nil, nil, { + desc_text = Engine.Localize("LUA_MENU_SAVE_DESC") + }) + + LUI.Options.InitScrollingList(menu.list, nil) + LUI.Options.AddOptionTextInfo(menu) + + menu:AddBackButton(function() + if (saved) then + LUI.FlowManager.RequestLeaveMenu(menu) + return + end + + LUI.yesnopopup({ + title = Engine.Localize("@MENU_NOTICE"), + text = Engine.Localize("@LUA_MENU_UNSAVED_CHANGES"), + callback = function(result) + if (result) then + LUI.FlowManager.RequestLeaveMenu(menu) + end + end + }) + end) + + return menu +end) + +function prestigeeditbutton(menu, callback) + local options = {} + local max = luiglobals.Lobby.GetMaxPrestigeLevel() + local prestige = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "prestige") or 0 + + for i = 0, max do + game:addlocalizedstring("LUA_MENU_" .. i, i .. "") + + table.insert(options, { + text = "@" .. i, + value = i .. "" + }) + end + + Engine.SetDvarFromString("ui_prestige_level", prestige .. "") + + LUI.Options.CreateOptionButton( + menu, + "ui_prestige_level", + "@LUA_MENU_PRESTIGE", + "@LUA_MENU_PRESTIGE_DESC", + options, + nil, + nil, + callback + ) +end + +function rankeditbutton(menu, callback) + local options = {} + local prestige = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "prestige") or 0 + local experience = Engine.GetPlayerData(0, CoD.StatsGroup.Ranked, "experience") or 0 + + local rank = luiglobals.Lobby.GetRankForXP(experience, prestige) + local max = luiglobals.Rank.GetMaxRank(prestige) + local maxprestige = luiglobals.Lobby.GetMaxPrestigeLevel() + + for i = 0, max do + game:addlocalizedstring("LUA_MENU_" .. i, i .. "") + + table.insert(options, { + text = "@" .. (i + 1), + value = i .. "" + }) + end + + Engine.SetDvarFromString("ui_rank_level_", rank .. "") + + return LUI.Options.CreateOptionButton( + menu, + "ui_rank_level_", + "@LUA_MENU_RANK", + "@LUA_MENU_RANK_DESC", + options, + nil, + nil, + callback + ) +end + +local isclasslocked = luiglobals.Cac.IsCustomClassLocked +luiglobals.Cac.IsCustomClassLocked = function(...) + if (Engine.GetDvarBool("cg_unlockall_classes")) then + return false + end + + return isclasslocked(table.unpack({...})) +end diff --git a/src/client/component/patches.cpp b/src/client/component/patches.cpp index 3d86b406..4498e272 100644 --- a/src/client/component/patches.cpp +++ b/src/client/component/patches.cpp @@ -65,11 +65,6 @@ namespace patches return com_register_dvars_hook.invoke(); } - int is_item_unlocked() - { - return 0; // 0 == yes - } - void set_client_dvar_from_server_stub(void* a1, void* a2, const char* dvar, const char* value) { if (dvar == "cg_fov"s || dvar == "cg_fovMin"s) @@ -214,11 +209,6 @@ namespace patches true); utils::hook::call(0x14009EE9E, aim_assist_add_to_target_list); - // unlock all items - utils::hook::jump(0x140413E60, is_item_unlocked); // LiveStorage_IsItemUnlockedFromTable_LocalClient - utils::hook::jump(0x140413860, is_item_unlocked); // LiveStorage_IsItemUnlockedFromTable - utils::hook::jump(0x140412B70, is_item_unlocked); // idk ( unlocks loot etc ) - // isProfanity utils::hook::set(0x1402877D0, 0xC3C033); diff --git a/src/client/component/stats.cpp b/src/client/component/stats.cpp new file mode 100644 index 00000000..8e238178 --- /dev/null +++ b/src/client/component/stats.cpp @@ -0,0 +1,75 @@ +#include +#include "loader/component_loader.hpp" + +#include "scheduler.hpp" +#include "dvars.hpp" + +#include "game/game.hpp" +#include "game/dvars.hpp" + +#include +#include +#include + +namespace stats +{ + namespace + { + game::dvar_t* cg_unlock_all_items; + + utils::hook::detour is_item_unlocked_hook; + utils::hook::detour is_item_unlocked_hook2; + utils::hook::detour is_item_unlocked_hook3; + + int is_item_unlocked_stub(void* a1, void* a2, void* a3) + { + if (cg_unlock_all_items->current.enabled) + { + return 0; + } + + return is_item_unlocked_hook.invoke(a1, a2, a3); + } + + int is_item_unlocked_stub2(void* a1, void* a2, void* a3, void* a4, void* a5, void* a6) + { + if (cg_unlock_all_items->current.enabled) + { + return 0; + } + + return is_item_unlocked_hook2.invoke(a1, a2, a3, a4, a5, a6); + } + + int is_item_unlocked_stub3(void* a1) + { + if (cg_unlock_all_items->current.enabled) + { + return 0; + } + + return is_item_unlocked_hook3.invoke(a1); + } + } + + class component final : public component_interface + { + public: + void post_unpack() override + { + if (!game::environment::is_mp()) + { + return; + } + + cg_unlock_all_items = dvars::register_bool("cg_unlockall_items", false, game::DVAR_FLAG_SAVED, true); + dvars::register_bool("cg_unlockall_classes", false, game::DVAR_FLAG_SAVED, true); + + is_item_unlocked_hook.create(0x140413E60, is_item_unlocked_stub); + is_item_unlocked_hook2.create(0x140413860, is_item_unlocked_stub2); + is_item_unlocked_hook3.create(0x140412B70, is_item_unlocked_stub3); + } + }; +} + +REGISTER_COMPONENT(stats::component) diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index 941dda10..d58079cc 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -28,6 +28,8 @@ namespace ui_scripting utils::hook::detour hks_shutdown_hook; utils::hook::detour hks_allocator_hook; utils::hook::detour hks_frame_hook; + utils::hook::detour lui_error_hook; + utils::hook::detour hksi_hks_error_hook; bool error_hook_enabled = false; @@ -52,6 +54,39 @@ namespace ui_scripting } } + int hksi_hks_error_stub(game::hks::lua_State* s, int a2) + { + if (!error_hook_enabled) + { + return hksi_hks_error_hook.invoke(s, a2); + } + else + { + throw std::runtime_error("unknown error"); + } + } + + int lui_error_stub(game::hks::lua_State* s) + { + if (!error_hook_enabled) + { + return lui_error_hook.invoke(s); + } + else + { + const auto count = static_cast(s->m_apistack.top - s->m_apistack.base); + const auto arguments = get_return_values(count); + + std::string error_str = "LUI Error"; + if (count && arguments[0].is()) + { + error_str = arguments[0].as(); + } + + throw std::runtime_error(error_str); + } + } + void* hks_start_stub(char a1) { const auto _1 = gsl::finally([]() @@ -162,6 +197,8 @@ namespace ui_scripting hksi_lual_error_hook.create(SELECT_VALUE(0x1400A5EA0, 0x14012F300), hksi_lual_error_stub); hks_allocator_hook.create(SELECT_VALUE(0x14009B570, 0x14012BAC0), hks_allocator_stub); hks_frame_hook.create(SELECT_VALUE(0x1400E37F0, 0x1401755B0), hks_frame_stub); + lui_error_hook.create(SELECT_VALUE(0x14007D7D0, 0x14010C9E0), lui_error_stub); + hksi_hks_error_hook.create(SELECT_VALUE(0x14009DD80, 0x14012E390), hksi_hks_error_stub); if (game::environment::is_mp()) {