Skip to content

Commit

Permalink
Proper python error handling in script context (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte authored May 21, 2023
1 parent 5525573 commit 68698a2
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 76 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/pvs-studio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ jobs:
sudo apt install g++-11
- name: installing pvs-studio
env:
# The Secret variable setup in GitHub must be in format: "name_or_email key", on a single line
PVS_STUDIO_LICENSE: ${{ secrets.PVS_STUDIO_LICENSE }}
run: |
wget -q -O - https://files.pvs-studio.com/etc/pubkey.txt \
| sudo apt-key add -
Expand Down Expand Up @@ -54,7 +51,7 @@ jobs:

- name: run pvs studio
run: |
pvs-studio-analyzer analyze -f builds/compile_commands.json -j -e include -e docs -e assets -e test_games --disableLicenseExpirationCheck
pvs-studio-analyzer analyze -f builds/compile_commands.json -j -e include -e docs -e assets -e test_games
- name: convert pvs studio report
run: |
Expand Down
4 changes: 4 additions & 0 deletions crescent_py_api/crescent_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,10 @@ def set_time_dilation(value: float) -> None:
def get_time_dilation() -> float:
return crescent_api_internal.world_get_time_dilation()

@staticmethod
def get_delta_time() -> float:
return crescent_api_internal.world_get_delta_time()


# AUDIO MANAGER
class AudioManager:
Expand Down
4 changes: 4 additions & 0 deletions crescent_py_api/crescent_api_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ def world_get_time_dilation() -> float:
return 1.0


def world_get_delta_time() -> float:
return 1.0


def audio_manager_play_sound(path: str, loops: bool) -> None:
pass

Expand Down
8 changes: 4 additions & 4 deletions engine/src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void cre_update() {
}

void cre_process_game_update() {
cre_ec_system_pre_process_all_systems();
cre_ec_system_pre_update_all_systems();

static const uint32_t MILLISECONDS_PER_TICK = 1000; // TODO: Put in another place
static uint32_t lastFrameTime = 0;
Expand All @@ -229,7 +229,7 @@ void cre_process_game_update() {

// Variable Time Step
const float variableDeltaTime = (float) (SDL_GetTicks() - lastFrameTime) / (float) MILLISECONDS_PER_TICK;
cre_ec_system_process_systems(variableDeltaTime);
cre_ec_system_update_systems(variableDeltaTime);

// Fixed Time Step
static uint32_t fixedCurrentTime = 0;
Expand All @@ -246,14 +246,14 @@ void cre_process_game_update() {
while (accumulator >= CRE_GLOBAL_PHYSICS_DELTA_TIME) {
accumulator -= CRE_GLOBAL_PHYSICS_DELTA_TIME;
sf_fixed_update(CRE_GLOBAL_PHYSICS_DELTA_TIME);
cre_ec_system_physics_process_systems(CRE_GLOBAL_PHYSICS_DELTA_TIME);
cre_ec_system_fixed_update_systems(CRE_GLOBAL_PHYSICS_DELTA_TIME);
se_input_clean_up_flags();
}

se_input_clean_up_flags();
lastFrameTime = SDL_GetTicks();

cre_ec_system_post_process_all_systems();
cre_ec_system_post_update_all_systems();
}

void cre_render() {
Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/ecs/system/collision_ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CreEntitySystem* cre_collision_ec_system_create() {

collisionSystem->on_entity_entered_scene_func = collision_system_on_node_entered_scene;
collisionSystem->on_entity_unregistered_func = collision_system_entity_unregistered;
collisionSystem->physics_process_func = collision_system_fixed_update;
collisionSystem->fixed_update_func = collision_system_fixed_update;

CREGameProperties* gameProps = cre_game_props_get();
SE_ASSERT(cre_game_props_get() != NULL);
Expand Down
80 changes: 40 additions & 40 deletions engine/src/core/ecs/system/ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ typedef struct EntitySystemData {
size_t on_entity_end_systems_count;
size_t on_entity_entered_scene_systems_count;
size_t render_systems_count;
size_t pre_process_all_systems_count;
size_t post_process_all_systems_count;
size_t process_systems_count;
size_t physics_process_systems_count;
size_t pre_update_all_systems_count;
size_t post_update_all_systems_count;
size_t update_systems_count;
size_t fixed_update_systems_count;
size_t network_callback_systems_count;
CreEntitySystem* entity_systems[CRE_MAX_COMPONENTS];
CreEntitySystem* on_entity_start_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* on_entity_end_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* on_entity_entered_scene_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* render_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* process_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* pre_process_all_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* post_process_all_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* physics_process_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* update_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* pre_update_all_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* post_update_all_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* fixed_update_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
CreEntitySystem* network_callback_systems[MAX_ENTITY_SYSTEMS_PER_HOOK];
} EntitySystemData;

Expand All @@ -49,10 +49,10 @@ void cre_ec_system_initialize() {
entitySystemData.on_entity_end_systems[i] = NULL;
entitySystemData.on_entity_entered_scene_systems[i] = NULL;
entitySystemData.render_systems[i] = NULL;
entitySystemData.pre_process_all_systems[i] = NULL;
entitySystemData.post_process_all_systems[i] = NULL;
entitySystemData.process_systems[i] = NULL;
entitySystemData.physics_process_systems[i] = NULL;
entitySystemData.pre_update_all_systems[i] = NULL;
entitySystemData.post_update_all_systems[i] = NULL;
entitySystemData.update_systems[i] = NULL;
entitySystemData.fixed_update_systems[i] = NULL;
entitySystemData.network_callback_systems[i] = NULL;
}
// Fill up entity id queue
Expand All @@ -66,10 +66,10 @@ void cre_ec_system_initialize() {
entitySystemData.on_entity_end_systems_count = 0;
entitySystemData.on_entity_entered_scene_systems_count = 0;
entitySystemData.render_systems_count = 0;
entitySystemData.pre_process_all_systems_count = 0;
entitySystemData.post_process_all_systems_count = 0;
entitySystemData.process_systems_count = 0;
entitySystemData.physics_process_systems_count = 0;
entitySystemData.pre_update_all_systems_count = 0;
entitySystemData.post_update_all_systems_count = 0;
entitySystemData.update_systems_count = 0;
entitySystemData.fixed_update_systems_count = 0;
entitySystemData.network_callback_systems_count = 0;
}

Expand All @@ -90,10 +90,10 @@ CreEntitySystem* cre_ec_system_create() {
newSystem->on_entity_end_func = NULL;
newSystem->on_entity_unregistered_func = NULL;
newSystem->render_func = NULL;
newSystem->pre_process_all_func = NULL;
newSystem->post_process_all_func = NULL;
newSystem->process_func = NULL;
newSystem->physics_process_func = NULL;
newSystem->pre_update_all_func = NULL;
newSystem->post_update_all_func = NULL;
newSystem->update_func = NULL;
newSystem->fixed_update_func = NULL;
newSystem->network_callback_func = NULL;
newSystem->component_signature = CreComponentType_NONE;
return newSystem;
Expand All @@ -118,17 +118,17 @@ void cre_ec_system_register(CreEntitySystem* system) {
if (system->render_func != NULL) {
entitySystemData.render_systems[entitySystemData.render_systems_count++] = system;
}
if (system->pre_process_all_func != NULL) {
entitySystemData.pre_process_all_systems[entitySystemData.pre_process_all_systems_count++] = system;
if (system->pre_update_all_func != NULL) {
entitySystemData.pre_update_all_systems[entitySystemData.pre_update_all_systems_count++] = system;
}
if (system->post_process_all_func != NULL) {
entitySystemData.post_process_all_systems[entitySystemData.post_process_all_systems_count++] = system;
if (system->post_update_all_func != NULL) {
entitySystemData.post_update_all_systems[entitySystemData.post_update_all_systems_count++] = system;
}
if (system->process_func != NULL) {
entitySystemData.process_systems[entitySystemData.process_systems_count++] = system;
if (system->update_func != NULL) {
entitySystemData.update_systems[entitySystemData.update_systems_count++] = system;
}
if (system->physics_process_func != NULL) {
entitySystemData.physics_process_systems[entitySystemData.physics_process_systems_count++] = system;
if (system->fixed_update_func != NULL) {
entitySystemData.fixed_update_systems[entitySystemData.fixed_update_systems_count++] = system;
}
if (system->network_callback_func != NULL) {
entitySystemData.network_callback_systems[entitySystemData.network_callback_systems_count++] = system;
Expand Down Expand Up @@ -196,27 +196,27 @@ void cre_ec_system_render_systems() {
}
}

void cre_ec_system_pre_process_all_systems() {
for (size_t i = 0; i < entitySystemData.pre_process_all_systems_count; i++) {
entitySystemData.pre_process_all_systems[i]->pre_process_all_func();
void cre_ec_system_pre_update_all_systems() {
for (size_t i = 0; i < entitySystemData.pre_update_all_systems_count; i++) {
entitySystemData.pre_update_all_systems[i]->pre_update_all_func();
}
}

void cre_ec_system_post_process_all_systems() {
for (size_t i = 0; i < entitySystemData.post_process_all_systems_count; i++) {
entitySystemData.post_process_all_systems[i]->post_process_all_func();
void cre_ec_system_post_update_all_systems() {
for (size_t i = 0; i < entitySystemData.post_update_all_systems_count; i++) {
entitySystemData.post_update_all_systems[i]->post_update_all_func();
}
}

void cre_ec_system_process_systems(float deltaTime) {
for (size_t i = 0; i < entitySystemData.process_systems_count; i++) {
entitySystemData.process_systems[i]->process_func(deltaTime);
void cre_ec_system_update_systems(float deltaTime) {
for (size_t i = 0; i < entitySystemData.update_systems_count; i++) {
entitySystemData.update_systems[i]->update_func(deltaTime);
}
}

void cre_ec_system_physics_process_systems(float deltaTime) {
for (size_t i = 0; i < entitySystemData.physics_process_systems_count; i++) {
entitySystemData.physics_process_systems[i]->physics_process_func(deltaTime);
void cre_ec_system_fixed_update_systems(float deltaTime) {
for (size_t i = 0; i < entitySystemData.fixed_update_systems_count; i++) {
entitySystemData.fixed_update_systems[i]->fixed_update_func(deltaTime);
}
}

Expand Down
20 changes: 10 additions & 10 deletions engine/src/core/ecs/system/ec_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ typedef void (*OnEntityEnteredSceneFunc) (CreEntity); // When entity enters a sc
typedef void (*RenderFunc) ();
typedef void (*PreProcessAllFunc) ();
typedef void (*PostProcessAllFunc) ();
typedef void (*ProcessFunc) (float);
typedef void (*PhysicsProcessFunc) (float);
typedef void (*UpdateFunc) (float);
typedef void (*FixedUpdateFunc) (float);
typedef void (*NetworkCallbackFunc) (const char*);

typedef struct CreEntitySystem {
Expand All @@ -24,10 +24,10 @@ typedef struct CreEntitySystem {
OnEntityUnRegisteredFunc on_entity_unregistered_func;
OnEntityEnteredSceneFunc on_entity_entered_scene_func;
RenderFunc render_func;
PreProcessAllFunc pre_process_all_func;
PostProcessAllFunc post_process_all_func;
ProcessFunc process_func;
PhysicsProcessFunc physics_process_func;
PreProcessAllFunc pre_update_all_func;
PostProcessAllFunc post_update_all_func;
UpdateFunc update_func;
FixedUpdateFunc fixed_update_func;
NetworkCallbackFunc network_callback_func;
CreComponentType component_signature;
size_t entity_count;
Expand All @@ -47,10 +47,10 @@ void cre_ec_system_entity_start(CreEntity entity);
void cre_ec_system_entity_end(CreEntity entity);
void cre_ec_system_entity_entered_scene(CreEntity entity);
void cre_ec_system_render_systems();
void cre_ec_system_pre_process_all_systems();
void cre_ec_system_post_process_all_systems();
void cre_ec_system_process_systems(float deltaTime);
void cre_ec_system_physics_process_systems(float deltaTime);
void cre_ec_system_pre_update_all_systems();
void cre_ec_system_post_update_all_systems();
void cre_ec_system_update_systems(float deltaTime);
void cre_ec_system_fixed_update_systems(float deltaTime);

void cre_ec_system_network_callback(const char* message);

Expand Down
2 changes: 1 addition & 1 deletion engine/src/core/ecs/system/parallax_ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct CreEntitySystem* cre_parallax_ec_system_create() {
parallaxSystem->name = se_strdup("Parallax");
parallaxSystem->on_entity_entered_scene_func = parallax_system_on_entity_entered_scene;
parallaxSystem->on_entity_unregistered_func = parallax_system_on_entity_unregistered;
parallaxSystem->physics_process_func = parallax_system_physics_process;
parallaxSystem->fixed_update_func = parallax_system_physics_process;
parallaxSystem->component_signature = CreComponentType_TRANSFORM_2D | CreComponentType_PARALLAX;
return parallaxSystem;
}
Expand Down
8 changes: 4 additions & 4 deletions engine/src/core/ecs/system/script_ec_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ CreEntitySystem* cre_script_ec_system_create() {
scriptSystem->on_entity_unregistered_func = script_system_on_entity_unregistered;
scriptSystem->on_entity_start_func = script_system_entity_start;
scriptSystem->on_entity_end_func = script_system_entity_end;
scriptSystem->pre_process_all_func = script_system_pre_update_all;
scriptSystem->post_process_all_func = script_system_post_update_all;
scriptSystem->process_func = script_system_instance_update;
scriptSystem->physics_process_func = script_system_instance_fixed_update;
scriptSystem->pre_update_all_func = script_system_pre_update_all;
scriptSystem->post_update_all_func = script_system_post_update_all;
scriptSystem->update_func = script_system_instance_update;
scriptSystem->fixed_update_func = script_system_instance_fixed_update;
scriptSystem->network_callback_func = script_system_network_callback;
scriptSystem->component_signature = CreComponentType_SCRIPT;
// Python Context
Expand Down
30 changes: 30 additions & 0 deletions engine/src/core/scripting/python/cre_py.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ void cre_py_finalize() {
Py_Finalize();
}

bool cre_py_handle_if_errors(const char* context, CrePythonHandleErrorType errorType) {
if (PyErr_Occurred()) {
// Get the exception type, value, and traceback objects
PyObject* excType = NULL;
PyObject* excValue = NULL;
PyObject* excTraceback = NULL;
PyErr_Fetch(&excType, &excValue, &excTraceback);

// Convert the exception objects to strings
const char *typeStr = PyUnicode_AsUTF8(PyObject_Str(excType));
const char *valueStr = PyUnicode_AsUTF8(PyObject_Str(excValue));

// Format the error message
char errorMessage[256];
snprintf(errorMessage, sizeof(errorMessage), "Python error: %s: %s", typeStr, valueStr);

// Print or log the error message
se_logger_error(errorMessage);

PyErr_Clear();

if (errorType == CrePythonHandleErrorType_FATAL) {
SE_ASSERT_FMT(false, "Fatal python error, context: '%s'", context);
}

return true;
}
return false;
}

void cre_py_import_module_source(const char* moduleName, const char* moduleText) {
#define IMPORT_COMMAND_BUFFER_SIZE 65536
char importCommandBuffer[IMPORT_COMMAND_BUFFER_SIZE];
Expand Down
6 changes: 6 additions & 0 deletions engine/src/core/scripting/python/cre_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ extern "C" {

#include <stdbool.h>

typedef enum CrePythonHandleErrorType {
CrePythonHandleErrorType_FATAL = 0,
CrePythonHandleErrorType_NON_FATAL = 1,
} CrePythonHandleErrorType;

void cre_py_initialize(const char* embeddedPythonPath);
void cre_py_finalize();
bool cre_py_handle_if_errors(const char* context, CrePythonHandleErrorType errorType);
void cre_py_import_module_source(const char* moduleName, const char* moduleText);
// Exports game, will probably put in another place as this is needed by the editor and abstracts python usage
void cre_py_export_game_project(const char* gameTitle, const char* archivePath, const char* engineBinPath, const char* projectPath, const char* tempPath);
Expand Down
6 changes: 6 additions & 0 deletions engine/src/core/scripting/python/cre_py_api_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "../seika/src/utils/se_file_system_utils.h"
#include "../seika/src/utils/se_string_util.h"

#include "cre_py.h"
#include "py_cache.h"
#include "py_script_context.h"
#include "../../engine_context.h"
Expand Down Expand Up @@ -757,6 +758,10 @@ PyObject* cre_py_api_world_get_time_dilation(PyObject* self, PyObject* args) {
return Py_BuildValue("f", cre_world_get_time_dilation());
}

PyObject* cre_py_api_world_get_delta_time(PyObject* self, PyObject* args) {
return Py_BuildValue("f", cre_world_get_time_dilation() * CRE_GLOBAL_PHYSICS_DELTA_TIME);
}

// Audio Manager
PyObject* cre_py_api_audio_manager_play_sound(PyObject* self, PyObject* args, PyObject* kwargs) {
char* audioPath;
Expand Down Expand Up @@ -1043,6 +1048,7 @@ void py_api_node_event_callback(void* observerData, NodeEventNotifyPayload* noti

PyObject* listenerFuncArg = Py_BuildValue("(O)", pyEventArgs);
PyObject_CallObject(pyCallbackFunc, listenerFuncArg);
cre_py_handle_if_errors("py_api_node_event_callback", CrePythonHandleErrorType_FATAL);
}

void py_api_node_event_data_delete_callback(void* data) {
Expand Down
5 changes: 5 additions & 0 deletions engine/src/core/scripting/python/cre_py_api_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ PyObject* cre_py_api_scene_tree_get_root(PyObject* self, PyObject* args);
// World
PyObject* cre_py_api_world_set_time_dilation(PyObject* self, PyObject* args, PyObject* kwargs);
PyObject* cre_py_api_world_get_time_dilation(PyObject* self, PyObject* args);
PyObject* cre_py_api_world_get_delta_time(PyObject* self, PyObject* args);

// Audio Manager
PyObject* cre_py_api_audio_manager_play_sound(PyObject* self, PyObject* args, PyObject* kwargs);
Expand Down Expand Up @@ -458,6 +459,10 @@ static struct PyMethodDef crePyApiMethods[] = {
"world_get_time_dilation", cre_py_api_world_get_time_dilation,
METH_VARARGS, "Returns the global time dilation for world."
},
{
"world_get_delta_time", cre_py_api_world_get_delta_time,
METH_VARARGS, "Returns the delta time (global time dilation * physics delta time) for world."
},
// AUDIO MANAGER
{
"audio_manager_play_sound", (PyCFunction) cre_py_api_audio_manager_play_sound,
Expand Down
4 changes: 4 additions & 0 deletions engine/src/core/scripting/python/crescent_api_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,10 @@
" def get_time_dilation() -> float:\n"\
" return crescent_api_internal.world_get_time_dilation()\n"\
"\n"\
" @staticmethod\n"\
" def get_delta_time() -> float:\n"\
" return crescent_api_internal.world_get_delta_time()\n"\
"\n"\
"\n"\
"# AUDIO MANAGER\n"\
"class AudioManager:\n"\
Expand Down
Loading

0 comments on commit 68698a2

Please sign in to comment.