Переведены в статические классы (не правильная реализация, утечка памяти)

This commit is contained in:
Kernell 2015-11-28 04:37:03 +03:00
parent 7a155160f8
commit 7e62cf4d7e
4 changed files with 216 additions and 279 deletions

View File

@ -1,105 +1,100 @@
#include "CMonoClass.h" #include "CMonoClass.h"
CMonoClass::CMonoClass( MonoClass* pMonoClass ) MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain )
{ {
this->m_pMonoClass = pMonoClass; MonoObject* pObject = mono_object_new( pMonoDomain, pMonoClass );
}
CMonoClass::~CMonoClass()
{
}
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain )
{
MonoObject* pObject = mono_object_new( pMonoDomain, this->m_pMonoClass );
if( !pObject ) if( !pObject )
{ {
g_pModuleManager->ErrorPrintf( "%s:%d: Failed to create mono object for %s::%s\n", this->GetNameSpace(), this->GetName(), __FILE__, __LINE__ ); g_pModuleManager->ErrorPrintf( "%s:%d: Failed to create mono object for %s::%s\n",
CMonoClass::GetNameSpace( pMonoClass ),
CMonoClass::GetName( pMonoClass ),
__FILE__,
__LINE__
);
return NULL; return nullptr;
} }
mono_runtime_object_init( pObject ); mono_runtime_object_init( pObject );
return new CMonoObject( pObject ); return pObject;
} }
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, SColor& pColor ) MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, SColor& pColor )
{ {
void *args[] = { &pColor.R, &pColor.G, &pColor.B, &pColor.A }; void *args[] = { &pColor.R, &pColor.G, &pColor.B, &pColor.A };
return this->New( pMonoDomain, args, 4 ); return CMonoClass::New( pMonoClass, pMonoDomain, args, 4 );
} }
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, Vector2& vecVector ) MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector2& vecVector )
{ {
void *args[] = { &vecVector.fX, &vecVector.fY }; void *args[] = { &vecVector.fX, &vecVector.fY };
return this->New( pMonoDomain, args, 2 ); return CMonoClass::New( pMonoClass, pMonoDomain, args, 2 );
} }
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, Vector3& vecVector ) MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector3& vecVector )
{ {
void *args[] = { &vecVector.fX, &vecVector.fY, &vecVector.fZ }; void *args[] = { &vecVector.fX, &vecVector.fY, &vecVector.fZ };
return this->New( pMonoDomain, args, 3 ); return CMonoClass::New( pMonoClass, pMonoDomain, args, 3 );
} }
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, void** args, int argc ) MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, void** args, int argc )
{ {
MonoObject* pObject = mono_object_new( pMonoDomain, this->m_pMonoClass ); MonoObject* pObject = mono_object_new( pMonoDomain, pMonoClass );
if( !pObject ) if( !pObject )
{ {
g_pModuleManager->ErrorPrintf( "%s:%d: Failed to create mono object for %s::%s\n", this->GetNameSpace(), this->GetName(), __FILE__, __LINE__ ); g_pModuleManager->ErrorPrintf( "%s:%d: Failed to create mono object for %s::%s\n", GetNameSpace( pMonoClass ), GetName( pMonoClass ), __FILE__, __LINE__ );
return NULL; return nullptr;
} }
MonoMethod* pMonoMethod = this->GetMethod( ".ctor", argc ); MonoMethod* pMonoMethod = CMonoClass::GetMethod( pMonoClass, ".ctor", argc );
if( !pMonoMethod ) if( !pMonoMethod )
{ {
g_pModuleManager->ErrorPrintf( "%s:%d: Could not lookup constructor for class %s::%s\n", this->GetNameSpace(), this->GetName(), __FILE__, __LINE__ ); g_pModuleManager->ErrorPrintf( "%s:%d: Could not lookup constructor for class %s::%s\n", GetNameSpace( pMonoClass ), GetName( pMonoClass ), __FILE__, __LINE__ );
return NULL; return nullptr;
} }
mono_runtime_invoke( pMonoMethod, pObject, args, NULL ); mono_runtime_invoke( pMonoMethod, pObject, args, nullptr );
return new CMonoObject( pObject ); return pObject;
} }
const char* CMonoClass::GetName() const char* CMonoClass::GetName( MonoClass* pMonoClass )
{ {
return mono_class_get_name( this->m_pMonoClass ); return mono_class_get_name( pMonoClass );
} }
const char* CMonoClass::GetNameSpace() const char* CMonoClass::GetNameSpace( MonoClass* pMonoClass )
{ {
return mono_class_get_namespace( this->m_pMonoClass ); return mono_class_get_namespace( pMonoClass );
} }
MonoMethod* CMonoClass::GetMethod( const char* szMethodName, int iParamCount ) MonoMethod* CMonoClass::GetMethod( MonoClass* pMonoClass, const char* szMethodName, int iParamCount )
{ {
return mono_class_get_method_from_name( this->m_pMonoClass, szMethodName, iParamCount ); return mono_class_get_method_from_name( pMonoClass, szMethodName, iParamCount );
} }
MonoMethod* CMonoClass::GetMethod( const char* szMethodName ) MonoMethod* CMonoClass::GetMethod( MonoClass* pMonoClass, const char* szMethodName )
{ {
MonoMethodDesc* pMonoMethodDesc = mono_method_desc_new( szMethodName, false ); MonoMethodDesc* pMonoMethodDesc = mono_method_desc_new( szMethodName, false );
if( pMonoMethodDesc ) if( pMonoMethodDesc )
{ {
MonoMethod* pMethod = mono_method_desc_search_in_class( pMonoMethodDesc, this->m_pMonoClass ); MonoMethod* pMethod = mono_method_desc_search_in_class( pMonoMethodDesc, pMonoClass );
mono_method_desc_free( pMonoMethodDesc ); mono_method_desc_free( pMonoMethodDesc );
return pMethod; return pMethod;
} }
return NULL; return nullptr;
} }

