Exemple #1
0
        public void ClrPackage()
        {
            using (var runtime = new LuaRuntime()) {
                runtime.InitializeClrPackage();

                // adds a `clr` package with the following functions:
                //   clr.assembly  | obtain an assembly with the provided name
                //   clr.namespace | obtain a table

                runtime.DoString(@"
                    local ass = clr.assembly('Eluant.Tests')
                    local EluantTests = clr.namespace(ass, 'Eluant.Tests')

                    local s1 = EluantTests.Functions.Test()

                    local Functions = clr.type(ass, 'Eluant.Tests.Functions')
                    
                    local s2 = Functions.Test()

                    if s1 ~= s2 then
                        error('The strings are different')
                    end
                ");
            }
        }
Exemple #2
0
        public void InstantiatingClasses()
        {
            using (var runtime = new LuaRuntime()) {
                runtime.InitializeClrPackage();
                var results = runtime.DoString(@"
                    local ass = clr.assembly('Eluant.Tests')
                    local Something = clr.type(ass, 'Eluant.Tests.Functions/Something')
                    local a = Something(100, 200, 300)
                    local b = Something(10)

                    return a, b
                ");
                using (results) {
                    Assert.AreEqual(2, results.Count);
                    Assert.IsInstanceOf(typeof(Something), results [0].CLRMappedObject);
                    Assert.IsInstanceOf(typeof(Something), results [1].CLRMappedObject);

                    var a = results [0].CLRMappedObject as Something;
                    var b = results [1].CLRMappedObject as Something;

                    Assert.AreEqual(100, a.A);
                    Assert.AreEqual(200, a.B);
                    Assert.AreEqual(300, a.C);

                    Assert.AreEqual(10, b.A);
                    Assert.AreEqual(10, b.B);
                    Assert.AreEqual(10, b.C);
                }
            }
        }
Exemple #3
0
 internal void RefreshLuaState()
 {
     if (LuaState != null)
     {
         LuaState.Dispose();
     }
     LuaState = new LuaRuntime();
     LuaState.MonoStackTraceWorkaround = true;
     // The version of Unity that Gungeon uses uses Mono 2.6.5, released in 2009
     // Read the comment on MonoStackTraceWorkaround to learn more
     LuaState.InitializeClrPackage();
 }
Exemple #4
0
        public static void InitializeLua()
        {
            if (!File.Exists(StockMainLuaPath))
            {
                Logger.Warn("No main.lua - bailing out.");
                return;
            }

            try {
                if (LuaState != null)
                {
                    DeinitializeLua();
                }
                LuaState = new LuaRuntime();
                Logger.Info("Running Lua");
                LuaState.MonoStackTraceWorkaround = true;
                LuaState.InitializeClrPackage();
                using (var package = LuaState.Globals["package"] as LuaTable) {
                    package["path"]  = $"{Path.Combine(StockPath, "?.lua")};{Path.Combine(StockPath, "?/init.lua")}";
                    package["cpath"] = "";
                }
                using (var stock_table = LuaState.CreateTable()) {
                    stock_table["_VERSION"] = Assembly.GetExecutingAssembly().GetName().Version.ToString();

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new Action <LuaTable, LuaFunction>(LuaFunctions.Hook)
                               )) stock_table["hook"] = func;

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new Func <string, AssemblyHelper>(LuaFunctions.Assembly)
                               )) stock_table["assembly"] = func;

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new LuaFunctions.ArrayTypeFuncDelegate(LuaFunctions.ArrayType)
                               )) stock_table["array_type"] = func;

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new Func <Type, LuaTable, LuaClrTypeObject>(LuaFunctions.GenericType)
                               )) stock_table["generic_type"] = func;

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new Func <LuaVararg, object>(LuaFunctions.Get)
                               )) stock_table["get"] = func;

                    using (var func = LuaState.CreateFunctionFromDelegate(
                               new Action <LuaVararg>(LuaFunctions.Set)
                               )) stock_table["set"] = func;

                    LuaState.Globals["stock"] = stock_table;
                }
                LuaState.DoFile(StockMainLuaPath);

                using (var stock = LuaState.Globals["stock"]) {
                    var stock_table = stock as LuaTable;
                    if (stock_table != null)
                    {
                        var unload = stock_table["unload"];
                        if (!(unload is LuaFunction))
                        {
                            unload.Dispose();
                            unload = null;
                        }
                        var ready = stock_table["ready"];
                        if (!(ready is LuaFunction))
                        {
                            ready.Dispose();
                            ready = null;
                        }

                        if (unload != null)
                        {
                            Unload = unload as LuaFunction;
                        }
                        if (ready != null)
                        {
                            Ready = ready as LuaFunction;
                        }
                    }
                }
            } catch (LuaException e) {
                Logger.Error($"Failed initializing Lua: [{e.GetType()}]");
                Logger.ErrorIndent(e.Message);
                foreach (var line in e.TracebackArray)
                {
                    Logger.ErrorIndent(line);
                }
            } catch (Exception e) {
                Logger.Error($"Failed initializing Lua: [{e.GetType()}]");
                Logger.ErrorIndent(e.Message);
                foreach (var line in e.StackTrace.Split('\n'))
                {
                    Logger.ErrorIndent(line.Replace("  ", ""));
                }
            }

            if (_GameObject != null)
            {
                if (Ready != null)
                {
                    SafeExecLua(Ready, "running stock.ready");
                }
            }
        }