Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code deduplication for plPythonSDLModifier. #1465

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected]
#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)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:trollface:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably one of those used to be a PyInt_*... I hope >.>

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