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.blend_file_path = path
proc loadAsync(self: BlendLoader, callback: proc()) =
proc loadAsync(self: BlendLoader, callback: proc(err: string)) =
if self.blend_file != nil:
callback()
callback("")
else:
self.close()
self.resource = loadUri(self.blend_file_path,
@ -111,9 +111,7 @@ proc loadAsync(self: BlendLoader, callback: proc()) =
self.resource = nil
if ok:
self.blend_file = openBlendFile(self.blend_file_path, data.data, data.byte_len)
callback()
else:
raise IOError.newException err
callback(err)
)
type BlenderObTypes = enum
@ -573,9 +571,12 @@ proc get_active_scene_name(self: BlendLoader): string =
if scene.valid:
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"
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
var name = 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
if scene == nil:
scene = self.engine.new_scene(name=name)
self.loadSceneImpl(node, scene)
callback(scene)
try:
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
# finished
if was_first_scene and not scene.enabled:

View file

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

View file

@ -201,7 +201,7 @@ proc run*(self: MyouEngine) =
when not defined(nimdoc):
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 == "":
uri.rsplit('.', 1)[1]
else: