Skip to content

Commit

Permalink
Merge pull request #1465 from Hoikas/pysdl_code_dedupe
Browse files Browse the repository at this point in the history
Some code deduplication for plPythonSDLModifier.
  • Loading branch information
Hoikas authored Aug 16, 2023
2 parents 49eb827 + 4c5d799 commit d863078
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "pyObjectRef.h"
#include "cyMisc.h"

#include <optional>

#include "pnNetCommon/plNetApp.h"
#include "pnSceneObject/plSceneObject.h"
#include "plResMgr/plKeyFinder.h"
Expand Down Expand Up @@ -280,10 +282,9 @@ void plPythonSDLModifier::ISetCurrentStateFrom(const plStateDataRecord* srcState
ST::string name = var->GetName();

// Get the SDL value in Python format
PyObject* pyVar = ISDLVarToPython(var);
pyObjectRef pyVar = ISDLVarToPython(var);

SetItem(name, pyVar);
Py_DECREF(pyVar);
SetItem(name, pyVar.Get());
}

// Notify the Python code that we updated the SDL record
Expand Down Expand Up @@ -314,6 +315,49 @@ void plPythonSDLModifier::IDirtySynchState(const ST::string& name, bool sendImme
}
}

template<typename T>
std::optional<T> IConvertPythonNumber(PyObject* o) = delete;

template<>
std::optional<int> IConvertPythonNumber(PyObject* o)
{
if (PyLong_Check(o))
return PyLong_AsLong(o);
if (PyNumber_Check(o)) {
pyObjectRef pyLong = PyNumber_Long(o);
return PyLong_AsLong(pyLong.Get());
}
return std::nullopt;
}

template<>
std::optional<double> IConvertPythonNumber(PyObject* o)
{
if (PyFloat_Check(o))
return PyFloat_AS_DOUBLE(o);
if (PyNumber_Check(o)) {
pyObjectRef pyFloat = PyNumber_Float(o);
// pyFloat might have come from some strange land where they return
// unexpected things, so don't use the unsafe macro here.
return PyFloat_AsDouble(pyFloat.Get());
}
return std::nullopt;
}

template<>
std::optional<float> IConvertPythonNumber(PyObject* o)
{
if (PyFloat_Check(o))
return static_cast<float>(PyFloat_AS_DOUBLE(o));
if (PyNumber_Check(o)) {
pyObjectRef pyFloat = PyNumber_Float(o);
// pyFloat might have come from some strange land where they return
// unexpected things, so don't use the unsafe macro here.
return static_cast<float>(PyFloat_AsDouble(pyFloat.Get()));
}
return std::nullopt;
}

bool plPythonSDLModifier::IPythonVarIdxToSDL(plSimpleStateVariable* var, int varIdx, int type, PyObject* pyVar,
const ST::string& hintstring)
{
Expand All @@ -322,37 +366,17 @@ bool plPythonSDLModifier::IPythonVarIdxToSDL(plSimpleStateVariable* var, int var
case plVarDescriptor::kByte:
case plVarDescriptor::kBool:
case plVarDescriptor::kInt:
if (PyLong_Check(pyVar)) {
int v = PyLong_AsLong(pyVar);
var->Set(v, varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
} else if (PyLong_Check(pyVar)) {
int v = (int)PyLong_AsLong(pyVar);
var->Set(v, varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
} else if (PyFloat_Check(pyVar)) {
int v = (int)PyFloat_AsDouble(pyVar);
var->Set(v, varIdx);
if (auto v = IConvertPythonNumber<int>(pyVar)) {
var->Set(v.value(), varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
}
break;

case plVarDescriptor::kFloat:
if (PyFloat_Check(pyVar)) {
float v = (float)PyFloat_AsDouble(pyVar);
var->Set(v, varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
} else if (PyLong_Check(pyVar)) {
float v = (float)PyLong_AsLong(pyVar);
var->Set(v, varIdx);
if (auto v = IConvertPythonNumber<float>(pyVar)) {
var->Set(v.value(), varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
Expand Down Expand Up @@ -380,15 +404,8 @@ bool plPythonSDLModifier::IPythonVarIdxToSDL(plSimpleStateVariable* var, int var
break;

case plVarDescriptor::kDouble:
if (PyFloat_Check(pyVar)) {
double v = PyFloat_AsDouble(pyVar);
var->Set(v, varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
} else if (PyLong_Check(pyVar)) {
double v = (double)PyLong_AsLong(pyVar);
var->Set(v, varIdx);
if (auto v = IConvertPythonNumber<double>(pyVar)) {
var->Set(v.value(), varIdx);
if (!hintstring.empty())
var->GetNotificationInfo().SetHintString(hintstring);
return true;
Expand Down

0 comments on commit d863078

Please sign in to comment.