Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "amx.h"
- #include "amxaux.h"
- #include "amxcons.h"
- #include <memory>
- #include <vector>
- static cell AMXAPI myfunc_test(AMX* amx,const cell* params)
- {
- char* name,*text;
- amx_StrParam_Type(amx,params[1],name,char*);
- amx_StrParam_Type(amx,params[2],text,char*);
- MessageBox(NULL,text,name,MB_OK);
- return 0;
- }
- static cell AMXAPI AsAddress(AMX* amx,const cell* params)
- {
- return (cell)amx_Address(amx,params[1]);
- }
- static cell AMXAPI ParamCount(AMX* amx,const cell* params)
- {
- return amx->paramcount;
- }
- const AMX_NATIVE_INFO myfuncs_Natives[] = {
- {"myfunc_test",myfunc_test},
- {"AsAddress",AsAddress},
- {"ParamCount",ParamCount},
- {NULL,NULL}
- };
- typedef enum {
- Invalid = -1,
- MyObject
- } pawnhandletype_t;
- typedef void (*handlecallback_free_t)(void*);
- typedef struct {
- pawnhandletype_t type;
- int refcount;
- void* data;
- handlecallback_free_t freeclk;
- } pawnhandle_t;
- typedef int PHandle;
- class CHandleSystem
- {
- public:
- PHandle AllocateHandle(pawnhandletype_t type,void* data,handlecallback_free_t freeclk = NULL)
- {
- pawnhandle_t hnd;
- hnd.type = type;
- hnd.refcount = 1;
- hnd.data = data;
- hnd.freeclk = freeclk;
- m_Handles.push_back(hnd);
- return m_Handles.size()-1;
- }
- bool IsValid(PHandle handle)
- {
- return (handle >= 0 && handle < m_Handles.size());
- }
- bool CheckHandle(PHandle handle,pawnhandletype_t type)
- {
- if(!IsValid(handle)) return false;
- return (m_Handles[handle].type==type);
- }
- void* GetHandleData(PHandle handle)
- {
- return m_Handles[handle].data;
- }
- void FreeHandle(PHandle handle)
- {
- if(!IsValid(handle)) return;
- pawnhandle_t& hnd = m_Handles[handle];
- if(--hnd.refcount == 0)
- {
- hnd.freeclk(hnd.data);
- m_Handles.erase(m_Handles.begin()+handle);
- }
- }
- void IncrementRef(PHandle handle)
- {
- if(!IsValid(handle)) return;
- m_Handles[handle].refcount++;
- }
- private:
- std::vector<pawnhandle_t> m_Handles;
- } g_HandleSys;
- template<typename T>
- T* GetHandleData(PHandle handle,pawnhandletype_t type)
- {
- if(!g_HandleSys.CheckHandle(handle,type)) return NULL;
- return (T*)g_HandleSys.GetHandleData(handle);
- }
- class CMyObject
- {
- public:
- CMyObject(int a,int b) : m_iA(a),m_iB(b)
- {
- }
- virtual int GetA(){return m_iA;}
- virtual int GetB(){return m_iB;}
- virtual void SetA(int a){m_iA=a;}
- virtual void SetB(int b){m_iB=b;}
- virtual int GetSum(){return m_iA+m_iB;}
- private:
- int m_iA;
- int m_iB;
- };
- void FreeHandle_MyObject(void* data)
- {
- delete (CMyObject*)data;
- }
- static cell AMXAPI amx_MyObject_Create(AMX* amx,const cell* params)
- {
- CMyObject* pObj = new CMyObject(params[1],params[2]);
- return (cell)g_HandleSys.AllocateHandle(MyObject,pObj,FreeHandle_MyObject);
- }
- static cell AMXAPI amx_MyObject_Destroy(AMX* amx,const cell* params)
- {
- PHandle handle = (PHandle)params[1];
- if(!g_HandleSys.CheckHandle(handle,MyObject))
- return amx_RaiseError(amx,AMX_ERR_PARAMS);
- g_HandleSys.FreeHandle(handle);
- return 0;
- }
- static cell AMXAPI amx_MyObject_GetA(AMX* amx,const cell* params)
- {
- CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
- if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
- return obj->GetA();
- }
- static cell AMXAPI amx_MyObject_GetB(AMX* amx,const cell* params)
- {
- CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
- if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
- return obj->GetB();
- }
- static cell AMXAPI amx_MyObject_SetA(AMX* amx,const cell* params)
- {
- CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
- if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
- obj->SetA(params[2]);
- return 0;
- }
- static cell AMXAPI amx_MyObject_SetB(AMX* amx,const cell* params)
- {
- CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
- if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
- obj->SetB(params[2]);
- return 0;
- }
- static cell AMXAPI amx_MyObject_GetSum(AMX* amx,const cell* params)
- {
- CMyObject* obj = GetHandleData<CMyObject>((PHandle)params[1],MyObject);
- if(!obj) return amx_RaiseError(amx,AMX_ERR_PARAMS);
- return obj->GetSum();
- }
- const AMX_NATIVE_INFO myobject_Natives[] = {
- {"MyObject_Create",amx_MyObject_Create},
- {"MyObject_Destroy",amx_MyObject_Destroy},
- {"MyObject_GetA",amx_MyObject_GetA},
- {"MyObject_GetB",amx_MyObject_GetB},
- {"MyObject_SetA",amx_MyObject_SetA},
- {"MyObject_SetB",amx_MyObject_SetB},
- {"MyObject_GetSum",amx_MyObject_GetSum},
- {NULL,NULL}
- };
- int main()
- {
- AMX amx;
- int err;
- err = aux_LoadProgram(&amx,"test.amx",NULL);
- if(err != AMX_ERR_NONE)
- {
- fprintf(stderr,"1 AMX Error: %s\n",aux_StrError(err));
- return 1;
- }
- amx_ConsoleInit(&amx);
- amx_Register(&amx,myfuncs_Natives,-1);
- amx_Register(&amx,myobject_Natives,-1);
- cell retval = -1;
- int entry;
- err = amx_FindPublic(&amx,"Script_EntryPoint",&entry);
- if(err != AMX_ERR_NONE)
- {
- fprintf(stderr,"2 AMX Error: %s\n",aux_StrError(err));
- return 1;
- }
- printf("entry %lu\n",entry);
- err = amx_Exec(&amx,&retval,entry);
- if(err != AMX_ERR_NONE)
- {
- fprintf(stderr,"3 AMX Error: %s\n",aux_StrError(err));
- }
- printf("retval %d\n",retval);
- aux_FreeProgram(&amx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement