Ejemplo n.º 1
0
        protected void LoadWasm(WasmFile file, ContentsStore store = null)
        {
            if (store == null)
            {
                store = new ContentsStore();
            }

            var importer = new PredefinedImporter();

            var wasiFunctions = new List <string>()
            {
                "proc_exit",
                "fd_write",
                "fd_prestat_get",
                "fd_prestat_dir_name",
                "environ_sizes_get",
                "environ_get",
                //"env.abort",
                "abort",
            };

            foreach (var wasiFunction in wasiFunctions)
            {
                importer.DefineFunction(wasiFunction,
                                        new DelegateFunctionDefinition(
                                            new WasmValueType[] { },
                                            new WasmValueType[] { },
                                            x => x
                                            ));
            }

            var gameObjectBinding = new GameObjectBinding(transform, store);

            importer.IncludeDefinitions(gameObjectBinding.Importer);

            var transformBinding = new TransformBinding(transform, store);

            importer.IncludeDefinitions(transformBinding.Importer);

            var physicsBinding = new PhysicsBinding(transform, store);

            importer.IncludeDefinitions(physicsBinding.Importer);

            var timeBinding = new TimeBinding();

            importer.IncludeDefinitions(timeBinding.Importer);

            module = ModuleInstance.Instantiate(file, importer);

            var exportedFunctions = module.ExportedFunctions;

            exportedFunctions.TryGetValue("update", out updateFunction);
            exportedFunctions.TryGetValue("on_touch_start", out onTouchStartFunction);
        }
Ejemplo n.º 2
0
        public async Task LoadWasmFromUrl(string url, ContentsStore contentsStore = null)
        {
            var httpClient = new HttpClient();
            var result     = await httpClient.GetAsync(url);

            if (result.IsSuccessStatusCode)
            {
                var stream = await result.Content.ReadAsStreamAsync();

                LoadWasm(stream, contentsStore);
            }
        }
Ejemplo n.º 3
0
        private void Awake()
        {
            contentsStore = new ContentsStore()
            {
                RootTransform = transform
            };

            var resourceObject = new GameObject("resource");

            resourceObject.SetActive(false);
            resourceRoot = resourceObject.transform;
            resourceRoot.SetParent(transform, false);
            LoadFromFile(path);
        }
Ejemplo n.º 4
0
        protected bool TryGetElementWithArg(ArgumentParser parser, ContentsStore store, out Element element)
        {
            if (!parser.TryReadInt(out var elementIndex))
            {
                element = null;
                return(false);
            }

            if (elementIndex == 0)
            {
                element = thisElement;
                return(true);
            }

            if (!store.TryGetElementByElementIndex(elementIndex, out element))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
 public PhysicsBinding(Transform transform, ContentsStore store) : base()
 {
     this.transform = transform;
     this.store     = store;
 }
Ejemplo n.º 6
0
        public void LoadWasm(Stream stream, ContentsStore store = null)
        {
            var file = WasmFile.ReadBinary(stream);

            LoadWasm(file, store);
        }
Ejemplo n.º 7
0
        public void LoadWasm(string path, ContentsStore store = null)
        {
            var file = WasmFile.ReadBinary(path);

            LoadWasm(file, store);
        }
Ejemplo n.º 8
0
 public TransformBinding(Element element, ContentsStore store) : base(element, store)
 {
 }
Ejemplo n.º 9
0
 public ArgsBinding(Element element, ContentsStore store, List <string> args) : base(element, store)
 {
     this.args = args;
 }
Ejemplo n.º 10
0
 public TimeBinding(Element element, ContentsStore store) : base(element, store)
 {
 }
Ejemplo n.º 11
0
 public PhysicsBinding(Element element, ContentsStore store) : base(element, store)
 {
 }
Ejemplo n.º 12
0
 public DebugBinding(Element element, ContentsStore store) : base(element, store)
 {
 }
Ejemplo n.º 13
0
 public GameObjectBinding(Transform transform, ContentsStore store) : base()
 {
     this.transform = transform;
     this.store     = store;
 }
Ejemplo n.º 14
0
        public void LoadWasm(Stream stream, ContentsStore store = null, List <string> args = null)
        {
            var file = WasmFile.ReadBinary(stream);

            LoadWasm(file, store, args);
        }
Ejemplo n.º 15
0
 public BindingBase(Element element, ContentsStore store)
 {
     Importer         = GenerateImporter();
     this.thisElement = element;
     this.store       = store;
 }
Ejemplo n.º 16
0
        protected void LoadWasm(WasmFile file, ContentsStore store = null, List <string> args = null)
        {
            if (store == null)
            {
                store = new ContentsStore();
            }

            var importer = new PredefinedImporter();

            var wasiFunctions = new List <string>()
            {
                "proc_exit",
                "fd_read",
                "fd_write",
                "fd_prestat_get",
                "fd_prestat_dir_name",
                "environ_sizes_get",
                "environ_get",
                // "random_get",
                //"env.abort",
                "abort",
            };

            foreach (var wasiFunction in wasiFunctions)
            {
                importer.DefineFunction(wasiFunction,
                                        new DelegateFunctionDefinition(
                                            new WasmValueType[] { },
                                            new WasmValueType[] { },
                                            x => x
                                            ));
            }
            importer.DefineFunction("random_get",
                                    new DelegateFunctionDefinition(
                                        ValueType.PointerAndPointer,
                                        ValueType.Int,
                                        x => ReturnValue.FromObject(0)
                                        ));

            var element = new Element()
            {
                GameObject = gameObject
            };

            if (args == null)
            {
                args = new List <string>();
            }
            var scriptName = "";

            args.Insert(0, scriptName);

            var argsBinding = new ArgsBinding(element, store, args);

            importer.IncludeDefinitions(argsBinding.Importer);

            var debugBinding = new DebugBinding(element, store);

            importer.IncludeDefinitions(debugBinding.Importer);

            var gameObjectBinding = new GameObjectBinding(element, store);

            importer.IncludeDefinitions(gameObjectBinding.Importer);

            var transformBinding = new TransformBinding(element, store);

            importer.IncludeDefinitions(transformBinding.Importer);

            var physicsBinding = new PhysicsBinding(element, store);

            importer.IncludeDefinitions(physicsBinding.Importer);

            var timeBinding = new TimeBinding(element, store);

            importer.IncludeDefinitions(timeBinding.Importer);

            module = ModuleInstance.Instantiate(file, importer);

            argsBinding.ModuleInstance       = module;
            gameObjectBinding.ModuleInstance = module;
            debugBinding.ModuleInstance      = module;

            var exportedFunctions = module.ExportedFunctions;

            exportedFunctions.TryGetValue("start", out startFunction);
            exportedFunctions.TryGetValue("update", out updateFunction);
            exportedFunctions.TryGetValue("on_touch_start", out onTouchStartFunction);
            exportedFunctions.TryGetValue("on_use", out onUseFunction);
        }
Ejemplo n.º 17
0
 public GameObjectBinding(Element element, ContentsStore store) : base(element, store)
 {
 }