KX_LibLoadStatus.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Contributor(s): Mitchell Stokes
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file KX_LibLoadStatus.cpp
  23. * \ingroup bgeconv
  24. */
  25. #include "KX_LibLoadStatus.h"
  26. #include "PIL_time.h"
  27. KX_LibLoadStatus::KX_LibLoadStatus(class KX_BlenderSceneConverter* kx_converter,
  28. class KX_KetsjiEngine* kx_engine,
  29. class KX_Scene* merge_scene,
  30. const char *path) :
  31. m_converter(kx_converter),
  32. m_engine(kx_engine),
  33. m_mergescene(merge_scene),
  34. m_data(NULL),
  35. m_libname(path),
  36. m_progress(0.0f),
  37. m_finished(false)
  38. #ifdef WITH_PYTHON
  39. ,
  40. m_finish_cb(NULL),
  41. m_progress_cb(NULL)
  42. #endif
  43. {
  44. m_endtime = m_starttime = PIL_check_seconds_timer();
  45. }
  46. void KX_LibLoadStatus::Finish()
  47. {
  48. m_finished = true;
  49. m_progress = 1.f;
  50. m_endtime = PIL_check_seconds_timer();
  51. RunFinishCallback();
  52. RunProgressCallback();
  53. }
  54. void KX_LibLoadStatus::RunFinishCallback()
  55. {
  56. #ifdef WITH_PYTHON
  57. if (m_finish_cb) {
  58. PyObject* args = Py_BuildValue("(O)", GetProxy());
  59. if (!PyObject_Call(m_finish_cb, args, NULL)) {
  60. PyErr_Print();
  61. PyErr_Clear();
  62. }
  63. Py_DECREF(args);
  64. }
  65. #endif
  66. }
  67. void KX_LibLoadStatus::RunProgressCallback()
  68. {
  69. // Progess callbacks are causing threading problems with Python, so they're disabled for now
  70. #if 0
  71. #ifdef WITH_PYTHON
  72. if (m_progress_cb) {
  73. //PyGILState_STATE gstate = PyGILState_Ensure();
  74. PyObject* args = Py_BuildValue("(O)", GetProxy());
  75. if (!PyObject_Call(m_progress_cb, args, NULL)) {
  76. PyErr_Print();
  77. PyErr_Clear();
  78. }
  79. Py_DECREF(args);
  80. //PyGILState_Release(gstate);
  81. }
  82. #endif
  83. #endif
  84. }
  85. class KX_BlenderSceneConverter *KX_LibLoadStatus::GetConverter()
  86. {
  87. return m_converter;
  88. }
  89. class KX_KetsjiEngine *KX_LibLoadStatus::GetEngine()
  90. {
  91. return m_engine;
  92. }
  93. class KX_Scene *KX_LibLoadStatus::GetMergeScene()
  94. {
  95. return m_mergescene;
  96. }
  97. void KX_LibLoadStatus::SetLibName(const char *name)
  98. {
  99. m_libname = name;
  100. }
  101. const char *KX_LibLoadStatus::GetLibName()
  102. {
  103. return m_libname;
  104. }
  105. void KX_LibLoadStatus::SetData(void *data)
  106. {
  107. m_data = data;
  108. }
  109. void *KX_LibLoadStatus::GetData()
  110. {
  111. return m_data;
  112. }
  113. void KX_LibLoadStatus::SetProgress(float progress)
  114. {
  115. m_progress = progress;
  116. RunProgressCallback();
  117. }
  118. float KX_LibLoadStatus::GetProgress()
  119. {
  120. return m_progress;
  121. }
  122. void KX_LibLoadStatus::AddProgress(float progress)
  123. {
  124. m_progress += progress;
  125. RunProgressCallback();
  126. }
  127. #ifdef WITH_PYTHON
  128. PyMethodDef KX_LibLoadStatus::Methods[] =
  129. {
  130. {NULL} //Sentinel
  131. };
  132. PyAttributeDef KX_LibLoadStatus::Attributes[] = {
  133. KX_PYATTRIBUTE_RW_FUNCTION("onFinish", KX_LibLoadStatus, pyattr_get_onfinish, pyattr_set_onfinish),
  134. // KX_PYATTRIBUTE_RW_FUNCTION("onProgress", KX_LibLoadStatus, pyattr_get_onprogress, pyattr_set_onprogress),
  135. KX_PYATTRIBUTE_FLOAT_RO("progress", KX_LibLoadStatus, m_progress),
  136. KX_PYATTRIBUTE_STRING_RO("libraryName", KX_LibLoadStatus, m_libname),
  137. KX_PYATTRIBUTE_RO_FUNCTION("timeTaken", KX_LibLoadStatus, pyattr_get_timetaken),
  138. KX_PYATTRIBUTE_BOOL_RO("finished", KX_LibLoadStatus, m_finished),
  139. { NULL } //Sentinel
  140. };
  141. PyTypeObject KX_LibLoadStatus::Type = {
  142. PyVarObject_HEAD_INIT(NULL, 0)
  143. "KX_LibLoadStatus",
  144. sizeof(PyObjectPlus_Proxy),
  145. 0,
  146. py_base_dealloc,
  147. 0,
  148. 0,
  149. 0,
  150. 0,
  151. py_base_repr,
  152. 0,0,0,0,0,0,0,0,0,
  153. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  154. 0,0,0,0,0,0,0,
  155. Methods,
  156. 0,
  157. 0,
  158. &PyObjectPlus::Type,
  159. 0,0,0,0,0,0,
  160. py_base_new
  161. };
  162. PyObject* KX_LibLoadStatus::pyattr_get_onfinish(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
  163. {
  164. KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v);
  165. if (self->m_finish_cb) {
  166. Py_INCREF(self->m_finish_cb);
  167. return self->m_finish_cb;
  168. }
  169. Py_RETURN_NONE;
  170. }
  171. int KX_LibLoadStatus::pyattr_set_onfinish(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
  172. {
  173. KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v);
  174. if (!PyCallable_Check(value)) {
  175. PyErr_SetString(PyExc_TypeError, "KX_LibLoadStatus.onFinished requires a callable object");
  176. return PY_SET_ATTR_FAIL;
  177. }
  178. if (self->m_finish_cb)
  179. Py_DECREF(self->m_finish_cb);
  180. Py_INCREF(value);
  181. self->m_finish_cb = value;
  182. return PY_SET_ATTR_SUCCESS;
  183. }
  184. PyObject* KX_LibLoadStatus::pyattr_get_onprogress(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
  185. {
  186. KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v);
  187. if (self->m_progress_cb) {
  188. Py_INCREF(self->m_progress_cb);
  189. return self->m_progress_cb;
  190. }
  191. Py_RETURN_NONE;
  192. }
  193. int KX_LibLoadStatus::pyattr_set_onprogress(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
  194. {
  195. KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v);
  196. if (!PyCallable_Check(value)) {
  197. PyErr_SetString(PyExc_TypeError, "KX_LibLoadStatus.onProgress requires a callable object");
  198. return PY_SET_ATTR_FAIL;
  199. }
  200. if (self->m_progress_cb)
  201. Py_DECREF(self->m_progress_cb);
  202. Py_INCREF(value);
  203. self->m_progress_cb = value;
  204. return PY_SET_ATTR_SUCCESS;
  205. }
  206. PyObject* KX_LibLoadStatus::pyattr_get_timetaken(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
  207. {
  208. KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v);
  209. return PyFloat_FromDouble(self->m_endtime - self->m_starttime);
  210. }
  211. #endif // WITH_PYTHON