Created
October 1, 2022 18:33
-
-
Save wchristian/b718a729ee05490a3511c9ba811a4abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub update_memory_use { | |
require Win32::OLE; | |
import Win32::OLE qw(in); | |
my @state_array; | |
my $pid = $PROCESS_ID; | |
my $sleep_time = 2; | |
my $small_sleep_time = 0.1; | |
sleep $small_sleep_time while ( !$config_loaded ); | |
while (1) { | |
@state_array = in( | |
Win32::OLE->GetObject("winmgmts:\\\\.\\root\\CIMV2")->ExecQuery( | |
'SELECT PrivatePageCount FROM Win32_Process' . " WHERE ProcessId = $pid", | |
'WQL', 0x10 | 0x20 | |
) | |
); | |
$memory_use = $state_array[0]->{PrivatePageCount}; | |
if ( $memory_use > $memory_limit && !$all_protected ) { | |
$memory_needs_clears = 1; | |
sleep $small_sleep_time; | |
} | |
else { | |
$all_protected = 0; | |
sleep $sleep_time; | |
} | |
} | |
Win32::OLE->Uninitialize(); | |
return 1; | |
} | |
sub init_process_connection { | |
### get dwarf process id ####################################################### | |
my %list = Win32::Process::List->new()->GetProcesses(); | |
for my $key ( keys %list ) { | |
$dwarf_pid = $key if ( $list{$key} =~ /dwarfort.exe/ ); | |
} | |
fatal_error( 'Could not find process ID, make sure DF is running and' . ' a savegame is loaded.' ) | |
unless ($dwarf_pid); | |
### lower priority of dwarf fortress ########################################### | |
Win32::Process::Open( my $dwarf_process, $dwarf_pid, 1 ); | |
$dwarf_process->SetPriorityClass(IDLE_PRIORITY_CLASS); | |
croak 'Could not lower DF process priority, this is really odd and' | |
. ' should not happen, try running as administrator or poke Mithaldu/Xenofur.' | |
unless ($dwarf_process); | |
Win32::Process::Open( my $self_process, $PROCESS_ID, 1 ); | |
$self_process->SetPriorityClass(IDLE_PRIORITY_CLASS); | |
croak 'Could not lower own process priority, this is really odd and' | |
. ' should not happen, try running as administrator or poke Mithaldu/Xenofur.' | |
unless ($self_process); | |
### actually read stuff from memory ############################################ | |
$proc = | |
Win32::Process::Memory->new( { pid => $dwarf_pid, access => 'read/write/query' } ) | |
; # open process with read access | |
croak 'Could not open memory access to Dwarf Fortress, this is really odd' | |
. ' and should not happen, try running as' | |
. ' administrator or poke Mithaldu/Xenofur.' | |
unless ($proc); | |
$df_proc_handle = $proc->{hProcess}; | |
### Let's Pla... erm, figure out what version this is ########################## | |
for my $i ( 0 .. $#OFFSETS ) { | |
$pe_timestamp = $proc->get_u32( $OFFSETS[$i]{pe_timestamp_offset} ); | |
return $i if ( $OFFSETS[$i]{PE} == $pe_timestamp ); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment