.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
Re: Storing an array of function calls, and their params.....
Old
  (#11)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Storing an array of function calls, and their params..... - 16-05-2005

Because, normal C++ is only fun when you do crazy things that either won't work or are a miracle when they do.

The crazy idea I had was to dynamically make a function that would call the other function with the appropriate parameters.

Like...
push ( b )
push ( a )
call ( MyCoolFunc )
pop ( eax )
pop ( eax )
ret

Then set that little function as your function pointer, though since I don't know that much about assembly it's a little harder for me to get this working. Almost done though.
  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#12)
@$3.1415rin
Council Member, Author of JoeBOT
 
@$3.1415rin's Avatar
 
Status: Offline
Posts: 1,381
Join Date: Nov 2003
Location: Germany
Default Re: Storing an array of function calls, and their params..... - 16-05-2005

yep, I know such stuff ... jump to subroutines with the normal jump, to save a following 'return from subroutine' etc ... but normally I rather try to keep assembly and C/C++ separated. this had already positive effects when I had to ran a program on a non x86 machine at university :-)


  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#13)
Maleficus
Member
 
Maleficus's Avatar
 
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
Default Re: Storing an array of function calls, and their params..... - 16-05-2005

I'm coding for a game thats written in straight C, so virtual functions aren't available to me (easily anyway).

I'm creating a script system that is event based. The user can tell the game a bunch of different commands to execute when a certain event happens.

What I was looking to do, is parse the file, then save off the commands that are linked to each event happening in a stack like the one we're talking about.

Then, when the event happens, I can quickly execute the commands linked to that event, without reparsing anything, or working with strings.

It seemed the quickest way to do what I needed to do. If theres a better way, I'd be happy to hear about it, I'm always open to learning more, especially if its faster, or more efficient.


Dum Spiro Spero


  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#14)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Storing an array of function calls, and their params..... - 16-05-2005

There is probably a way to make the compiler do all the work but I haven't found it yet.
Not fun writing a function byte by byte though, calls fine now, just won't pop the stuff off the stack lol.
*goes insane*
  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#15)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Storing an array of function calls, and their params..... - 16-05-2005

WARNING: Disgusting, hacked up code!

Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "remotemodule.h"

#define FUNCTION_SIZE 18
// Dynamically generated function
// ------
// push ( b )		5 bytes
// push ( a )		5 bytes
// call ( pfnAdd )   5 bytes
// pop ( eax )	   1 byte
// pop ( eax )	   1 byte
// ret			   1 byte

int Add( int iA, int iB ) {
   return printf( "%d\n", iA + iB );
}

typedef int ( __cdecl* tAdd ) ( int, int );

struct function_s {
   tAdd pfnAdd; // Adds two numbers
} 
gFunctionlist[ 4 ];

BYTE* CreateAddFunction( int iA, int iB ) {
   BYTE* pMemory = NULL;

   // Use this instead of malloc so this area in memory can run code
   pMemory = ( BYTE* ) VirtualAlloc( NULL, FUNCTION_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE );

   if ( pMemory ) {
	  pMemory[ 0 ] = 0x68;
	  pMemory[ 1 ] = LOBYTE( LOWORD( iB ) );
	  pMemory[ 2 ] = HIBYTE( LOWORD( iB ) );
	  pMemory[ 3 ] = LOBYTE( HIWORD( iB ) );
	  pMemory[ 4 ] = HIBYTE( HIWORD( iB ) );

	  pMemory[ 5 ] = 0x68;
	  pMemory[ 6 ] = LOBYTE( LOWORD( iA ) );
	  pMemory[ 7 ] = HIBYTE( LOWORD( iA ) );
	  pMemory[ 8 ] = LOBYTE( HIWORD( iA ) );
	  pMemory[ 9 ] = HIBYTE( HIWORD( iA ) );

	  int a = ( ( int ) Add - ( int ) &pMemory[ 10 ] ) - 5;

	  pMemory[ 10 ] = 0xE8;
	  pMemory[ 11 ] = LOBYTE( LOWORD( a ) );
	  pMemory[ 12 ] = HIBYTE( LOWORD( a ) );
	  pMemory[ 13 ] = LOBYTE( HIWORD( a ) );
	  pMemory[ 14 ] = HIBYTE( HIWORD( a ) );

	  pMemory[ 15 ] = 0x58;
	  pMemory[ 16 ] = 0x58;

	  pMemory[ 17 ] = 0xC3;
   }

   return pMemory;
}

int main( void ) {
   int i = 0;

   for ( i = 0; i < 4; i++ ) {
	  gFunctionlist[ i ].pfnAdd = ( tAdd ) CreateAddFunction( i, 5 );

	  if ( gFunctionlist[ i ].pfnAdd ) {
		 ( gFunctionlist[ i ].pfnAdd ) ( 0, 0 );
		 VirtualFree( gFunctionlist[ i ].pfnAdd, FUNCTION_SIZE, MEM_RELEASE );
	  }
   }

   return 0;
}
Note that the call to pfnAdd has zeros as parameters but it always comes out as what the call to CreateAddFunction says.
  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#16)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: Storing an array of function calls, and their params..... - 17-05-2005

What Maleficus is doing right now IS portable. As long as all the functions have identical arguments, storing them in the struct is perfectly doable.
Code:
typedef struct
{
	int foo; 
	int (*fooFunc) (int *param1, int *param2);
	int param1;
	int param2;
} my_func_t;
Code:
// instancing
my_func_t *callerFunc;
callerFunc = new (my_func_t);
 
 
// filling in
callerFunc->fooFunc = myCoolFunc;
callerFunc->param1 = 14;
callerFunc->param2 = 22;
 
 
// calling
callerFunc->fooFunc (callerFunc->param1, callerFunc->param2);
This is 100% portable.

If you want to call funcs that have variable types of arguments instead, you can do something like this:
Code:
typedef struct
{
	union
	{
		bool bparam;
		char cparam;
		long iparam;
		float fparam;
		long long lparam;
		double dparam;
	};
	char param_type;
} param_t;
 
 
typedef struct
{
	int foo; 
	int (*fooFunc) (int *param1, int *param2);
	param_t params[256];
	int param_count;
} my_func_t;
Code:
// instancing
my_func_t *callerFunc;
callerFunc = new (my_func_t);
 
 
// filling in
callerFunc->fooFunc = myCoolFunc;
callerFunc->params[0].fparam = 12.3456789f;
callerFunc->params[0].type = PARAM_FLOAT;
callerFunc->params[1].lparam = 24;
callerFunc->params[1].type = PARAM_LONG;
callerFunc->param_count = 2;
 
 
// calling
// HERE GOES DIRTY ASM CODE TO STACK UP THE ARGUMENTS AND CALL THE FUNCTION
of course THIS, will not be portable.



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: Storing an array of function calls, and their params.....
Old
  (#17)
Maleficus
Member
 
Maleficus's Avatar
 
Status: Offline
Posts: 1,054
Join Date: May 2004
Location: Planet Earth
Default Re: Storing an array of function calls, and their params..... - 17-05-2005

Quote:
Originally Posted by Pierre-Marie Baty
What Maleficus is doing right now IS portable. As long as all the functions have identical arguments, storing them in the struct is perfectly doable.
Good to know - thanks for clearing that up.

Thanks for the help guys!


Dum Spiro Spero


  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com