View File

@ -4,37 +4,25 @@ class CMonoClass;
#define _C_MONO_CLASS #define _C_MONO_CLASS
#include "Common.h" #include "Common.h"
#include "CMonoObject.h"
extern ILuaModuleManager10 *g_pModuleManager; extern ILuaModuleManager10 *g_pModuleManager;
class CMonoClass class CMonoClass
{ {
private:
MonoClass* m_pMonoClass;
public: public:
CMonoClass( MonoClass* pMonoClass ); static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain );
~CMonoClass(); static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, SColor& pColor );
static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector2& vecVector );
static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector3& vecVector );
static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, void** args, int argc );
CMonoObject* New( MonoDomain* pMonoDomain ); static const char* GetName( MonoClass* pMonoClass );
CMonoObject* New( MonoDomain* pMonoDomain, SColor& pColor ); static const char* GetNameSpace( MonoClass* pMonoClass );
CMonoObject* New( MonoDomain* pMonoDomain, Vector2& vecVector );
CMonoObject* New( MonoDomain* pMonoDomain, Vector3& vecVector );
CMonoObject* New( MonoDomain* pMonoDomain, void** args, int argc );
const char* GetName(); static MonoMethod* GetMethod( MonoClass* pMonoClass, const char* szMethodName, int iParamCount );
const char* GetNameSpace(); static MonoMethod* GetMethod( MonoClass* pMonoClass, const char* szMethodName );
MonoMethod* GetMethod( const char* szMethodName, int iParamCount ); static MonoClass* GetClass( MonoClass* pMonoClass );
MonoMethod* GetMethod( const char* szMethodName );
MonoClass* GetMonoClass() { return this->m_pMonoClass; };
MonoClass* GetClass()
{
return this->m_pMonoClass;
}
}; };
#endif #endif

View File

@ -12,60 +12,43 @@
#include "CMonoObject.h" #include "CMonoObject.h"
CMonoObject::CMonoObject( MonoObject* pMonoObject ) MonoClass* CMonoObject::GetClass( MonoObject* pObject )
{ {
this->m_pMonoObject = pMonoObject; return mono_object_get_class( pObject );
} }
MonoObject* CMonoObject::GetPropertyValue( MonoObject* pObject, const char* szPropertyName )
CMonoObject::~CMonoObject()
{ {
} MonoClass* pMonoClass = mono_object_get_class( pObject );
CMonoClass* CMonoObject::GetClass()
{
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject );
if( pMonoClass )
{
return new CMonoClass( pMonoClass );
}
return NULL;
}
MonoObject* CMonoObject::GetPropertyValue( const char* szPropertyName )
{
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject );
MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName ); MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName );
if( !pMonoProperty ) if( !pMonoProperty )
{ {
return NULL; return nullptr;
} }
return mono_property_get_value( pMonoProperty, this->m_pMonoObject, NULL, NULL ); return mono_property_get_value( pMonoProperty, pObject, NULL, NULL );
} }
bool CMonoObject::SetPropertyValue( const char* szPropertyName, int iValue ) bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, int iValue )
{ {
return this->SetPropertyValue( szPropertyName, (void*)&iValue ); return CMonoObject::SetPropertyValue( pObject, szPropertyName, (void*)&iValue );
} }
bool CMonoObject::SetPropertyValue( const char* szPropertyName, float fValue ) bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, float fValue )
{ {
return this->SetPropertyValue( szPropertyName, (void*)&fValue ); return CMonoObject::SetPropertyValue( pObject, szPropertyName, (void*)&fValue );
} }
bool CMonoObject::SetPropertyValue( const char* szPropertyName, char* szValue ) bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, char* szValue )
{ {
return this->SetPropertyValue( szPropertyName, mono_string_new( mono_domain_get(), szValue ) ); return CMonoObject::SetPropertyValue( pObject, szPropertyName, mono_string_new( mono_domain_get(), szValue ) );
} }
bool CMonoObject::SetPropertyValue( const char* szPropertyName, gpointer gValue ) bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, gpointer gValue )
{ {
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject ); MonoClass* pMonoClass = mono_object_get_class( pObject );
MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName ); MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName );
@ -76,7 +59,7 @@ bool CMonoObject::SetPropertyValue( const char* szPropertyName, gpointer gValue
gpointer args[ 1 ] = { gValue }; gpointer args[ 1 ] = { gValue };
mono_property_set_value( pMonoProperty, this->m_pMonoObject, args, NULL ); mono_property_set_value( pMonoProperty, pObject, args, NULL );
return true; return true;
} }

