Commit b6d783af authored by gerstrong@gmail.com's avatar gerstrong@gmail.com

Improved performance of Python code

parent 674a4a5f
......@@ -55,6 +55,29 @@ void duplicateBackslashes(std::string &text)
#endif
}
GsPython::GsPython()
{
Py_Initialize();
}
GsPython::~GsPython()
{
Py_Finalize();
}
GsPythonModule::~GsPythonModule()
{
Py_XDECREF(mpModule);
}
GsPythonFunc::~GsPythonFunc()
{
Py_XDECREF(mpFunction);
}
bool
GsPythonModule::
load(const std::string &modName,
......@@ -65,7 +88,6 @@ load(const std::string &modName,
return (mpModule != nullptr) ? true : false;
}
bool
GsPythonFunc::
load(GsPythonModule &module,
......@@ -95,30 +117,18 @@ load(GsPythonModule &module,
}
void
PyObject*
GsPythonFunc::call()
{
/*PyObject *pValue = */PyObject_CallObject(mpFunction, nullptr);
PyObject *pValue = PyObject_CallObject(mpFunction, nullptr);
/*if (pValue != nullptr)
{
if( PyObject_IsTrue(pValue) )
{
value = true;
}
else
{
value = false;
}
Py_DECREF(pValue);
}
else
if (pValue == nullptr)
{
Py_DECREF(pFunc);
PyErr_Print();
gLogging.ftextOut("Call failed\n");
return false;
}*/
}
return pValue;
}
......@@ -152,10 +162,7 @@ PyObject *GsPython::loadModule(const std::string &scriptBaseName,
const std::string pythonHome = JoinPaths(searchPath,"python3.5");
setenv("PYTHONHOME", pythonHome.c_str(), 1);
setenv("PYTHONPATH", pythonHome.c_str(), 1);
#endif
Py_Initialize();
#endif
PyObject* programName = PyUnicode_FromString(scriptBaseName.c_str());
......
......@@ -18,10 +18,7 @@ public:
bool load(const std::string &modName,
const std::string &dirPath);
~GsPythonModule()
{
Py_XDECREF(mpModule);
}
virtual ~GsPythonModule();
PyObject *rawPtr() const
{
......@@ -45,6 +42,8 @@ class GsPythonFunc
public:
virtual ~GsPythonFunc();
bool load(GsPythonModule &module,
const std::string &funcName);
......@@ -55,12 +54,7 @@ public:
}
~GsPythonFunc()
{
Py_XDECREF(mpFunction);
}
void call();
PyObject *call();
private:
PyObject *mpFunction = nullptr;
......@@ -72,6 +66,10 @@ class GsPython : public GsSingleton<GsPython>
{
public:
GsPython();
virtual ~GsPython();
/**
* @brief loadModule Load the module and return as python object
* @param scriptBaseName name of script to load
......
......@@ -186,7 +186,7 @@ bool CSpriteObject::loadPythonScripts(const std::string &scriptBaseName)
return false;
}
Py_Finalize();
//Py_Finalize();
return true;
......
......@@ -113,7 +113,7 @@ class CSpriteObject
{
public:
CSpriteObject(CMap *pmap, Uint32 x, Uint32 y, const int spriteVar);
unsigned int m_index; // Like an ID for some objects that need this implementation
......@@ -399,6 +399,10 @@ class CSpriteObject
* @return if load was successful true, otherwise false.
*/
virtual bool loadPythonScripts(const std::string &scriptBaseName);
GsPythonModule mModule;
GsPythonFunc mProcessFunc;
#endif
CMap *mpMap;
......
......@@ -119,7 +119,7 @@ bool CAmpton::loadPythonScripts(const std::string &scriptBaseName)
return false;
}
Py_Finalize();
//Py_Finalize();
#endif
return true;
......
......@@ -171,7 +171,7 @@ bool CFuse::loadPythonScripts(const std::string &scriptBaseName)
return false;
}
Py_Finalize();
//Py_Finalize();
#endif
return true;
......
......@@ -100,7 +100,7 @@ bool CRoboRed::loadPythonScripts(const std::string &scriptBaseName)
return false;
}
Py_Finalize();
//Py_Finalize();
#endif
return true;
......
......@@ -12,6 +12,7 @@
#include "../../common/ai/CEnemyShot.h"
#include <base/utils/misc.h>
/*
$2D10W #Shikadi stand
$2D2EW #Shikadi stand
......@@ -41,7 +42,7 @@ A_SHIKADI_STUNNED = 10
constexpr int WALK_SPEED = 25;
constexpr int TIME_UNTIL_STAND = 150;
constexpr int TIME_TO_ZAP = 30;
//constexpr int TIME_TO_ZAP = 30;
CShikadi::CShikadi(CMap *pmap, const Uint16 foeID, const Uint32 x, const Uint32 y) :
CStunnable(pmap, foeID, x, y),
......@@ -73,14 +74,15 @@ bool CShikadi::loadPythonScripts(const std::string &scriptBaseName)
mp_processState = static_cast<GASOFctr>(&CShikadi::processPython);
Py_DECREF(pModule);
mModule.load( "shikadi", JoinPaths(gKeenFiles.gameDir ,"ai") );
mProcessFunc.load(mModule, "process");
}
else
{
return false;
}
Py_Finalize();
//Py_Finalize();
#endif
return true;
}
......@@ -88,7 +90,42 @@ bool CShikadi::loadPythonScripts(const std::string &scriptBaseName)
void CShikadi::processPython()
{
// TODO: Needs to be put into members of class. This part is yet too CPU heavy
// NOTE: My tests for using Pyton in CG. These will be forwared to a more common function
// The following will atempt to get a walk instruction from Python process.
if (mProcessFunc)
{
PyObject *pDict = mProcessFunc.call();
// Process return a dictionary of tasks to perform with some values
if (pDict != nullptr)
{
// TODO: The commented still crashes. I think it is related to some DECREF
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(pDict, &pos, &key, &value))
{
PyObject* repr = PyObject_Repr(key);
PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
const std::string keyAsStr = PyBytes_AS_STRING(str);
const long iValue = PyLong_AsLong(value);
//gLogging.ftextOut("%s : %ld\n", bytes, iValue);
if(keyAsStr == "'walk'")
{
moveXDir(iValue);
}
Py_XDECREF(repr);
Py_XDECREF(str);
}
Py_DECREF(pDict);
}
}
}
......
......@@ -79,7 +79,7 @@ bool CShikadiMaster::loadPythonScripts(const std::string &scriptBaseName)
return false;
}
Py_Finalize();
//Py_Finalize();
#endif
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment