@@ -507,6 +507,68 @@ static PyObject *py_ue_iconsole_manager_register_variable_float(PyObject *cls, P
507507 Py_RETURN_NONE;
508508}
509509
510+ void UPythonConsoleDelegate::OnConsoleCommand (const TArray < FString > & InArgs)
511+ {
512+ FScopePythonGIL gil;
513+
514+ PyObject *ret = nullptr ;
515+ if (InArgs.Num () == 0 )
516+ {
517+ ret = PyObject_CallFunction (py_callable, nullptr );
518+ }
519+ else
520+ {
521+ PyObject *py_args = PyTuple_New (InArgs.Num ());
522+ for (int32 i = 0 ; i < InArgs.Num (); i++)
523+ {
524+ PyTuple_SetItem (py_args, i, PyUnicode_FromString (TCHAR_TO_UTF8 (*InArgs[i])));
525+ }
526+ ret = PyObject_CallObject (py_callable, py_args);
527+ Py_DECREF (py_args);
528+ }
529+ if (!ret)
530+ {
531+ unreal_engine_py_log_error ();
532+ return ;
533+ }
534+ Py_DECREF (ret);
535+ }
536+
537+ static PyObject *py_ue_iconsole_manager_register_command (PyObject *cls, PyObject * args)
538+ {
539+ char *key;
540+ PyObject *py_callable;
541+ char *help = nullptr ;
542+ if (!PyArg_ParseTuple (args, " sO|s:register_command" , &key, &py_callable, &help))
543+ {
544+ return nullptr ;
545+ }
546+
547+ if (!PyCallable_Check (py_callable))
548+ {
549+ return PyErr_Format (PyExc_Exception, " argument is not callable" );
550+ }
551+
552+ IConsoleObject *c_object = IConsoleManager::Get ().FindConsoleObject (UTF8_TO_TCHAR (key));
553+ if (c_object)
554+ {
555+ return PyErr_Format (PyExc_Exception, " console object \" %s\" already exists" , key);
556+ }
557+
558+ UPythonConsoleDelegate *py_delegate = NewObject<UPythonConsoleDelegate>();
559+ py_delegate->SetPyCallable (py_callable);
560+ py_delegate->AddToRoot ();
561+ FConsoleCommandWithArgsDelegate console_delegate;
562+ console_delegate.BindUObject (py_delegate, &UPythonConsoleDelegate::OnConsoleCommand);
563+
564+ if (!IConsoleManager::Get ().RegisterConsoleCommand (UTF8_TO_TCHAR (key), help ? UTF8_TO_TCHAR (help) : UTF8_TO_TCHAR (key), console_delegate, 0 ))
565+ {
566+ return PyErr_Format (PyExc_Exception, " unable to register console command \" %s\" " , key);
567+ }
568+
569+ Py_RETURN_NONE;
570+ }
571+
510572static PyMethodDef ue_PyIConsoleManager_methods[] = {
511573 { " get_history" , (PyCFunction)py_ue_iconsole_manager_get_history, METH_VARARGS | METH_CLASS , " " },
512574 { " add_history_entry" , (PyCFunction)py_ue_iconsole_manager_add_history_entry, METH_VARARGS | METH_CLASS , " " },
@@ -533,6 +595,7 @@ static PyMethodDef ue_PyIConsoleManager_methods[] = {
533595 { " register_variable_string" , (PyCFunction)py_ue_iconsole_manager_register_variable_string, METH_VARARGS | METH_CLASS , " " },
534596 { " register_variable_int" , (PyCFunction)py_ue_iconsole_manager_register_variable_int, METH_VARARGS | METH_CLASS , " " },
535597 { " register_variable_float" , (PyCFunction)py_ue_iconsole_manager_register_variable_float, METH_VARARGS | METH_CLASS , " " },
598+ { " register_command" , (PyCFunction)py_ue_iconsole_manager_register_command, METH_VARARGS | METH_CLASS , " " },
536599 { NULL } /* Sentinel */
537600};
538601
0 commit comments