Add rudimentary error management to loader callbacks.

This commit is contained in:
Alberto Torres 2024-09-21 02:10:53 +02:00
parent dca0123f13
commit 55d0c859ab
3 changed files with 23 additions and 11 deletions

View file

@ -101,9 +101,9 @@ method openAssetFile*(self: BlendLoader, path: string) =
# self.on_destroy = OnDestroy(destructor: proc() = self.close()) # self.on_destroy = OnDestroy(destructor: proc() = self.close())
self.blend_file_path = path self.blend_file_path = path
proc loadAsync(self: BlendLoader, callback: proc()) = proc loadAsync(self: BlendLoader, callback: proc(err: string)) =
if self.blend_file != nil: if self.blend_file != nil:
callback() callback("")
else: else:
self.close() self.close()
self.resource = loadUri(self.blend_file_path, self.resource = loadUri(self.blend_file_path,
@ -111,9 +111,7 @@ proc loadAsync(self: BlendLoader, callback: proc()) =
self.resource = nil self.resource = nil
if ok: if ok:
self.blend_file = openBlendFile(self.blend_file_path, data.data, data.byte_len) self.blend_file = openBlendFile(self.blend_file_path, data.data, data.byte_len)
callback() callback(err)
else:
raise IOError.newException err
) )
type BlenderObTypes = enum type BlenderObTypes = enum
@ -573,9 +571,12 @@ proc get_active_scene_name(self: BlendLoader): string =
if scene.valid: if scene.valid:
return scene.id.name.str[2 .. ^1] return scene.id.name.str[2 .. ^1]
method loadScene*(self: BlendLoader, name: string="", scene: Scene=nil, callback: proc(scene: Scene)) = method loadScene*(self: BlendLoader, name: string="", scene: Scene=nil, callback: proc(err: string, scene: Scene)) =
assert self.blend_file_path != "", "Blend file is not loaded" assert self.blend_file_path != "", "Blend file is not loaded"
self.loadAsync proc() = self.loadAsync proc(err: string) =
if err != "":
callback(err, nil)
return
assert self.blend_file != nil, "Error loading blend file " & self.blend_file_path assert self.blend_file != nil, "Error loading blend file " & self.blend_file_path
var name = name var name = name
if name == "": if name == "":
@ -588,8 +589,19 @@ method loadScene*(self: BlendLoader, name: string="", scene: Scene=nil, callback
let was_first_scene = self.engine.scenes.len == 0 let was_first_scene = self.engine.scenes.len == 0
if scene == nil: if scene == nil:
scene = self.engine.new_scene(name=name) scene = self.engine.new_scene(name=name)
self.loadSceneImpl(node, scene) try:
callback(scene) self.loadSceneImpl(node, scene)
except Exception as e:
for line in e.getStackTrace.split '\n':
echo line
echo getCurrentExceptionMsg()
scene.destroy()
callback(getCurrentExceptionMsg(), nil)
return
callback("", scene)
if scene.name notin self.engine.new_scenes:
# it was deleted
return
# TODO: when loading is async, move this stuff after loading has # TODO: when loading is async, move this stuff after loading has
# finished # finished
if was_first_scene and not scene.enabled: if was_first_scene and not scene.enabled:

View file

@ -39,7 +39,7 @@ when defined(nimdoc):
method openAssetFile*(self: Loader, path: string) {.base.} = method openAssetFile*(self: Loader, path: string) {.base.} =
discard discard
method loadScene*(self: Loader, name: string="", scene: Scene=nil, method loadScene*(self: Loader, name: string="", scene: Scene=nil,
callback: proc(scene: Scene)) {.base.} = callback: proc(err: string, scene: Scene)) {.base.} =
discard discard
method loadImageImpl*(self: Loader) {.base.} = method loadImageImpl*(self: Loader) {.base.} =
discard discard

View file

@ -201,7 +201,7 @@ proc run*(self: MyouEngine) =
when not defined(nimdoc): when not defined(nimdoc):
start_platform_main_loop(self, myou_main_loop) start_platform_main_loop(self, myou_main_loop)
proc loadScene*(self: MyouEngine, uri: string, callback: proc(scene: Scene), name = "", ext = "") = proc loadScene*(self: MyouEngine, uri: string, callback: proc(err: string, scene: Scene), name = "", ext = "") =
let ext = if ext == "": let ext = if ext == "":
uri.rsplit('.', 1)[1] uri.rsplit('.', 1)[1]
else: else: