diff --git a/src/loaders/blend.nim b/src/loaders/blend.nim index 80f6be3..5988ebc 100644 --- a/src/loaders/blend.nim +++ b/src/loaders/blend.nim @@ -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: diff --git a/src/loaders/loader_base.nim b/src/loaders/loader_base.nim index e767304..7bc32bc 100644 --- a/src/loaders/loader_base.nim +++ b/src/loaders/loader_base.nim @@ -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 diff --git a/src/myou_engine.nim b/src/myou_engine.nim index fed32b3..11ec4be 100644 --- a/src/myou_engine.nim +++ b/src/myou_engine.nim @@ -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: