|
| 1 | +package com.synthmodloader.modloader.synth; |
| 2 | + |
| 3 | +import com.dylibso.chicory.runtime.ExportFunction; |
| 4 | +import com.dylibso.chicory.runtime.HostFunction; |
| 5 | +import com.dylibso.chicory.runtime.Instance; |
| 6 | +import com.dylibso.chicory.runtime.Store; |
| 7 | +import com.dylibso.chicory.wasm.Parser; |
| 8 | +import com.synthmodloader.modloader.SynthModLoader; |
| 9 | +import net.minecraft.client.Minecraft; |
| 10 | +import net.minecraft.resources.ResourceLocation; |
| 11 | +import net.neoforged.fml.ModContainer; |
| 12 | +import net.neoforged.fml.ModList; |
| 13 | +import net.neoforged.fml.ModLoader; |
| 14 | +import net.neoforged.neoforge.server.ServerLifecycleHooks; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.time.chrono.MinguoEra; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +public class SynthInstance { |
| 24 | + public final ResourceLocation location; |
| 25 | + private final Instance wasmInstance; |
| 26 | + private SynthInstance(ResourceLocation _location, Collection<HostFunction> functions) throws IOException { |
| 27 | + location = _location; |
| 28 | + var store = new Store(); |
| 29 | + functions.forEach(store::addFunction); |
| 30 | + if (ModList.get().getModFileById(location.getNamespace()) == null) |
| 31 | + throw new IllegalArgumentException("Namespace `" + location.getNamespace() + "` does not exist !"); |
| 32 | + var path = ModList.get().getModFileById(location.getNamespace()).getFile().findResource(location.getPath()); |
| 33 | + wasmInstance = store.instantiate(location.getPath(), Parser.parse(path)); |
| 34 | + } |
| 35 | + |
| 36 | + public Optional<ExportFunction> export(String functionName) { |
| 37 | + if (wasmInstance != null && wasmInstance.export(functionName) != null) |
| 38 | + return Optional.of(wasmInstance.export(functionName)); |
| 39 | + else |
| 40 | + return Optional.empty(); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public static class Builder { |
| 45 | + private List<HostFunction> functions; |
| 46 | + private ResourceLocation location; |
| 47 | + private Builder() {} |
| 48 | + |
| 49 | + public Builder function(HostFunction function) { |
| 50 | + functions.add(function); |
| 51 | + return this; |
| 52 | + } |
| 53 | + public Builder location(ResourceLocation location) { |
| 54 | + this.location = location; |
| 55 | + return this; |
| 56 | + } |
| 57 | + public SynthInstance build() { |
| 58 | + try { |
| 59 | + return new SynthInstance(location, functions); |
| 60 | + } catch (Exception e) { |
| 61 | + SynthModLoader.LOGGER.error("Couldn't create SynthInstance !", e); |
| 62 | + } |
| 63 | + return null; |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + public static Builder create() { |
| 68 | + var builder = new Builder(); |
| 69 | + builder.functions = new ArrayList<>(); |
| 70 | + return builder; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments