Browse Source

Now all the kids can play with ConvertString (and some others)

Adam Johnson 13 years ago
parent
commit
a64b909f4f
  1. 1
      Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt
  2. 63
      Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.cpp
  3. 6
      Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h
  4. 21
      Sources/Plasma/FeatureLib/pfPython/pyPlayerGlue.cpp

1
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

63
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 <http://www.gnu.org/licenses/>.
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));
}

6
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 <Python.h>
// 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
/////////////////////////////////////////////////////////////////////

21
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;

Loading…
Cancel
Save