Author Topic: Get useful Infos using AngelScript and C++  (Read 4108 times)

Offline adumbperson

  • Turn around in orbit!
Get useful Infos using AngelScript and C++
« on: June 14, 2016, 04:09:54 am »
3 functions exploiting AngelScript's API to reflect global vars/funcs also classes, their properties and member functions.
To use the functions you have to bind their calls using _defines.fos (look at end) and add it:
Code: [Select]
// Test function
# ifdef __SERVER
#  pragma bindfunc "void GetObjectType()      -> fonline_test.dll GetObjectType"
#  pragma bindfunc "void GetGlobalFunc()      -> fonline_test.dll GetGlobalFunc"
#  pragma bindfunc "void GetGlobalProperty()  -> fonline_test.dll GetGlobalProperty"
# endif
# ifdef __CLIENT
#  pragma bindfunc "void GetObjectType()      -> fonline_test_client.dll GetObjectType"
#  pragma bindfunc "void GetGlobalFunc()      -> fonline_test_client.dll GetGlobalFunc"
#  pragma bindfunc "void GetGlobalProperty()  -> fonline_test_client.dll GetGlobalProperty"
also you shud add it to fonline_test.cpp
Code: [Select]
EXPORT void GetGlobalProperty()
{
    // global properties
    Log("##################### global properties #####################\n");
    for( asUINT i = 0; i < ASEngine->GetGlobalPropertyCount(); i++ )
    {
        const char *name;
        const char *pnamespace;
        int typeId;
        bool isConst;
        const char *configGroup;
        void *pointer;
        asDWORD accessMask;
        ASEngine->GetGlobalPropertyByIndex( i, &name, &pnamespace, &typeId, &isConst, &configGroup, &pointer, &accessMask);
        if (name) {
            Log("%s %s\n", ASEngine->GetTypeDeclaration(typeId, true), name);
        }
    }
}

EXPORT void GetGlobalFunc()
{
    // global functions
    Log("##################### global functions #####################\n");
    for( asUINT i = 0; i < ASEngine->GetGlobalFunctionCount(); i++ )
    {
        asIScriptFunction * ptr;
        ptr = ASEngine->GetGlobalFunctionByIndex( i );
        if (ptr) {
            Log("%s\n", ptr->GetDeclaration(true,true));
        }
    }
}

EXPORT void GetObjectType()
{
    // objects
    Log("##################### objects #####################\n");
    for( asUINT i = 0; i < ASEngine->GetObjectTypeCount(); i++ )
    {
        asIObjectType * ptr;
        ptr = ASEngine->GetObjectTypeByIndex( i );
        Log("##################### %s #####################\n", ptr->GetName());
        // through member properties
        for(asUINT p = 0; p < ptr->GetPropertyCount(); ++p)
            Log("%s\n", ptr->GetPropertyDeclaration(p) );

        // through member functions
        for (asUINT x = 0; x < ptr->GetMethodCount(); ++x)
        {
            asIScriptFunction * assf;
            assf = ptr->GetMethodByIndex(x, false);
            Log("%s\n", assf->GetDeclaration(true,false) );
        }
    }
}

Then after that you should compile the dlls... and you can start using it directly with AScompiler.exe:
Code: [Select]
ASCompiler.exe runme.fos -run main  > smartsense.h
ASCompiler.exe runme.fos -client -run main  > smartsense.h
something alike depending your environment...

runme.fos:
Code: [Select]
#include "_defines.fos"

void main()
{
    GetGlobalProperty();
    GetGlobalFunc();
    GetObjectType();
}

Thank to wipe.
« Last Edit: June 14, 2016, 08:20:22 am by adumbperson »
The Death After Life.
About me. I hate people generally I still may have respectful talk and recognize the good persons. Careful to what you say, I'm not a kid. My experiences defines me.
[Awaxx] -2024/10/17-