Пример #1
0
        public async Task LoadWasmFromUrl(string url, ContentsStore contentsStore = null, List <string> args = null)
        {
            var httpClient = new HttpClient();
            var result     = await httpClient.GetAsync(url);

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

                LoadWasm(stream, contentsStore, args);
            }
        }
Пример #2
0
        private void Awake()
        {
            contentsStore = new ContentsStore()
            {
                RootTransform = transform
            };

            var resourceObject = new GameObject("resource");

            resourceObject.transform.localScale = Vector3.zero;
            // resourceObject.SetActive(false);
            resourceRoot = resourceObject.transform;
            resourceRoot.SetParent(transform, false);

            if (!string.IsNullOrEmpty(path))
            {
                _ = LoadAsync(path);
            }
        }
Пример #3
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_prestat_get",
                "fd_prestat_dir_name",
                // "random_get",
                "abort",
            };

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

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

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

            args.Insert(0, scriptName);
            var envs        = new List <string>();
            var argsBinding = new ArgsBinding(element, store, args, envs);

            importer.IncludeDefinitions(argsBinding.Importer);

            var fileDescriptorBinding = new FileDescriptorBinding(element, store);

            importer.IncludeDefinitions(fileDescriptorBinding.Importer);


            // Spirare functions
            var debugBinding = new DebugBinding(element, store);

            importer.IncludeDefinitions(debugBinding.Importer);

            var gameObjectBinding = new GameObjectBinding(element, store, context, mainThread);

            importer.IncludeDefinitions(gameObjectBinding.Importer);

            var transformBinding = new TransformBinding(element, store, context, mainThread);

            importer.IncludeDefinitions(transformBinding.Importer);

            var physicsBinding = new PhysicsBinding(element, store, context, mainThread);

            importer.IncludeDefinitions(physicsBinding.Importer);

            var textBinding = new TextBinding(element, store, context, mainThread);

            importer.IncludeDefinitions(textBinding.Importer);

            var timeBinding = new TimeBinding(element, store);

            importer.IncludeDefinitions(timeBinding.Importer);

            try
            {
                module = ModuleInstance.Instantiate(file, importer);

                argsBinding.ModuleInstance           = module;
                fileDescriptorBinding.ModuleInstance = module;

                gameObjectBinding.ModuleInstance = module;
                debugBinding.ModuleInstance      = module;
                textBinding.ModuleInstance       = module;

                var exportedFunctions = module.ExportedFunctions;
                exportedFunctions.TryGetValue("start", out startFunction);
                exportedFunctions.TryGetValue("update", out updateFunction);
                exportedFunctions.TryGetValue("on_use", out onUseFunction);
                exportedFunctions.TryGetValue("on_select", out onSelectFunction);
                exportedFunctions.TryGetValue("on_equip", out onEquipFunction);
                exportedFunctions.TryGetValue("on_unequip", out onEquipFunction);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
Пример #4
0
        public void LoadWasm(Stream stream, ContentsStore store = null, List <string> args = null)
        {
            var file = WasmFile.ReadBinary(stream);

            LoadWasm(file, store, args);
        }