<commit_before>#ifdef HAVE_PYTHON
#include <Python.h>
#endif 

#include "script.h"
#include "object.h"
#include "python.h"

#include "platformer/game/camera.h"
#include "platformer/game/world.h"
#include "platformer/object/object.h"
#include "platformer/game/collision-map.h"

#include "util/file-system.h"
#include "util/funcs.h"

#ifdef HAVE_PYTHON

static PyObject * createObject(PyObject *, PyObject * args){
    PyObject * worldObject;
    char * module;
    char * initFunction;

    if (PyArg_ParseTuple(args, "Oss", &worldObject, &module, &initFunction)){
        Platformer::World * world = reinterpret_cast<Platformer::World*>(PyCapsule_GetPointer(worldObject, "world"));
        Util::ReferenceCount<Platformer::Object> object(new Platformer::ScriptObject(module, initFunction));
        world->addObject(object);
    }
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef Methods[] = {
    {"createObject",  createObject, METH_VARARGS, "Create a new object."},
    {NULL, NULL, 0, NULL}
};

// Python script support
class Python : public Platformer::Scriptable{
public:
    Python(Platformer::World * world):
    world(world){
        Py_Initialize();
        Py_InitModule("platformer", Methods);
        Py_InitModule("platformer_object", Platformer::ScriptObject::Methods);
    }
    
    virtual ~Python(){
        Py_Finalize();
    }
    
    void act(){
    }
    
    void render(const Platformer::Camera & camera){
    }
    
    void loadScript(const std::string & module, const std::string & func){
        Platformer::Script::Runnable runnable(module, func);
        PyObject * worldObject = PyCapsule_New((void *) world, "world", NULL);
        if (worldObject == NULL){
            PyErr_Print();
        }
        
        // Execute
        Platformer::Script::AutoRef function = runnable.getFunction();
        PyObject * result = PyObject_CallFunction(function.getObject(), (char*) "(O)", worldObject);
        if (result == NULL){
            PyErr_Print();
        }
        Py_DECREF(worldObject);
        Py_DECREF(result);
    }

    Platformer::World * world;
};

#endif

Util::ReferenceCount<Platformer::Scriptable> Platformer::Scriptable::getInstance(World * world){
#ifdef HAVE_PYTHON
    return Util::ReferenceCount<Platformer::Scriptable>(new Python(world));
#else
    // No script support
    class NoScript : public Platformer::Scriptable{
    public:
        NoScript(){
        }
        virtual ~NoScript(){
        }
        
        void act(){
        }
        
        void render(const Camera &){
        }
        void registerAnimation(void *){
        }
    };
    return Util::ReferenceCount<Platformer::Scriptable>(new NoScript());
#endif
}<commit_msg>[platformer] Fix if python is not available and remove warning.<commit_after>#ifdef HAVE_PYTHON
#include <Python.h>
#endif 

#include "script.h"
#include "object.h"
#include "python.h"

#include "platformer/game/camera.h"
#include "platformer/game/world.h"
#include "platformer/object/object.h"
#include "platformer/game/collision-map.h"

#include "util/file-system.h"
#include "util/funcs.h"

#ifdef HAVE_PYTHON

static PyObject * createObject(PyObject *, PyObject * args){
    PyObject * worldObject;
    char * module;
    char * initFunction;

    if (PyArg_ParseTuple(args, "Oss", &worldObject, &module, &initFunction)){
        Platformer::World * world = reinterpret_cast<Platformer::World*>(PyCapsule_GetPointer(worldObject, "world"));
        Util::ReferenceCount<Platformer::Object> object(new Platformer::ScriptObject(module, initFunction));
        world->addObject(object);
    }
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef Methods[] = {
    {"createObject",  createObject, METH_VARARGS, "Create a new object."},
    {NULL, NULL, 0, NULL}
};

// Python script support
class Python : public Platformer::Scriptable{
public:
    Python(Platformer::World * world):
    world(world){
        Py_Initialize();
        Py_InitModule("platformer", Methods);
        Py_InitModule("platformer_object", Platformer::ScriptObject::Methods);
    }
    
    virtual ~Python(){
        Py_Finalize();
    }
    
    void act(){
    }
    
    void render(const Platformer::Camera & camera){
    }
    
    void loadScript(const std::string & module, const std::string & func){
        Platformer::Script::Runnable runnable(module, func);
        PyObject * worldObject = PyCapsule_New((void *) world, "world", NULL);
        if (worldObject == NULL){
            PyErr_Print();
        }
        
        // Execute
        Platformer::Script::AutoRef function = runnable.getFunction();
        PyObject * result = PyObject_CallFunction(function.getObject(), (char*) "(O)", worldObject);
        if (result == NULL){
            PyErr_Print();
        }
        Py_DECREF(worldObject);
        Py_DECREF(result);
    }

    Platformer::World * world;
};

#endif

Util::ReferenceCount<Platformer::Scriptable> Platformer::Scriptable::getInstance(World * world){
#ifdef HAVE_PYTHON
    return Util::ReferenceCount<Platformer::Scriptable>(new Python(world));
#endif
    // No script support
    class NoScript : public Platformer::Scriptable{
    public:
        NoScript(){
        }
        virtual ~NoScript(){
        }
        
        void act(){
        }
        
        void loadScript(const std::string &, const std::string &){
        }
        
        void render(const Camera &){
        }
        void registerAnimation(void *){
        }
    };
    return Util::ReferenceCount<Platformer::Scriptable>(new NoScript());
}
{'fraction_non_alphanumeric': 0.13341690544412607, 'fraction_numerical': 0.00035816618911174784, 'mean_word_length': 3.073668854850474, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418781', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1668, 'n_words': 339}