Esempio n. 1
0
        /// <summary>
        /// Registers all listed c# types for interop with Lua.
        /// You can also register types directly in the Awake method of any
        /// monobehavior in your scene using UserData.RegisterType().
        /// </summary>
        protected virtual void InitTypes()
        {
            bool isFungusInstalled = (Type.GetType("Fungus.Flowchart") != null);

            foreach (TextAsset textFile in registerTypes)
            {
                if (textFile == null)
                {
                    continue;
                }

                // Parse JSON file
                JSONObject jsonObject = new JSONObject(textFile.text);
                if (jsonObject == null ||
                    jsonObject.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("Error parsing JSON file " + textFile.name);
                    continue;
                }

                // Register types with MoonSharp
                JSONObject registerTypesArray = jsonObject.GetField("registerTypes");
                if (registerTypesArray != null &&
                    registerTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in registerTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            // Don't register fungus types if the Fungus library is not present
                            if (!isFungusInstalled &&
                                typeName.StartsWith("Fungus."))
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName);
                        }
                    }
                }

                // Register extension types with MoonSharp
                JSONObject extensionTypesArray = jsonObject.GetField("extensionTypes");
                if (extensionTypesArray != null &&
                    extensionTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in extensionTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            // Don't register fungus types if the Fungus library is not present
                            if (!isFungusInstalled &&
                                typeName.StartsWith("Fungus."))
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName, true);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Register some commonly used Unity classes and objects for Lua interop.
        /// To register more class objects externally to this class, register them in the Awake method of any
        /// monobehavior in your scene.
        /// </summary>
        protected virtual void InitFungusModule()
        {
            if (fungusModule == FungusModuleOptions.NoFungusModule)
            {
                return;
            }

            MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;

            // Require the Fungus module and assign it to the global 'fungus'
            Table fungusTable = null;

            MoonSharp.Interpreter.DynValue value = interpreter.RequireModule("fungus");
            if (value != null &&
                value.Type == DataType.Function)
            {
                fungusTable = value.Function.Call().Table;
            }
            if (fungusTable == null)
            {
                UnityEngine.Debug.LogError("Failed to create Fungus table");
                return;
            }
            interpreter.Globals["fungus"] = fungusTable;

            // Static classes
            fungusTable["time"]        = UserData.CreateStatic(typeof(Time));
            fungusTable["playerprefs"] = UserData.CreateStatic(typeof(PlayerPrefs));
            fungusTable["prefs"]       = UserData.CreateStatic(typeof(FungusPrefs));
            fungusTable["factory"]     = UserData.CreateStatic(typeof(PODTypeFactory));

            // Lua Environment and Lua Utils components
            fungusTable["luaenvironment"] = luaEnvironment;
            fungusTable["luautils"]       = this;

            // Provide access to the Unity Test Tools (if available).
            Type testType = Type.GetType("IntegrationTest");

            if (testType != null)
            {
                UserData.RegisterType(testType);
                fungusTable["test"] = UserData.CreateStatic(testType);
            }

            // Populate the string table by parsing the string table JSON files
            stringTable = new Table(interpreter);
            fungusTable["stringtable"] = stringTable;
            foreach (TextAsset stringFile in stringTables)
            {
                if (stringFile.text == "")
                {
                    continue;
                }

                JSONObject stringsJSON = new JSONObject(stringFile.text);
                if (stringsJSON == null ||
                    stringsJSON.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                    continue;
                }

                foreach (string stringKey in stringsJSON.keys)
                {
                    if (stringKey == "")
                    {
                        UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                        continue;
                    }

                    Table entriesTable = new Table(interpreter);
                    stringTable[stringKey] = entriesTable;

                    JSONObject entries = stringsJSON.GetField(stringKey);
                    if (entries.type != JSONObject.Type.OBJECT)
                    {
                        UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                        continue;
                    }

                    foreach (string language in entries.keys)
                    {
                        string translation = entries.GetField(language).str;
                        entriesTable[language] = translation;
                    }
                }
            }

            stringSubstituter = new StringSubstituter();

            if (fungusModule == FungusModuleOptions.UseGlobalVariables)
            {
                // Copy all items from the Fungus table to global variables
                foreach (TablePair p in fungusTable.Pairs)
                {
                    if (interpreter.Globals.Keys.Contains(p.Key))
                    {
                        UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key);
                    }
                    else
                    {
                        interpreter.Globals[p.Key] = p.Value;
                    }
                }

                interpreter.Globals["fungus"] = DynValue.Nil;

                // Note: We can't remove the fungus table itself because of dependencies between functions
            }
        }
        /// <summary>
        /// Registers all listed c# types for interop with Lua.
        /// You can also register types directly in the Awake method of any 
        /// monobehavior in your scene using UserData.RegisterType().
        /// </summary>
        protected virtual void InitTypes()
        {
            bool isFungusInstalled = (Type.GetType("Fungus.Flowchart") != null);

            // Always register these FungusLua utilities
            LuaEnvironment.RegisterType("Fungus.PODTypeFactory");
            LuaEnvironment.RegisterType("Fungus.FungusPrefs");

            foreach (TextAsset textFile in registerTypes)
            {
                if (textFile == null)
                {
                    continue;
                }

                // Parse JSON file
                JSONObject jsonObject = new JSONObject(textFile.text);
                if (jsonObject == null ||
                    jsonObject.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("Error parsing JSON file " + textFile.name);
                    continue;
                }

                // Register types with MoonSharp
                JSONObject registerTypesArray = jsonObject.GetField("registerTypes");
                if (registerTypesArray != null &&
                    registerTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in registerTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            // Don't register fungus types if the Fungus library is not present
                            if (!isFungusInstalled &&
                                typeName.StartsWith("Fungus."))
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName);
                        }
                    }
                }

                // Register extension types with MoonSharp
                JSONObject extensionTypesArray = jsonObject.GetField("extensionTypes");
                if (extensionTypesArray != null &&
                    extensionTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in extensionTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            // Don't register fungus types if the Fungus library is not present
                            if (!isFungusInstalled &&
                                typeName.StartsWith("Fungus."))
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName, true);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Registers all listed c# types for interop with Lua.
        /// You can also register types directly in the Awake method of any
        /// monobehavior in your scene using UserData.RegisterType().
        /// </summary>
        protected virtual void InitTypes()
        {
            // Always register these FungusLua utilities
            LuaEnvironment.RegisterType("Fungus.PODTypeFactory");
            LuaEnvironment.RegisterType("Fungus.FungusPrefs");

            foreach (TextAsset textFile in registerTypes)
            {
                if (textFile == null)
                {
                    continue;
                }

                // Parse JSON file
                JSONObject jsonObject = new JSONObject(textFile.text);
                if (jsonObject == null ||
                    jsonObject.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("Error parsing JSON file " + textFile.name);
                    continue;
                }

                // Register types with MoonSharp
                JSONObject registerTypesArray = jsonObject.GetField("registerTypes");
                if (registerTypesArray != null &&
                    registerTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in registerTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            if (Type.GetType(typeName) == null)
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName);
                        }
                    }
                }

                // Register extension types with MoonSharp
                JSONObject extensionTypesArray = jsonObject.GetField("extensionTypes");
                if (extensionTypesArray != null &&
                    extensionTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in extensionTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            if (Type.GetType(typeName) == null)
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName, true);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Register some commonly used Unity classes and objects for Lua interop.
        /// To register more class objects externally to this class, register them in the Awake method of any 
        /// monobehavior in your scene.
        /// </summary>
        protected virtual void InitFungusModule()
        {
            if (fungusModule == FungusModuleOptions.NoFungusModule)
            {
                return;
            }

            MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;

            // Require the Fungus module and assign it to the global 'fungus'
            Table fungusTable = null;
            MoonSharp.Interpreter.DynValue value = interpreter.RequireModule("fungus");
            if (value != null &&
                value.Type == DataType.Function)
            {
                fungusTable = value.Function.Call().Table;
            }
            if (fungusTable == null)
            {
                UnityEngine.Debug.LogError("Failed to create Fungus table");
                return;
            }
            interpreter.Globals["fungus"] = fungusTable;

            // Static classes
            fungusTable["time"] = UserData.CreateStatic(typeof(Time));
            fungusTable["playerprefs"] = UserData.CreateStatic(typeof(PlayerPrefs));
            fungusTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs));
            fungusTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory));

            // Lua Environment and Lua Utils components
            fungusTable["luaenvironment"] = luaEnvironment;
            fungusTable["luautils"] = this;

            // Provide access to the Unity Test Tools (if available).
            Type testType = Type.GetType("IntegrationTest");
            if (testType != null)
            {
                UserData.RegisterType(testType);
                fungusTable["test"] = UserData.CreateStatic(testType);
            }

            // Populate the string table by parsing the string table JSON files
            stringTable = new Table(interpreter); 
            fungusTable["stringtable"] = stringTable;
            foreach (TextAsset stringFile in stringTables)
            {
                if (stringFile.text == "")
                {
                    continue;
                }

                JSONObject stringsJSON = new JSONObject(stringFile.text);
                if (stringsJSON == null ||
                    stringsJSON.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                    continue;
                }

                foreach (string stringKey in stringsJSON.keys)
                {
                    if (stringKey == "")
                    {
                        UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                        continue;
                    }

                    Table entriesTable = new Table(interpreter);
                    stringTable[stringKey] = entriesTable;

                    JSONObject entries = stringsJSON.GetField(stringKey);
                    if (entries.type != JSONObject.Type.OBJECT)
                    {
                        UnityEngine.Debug.LogError("String table JSON format is not correct " + stringFile.name);
                        continue;
                    }

                    foreach (string language in entries.keys)
                    {
                        string translation = entries.GetField(language).str;
                        entriesTable[language] = translation;
                    }
                }
            }

            stringSubstituter = new StringSubstituter();
            stringSubstituter.CacheSubstitutionHandlers();

            conversationManager = new ConversationManager();
            conversationManager.PopulateCharacterCache();

            if (fungusModule == FungusModuleOptions.UseGlobalVariables)
            {               
                // Copy all items from the Fungus table to global variables
                foreach (TablePair p in fungusTable.Pairs)
                {
                    if (interpreter.Globals.Keys.Contains(p.Key))
                    {
                        UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key);
                    }
                    else
                    {
                        interpreter.Globals[p.Key] = p.Value;
                    }
                }

                interpreter.Globals["fungus"] = DynValue.Nil;

                // Note: We can't remove the fungus table itself because of dependencies between functions
            }
        }