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

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;
}
CMonoClass::~CMonoClass()
{
}
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain )
{ {
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",
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 };
return this->New( pMonoDomain, args, 4 );
}
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, Vector2& vecVector )
{
void *args[] = { &vecVector.fX, &vecVector.fY };
return this->New( pMonoDomain, args, 2 );
}
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, Vector3& vecVector )
{
void *args[] = { &vecVector.fX, &vecVector.fY, &vecVector.fZ };
return this->New( pMonoDomain, args, 3 );
}
CMonoObject* CMonoClass::New( MonoDomain* pMonoDomain, void** args, int argc )
{ {
MonoObject* pObject = mono_object_new( pMonoDomain, this->m_pMonoClass ); void *args[] = { &pColor.R, &pColor.G, &pColor.B, &pColor.A };
return CMonoClass::New( pMonoClass, pMonoDomain, args, 4 );
}
MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector2& vecVector )
{
void *args[] = { &vecVector.fX, &vecVector.fY };
return CMonoClass::New( pMonoClass, pMonoDomain, args, 2 );
}
MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector3& vecVector )
{
void *args[] = { &vecVector.fX, &vecVector.fY, &vecVector.fZ };
return CMonoClass::New( pMonoClass, pMonoDomain, args, 3 );
}
MonoObject* CMonoClass::New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, void** args, int argc )
{
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 );
return new CMonoObject( pObject ); mono_runtime_invoke( pMonoMethod, pObject, args, nullptr );
}
return pObject;
const char* CMonoClass::GetName() }
{
return mono_class_get_name( this->m_pMonoClass ); const char* CMonoClass::GetName( MonoClass* pMonoClass )
} {
return mono_class_get_name( pMonoClass );
const char* CMonoClass::GetNameSpace() }
{
return mono_class_get_namespace( this->m_pMonoClass ); const char* CMonoClass::GetNameSpace( MonoClass* pMonoClass )
} {
return mono_class_get_namespace( pMonoClass );
MonoMethod* CMonoClass::GetMethod( const char* szMethodName, int iParamCount ) }
{
return mono_class_get_method_from_name( this->m_pMonoClass, szMethodName, iParamCount ); MonoMethod* CMonoClass::GetMethod( MonoClass* pMonoClass, const char* szMethodName, int 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

@ -1,40 +1,28 @@
class CMonoClass; class CMonoClass;
#ifndef _C_MONO_CLASS #ifndef _C_MONO_CLASS
#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 {
{ public:
private: static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain );
MonoClass* m_pMonoClass; static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, SColor& pColor );
static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector2& vecVector );
public: static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, Vector3& vecVector );
CMonoClass( MonoClass* pMonoClass ); static MonoObject* New( MonoClass* pMonoClass, MonoDomain* pMonoDomain, void** args, int argc );
~CMonoClass();
static const char* GetName( MonoClass* pMonoClass );
CMonoObject* New( MonoDomain* pMonoDomain ); static const char* GetNameSpace( MonoClass* pMonoClass );
CMonoObject* New( MonoDomain* pMonoDomain, SColor& pColor );
CMonoObject* New( MonoDomain* pMonoDomain, Vector2& vecVector ); static MonoMethod* GetMethod( MonoClass* pMonoClass, const char* szMethodName, int iParamCount );
CMonoObject* New( MonoDomain* pMonoDomain, Vector3& vecVector ); static MonoMethod* GetMethod( MonoClass* pMonoClass, const char* szMethodName );
CMonoObject* New( MonoDomain* pMonoDomain, void** args, int argc );
static MonoClass* GetClass( MonoClass* pMonoClass );
const char* GetName(); };
const char* GetNameSpace();
MonoMethod* GetMethod( const char* szMethodName, int iParamCount );
MonoMethod* GetMethod( const char* szMethodName );
MonoClass* GetMonoClass() { return this->m_pMonoClass; };
MonoClass* GetClass()
{
return this->m_pMonoClass;
}
};
#endif #endif

View File

