Skip to content

Instantly share code, notes, and snippets.

@mabako

mabako/customtype.hpp

Last active Aug 29, 2015
Embed
What would you like to do?
#ifndef __CUSTOMTYPE_H
#define __CUSTOMTYPE_H
#include <cassert>
#include <map>
#include "SDK/ScriptInstance.h"
#include "SDK/ScriptArguments.h"
class CustomType : public SDK::ScriptInstance
{
private:
SDK::ScriptClass* pClass;
void* pUserPointer;
std::map<void*, void*> m_mapScriptPointers;
public:
CustomType(SDK::ScriptClass* pClass, void* pUserPointer)
{
assert(pClass != nullptr);
this->pClass = pClass;
assert(pUserPointer != nullptr);
this->pUserPointer = pUserPointer;
}
SDK::ScriptClass* GetClass()
{
assert(pClass != nullptr);
return pClass;
}
bool IsTypeOf(const char* szType)
{
assert(pClass != nullptr);
return pClass->IsTypeOf(szType);
}
void SetUserPointer(void* pUserPointer)
{
throw "lets not set a userpointer";
}
void* GetUserPointer()
{
assert(pUserPointer != nullptr);
return pUserPointer;
}
void* GetScriptPointer(void* pScriptPointer) { return m_mapScriptPointers[pScriptPointer]; }
void SetScriptPointer(void* pScriptPointer, void* pPointer) { m_mapScriptPointers[pScriptPointer] = pPointer; }
void RemoveScriptPointer(void* pScriptPointer) { m_mapScriptPointers.erase(pScriptPointer); }
/* no-op event implementation */
virtual void AddEvent(std::string eventName, SDK::Package* package, void* callback)
{
}
virtual SDK::ScriptArguments ExecuteEvent(std::string eventName, SDK::ScriptArguments& arguments)
{
return SDK::ScriptArguments();
}
virtual SDK::ScriptArguments ExecuteEvent(std::string eventName, const char* format, ...)
{
return SDK::ScriptArguments();
}
virtual SDK::ScriptArguments ExecuteEvent(SDK::Package* pPackage, std::string eventName, SDK::ScriptArguments& arguments)
{
return SDK::ScriptArguments();
}
virtual SDK::ScriptArguments ExecuteEvent(SDK::Package* pPackage, std::string eventName, const char* format, ...)
{
return SDK::ScriptArguments();
}
virtual void RemoveEvents(SDK::Package* package)
{
}
};
#endif
extern SDK::ScriptClass* g_pDemoClass;
class demo : public CustomType
{
public:
demo() : CustomType(g_pDemoClass, this) {}
void work() { printf("no, i dun work"); };
}
SDK::ScriptClass* g_pDemoClass = nullptr;
extern SDK::ModuleData* g_pData;
void DemoDefs::Initialize(SDK::ModuleData* data)
{
g_pDemoClass = data->CreateClass("Demo");
g_pDemoClass->AddStaticFunction("DoCoolStuff", &DoCoolStuff, "");
g_pDemoClass->AddInstanceFunction("Work", &Work, "x");
}
SDK::ScriptArgument* DemoDefs::DoCoolStuff(SDK::ScriptArguments& arguments)
{
demo* pDemo = new demo();
g_pData->AddInstance(pDemo);
return new CScriptArgument(pDemo);
}
SDK::ScriptArgument* DemoDefs::Work(SDK::ScriptArguments& arguments)
{
SDK::ScriptInstance* pInstance = arguments.GetInstance(0);
if (pInstance->GetClass() == g_pDemoClass)
{
demo* pDemo = (demo*)pInstance->GetUserPointer();
pDemo->work();
return new CScriptArgument();
}
throw "wut kinda magic is dat";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment