Skip to content

Commit

Permalink
minor refractor, add some reminders, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hero622 committed Nov 19, 2023
1 parent 3a205bc commit 8f0c82f
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 31 deletions.
16 changes: 16 additions & 0 deletions example-mod/src/huds/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

#include <wormhole.h>

c_example_hud *ex_hud;
c_example_thud *ex_thud;

bool huds::initialize( ) {
wh->huds->reg( ex_hud = new c_example_hud( ) );
wh->huds->reg( ex_thud = new c_example_thud( ) );

return true;
}

void huds::uninitialize( ) {
wh->huds->unreg( ex_hud );
wh->huds->unreg( ex_thud );
}


void c_example_hud::paint( ) {
draw_filled_rect( 0, 0, 20, 20, sdk::color_t( 255, 0, 0, 255 ) );
draw_filled_rect( 20, 20, 20, 20, sdk::color_t( 0, 255, 0, 255 ) );
Expand Down
5 changes: 5 additions & 0 deletions example-mod/src/huds/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <wormhole.h>

namespace huds {
bool initialize( );
void uninitialize( );
} // namespace huds

class c_example_hud : public wh_api::i_hud {
virtual void paint( );
virtual const char *get_name( );
Expand Down
15 changes: 4 additions & 11 deletions example-mod/src/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ wh_api::c_shared *wh;

sdk::h_font font;

c_example_hud *ex_hud;
c_example_thud *ex_thud;

bool c_wormhole_mod::load( wh_api::c_shared *wh ) {
::wh = wh;

if ( example_mod::hooks::initialize( ) ) {
convars::initialize( );

wh->huds->reg( ex_hud = new c_example_hud( ) );
wh->huds->reg( ex_thud = new c_example_thud( ) );
huds::initialize( );

wh->portal2->console->msg( "example mod loaded.\n" );

// post example event
// this will look like: "example mod:load" for other mods
// post example event
// this will look like: "example mod:load" for other mods
wh->events->post( &mod, "load" );

wh->render->create_font( font, "Tahoma", 12, false, sdk::fontflag_dropshadow );
Expand All @@ -37,8 +33,7 @@ bool c_wormhole_mod::load( wh_api::c_shared *wh ) {
}

void c_wormhole_mod::unload( ) {
wh->huds->unreg( ex_hud );
wh->huds->unreg( ex_thud );
huds::uninitialize( );

convars::uninitialize( );

Expand Down Expand Up @@ -80,6 +75,4 @@ void c_wormhole_mod::paint_menu( ) {
static std::size_t example_multicombo_val;
const char *example_multicombo_items[] = { "value 1", "value 2", "value 3" };
wh->menu->multicombo( example_multicombo_val, example_multicombo_items, 3, "example multicombo" );

wh->portal2->console->msg( "%lu\n", example_multicombo_val );
}
11 changes: 8 additions & 3 deletions src/core/hooks/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ hk_fn( void, hooks::frame ) {

frame( ecx );

// !!! look into why this broke
// todo: look into why this broke
if ( wh )
wh->events->post( &wormhole, "post_frame" );
}

hk_fn( void, hooks::set_signon_state, int state, int count, void *unk ) {
set_signon_state( ecx, state, count, unk );

// this is probably not the best way, i saw SAR do something similar but this needs further thought
if ( state == sdk::signonstate_full )
wh->events->post( &wormhole, "session_start" );
else
Expand All @@ -65,7 +66,7 @@ hk_fn( void, hooks::paint, sdk::paint_mode_t mode ) {
wh->portal2->surface->start_drawing( );

if ( mode == sdk::paint_uipanels ) {
wh->input->poll_input( ); // not sure if this is the best place to call this
wh->input->poll_input( ); // not sure if this is the best place to call this

huds::paint( );

Expand All @@ -83,6 +84,7 @@ hk_fn( void, hooks::paint, sdk::paint_mode_t mode ) {
wh->portal2->surface->finish_drawing( );
}

// unlock the cursor from the game when menu is open
hk_fn( void, hooks::lock_cursor ) {
static void *input_ctx = wh->portal2->engine_client->get_input_context( 0 );

Expand All @@ -97,22 +99,25 @@ hk_fn( void, hooks::lock_cursor ) {
}
}

// block input to the game when wormhole's menu is open, currently only works in game, not in the menu
hk_fn( int, hooks::in_key_event, int eventcode, sdk::button_code_t keynum, const char *current_binding ) {
if ( gui::open )
return 0;

return in_key_event( ecx, eventcode, keynum, current_binding );
}

// recreate fonts because they get cleared everytime you change screen resolution
hk_fn( void, hooks::on_screen_size_changed, int old_width, int old_height ) {
on_screen_size_changed( ecx, old_width, old_height );

wh->events->post( &wormhole, "on_screen_size_changed" );

// recreate fonts
// recreate fonts
gui::initialize( );
}

// we need to unhook cengine::frame before the plugin gets unloaded
hk_cmd_fn( hooks::plugin_unload ) {
if ( args.arg_c( ) >= 2 && wormhole.get_plugin( ) && ( !strcmp( args[ 1 ], "wormhole" ) || std::atoi( args[ 1 ] ) == wormhole.plugin->index ) )
wormhole.unload( );
Expand Down
5 changes: 3 additions & 2 deletions src/core/huds/huds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void align_hud_element( wh_api::hud_t *hud, wh_api::hud_t *other_hud ) {
other_hud_pos.x + other_hud->bounds.x / 2,
other_hud_pos.y + other_hud->bounds.y / 2 };

// todo: sort everything by the distance and then use the closest one
for ( int i = 0; i < 6; ++i ) {
for ( int j = 0; j < 6; ++j ) {
if ( i % 2 != j % 2 )
Expand Down Expand Up @@ -99,7 +100,7 @@ void huds::paint( ) {
}

for ( const auto &thud : thuds ) {
// todo: some formatting system
// todo: some formatting system
auto text = std::string( thud->format );

utils::string::replace( text, "{name}", std::string( thud->get_name( ) ) );
Expand Down Expand Up @@ -163,7 +164,7 @@ void huds::paint_ui( ) {

set_abs_pos( hud, wh->input->get_cursor_position( ) - grab_pos );

// dummy hud element for safezone
// dummy hud element for safezone
auto safezone = wh_api::hud_t( );
safezone.pos = wh->render->normalize( huds::safezone );
safezone.bounds = screen_size - huds::safezone * 2;
Expand Down
14 changes: 7 additions & 7 deletions src/core/menu/framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void gui::framework::end( ) {
{
auto cur_pos = cur_menu.pos + cur_dropdown.pos;

const auto size = sdk::vec2_t( 140, 26 );
const auto size = sdk::vec2_t( 160, 26 );

wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, cur_dropdown.items.size( ) * size.y, colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 1, cur_pos.y + 1, size.x - 2, cur_dropdown.items.size( ) * size.y - 2, colors::bg );
Expand Down Expand Up @@ -182,7 +182,7 @@ void gui::framework::checkbox( bool &val, std::string label ) {
void gui::framework::slider( int &val, int min, int max, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 20 );
const auto size = sdk::vec2_t( 160, 18 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
Expand All @@ -202,15 +202,15 @@ void gui::framework::slider( int &val, int min, int max, std::string label ) {
wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, clicking ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, value * ( size.x - 6 ), size.y - 6, colors::white );

wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 2, fonts::normal, colors::white, false, utils::string::ssprintf( "%d", val ) );
wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 3, fonts::normal, colors::white, false, utils::string::ssprintf( "%d", val ) );

cur_menu.cursor.y += size.y + text_size.y + 4;
}

void gui::framework::sliderf( float &val, float min, float max, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 20 );
const auto size = sdk::vec2_t( 160, 18 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
Expand All @@ -230,15 +230,15 @@ void gui::framework::sliderf( float &val, float min, float max, std::string labe
wh->render->draw_outlined_rect( cur_pos.x, cur_pos.y, size.x, size.y, clicking ? colors::white : colors::dark );
wh->render->draw_filled_rect( cur_pos.x + 3, cur_pos.y + 3, value * ( size.x - 6 ), size.y - 6, colors::white );

wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 2, fonts::normal, colors::white, false, utils::string::ssprintf( "%.1f", val ) );
wh->render->draw_text( cur_pos.x + size.x + 4, cur_pos.y - 3, fonts::normal, colors::white, false, utils::string::ssprintf( "%.1f", val ) );

cur_menu.cursor.y += size.y + text_size.y + 4;
}

void gui::framework::combo( std::size_t &val, std::vector<std::string> items, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 26 );
const auto size = sdk::vec2_t( 160, 26 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
Expand Down Expand Up @@ -280,7 +280,7 @@ void gui::framework::combo( std::size_t &val, std::vector<std::string> items, st
void gui::framework::multicombo( std::size_t &val, std::vector<std::string> items, std::string label ) {
auto cur_pos = cur_menu.pos + cur_menu.cursor;

const auto size = sdk::vec2_t( 140, 26 );
const auto size = sdk::vec2_t( 160, 26 );
const auto text_size = wh->render->get_text_size( fonts::normal, label );

bool hover = wh->input->is_cursor_in_area( cur_pos.x + 3, cur_pos.y + text_size.y + 3, cur_pos.x + size.x - 3, cur_pos.y + text_size.y + size.y - 3 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/menu/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace gui {
sdk::vec2_t pos;

std::vector<std::string> items;
std::size_t value; // this is a bitmask when the dropdown is a multiselect and just a regular index when it isnt
std::size_t value; // this is a bitmask when the dropdown is a multiselect and just a regular index when it isnt

bool multiselect;
bool done;
Expand Down
5 changes: 4 additions & 1 deletion src/core/shared/convar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#include "wormhole-sdk/wormhole.h"

// this whole thing is still a bit scuffed, and i do not understand everything 100%, needs some work
// also doesnt work on linux currently i think

sdk::con_var *c_convar::create_convar( const char *name, const char *default_value, int flags, const char *help_string, bool has_min, float min, bool has_max, float max ) {
void *cvar = wh->portal2->mem_alloc->alloc( 0 ); // honestly i have no clue what size i should pass here but this works
void *cvar = wh->portal2->mem_alloc->alloc( 0 ); // honestly i have no clue what size i should pass here but this works

using ctor_fn_t = sdk::con_var *( __rescall * ) ( void *, const char *, const char *, int, const char *, bool, float, bool, float );
const auto ctor_fn = reinterpret_cast<ctor_fn_t>( utils::memory::pattern_scan( modules::tier1, signatures::convar_ctor ) );
Expand Down
10 changes: 5 additions & 5 deletions src/core/source-sdk/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ namespace sdk {
};

struct c_plugin {
char name[ 128 ]; // 0
bool disable; // 128
i_server_plugin_callbacks *plugin; // 132
int plugin_interface_version; // 136
void *plugin_module; // 140
char name[ 128 ];
bool disable;
i_server_plugin_callbacks *plugin;
int plugin_interface_version;
void *plugin_module;
};
} // namespace sdk

Expand Down
2 changes: 1 addition & 1 deletion src/wormhole-sdk

0 comments on commit 8f0c82f

Please sign in to comment.