From a64b909f4f3240246cee97dc3e1f655c7259fe6f Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sat, 28 Jan 2012 21:36:38 -0500 Subject: [PATCH] Now all the kids can play with ConvertString (and some others) --- .../Plasma/FeatureLib/pfPython/CMakeLists.txt | 1 + .../FeatureLib/pfPython/pyGlueHelpers.cpp | 63 +++++++++++++++++++ .../FeatureLib/pfPython/pyGlueHelpers.h | 6 ++ .../FeatureLib/pfPython/pyPlayerGlue.cpp | 21 +------ 4 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp diff --git a/Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt b/Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt index c005002c..b390708f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt +++ b/Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt @@ -38,6 +38,7 @@ set(pfPython_SOURCES pyEnum.cpp pyGameScore.cpp pyGeometry3.cpp + pyGlueHelpers.cpp pyGrassShader.cpp pyGUIControl.cpp pyGUIControlButton.cpp diff --git a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp new file mode 100644 index 00000000..e6dbf4a9 --- /dev/null +++ b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp @@ -0,0 +1,63 @@ +/*==LICENSE==* + +CyanWorlds.com Engine - MMOG client, server and tools +Copyright (C) 2011 Cyan Worlds, Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Additional permissions under GNU GPL version 3 section 7 + +If you modify this Program, or any covered work, by linking or +combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK, +NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent +JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK +(or a modified version of those libraries), +containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA, +PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG +JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the +licensors of this Program grant you additional +permission to convey the resulting work. Corresponding Source for a +non-source form of such a combination shall include the source code for +the parts of OpenSSL and IJG JPEG Library used as well as that of the covered +work. + +You can contact Cyan Worlds, Inc. by email legal@cyan.com + or by snail mail at: + Cyan Worlds, Inc. + 14617 N Newport Hwy + Mead, WA 99021 + +*==LICENSE==*/ + +#include "HeadSpin.h" +#include "pyGlueHelpers.h" + +char* PyString_AsStringEx(PyObject* obj) +{ + if (PyString_Check(obj)) + return hsStrcpy(PyString_AsString(obj)); + else if (PyUnicode_Check(obj)) + { + PyObject* utf8 = PyUnicode_AsUTF8String(obj); + char* buf = hsStrcpy(PyString_AsString(utf8)); + Py_DECREF(utf8); + return buf; + } else + return NULL; // You suck. +} + +bool PyString_CheckEx(PyObject* obj) +{ + return (PyString_Check(obj) || PyUnicode_Check(obj)); +} diff --git a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h index 1184c1ce..26cf4d5f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h @@ -42,6 +42,12 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #ifndef _pyGlueHelpers_h_ #define _pyGlueHelpers_h_ +#include + +// Useful string functions +char* PyString_AsStringEx(PyObject*); +inline bool PyString_CheckEx(PyObject*); + // A set of macros to take at least some of the tediousness out of creating straight python glue code ///////////////////////////////////////////////////////////////////// diff --git a/Sources/Plasma/FeatureLib/pfPython/pyPlayerGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyPlayerGlue.cpp index e64ee87f..f2dd94bd 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyPlayerGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyPlayerGlue.cpp @@ -49,23 +49,6 @@ PYTHON_CLASS_DEFINITION(ptPlayer, pyPlayer); PYTHON_DEFAULT_NEW_DEFINITION(ptPlayer, pyPlayer) PYTHON_DEFAULT_DEALLOC_DEFINITION(ptPlayer) -static char* ConvertString(PyObject* obj) -{ - if (PyString_Check(obj)) - { - return hsStrcpy(PyString_AsString(obj)); - } else if (PyUnicode_Check(obj)) { - size_t size = PyUnicode_GetSize(obj); - wchar_t* temp = new wchar_t[size + 1]; - PyUnicode_AsWideChar((PyUnicodeObject*)obj, temp, size); - temp[size] = 0; - char* buf = hsWStringToString(temp); - delete[] temp; - return buf; - } else - return NULL; // You suck. -} - PYTHON_INIT_DEFINITION(ptPlayer, args, keywords) { // we have two sets of arguments we can use, hence the generic PyObject* pointers @@ -89,7 +72,7 @@ PYTHON_INIT_DEFINITION(ptPlayer, args, keywords) if (pyKey::Check(firstObj)) { key = pyKey::ConvertFrom(firstObj); - if (!(name = ConvertString(secondObj))) + if (!(name = PyString_AsStringEx(secondObj))) { PyErr_SetString(PyExc_TypeError, "__init__ expects one of two argument lists: (ptKey, string, unsigned long, float) or (string, unsigned long)"); PYTHON_RETURN_INIT_ERROR; @@ -103,7 +86,7 @@ PYTHON_INIT_DEFINITION(ptPlayer, args, keywords) pid = PyLong_AsUnsignedLong(thirdObj); distSeq = (float)PyFloat_AsDouble(fourthObj); - } else if (name = ConvertString(firstObj)){ + } else if (name = PyString_AsStringEx(firstObj)){ if (!PyLong_Check(secondObj) || thirdObj || fourthObj) { delete[] name;