Skip to content

Commit

Permalink
v1.0.2 (#8)
Browse files Browse the repository at this point in the history
* add mat ambient light modifications

* feat(trails): add trails

* refactor(input): add new prop

* some sdk tweaks, add trail life time option

* gui changes

* add portal colors modification

* gui changes

* fix grammar

* ghetto workaround to get custom portals working in singleplayer

* feat(cfg): add randomize/sync rainbow

* docs(readme): add new features to readme

* refactor: minor optimizations

* docs(readme): update readme yet again

* refactor(sdk): remove redundant variable
  • Loading branch information
es3n1n authored May 16, 2023
1 parent e00b593 commit 823110d
Show file tree
Hide file tree
Showing 46 changed files with 692 additions and 146 deletions.
6 changes: 4 additions & 2 deletions portal2-internal/hack/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace hack {
bool startup(HANDLE dll_handle) {
TRACE_FN;
_dll_handle = dll_handle;
CreateThread(nullptr, 0, LPTHREAD_START_ROUTINE(_initial_routine), 0, 0, nullptr);

std::thread([=]() -> void { _initial_routine(dll_handle); }).detach();
return true;
}

Expand All @@ -49,8 +50,9 @@ namespace hack {
TRACE_FN;
hooks::unhook();
util::input::deinit();
if (menu::opened)
util::game::unlock_cursor();
FreeLibraryAndExitThread(static_cast<HMODULE>(_dll_handle), 0x1);
}

} // namespace bootstrap
} // namespace hack
67 changes: 53 additions & 14 deletions portal2-internal/hack/cfg/cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
#include <format>
#include <fstream>
#include <imgui.h>
#include <random>