View File

@ -16,73 +16,44 @@ class CMonoObject;
#define _C_MONO_OBJECT #define _C_MONO_OBJECT
#include "Common.h" #include "Common.h"
#include "CMonoClass.h"
class CMonoObject class CMonoObject
{ {
private:
MonoObject* m_pMonoObject;
public: public:
CMonoObject( MonoObject* pMonoObject ); static MonoClass* GetClass( MonoObject* pMonoObject );
~CMonoObject();
CMonoClass* GetClass(); static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, int iValue );
static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, float fValue );
static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, char* szValue );
static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, gpointer gValue );
bool SetPropertyValue( const char* szPropertyName, int iValue ); static MonoObject* GetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName );
bool SetPropertyValue( const char* szPropertyName, float fValue );
bool SetPropertyValue( const char* szPropertyName, char* szValue );
bool SetPropertyValue( const char* szPropertyName, gpointer gValue );
MonoObject* GetPropertyValue( const char* szPropertyName ); static SColor GetColor( MonoObject* pMonoObject )
Vector2 GetVector2()
{
float fX = this->GetPropertyValue< float >( "X" );
float fY = this->GetPropertyValue< float >( "Y" );
return Vector2( fX, fY );
}
Vector3 GetVector3()
{
float fX = this->GetPropertyValue< float >( "X" );
float fY = this->GetPropertyValue< float >( "Y" );
float fZ = this->GetPropertyValue< float >( "Z" );
return Vector3( fX, fY, fZ );
}
SColor GetColor()
{ {
SColor pColor; SColor pColor;
pColor.R = this->GetPropertyValue< unsigned char >( "R" ); pColor.R = GetPropertyValue< unsigned char >( pMonoObject, "R" );
pColor.G = this->GetPropertyValue< unsigned char >( "G" ); pColor.G = GetPropertyValue< unsigned char >( pMonoObject, "G" );
pColor.B = this->GetPropertyValue< unsigned char >( "B" ); pColor.B = GetPropertyValue< unsigned char >( pMonoObject, "B" );
pColor.A = this->GetPropertyValue< unsigned char >( "A" ); pColor.A = GetPropertyValue< unsigned char >( pMonoObject, "A" );
return pColor; return pColor;
} }
template <class T> T GetPropertyValue( char* szPropertyName ) template <class T> static T GetPropertyValue( MonoObject* pMonoObject, char* szPropertyName )
{ {
return *( reinterpret_cast<T*>( mono_object_unbox( this->GetPropertyValue( szPropertyName ) ) ) ); return *( reinterpret_cast<T*>( mono_object_unbox( GetPropertyValue( pMonoObject, szPropertyName ) ) ) );
} }
void* GetValue() static void* GetValue( MonoObject* pMonoObject )
{ {
return mono_object_unbox( this->m_pMonoObject ); return mono_object_unbox( pMonoObject );
} }
template <class T> T GetValue() template <class T> static T GetValue( MonoObject* pMonoObject )
{ {
return *( reinterpret_cast<T*>( mono_object_unbox( this->m_pMonoObject ) ) ); return *( reinterpret_cast<T*>( mono_object_unbox( pMonoObject ) ) );
}
MonoObject *GetObject()
{
return this->m_pMonoObject;
} }
}; };