Esempio n. 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 (luaEnvironment == null)
            {
                // Create a Lua Environment if none exists yet
                luaEnvironment = LuaEnvironment.GetLua();
            }

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

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

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

            string s = GetLuaString();

            luaFunction = luaEnvironment.LoadLuaFunction(s, friendlyName);

            initialised = true;
        }
Esempio n. 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 (luaEnvironment == null)
            {
                luaEnvironment = GetComponent <LuaEnvironment>();
                if (luaEnvironment != null)
                {
                    luaEnvironment.InitEnvironment();
                }
            }

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

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

            MoonSharp.Interpreter.Script interpreter = luaEnvironment.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);
        }
Esempio n. 3
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 (luaEnvironment == null)
            {
                luaEnvironment = GetComponent<LuaEnvironment>();
                if (luaEnvironment != null)
                {
                    luaEnvironment.InitEnvironment();
                }
            }
                    
            if (luaEnvironment == null)
            {
                UnityEngine.Debug.LogError("No Lua Environment found");
                return false;
            }

            if (luaEnvironment.Interpreter == null)
            {
                UnityEngine.Debug.LogError("No Lua interpreter found");
                return false;
            }
                
            MoonSharp.Interpreter.Script interpreter = luaEnvironment.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;
        }