@ -1,82 +1,65 @@
/********************************************************* /*********************************************************
* *
* Copyright © 2013, Innovation Roleplay Engine. * Copyright © 2013, Innovation Roleplay Engine.
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
* with or without modification, * with or without modification,
* is permitted only for authors. * is permitted only for authors.
* *
*********************************************************/ *********************************************************/
#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 );
}
MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName );
CMonoClass* CMonoObject::GetClass()
{ if( !pMonoProperty )
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject ); {
return nullptr;
if( pMonoClass ) }
{
return new CMonoClass( pMonoClass ); return mono_property_get_value( pMonoProperty, pObject, NULL, NULL );
} }
return NULL; bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, int iValue )
} {
return CMonoObject::SetPropertyValue( pObject, szPropertyName, (void*)&iValue );
MonoObject* CMonoObject::GetPropertyValue( const char* szPropertyName ) }
{
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject ); bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, float fValue )
{
MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName ); return CMonoObject::SetPropertyValue( pObject, szPropertyName, (void*)&fValue );
}
if( !pMonoProperty )
{ bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, char* szValue )
return NULL; {
} return CMonoObject::SetPropertyValue( pObject, szPropertyName, mono_string_new( mono_domain_get(), szValue ) );
}
return mono_property_get_value( pMonoProperty, this->m_pMonoObject, NULL, NULL );
} bool CMonoObject::SetPropertyValue( MonoObject* pObject, const char* szPropertyName, gpointer gValue )
{
bool CMonoObject::SetPropertyValue( const char* szPropertyName, int iValue ) MonoClass* pMonoClass = mono_object_get_class( pObject );
{
return this->SetPropertyValue( szPropertyName, (void*)&iValue ); MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName );
}
if( !pMonoProperty )
bool CMonoObject::SetPropertyValue( const char* szPropertyName, float fValue ) {
{ return false;
return this->SetPropertyValue( szPropertyName, (void*)&fValue ); }
}
gpointer args[ 1 ] = { gValue };
bool CMonoObject::SetPropertyValue( const char* szPropertyName, char* szValue )
{ mono_property_set_value( pMonoProperty, pObject, args, NULL );
return this->SetPropertyValue( szPropertyName, mono_string_new( mono_domain_get(), szValue ) );
} return true;
bool CMonoObject::SetPropertyValue( const char* szPropertyName, gpointer gValue )
{
MonoClass* pMonoClass = mono_object_get_class( this->m_pMonoObject );
MonoProperty* pMonoProperty = mono_class_get_property_from_name( pMonoClass, szPropertyName );
if( !pMonoProperty )
{
return false;
}
gpointer args[ 1 ] = { gValue };
mono_property_set_value( pMonoProperty, this->m_pMonoObject, args, NULL );
return true;
} }

View File

@ -1,89 +1,60 @@
/********************************************************* /*********************************************************
* *
* Copyright © 2013, Innovation Roleplay Engine. * Copyright © 2013, Innovation Roleplay Engine.
* *
* All Rights Reserved. * All Rights Reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
* with or without modification, * with or without modification,
* is permitted only for authors. * is permitted only for authors.
* *
*********************************************************/ *********************************************************/
class CMonoObject; class CMonoObject;
#ifndef _C_MONO_OBJECT #ifndef _C_MONO_OBJECT
#define _C_MONO_OBJECT #define _C_MONO_OBJECT
#include "Common.h" #include "Common.h"
#include "CMonoClass.h"
class CMonoObject
class CMonoObject {
{ public:
private: static MonoClass* GetClass( MonoObject* pMonoObject );
MonoObject* m_pMonoObject;
static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, int iValue );
public: static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, float fValue );
CMonoObject( MonoObject* pMonoObject ); static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, char* szValue );
~CMonoObject(); static bool SetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName, gpointer gValue );
CMonoClass* GetClass(); static MonoObject* GetPropertyValue( MonoObject* pMonoObject, const char* szPropertyName );
bool SetPropertyValue( const char* szPropertyName, int iValue ); static SColor GetColor( MonoObject* pMonoObject )
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 );
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;
pColor.R = this->GetPropertyValue< unsigned char >( "R" );
pColor.G = this->GetPropertyValue< unsigned char >( "G" );
pColor.B = this->GetPropertyValue< unsigned char >( "B" );
pColor.A = this->GetPropertyValue< unsigned char >( "A" );
return pColor;
}
template <class T> T GetPropertyValue( char* szPropertyName )
{ {
return *( reinterpret_cast<T*>( mono_object_unbox( this->GetPropertyValue( szPropertyName ) ) ) ); SColor pColor;
}
pColor.R = GetPropertyValue< unsigned char >( pMonoObject, "R" );
void* GetValue() pColor.G = GetPropertyValue< unsigned char >( pMonoObject, "G" );
{ pColor.B = GetPropertyValue< unsigned char >( pMonoObject, "B" );
return mono_object_unbox( this->m_pMonoObject ); pColor.A = GetPropertyValue< unsigned char >( pMonoObject, "A" );
}
return pColor;
template <class T> T GetValue() }
template <class T> static T GetPropertyValue( MonoObject* pMonoObject, char* szPropertyName )
{ {
return *( reinterpret_cast<T*>( mono_object_unbox( this->m_pMonoObject ) ) ); return *( reinterpret_cast<T*>( mono_object_unbox( GetPropertyValue( pMonoObject, szPropertyName ) ) ) );
} }
MonoObject *GetObject() static void* GetValue( MonoObject* pMonoObject )
{ {
return this->m_pMonoObject; return mono_object_unbox( pMonoObject );
} }
};
template <class T> static T GetValue( MonoObject* pMonoObject )
{
return *( reinterpret_cast<T*>( mono_object_unbox( pMonoObject ) ) );
}
};
#endif #endif