daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 59 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "amx.h"
  5. #include "amxaux.h"
  6. #include "amxcons.h"
  7. #include <memory>
  8. #include <vector>
  9.  
  10. static cell AMXAPI myfunc_test(AMX* amx,const cell* params)
  11. {
  12.     char* name,*text;
  13.     amx_StrParam_Type(amx,params[1],name,char*);
  14.     amx_StrParam_Type(amx,params[2],text,char*);
  15.     MessageBox(NULL,text,name,MB_OK);
  16.     return 0;
  17. }
  18.  
  19. static cell AMXAPI AsAddress(AMX* amx,const cell* params)
  20. {
  21.     return (cell)amx_Address(amx,params[1]);
  22. }
  23.  
  24. static cell AMXAPI ParamCount(AMX* amx,const cell* params)
  25. {
  26.     return amx->paramcount;
  27. }
  28.  
  29. const AMX_NATIVE_INFO myfuncs_Natives[] = {
  30.     {"myfunc_test",myfunc_test},
  31.     {"AsAddress",AsAddress},
  32.     {"ParamCount",ParamCount},
  33.     {NULL,NULL}
  34. };
  35.  
  36. typedef enum {
  37.     Invalid = -1,
  38.     MyObject
  39. } pawnhandletype_t;
  40.  
  41. typedef void (*handlecallback_free_t)(void*);
  42.  
  43. typedef struct {
  44.     pawnhandletype_t type;
  45.     int refcount;
  46.     void* data;
  47.     handlecallback_free_t freeclk;
  48. } pawnhandle_t;
  49.  
  50. typedef int PHandle;
  51.  
  52. class CHandleSystem
  53. {
  54. public:
  55.     PHandle AllocateHandle(pawnhandletype_t type,void* data,handlecallback_free_t freeclk = NULL)
  56.     {
  57.         pawnhandle_t hnd;
  58.         hnd.type = type;
  59.         hnd.refcount = 1;
  60.         hnd.data = data;
  61.         hnd.freeclk = freeclk;
  62.        
  63.         m_Handles.push_back(hnd);
  64.         return m_Handles.size()-1;
  65.     }
  66.  
  67.     bool IsValid(PHandle handle)
  68.     {
  69.         return (handle >= 0 && handle < m_Handles.size());
  70.     }
  71.  
  72.     bool CheckHandle(PHandle handle,pawnhandletype_t type)
  73.     {
  74.         if(!IsValid(handle)) return false;
  75.         return (m_Handles[handle].type==type);
  76.     }
  77.  
  78.     void* GetHandleData(PHandle handle)
  79.     {
  80.         return m_Handles[handle].data;
  81.     }
  82.  
  83.     void FreeHandle(PHandle handle)
  84.     {
  85.         if(!IsValid(handle)) return;
  86.         pawnhandle_t& hnd = m_Handles[handle];
  87.         if(--hnd.refcount == 0)
  88.         {
  89.             hnd.freeclk(hnd.data);
  90.             m_Handles.erase(m_Handles.begin()+handle);
  91.         }
  92.     }
  93.  
  94.     void IncrementRef(PHandle handle)
  95.     {
  96.         if(!IsValid(handle)) return;
  97.         m_Handles[handle].refcount++;
  98.     }
  99. private:
  100.     std::vector<pawnhandle_t> m_Handles;
  101. } g_HandleSys;
  102.  
  103. template<typename T>
  104. T* GetHandleData(PHandle handle,pawnhandletype_t type)
  105. {
  106.     if(!g_HandleSys.CheckHandle(handle,type)) return NULL;
  107.     return (T*)g_HandleSys.GetHandleData(handle);
  108. }
  109.  
  110. class CMyObject
  111. {
  112. public:
  113.     CMyObject(int a,int b) : m_iA(a),m_iB(b)
  114.     {
  115.     }
  116.  
  117.     virtual int GetA(){return m_iA;}
  118.     virtual int GetB(){return m_iB;}
  119.     virtual void SetA(int a){m_iA=a;}
  120.     virtual void SetB(int b){m_iB=b;}
  121.     virtual int GetSum(){return m_iA+m_iB;}
  122. private:
  123.     int m_iA;
  124.     int m_iB;
  125. };
  126.  
  127. void FreeHandle_MyObject(void* data)
  128. {
  129.     delete (CMyObject*)data;
  130. }
  131.  
  132. static cell AMXAPI amx_MyObject_Create(AMX* amx,const cell* params)
  133. {
  134.     CMyObject* pObj = new CMyObject(params[1],params[2]);
  135.     return (cell)g_HandleSys.AllocateHandle(MyObject,pObj,FreeHandle_MyObject);
  136. }
  137.  
  138. static cell AMXAPI amx_MyObject_Destroy(AMX* amx,const cell* params)
  139. {
  140.     PHandle handle = (PHandle)params[1];
  141.     if(!g_HandleSys.CheckHandle(handle,MyObject))
  142.         return amx_RaiseError(amx,AMX_ERR_PARAMS);
  143.     g_HandleSys.FreeHandle(handle);
  144.     return 0;
  145. }
  146.  
  147. static cell AMXAPI amx_MyObject_GetA(AMX* amx,const cell* params)
  148. {
  149.     CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
  150.     if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
  151.     return obj->GetA();
  152. }
  153.  
  154. static cell AMXAPI amx_MyObject_GetB(AMX* amx,const cell* params)
  155. {
  156.     CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
  157.     if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
  158.     return obj->GetB();
  159. }
  160.  
  161. static cell AMXAPI amx_MyObject_SetA(AMX* amx,const cell* params)
  162. {
  163.     CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
  164.     if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
  165.     obj->SetA(params[2]);
  166.     return 0;
  167. }
  168.  
  169. static cell AMXAPI amx_MyObject_SetB(AMX* amx,const cell* params)
  170. {
  171.     CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
  172.     if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
  173.     obj->SetB(params[2]);
  174.     return 0;
  175. }
  176.  
  177. static cell AMXAPI amx_MyObject_GetSum(AMX* amx,const cell* params)
  178. {
  179.     CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
  180.     if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
  181.     return obj->GetSum();
  182. }
  183.  
  184. const AMX_NATIVE_INFO myobject_Natives[] = {
  185.     {"MyObject_Create",amx_MyObject_Create},
  186.     {"MyObject_Destroy",amx_MyObject_Destroy},
  187.     {"MyObject_GetA",amx_MyObject_GetA},
  188.     {"MyObject_GetB",amx_MyObject_GetB},
  189.     {"MyObject_SetA",amx_MyObject_SetA},
  190.     {"MyObject_SetB",amx_MyObject_SetB},
  191.     {"MyObject_GetSum",amx_MyObject_GetSum},
  192.     {NULL,NULL}
  193. };
  194.  
  195. int main()
  196. {
  197.     AMX amx;
  198.     int err;
  199.  
  200.     err = aux_LoadProgram(&amx,"test.amx",NULL);
  201.     if(err != AMX_ERR_NONE)
  202.     {
  203.         fprintf(stderr,"1 AMX Error: %s\n",aux_StrError(err));
  204.         return 1;
  205.     }
  206.  
  207.     amx_ConsoleInit(&amx);
  208.     amx_Register(&amx,myfuncs_Natives,-1);
  209.     amx_Register(&amx,myobject_Natives,-1);
  210.  
  211.     cell retval = -1;
  212.     int entry;
  213.  
  214.     err = amx_FindPublic(&amx,"Script_EntryPoint",&entry);
  215.     if(err != AMX_ERR_NONE)
  216.     {
  217.         fprintf(stderr,"2 AMX Error: %s\n",aux_StrError(err));
  218.         return 1;
  219.     }
  220.  
  221.     printf("entry %lu\n",entry);
  222.     err = amx_Exec(&amx,&retval,entry);
  223.  
  224.     if(err != AMX_ERR_NONE)
  225.     {
  226.         fprintf(stderr,"3 AMX Error: %s\n",aux_StrError(err));
  227.     }
  228.  
  229.     printf("retval %d\n",retval);
  230.     aux_FreeProgram(&amx);
  231.     return 0;
  232. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top