Skip to content

Commit

Permalink
cleaned stuff, more performance, cleaner menu
Browse files Browse the repository at this point in the history
  • Loading branch information
b1scoito committed Jun 15, 2021
1 parent 4006ef5 commit 3e4e019
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 198 deletions.
92 changes: 49 additions & 43 deletions clicker/clicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ void c_clicker::init()
if ( !this->m_is_right_clicking || !this->m_is_left_clicking )
std::this_thread::sleep_for( 1ms );

var::key::clicker_enabled.i_mode = config.clicker.i_key_type;
var::key::clicker_enabled.i_key = config.clicker.i_clicker_key;
vars::key::clicker_enabled.i_mode = config.clicker.i_key_type;
vars::key::clicker_enabled.i_key = config.clicker.i_clicker_key;

static bool first_click = true;
if ( var::key::clicker_enabled.get() )
static auto first_click = true;
if ( vars::key::clicker_enabled.get() )
{
if ( util::extra::is_window_focused() && util::extra::is_cursor_visible() && !( util::extra::is_application_focused() ) )
if ( util::extra::is_window_focused() && util::extra::is_cursor_visible() && !util::extra::is_application_focused() )
{
this->m_is_left_clicking = ( config.clicker.b_enable_left_clicker && var::key::left_clicker_down.get() && !var::key::right_clicker_down.get() );
// left
//
this->m_is_left_clicking = ( config.clicker.b_enable_left_clicker && vars::key::left_clicker_down.get() && !vars::key::right_clicker_down.get() );

if ( this->m_is_left_clicking )
this->send_click( button_t::left, config.clicker.f_left_cps, first_click );

this->m_is_right_clicking = ( config.clicker.b_enable_right_clicker && var::key::right_clicker_down.get() );
// right
//
this->m_is_right_clicking = ( config.clicker.b_enable_right_clicker && vars::key::right_clicker_down.get() );

if ( this->m_is_right_clicking )
this->send_click( button_t::right, config.clicker.f_right_cps, first_click );
}
Expand All @@ -32,37 +38,30 @@ void c_clicker::send_click( button_t b_button, float f_cps, bool& b_is_first_cli
{
const auto start = std::chrono::high_resolution_clock::now();

// ~~ return if the cps is 0
if ( f_cps <= 0 )
return;

/* ~~ If blatant is not enabled and the persistent values are enabled, apply persistent randomization, this applies to drop and spike chance too. */
if ( !config.clicker.b_enable_blatant )
f_cps += this->m_random;

/* ~~ How the delay works is pretty simple, basically the delay is 1000 divided by the cps (because we're working with milliseconds)
* ~~ divided by 2 because the delay will be called both on input down and input up! */
this->m_delay = ( 1000 / f_cps ) / 2;

// ~~ if blatant is not enabled apply given randomization values
if ( !config.clicker.b_enable_blatant )
{
this->m_delay += util::random::number( -config.clicker.f_default_timer_randomization,
config.clicker.f_default_timer_randomization );
}

// ~~ if it's our first click, lets wait for the delay, send the input to up and set it back to false.
if ( b_is_first_click )
{
sleep( this->m_delay );
this->send_mouse_input( input_type_t::up, b_button );
b_is_first_click = false;
}

// ~~ sleep and input down
sleep( this->m_delay );
this->send_mouse_input( input_type_t::down, b_button );

/* ~~ Blockhit
* ~~ If we have blockhit enabled, the chance is higher than 0 and we hit the randomization chance, send a right input down. */
if ( config.clicker.b_enable_blockhit && config.clicker.i_blockhit_chance > 0
&& std::rand() % ( 100 / config.clicker.i_blockhit_chance ) == 0 )
{
Expand All @@ -73,14 +72,13 @@ void c_clicker::send_click( button_t b_button, float f_cps, bool& b_is_first_cli
sleep( this->m_delay );
this->send_mouse_input( input_type_t::up, b_button );

// ~~ if we blockhitted, send up input corresponding to the before down input and set variable back to false.
if ( this->m_blockhitted )
{
send_mouse_input( input_type_t::up, button_t::right );
m_blockhitted = false;
}

++var::stats::i_clicks_this_session;
++vars::stats::i_clicks_this_session;

const auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> elapsed { end - start };
Expand All @@ -92,34 +90,42 @@ void c_clicker::update_thread()
{
while ( true )
{
// ~~ if we should update.
if ( this->m_should_update )
if ( this->m_is_right_clicking || this->m_is_left_clicking )
{
// ~~ if persistent values is enabled, apply persistent values!
if ( config.clicker.b_enable_persistence )
m_random = util::random::number( -config.clicker.f_persistence_value, config.clicker.f_persistence_value );

// ~~ if drop chance is enabled and the value is higher than 0 and the chance matches, apply it..
if ( config.clicker.b_enable_cps_drops &&
config.clicker.i_cps_drop_chance > 0 && ( std::rand() % ( 100 / config.clicker.i_cps_drop_chance ) == 0 ) )
m_random -= config.clicker.f_cps_drop_remove;

// ~~ if spike chance is enabled and the value is higher than 0 and the chance matches, apply it..
if ( config.clicker.b_enable_cps_spikes &&
config.clicker.i_cps_spike_chance > 0 && ( std::rand() % ( 100 / config.clicker.i_cps_spike_chance ) == 0 ) )
m_random += config.clicker.f_cps_spike_add;

// ~~ set it to false so we only set it one time..
this->m_should_update = false;
}
auto rate = util::random::number( 0,
config.clicker.f_persistence_update_rate );

m_persistent_value = util::random::number( config.clicker.f_persistence_update_rate / 2, config.clicker.f_persistence_update_rate );
if ( this->m_should_update )
{
log_debug( "update %.3fms", rate );

// ~ persistence
if ( config.clicker.b_enable_persistence )
{
m_random = util::random::number( -config.clicker.f_persistence_value, config.clicker.f_persistence_value );
}

// ~ cps drops
if ( config.clicker.b_enable_cps_drops &&
config.clicker.i_cps_drop_chance > 0 && ( std::rand() % ( 100 / config.clicker.i_cps_drop_chance ) == 0 ) )
{
m_random -= config.clicker.f_cps_drop_remove;
}

// ~ cps spikes
if ( config.clicker.b_enable_cps_spikes &&
config.clicker.i_cps_spike_chance > 0 && ( std::rand() % ( 100 / config.clicker.i_cps_spike_chance ) == 0 ) )
{
m_random += config.clicker.f_cps_spike_add;
}

this->m_should_update = false;
}

// ~~ wait for the next update...
std::this_thread::sleep_for( floating_ms( config.clicker.f_persistence_update_rate ) );
sleep( rate );

// ~~ and start it!...
this->m_random = {};
this->m_should_update = true;
this->m_random = {};
this->m_should_update = true;
}
}
}
83 changes: 10 additions & 73 deletions clicker/clicker.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#pragma once

// for timeBeginPeriod
#include <timeapi.h>
#pragma comment(lib, "Winmm.lib")
#pragma comment(lib, "winmm.lib")

using floating_ms = std::chrono::duration<float, std::chrono::milliseconds::period>;

enum class button_t: bool
enum button_t: bool
{
right = false,
left = true
right, left
};

enum class input_type_t: bool
enum input_type_t: bool
{
up = false,
down = true
up, down
};

#define sleep(ms) { timeBeginPeriod(1); clicker.precise_timer_sleep( static_cast<double>( ms / 1000.f ) ); timeEndPeriod(1); }
#define sleep(ms) { timeBeginPeriod(1); g_clicker.precise_timer_sleep( static_cast<double>( ms / 1000 ) ); timeEndPeriod(1); }

class c_clicker
{
Expand Down Expand Up @@ -45,9 +44,9 @@ class c_clicker
{
POINT pos; GetCursorPos( &pos );

static_cast<bool>( i_type ) ? ( static_cast<bool>( b_button ) ? PostMessage( GetForegroundWindow(), WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM( pos.x, pos.y ) ) :
i_type ? ( b_button ? PostMessage( GetForegroundWindow(), WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM( pos.x, pos.y ) ) :
PostMessage( GetForegroundWindow(), WM_RBUTTONDOWN, MK_RBUTTON, MAKELPARAM( pos.x, pos.y ) ) ) :
( static_cast<bool>( b_button ) ? PostMessage( GetForegroundWindow(), WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM( pos.x, pos.y ) ) :
( b_button ? PostMessage( GetForegroundWindow(), WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM( pos.x, pos.y ) ) :
PostMessage( GetForegroundWindow(), WM_RBUTTONUP, MK_RBUTTON, MAKELPARAM( pos.x, pos.y ) ) );
}

Expand All @@ -61,8 +60,6 @@ class c_clicker
bool m_is_left_clicking { false };
bool m_is_right_clicking { false };

float m_persistent_value { 0.f };

public:
void init();
void update_thread();
Expand All @@ -71,64 +68,4 @@ class c_clicker
c_clicker() = default;
};

inline auto clicker = c_clicker();

namespace thread
{
namespace click
{
inline void init()
{
clicker.init();
}

inline void randomization()
{
clicker.update_thread();
}
}

// had to
namespace hooking
{
inline HHOOK h_hook;

static LRESULT CALLBACK keyboard_callback( int nCode, WPARAM wParam, LPARAM lParam )
{
static auto* k_hook = reinterpret_cast<KBDLLHOOKSTRUCT*>( lParam );

if ( wParam == WM_KEYDOWN && nCode == HC_ACTION && ( wParam >= WM_KEYFIRST ) && ( wParam <= WM_KEYLAST ) )
{
if ( util::extra::is_window_focused() )
{
if ( k_hook->vkCode == 69 )
var::key::inventory_opened = !var::key::inventory_opened;

if ( k_hook->vkCode == VK_ESCAPE )
var::key::inventory_opened = false;
}
}

return CallNextHookEx( h_hook, nCode, wParam, lParam );
}

inline void spawn()
{
h_hook = SetWindowsHookEx(
WH_KEYBOARD_LL,
keyboard_callback,
nullptr,
NULL
);

MSG msg;
while ( GetMessage( &msg, nullptr, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

UnhookWindowsHookEx( h_hook );
}
}
}
inline auto g_clicker = c_clicker();
1 change: 1 addition & 0 deletions clicker/clicker.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="keybind.hpp" />
<ClInclude Include="pch.hpp" />
<ClInclude Include="threads.hpp" />
<ClInclude Include="utils.hpp" />
<ClInclude Include="vars.hpp" />
<ClInclude Include="menu.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions clicker/clicker.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
<ClInclude Include="keybind.hpp">
<Filter>utils\keybind</Filter>
</ClInclude>
<ClInclude Include="threads.hpp">
<Filter>utils\threads</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="clicker">
Expand Down Expand Up @@ -138,5 +141,8 @@
<Filter Include="utils\vars">
<UniqueIdentifier>{c87a7cbb-b9a7-4307-b963-a65bc9292368}</UniqueIdentifier>
</Filter>
<Filter Include="utils\threads">
<UniqueIdentifier>{2fddc2fe-d959-4e3a-b967-7234b06e9dc9}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion clicker/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "archivex.hpp"

#include <ShlObj.h>
#include <shlobj.h>
#include <fstream>
#include <filesystem>

Expand Down
37 changes: 16 additions & 21 deletions clicker/main.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
#include "pch.hpp"
#include "clicker.hpp"
#include "menu.hpp"

// ~ main entrypoint
INT WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd )
{
std::atexit( [] {} );
std::atexit( [] { threads::hooking::unhook(); } );

config.run( "clicker" );

log_debug( "initializing threads..." );

if ( !_beginthreadex( nullptr, 0,
reinterpret_cast<unsigned( __stdcall* )( void* )>( thread::click::init ), nullptr, 0, nullptr ) )
std::vector<std::pair<void*, std::string>> functions =
{
log_err( "Failed to initialize clicker thread!" );
return EXIT_FAILURE;
}
{ threads::clicker::init, "clicker" },
{ threads::clicker::randomization, "clicker randomization" },
{ threads::hooking::spawn, "hooking" }
};

if ( !_beginthreadex( nullptr, 0,
reinterpret_cast<unsigned( __stdcall* )( void* )>( thread::click::randomization ), nullptr, 0, nullptr ) )
for ( auto& [func, name] : functions )
{
log_err( "Failed to initialize clicker randomization thread!" );
return EXIT_FAILURE;
}
log_debug( "spawning %s thread...", name.c_str() );

if ( !_beginthreadex( nullptr, 0,
reinterpret_cast<unsigned( __stdcall* )( void* )>( thread::hooking::spawn ), nullptr, 0, nullptr ) )
{
log_err( "Failed to initialize hooking!" );
return EXIT_FAILURE;
if ( !_beginthreadex( nullptr, 0, reinterpret_cast<unsigned( __stdcall* )( void* )>( func ), nullptr, 0, nullptr ) )
{
log_err( "failed to spawn %s thread!", name.c_str() );
return EXIT_FAILURE;
}
}

log_debug( "waiting for program end." );
log_debug( "calling menu..." );
if ( !menu.initialize( 550, 350 ) )
{
log_err( "Failed to create DirectX9 device!" );
log_err( "failed to create DirectX9 device!" );
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
Expand Down
Loading

0 comments on commit 3e4e019

Please sign in to comment.