void InitLua()
        {
            //Init LuaBinding class that demonstrates communication
            LuaBinding   binding = new LuaBinding();
            DebugBinding debug   = new DebugBinding();

            //Init instance of Lua virtual machine (Note: Can only init ONCE)
            luaVM = new Lua();
            luaVM.DoFile(Application.streamingAssetsPath + Path.DirectorySeparatorChar + "vm.lua");
            //Tell Lua about the LuaBinding object to allow Lua to call C# functions
            luaVM["luabinding"] = binding;
            luaVM["debug"]      = debug;

            //Run the code contained within the file
            luaVM.DoFile(Application.streamingAssetsPath + "/" + "luademo.lua");

            //Trigger binding in c# to call the bound Lua function
            binding.MessageToLua();
        }
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_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);
            }
        }