Beispiel #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);
        }
Beispiel #2
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);
        }