namespace hack::cfg {
// @fixme: -
__forceinline void push_chams_opts(const std::string_view prefix, opts::chams_opts_t& opts) {
push(&opts.m_enabled, std::format("{}_enabled", prefix));

push(&opts.m_material, std::format("{}_material", prefix));
__forceinline void push_color(color_t* col, const std::string_view prefix) {
push(&col->r, std::format("{}_r", prefix));
push(&col->g, std::format("{}_g", prefix));
push(&col->b, std::format("{}_b", prefix));
push(&col->a, std::format("{}_a", prefix));
push(&col->rainbow, std::format("{}_rainbow", prefix));

_cols.emplace_back(col);
}

push(&opts.m_color.r, std::format("{}_r", prefix));
push(&opts.m_color.g, std::format("{}_g", prefix));
push(&opts.m_color.b, std::format("{}_b", prefix));
push(&opts.m_color.a, std::format("{}_a", prefix));
push(&opts.m_color.rainbow, std::format("{}_rainbow", prefix));
// @fixme: -
__forceinline void push_chams(opts::chams_opts_t* opts, const std::string_view prefix) {
push(&opts->m_enabled, std::format("{}_enabled", prefix));
push(&opts->m_material, std::format("{}_material", prefix));
push_color(&opts->m_color, prefix);
}

_cols.emplace_back(&opts.m_color);
// @fixme: -
__forceinline void push_portals(opts::portal_colors_t* opts, const std::string_view prefix) {
push_color(&opts->m_portal_1, std::format("{}_1", prefix));
push_color(&opts->m_portal_2, std::format("{}_2", prefix));
}

void init() {
Expand All @@ -28,19 +38,31 @@ namespace hack::cfg {
push(&opts::airacceleration_value, "misc_airacceleration_value");
push(&opts::fov_value, "misc_fov_value");

push_chams_opts("portal_gun_chams", opts::portal_gun_chams);
push_chams_opts("chell_chams", opts::chell_chams);
push_chams_opts("wheatley_chams", opts::wheatley_chams);
push_chams(&opts::portal_gun_chams, "portal_gun_chams");
push_chams(&opts::chell_chams, "chell_chams");
push_chams(&opts::wheatley_chams, "wheatley_chams");

push(&opts::mat_ambient_light_enabled, "misc_mat_ambient");
push_color(&opts::mat_ambient_light_value, "misc_mat_ambient");

push(&opts::trails, "misc_trails");
push(&opts::trails_life_time, "misc_trails_life_time");
push_color(&opts::trails_color, "misc_trails");

push_portals(&opts::portal_colors[0], "misc_portal_1");
push_portals(&opts::portal_colors[1], "misc_portal_2");

read("config"); // load default cfg

randomize_rainbow();
}

void apply_rainbow() {
for (auto* col : _cols) {
if (!col->rainbow)
continue;

col->rainbow_value += 0.1 * ImGui::GetIO().DeltaTime;
col->rainbow_value += 0.1f * ImGui::GetIO().DeltaTime;
if (col->rainbow_value > 1.f)
col->rainbow_value = 0.f;

Expand All @@ -49,6 +71,20 @@ namespace hack::cfg {
}
}

void sync_rainbow() {
for (auto* col : _cols)
col->rainbow_value = 0.f;
}

void randomize_rainbow() {
// @note: @es3n1n:
// ayo shout out to kaspersky lab and their extremely-safe password generators
static std::mt19937 _rnd(static_cast<unsigned int>(time(nullptr)));

for (auto* col : _cols)
col->rainbow_value = (_rnd() % 100) / 100.f;
}

std::string& get_path(std::string& path) {
std::filesystem::create_directory(_dir);
path = std::string{_dir} + "\\" + path + ".json";
Expand Down Expand Up @@ -78,6 +114,7 @@ namespace hack::cfg {

deserialize(data, _bools);
deserialize(data, _floats);
deserialize(data, _ints);
}

void save(std::string path) {
Expand All @@ -89,6 +126,8 @@ namespace hack::cfg {
data[b.m_name] = *b.m_ptr;
for (auto& f : _floats)
data[f.m_name] = *f.m_ptr;
for (auto& i : _ints)
data[i.m_name] = *i.m_ptr;

std::ofstream reader(path);
reader.clear();
Expand Down
5 changes: 5 additions & 0 deletions portal2-internal/hack/cfg/cfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ namespace hack::cfg {
};

void init();

void apply_rainbow();
void sync_rainbow();
void randomize_rainbow();

void read(std::string path);
void save(std::string path);
Expand All @@ -37,6 +40,8 @@ namespace hack::cfg {
return _floats.emplace_back(cfg_item_t(ptr, name));
} else if constexpr (std::is_same_v<t, int> || std::is_same_v<t, int32_t>) {
return _ints.emplace_back(cfg_item_t(ptr, name));
} else {
return _ints.emplace_back(cfg_item_t(ptr, name)); // oh no
}
}

Expand Down
29 changes: 27 additions & 2 deletions portal2-internal/hack/cfg/opts.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "sdk/misc/color_t.hpp"
#include <vcruntime.h>

namespace opts {
struct chams_opts_t {
Expand All @@ -9,13 +10,37 @@ namespace opts {
color_t m_color = {255, 255, 255};
};

struct portal_colors_t {
public:
color_t m_portal_1 = {255, 255, 100, 255};
color_t m_portal_2 = {100, 255, 255, 255};

color_t& operator[](size_t index) {
switch (index) {
case 0:
return m_portal_1;
default:
return m_portal_2;
}
}
};

inline bool bhop = false;
inline bool autostrafer = false;
inline bool airacceleration_fix = false;
inline float airacceleration_value = 5.f;
inline float fov_value = 90.f;


inline bool mat_ambient_light_enabled = false;
inline constinit color_t mat_ambient_light_value = {0, 0, 0};

inline bool trails = false;
inline constinit color_t trails_color = {255, 100, 255};
inline float trails_life_time = 1.f;

inline constinit portal_colors_t portal_colors[2];

inline constinit chams_opts_t portal_gun_chams;
inline constinit chams_opts_t chell_chams;
inline constinit chams_opts_t wheatley_chams;
} // namespace hack::cfg::opts
} // namespace opts
5 changes: 5 additions & 0 deletions portal2-internal/hack/features/features.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
namespace hack::features {
__forceinline void create_move(c_usercmd* cmd) {
misc::create_move(cmd);
visuals::create_move(cmd);
}

__forceinline void draw_model_execute(c_model_render* pthis, void* ctx, void* state, model_render_info_t* info, void* matrix, void* original_dme) {
visuals::draw_model_execute(pthis, ctx, state, info, matrix, original_dme);
}

__forceinline void present() {
visuals::present();
}
} // namespace hack::features
2 changes: 1 addition & 1 deletion portal2-internal/hack/features/misc/airacceleration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "hack/cfg/opts.hpp"

namespace hack::features::misc {
void apply_acceleration(std::optional<float> new_value) {
void apply_acceleration(const std::optional<float>& new_value) {
static auto sv_paintairacceleration = portal::cvar->find_convar("sv_paintairacceleration");

sv_paintairacceleration->m_float_value = opts::airacceleration_value = new_value.value_or(opts::airacceleration_value);
Expand Down
34 changes: 4 additions & 30 deletions portal2-internal/hack/features/misc/airacceleration_fix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,14 @@ namespace hack::features::misc {
0xFF, 0xE0 // jmp eax
};

*reinterpret_cast<std::uintptr_t*>(shellcode + 1) = portal::sig::airmove_velocity_check_exit;
*util::mem::addr_t(shellcode).offset(1).ptr<std::uintptr_t>() = portal::sig::airmove_velocity_check_exit;

// save orig bytes
//
memcpy(orig_bytes, portal::sig::airmove_velocity_check.cast<void*>(), sizeof(orig_bytes));
static_assert(sizeof(orig_bytes) == sizeof(shellcode));

// change protection so we could overwrite this fn
//
DWORD old;
VirtualProtect(portal::sig::airmove_velocity_check.cast<void*>(), sizeof(shellcode), PAGE_EXECUTE_READWRITE, &old);

// apply patch
//
memcpy(portal::sig::airmove_velocity_check.cast<void*>(), shellcode, sizeof(shellcode));

// restore protection + flush instruction cache
//
VirtualProtect(portal::sig::airmove_velocity_check.cast<void*>(), sizeof(shellcode), old, &old);
FlushInstructionCache(reinterpret_cast<HANDLE>(-1), portal::sig::airmove_velocity_check.cast<void*>(), sizeof(shellcode));
static_assert(sizeof(shellcode) == sizeof(orig_bytes));
util::mem::patch_text_section(portal::sig::airmove_velocity_check, shellcode, sizeof(shellcode), orig_bytes);
}

__forceinline void restore() {
// change protection
//
DWORD old;
VirtualProtect(portal::sig::airmove_velocity_check.cast<void*>(), sizeof(orig_bytes), PAGE_EXECUTE_READWRITE, &old);

// restore patch
//
memcpy(portal::sig::airmove_velocity_check.cast<void*>(), orig_bytes, sizeof(orig_bytes));

// restore protection
VirtualProtect(portal::sig::airmove_velocity_check.cast<void*>(), sizeof(orig_bytes), old, &old);
util::mem::patch_text_section(portal::sig::airmove_velocity_check, orig_bytes, sizeof(orig_bytes));
}
} // namespace

Expand Down
2 changes: 1 addition & 1 deletion portal2-internal/hack/features/misc/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace hack::features::misc {
// callbacks
//
void airacceleration_fix();
void apply_acceleration(std::optional<float> new_value = std::nullopt);
void apply_acceleration(const std::optional<float>& new_value = std::nullopt);

// create-move
//
Expand Down
7 changes: 6 additions & 1 deletion portal2-internal/hack/features/visuals/chams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ namespace hack::features::visuals {
material = mat_flat;
break;
default:
#ifdef _DEBUG
__debugbreak(); // unknown material
#endif
break;
}

if (!material) [[unlikely]]
return;

material->color_modulate(opts.m_color.r / 255.f, opts.m_color.g / 255.f, opts.m_color.b / 255.f);
material->alpha_modulate(opts.m_color.a / 255.f);

Expand All @@ -66,7 +71,7 @@ namespace hack::features::visuals {
break;

default:
util::logger::debug("Ignoring model '%s'", info->m_model->m_name);
// util::logger::debug("Ignoring model '%s'", info->m_model->m_name);
break;
}
}
Expand Down
81 changes: 81 additions & 0 deletions portal2-internal/hack/features/visuals/change_portal_colors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "hack/cfg/opts.hpp"
#include "visuals.hpp"

#include <cmath>

namespace hack::features::visuals {
__forceinline void apply_color(vec3_t* vec, color_t& col) {
vec->x = col.r / 255.f;
vec->y = col.g / 255.f;
vec->z = col.b / 255.f;
}

__forceinline void apply_color(i_material* mat) {
bool found = false;
i_material_var* var;

// @fixme: @es3n1n: ghetto workaround to fix lights.
// for a proper fix we should hook DrawSimplePortalMesh(or any other shit
// that gets called before/at the time of applying the tinted material)
// and replace colours there, before applying the tinted material on the
// portal textures.
// we only have one tinted material for both portals, smh
color_t dark_col = {0.f, 0.f, 0.f};
for (auto* name : {"$PortalColorGradientLight", "$PortalColorGradientDark"}) {
if (var = mat->find_var(name, &found); var && found)
apply_color(&var->vec3, dark_col);
}

if (var = mat->find_var("$PortalCoopColorPlayerOnePortalOne", &found); var && found)
apply_color(&var->vec3, opts::portal_colors[0][0]);

if (var = mat->find_var("$PortalCoopColorPlayerOnePortalTwo", &found); var && found)
apply_color(&var->vec3, opts::portal_colors[0][1]);

if (var = mat->find_var("$PortalCoopColorPlayerTwoPortalOne", &found); var && found)
apply_color(&var->vec3, opts::portal_colors[1][0]);

if (var = mat->find_var("$PortalCoopColorPlayerTwoPortalTwo", &found); var && found)
apply_color(&var->vec3, opts::portal_colors[1][1]);
}

__forceinline void patch_single_player_branch() {
// @note: @es3n1n: we are patching the "singleplayer" branch because the game won't apply
// our dynamic colors on the singleplayer-portal materials, dunno why and i don't want to
// spend any more time on this
static std::once_flag fl;
std::call_once(fl, []() -> void {
uint8_t patch[] = {
0xB8, 0x37, 0x13, 0x37, 0x13, // mov eax, ptr
0xFF, 0xE0 // jmp eax
};

// @todo: @es3n1n: sig the multiplayer branch instead
*util::mem::addr_t(patch).offset(1).ptr<uintptr_t>() = portal::sig::draw_portal_single_player_color_branch.offset(-12);

util::mem::patch_text_section(portal::sig::draw_portal_single_player_color_branch, patch, sizeof(patch));
});
}

void change_portal_color() {
if (!portal::prop_portal->m_materials.m_portal_static_overlay_tinted) // if we are not in game
return;

apply_color(portal::prop_portal->m_materials.m_portal_static_overlay_tinted);

for (std::size_t i = 0; i < 2; i++)
apply_color(portal::prop_portal->m_materials.m_portal_static_overlay[i]);

apply_color(portal::prop_portal->m_materials.m_portal_depth_doubler);

for (std::size_t i = 0; i < 2; i++)
apply_color(&portal::prop_portal->m_materials.m_single_player_portal_colors[i], opts::portal_colors[0][i]);

for (std::size_t i = 0; i < 2; i++) {
for (std::size_t j = 0; j < 2; j++)
apply_color(&portal::prop_portal->m_materials.m_coop_player_portal_colors[i][j], opts::portal_colors[i][j]);
}

patch_single_player_branch();
}
} // namespace hack::features::visuals
Loading

0 comments on commit 823110d

Please sign in to comment.