(int sceneId)
| 33 | } |
| 34 | |
| 35 | public SceneMeta load(int sceneId){ |
| 36 | // Get compiled script if cached |
| 37 | CompiledScript cs = ScriptLoader.getScriptByPath( |
| 38 | SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "." + ScriptLoader.getScriptType())); |
| 39 | |
| 40 | if (cs == null) { |
| 41 | Grasscutter.getLogger().warn("No script found for scene " + sceneId); |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | // Create bindings |
| 46 | this.context = ScriptLoader.getEngine().createBindings(); |
| 47 | |
| 48 | // Eval script |
| 49 | try { |
| 50 | cs.eval(this.context); |
| 51 | |
| 52 | this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, this.context.get("scene_config")); |
| 53 | |
| 54 | // TODO optimize later |
| 55 | // Create blocks |
| 56 | List<Integer> blockIds = ScriptLoader.getSerializer().toList(Integer.class, this.context.get("blocks")); |
| 57 | List<SceneBlock> blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, this.context.get("block_rects")); |
| 58 | |
| 59 | for (int i = 0; i < blocks.size(); i++) { |
| 60 | SceneBlock block = blocks.get(i); |
| 61 | block.id = blockIds.get(i); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | this.blocks = blocks.stream().collect(Collectors.toMap(b -> b.id, b -> b)); |
| 66 | this.sceneBlockIndex = SceneIndexManager.buildIndex(2, blocks, SceneBlock::toRectangle); |
| 67 | |
| 68 | } catch (ScriptException exception) { |
| 69 | Grasscutter.getLogger().error("An error occurred while running a script.", exception); |
| 70 | return null; |
| 71 | } |
| 72 | Grasscutter.getLogger().debug("Successfully loaded metadata in scene {}.", sceneId); |
| 73 | return this; |
| 74 | } |
| 75 | } |
no test coverage detected