Ejemplo n.º 1
0
        public static void cctor_Object()
        {
            var logger = new Logger("Stock");

            Stock.Logger   = logger;
            Stock.DataPath = Application.dataPath;

            logger.Info("Started.");
            logger.Info("Paths:");
            logger.InfoIndent($"Data path: '{Stock.DataPath}'");
            logger.InfoIndent($"Game path: '{Stock.GamePath}'");
            logger.InfoIndent($"Stock path: '{Stock.StockPath}'");
            logger.InfoIndent($"Main Lua path: '{Stock.StockMainLuaPath}'");

            Stock.InitializeStock();
        }
Ejemplo n.º 2
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");
                }
            }
        }