Advertisement
Guest User

Untitled

a guest
Jul 29th, 2010
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. // Skip rarely used Windows API functions
  2. #define WIN32_LEAN_AND_MEAN
  3.  
  4. // Headers
  5. #include <GMLuaModule.h>
  6.  
  7. // Module definition
  8. GMOD_MODULE( Init, Shutdown );
  9.  
  10. // Globals
  11. ILuaInterface* g_Lua;
  12.  
  13. // Typedefs
  14. typedef unsigned int uint;
  15. typedef unsigned char byte;
  16.  
  17. // Functions
  18. LUA_FUNCTION( GMAllocate ) {
  19.     // Check argument types
  20.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  21.  
  22.     // Read size
  23.     uint size = uint( g_Lua->GetInteger( 1 ) );
  24.  
  25.     // Allocate it
  26.     byte* ptrMem = new byte[ size ];
  27.    
  28.     // Push the result
  29.     g_Lua->Push( float( int( ptrMem ) ) );
  30.  
  31.     // We're done
  32.     return 1;
  33. }
  34.  
  35. LUA_FUNCTION( GMDeallocate ) {
  36.     // Check argument types
  37.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  38.  
  39.     // Read pointer
  40.     byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
  41.    
  42.     // Deallocate
  43.     delete [] ptrMem;
  44.  
  45.     // We're done
  46.     return 0;
  47. }
  48.  
  49. LUA_FUNCTION( GMSet ) {
  50.     // Check argument types
  51.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  52.     g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
  53.     g_Lua->CheckType( 3, GLua::TYPE_NUMBER );
  54.  
  55.     // Read pointer
  56.     byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
  57.  
  58.     // Read size and value
  59.     byte val = byte( g_Lua->GetInteger( 2 ) );
  60.     uint size = uint( g_Lua->GetInteger( 3 ) );
  61.    
  62.     // Set
  63.     memset( ptrMem, val, size );
  64.  
  65.     // We're done
  66.     return 0;
  67. }
  68.  
  69. LUA_FUNCTION( GMCopy ) {
  70.     // Check argument types
  71.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  72.     g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
  73.     g_Lua->CheckType( 3, GLua::TYPE_NUMBER );
  74.  
  75.     // Read pointers
  76.     byte* ptrMemSrc = (byte*)g_Lua->GetInteger( 1 );
  77.     byte* ptrMemDst = (byte*)g_Lua->GetInteger( 2 );
  78.  
  79.     // Read size
  80.     uint size = uint( g_Lua->GetInteger( 3 ) );
  81.    
  82.     // Copy
  83.     memcpy( ptrMemDst, ptrMemSrc, size );
  84.  
  85.     // We're done
  86.     return 0;
  87. }
  88.  
  89. LUA_FUNCTION( GMSetByte ) {
  90.     // Check argument types
  91.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  92.     g_Lua->CheckType( 2, GLua::TYPE_NUMBER );
  93.  
  94.     // Read pointer
  95.     byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
  96.  
  97.     // Read size and value
  98.     byte val = byte( g_Lua->GetInteger( 2 ) );
  99.    
  100.     // Set
  101.     memset( ptrMem, val, sizeof( byte ) );
  102.  
  103.     // We're done
  104.     return 0;
  105. }
  106.  
  107. LUA_FUNCTION( GMGetByte ) {
  108.     // Check argument types
  109.     g_Lua->CheckType( 1, GLua::TYPE_NUMBER );
  110.  
  111.     // Read pointer
  112.     byte* ptrMem = (byte*)g_Lua->GetInteger( 1 );
  113.  
  114.     // Push the result
  115.     g_Lua->Push( float( *ptrMem ) );
  116.  
  117.     // We're done
  118.     return 1;
  119. }
  120.  
  121. // Initialization
  122. int Init( lua_State *L )
  123. {
  124.     // Get the lua instance
  125.     g_Lua = Lua();
  126.  
  127.     // Register module
  128.     g_Lua->NewGlobalTable( "heap" );
  129.     ILuaObject* module = g_Lua->GetGlobal( "heap" );
  130.  
  131.     // Register functions
  132.     module->SetMember( "Allocate", GMAllocate );
  133.     module->SetMember( "Deallocate", GMDeallocate );
  134.     module->SetMember( "Set", GMSet );
  135.     module->SetMember( "SetByte", GMSetByte );
  136.     module->SetMember( "GetByte", GMGetByte );
  137.     module->SetMember( "Copy", GMCopy );
  138.  
  139.     // Clean up
  140.     module->UnReference();
  141.  
  142.     // Why hello there
  143.     g_Lua->Msg( "[HEAP] Module loaded!\n" );
  144.  
  145.     // Return
  146.     return 0;
  147. }
  148.  
  149.  
  150. // Shutdown
  151. int Shutdown( lua_State *L )
  152. {
  153.     // Good bye world
  154.     g_Lua->Msg( "[HEAP] Module unloaded!\n" );
  155.  
  156.     // Return
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement