1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 18:53:52 +00:00
openmw-tes3mp/apps/openmw-mp/Script/LangMono/LangMono.hpp

88 lines
2.1 KiB
C++
Raw Normal View History

2018-12-08 22:40:02 +00:00
//
// Created by koncord on 08.12.18.
//
#pragma once
#include <Script/Language.hpp>
#include <Script/SystemInterface.hpp>
#include <unordered_map>
#include <mono/metadata/object.h>
struct MethodKey
{
inline bool operator==(const MethodKey &other) const
{
return other.paramsCnt == paramsCnt && other.name == name;
}
MethodKey(const std::string &name, int paramsCnt)
{
this->name = name;
this->paramsCnt = paramsCnt;
}
std::string name;
int paramsCnt;
};
namespace std
{
template<>
struct hash<MethodKey>
{
std::size_t operator()(const MethodKey &key) const
{
return hash<string>()(key.name)
^ (hash<uint32_t>()(key.paramsCnt) >> 1);
}
};
}
struct MonoInstance
{
MonoObject *object;
MonoClass *klass;
MonoAssembly *assembly;
MonoImage *image;
};
class LangMono: public Language
{
MonoInstance *instance;
std::unordered_map<MethodKey, MonoMethod *> methodsCache;
public:
virtual lib_t GetInterface() override;
LangMono();
LangMono(MonoInstance *instance);
~LangMono();
virtual void LoadProgram(const char *filename) override;
virtual int FreeProgram() override;
virtual bool IsCallbackPresent(const char *name) override;
virtual boost::any Call(const char *name, const char *argl, int buf, ...) override;
virtual boost::any Call(const char *name, const char *argl, const std::vector<boost::any> &args) override;
2018-12-22 21:07:11 +00:00
static int CreateTimerEx(MonoObject *delegate, long msec, MonoString *monoStr, MonoArray *args);
2018-12-23 21:41:40 +00:00
static void MakePublic(MonoObject *delegate, MonoString *name) noexcept;
static MonoObject *CallPublic(MonoString *name, MonoArray *args);
static void Init();
static void Free();
2018-12-23 21:41:40 +00:00
template<typename T>
static T Unbox(MonoObject *obj)
{
return *(T *) mono_object_unbox(obj);
}
template<typename T>
static void ObjectSetValue(MonoObject *obj, T value)
{
*(T*) mono_object_unbox(obj) = value;
}
static boost::any ObjectToAny(MonoObject *obj);
static MonoObject *AnyToObject(boost::any any, char ret_type);
2018-12-08 22:40:02 +00:00
};