Exemple #1
0
        /// <summary>
        /// Initialises the Lua environment and compiles the Lua string for execution later on.
        /// </summary>
        protected virtual void InitLuaScript()
        {
            if (initialised)
            {
                return;
            }

            if (LuaEnv == null &&
                luaEnvironment != null)
            {
                LuaEnv = luaEnvironment as ILuaEnvironment;
            }

            if (LuaEnv == null)
            {
                // Create a Lua Environment if none exists yet
                LuaEnv = LuaEnvironment.GetLua();
            }

            if (LuaEnv == null)
            {
                Debug.LogError("No Lua Environment found");
                return;
            }

            // Ensure the LuaEnvironment is initialized before trying to execute code
            LuaEnv.InitEnvironment();

            // Cache a descriptive name to use in Lua error messages
            friendlyName = GetPath(transform) + ".LuaScript";

            string s = GetLuaString();

            luaFunction = LuaEnv.LoadLuaFunction(s, friendlyName);

            initialised = true;
        }
Exemple #2
0
        public virtual bool SubstituteStrings(StringBuilder input)
        {
            // This method could be called from the Start of another component, so
            // we need to ensure that the LuaEnvironment has been initialized.
            if (LuaEnv == null)
            {
                LuaEnv = GetComponent <ILuaEnvironment>();
                if (LuaEnv != null)
                {
                    LuaEnv.InitEnvironment();
                }
            }

            if (LuaEnv == null)
            {
                UnityEngine.Debug.LogError("No Lua Environment found");
                return(false);
            }

            if (LuaEnv.Interpreter == null)
            {
                UnityEngine.Debug.LogError("No Lua interpreter found");
                return(false);
            }

            MoonSharp.Interpreter.Script interpreter = LuaEnv.Interpreter;

            // Instantiate the regular expression object.
            Regex r = new Regex("\\{\\$.*?\\}");

            bool modified = false;

            // Match the regular expression pattern against a text string.
            var results = r.Matches(input.ToString());

            foreach (Match match in results)
            {
                string key = match.Value.Substring(2, match.Value.Length - 3);

                // Match against string table and active language (if specified)
                if (stringTable != null)
                {
                    DynValue stringTableVar = stringTable.Get(key);
                    if (stringTableVar.Type == DataType.Table)
                    {
                        DynValue languageEntry = stringTableVar.Table.Get(activeLanguage);
                        if (languageEntry.Type == DataType.String)
                        {
                            input.Replace(match.Value, languageEntry.String);
                            modified = true;
                        }
                        continue;
                    }
                }

                // Match against global variables
                DynValue globalVar = interpreter.Globals.Get(key);
                if (globalVar.Type != DataType.Nil)
                {
                    input.Replace(match.Value, globalVar.ToPrintString());
                    modified = true;
                    continue;
                }
            }

            return(modified);
        }