From b660db22738d78a765ff4f1acba46a6f7ce5626c Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 15 Apr 2016 17:18:01 +0100 Subject: [PATCH] Changed format of string tables to JSON --- .../Scripts/Editor/LuaUtilsEditor.cs | 49 +++++++--- .../Thirdparty/FungusLua/Scripts/LuaUtils.cs | 93 ++++++++++++------- Assets/Tests/Lua/LuaEnvironmentTests.unity | 26 +++++- .../Resources/Lua/teststringtable.txt | 9 -- .../Tests/TestAssets/Text/teststringtable.txt | 6 ++ .../Lua => Text}/teststringtable.txt.meta | 0 6 files changed, 120 insertions(+), 63 deletions(-) delete mode 100644 Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt create mode 100644 Assets/Tests/TestAssets/Text/teststringtable.txt rename Assets/Tests/TestAssets/{Resources/Lua => Text}/teststringtable.txt.meta (100%) diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs index 1236b34a..80f1ae72 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs @@ -12,23 +12,41 @@ namespace Fungus [CustomEditor (typeof(LuaUtils))] public class LuaUtilsEditor : Editor { + protected SerializedProperty stringTablesProp; + protected ReorderableList stringTablesList; + protected SerializedProperty registerTypesProp; protected ReorderableList registerTypeList; protected virtual void OnEnable() { - registerTypesProp = serializedObject.FindProperty("registerTypes"); - registerTypeList = new ReorderableList(serializedObject, registerTypesProp, true, true, true, true); - - registerTypeList.drawHeaderCallback = (Rect rect) => { - EditorGUI.LabelField(rect, "Type Lists"); - }; - - registerTypeList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { - Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); - SerializedProperty element = registerTypesProp.GetArrayElementAtIndex(index); - EditorGUI.PropertyField(r, element, new GUIContent("")); - }; + // String Tables property + stringTablesProp = serializedObject.FindProperty("stringTables"); + stringTablesList = new ReorderableList(serializedObject, stringTablesProp, true, true, true, true); + + stringTablesList.drawHeaderCallback = (Rect rect) => { + EditorGUI.LabelField(rect, "String Tables"); + }; + + stringTablesList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { + Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); + SerializedProperty element = stringTablesProp.GetArrayElementAtIndex(index); + EditorGUI.PropertyField(r, element, new GUIContent("")); + }; + + // Register Types property + registerTypesProp = serializedObject.FindProperty("registerTypes"); + registerTypeList = new ReorderableList(serializedObject, registerTypesProp, true, true, true, true); + + registerTypeList.drawHeaderCallback = (Rect rect) => { + EditorGUI.LabelField(rect, "Type Lists"); + }; + + registerTypeList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { + Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); + SerializedProperty element = registerTypesProp.GetArrayElementAtIndex(index); + EditorGUI.PropertyField(r, element, new GUIContent("")); + }; } public override void OnInspectorGUI() @@ -37,8 +55,11 @@ namespace Fungus serializedObject.Update(); - EditorGUILayout.PrefixLabel(new GUIContent("Register Types", "Text files which list the CLR types that should be registered with this Lua environment.")); - registerTypeList.DoLayoutList(); + EditorGUILayout.PrefixLabel(new GUIContent("String Tables", "A list of JSON files containing localised strings. These strings are loaded into a 'stringtable' global variable.")); + stringTablesList.DoLayoutList(); + + EditorGUILayout.PrefixLabel(new GUIContent("Register Types", "Text files which list the CLR types that should be registered with this Lua environment.")); + registerTypeList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs index b8aabd9f..156ebdea 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs @@ -29,12 +29,6 @@ namespace Fungus [Tooltip("Controls if the fungus utilities are accessed from globals (e.g. say) or via a fungus variable (e.g. fungus.say)")] public FungusModuleOptions fungusModule = FungusModuleOptions.UseGlobalVariables; - /// - /// Lua script file which defines the global string table used for localisation. - /// - [Tooltip("Lua script file which defines the global string table used for localisation.")] - public TextAsset stringTable; - /// /// The currently selected language in the string table. Affects variable substitution. /// @@ -50,10 +44,18 @@ namespace Fungus [Tooltip("Time scale factor to apply when running Lua scripts. If negative then uses the same values as the standard Time class.")] public float timeScale = -1f; + /// + /// Lua script file which defines the global string table used for localisation. + /// + [HideInInspector] + [Tooltip("List of JSON text files which contain localized strings. These strings are added to the 'stringTable' table in the Lua environment at startup.")] + public List stringTables = new List(); + /// - /// A text file listing the c# types that can be accessed from Lua. + /// JSON text files listing the c# types that can be accessed from Lua. /// [HideInInspector] + [Tooltip("JSON text files listing the c# types that can be accessed from Lua.")] public List registerTypes = new List(); /// @@ -64,7 +66,7 @@ namespace Fungus /// /// Cached reference to the string table (if loaded). /// - protected Table stringTableCached; + protected Table stringTable; /// /// Cached reference to the Lua Environment component. @@ -231,30 +233,49 @@ namespace Fungus fungusTable["test"] = UserData.CreateStatic(testType); } - // Register the stringtable (if one is set) - if (stringTable != null) - { - try - { - DynValue stringTableRes = interpreter.DoString(stringTable.text); - if (stringTableRes.Type == DataType.Table) - { - stringTableCached = stringTableRes.Table; - if (fungusTable != null) - { - fungusTable["stringtable"] = stringTableCached; - } - } - } - catch (ScriptRuntimeException ex) - { - LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); - } - catch (InterpreterException ex) - { - LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); - } - } + // 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(); @@ -285,10 +306,10 @@ namespace Fungus /// public virtual string GetString(string key) { - if (stringTableCached != null) + if (stringTable != null) { // Match against string table and active language - DynValue stringTableVar = stringTableCached.Get(key); + DynValue stringTableVar = stringTable.Get(key); if (stringTableVar.Type == DataType.Table) { DynValue languageEntry = stringTableVar.Table.Get(activeLanguage); @@ -347,9 +368,9 @@ namespace Fungus string key = match.Value.Substring(2, match.Value.Length - 3); // Match against string table and active language (if specified) - if (stringTableCached != null) + if (stringTable != null) { - DynValue stringTableVar = stringTableCached.Get(key); + DynValue stringTableVar = stringTable.Get(key); if (stringTableVar.Type == DataType.Table) { DynValue languageEntry = stringTableVar.Table.Get(activeLanguage); diff --git a/Assets/Tests/Lua/LuaEnvironmentTests.unity b/Assets/Tests/Lua/LuaEnvironmentTests.unity index 9a0d7786..5cfe865f 100644 --- a/Assets/Tests/Lua/LuaEnvironmentTests.unity +++ b/Assets/Tests/Lua/LuaEnvironmentTests.unity @@ -254,6 +254,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &156811089 GameObject: m_ObjectHideFlags: 0 @@ -320,6 +321,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &159008372 GameObject: m_ObjectHideFlags: 0 @@ -389,6 +391,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!4 &236999594 stripped Transform: m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} @@ -572,6 +575,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1001 &364012152 Prefab: m_ObjectHideFlags: 0 @@ -713,9 +717,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: fungusModule: 0 - stringTable: {fileID: 0} activeLanguage: en timeScale: -1 + stringTables: [] registerTypes: - {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3} --- !u!114 &550607926 @@ -905,6 +909,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &715881501 GameObject: m_ObjectHideFlags: 0 @@ -1025,9 +1030,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: fungusModule: 0 - stringTable: {fileID: 0} activeLanguage: en timeScale: -1 + stringTables: [] registerTypes: - {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3} - {fileID: 4900000, guid: 93fddea8208764a2dbb189cc238aed40, type: 3} @@ -1178,9 +1183,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: fungusModule: 0 - stringTable: {fileID: 0} activeLanguage: en timeScale: -1 + stringTables: [] registerTypes: - {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3} - {fileID: 4900000, guid: 93fddea8208764a2dbb189cc238aed40, type: 3} @@ -1303,6 +1308,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!4 &1167396341 stripped Transform: m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} @@ -1473,6 +1479,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &1394431693 GameObject: m_ObjectHideFlags: 0 @@ -1601,9 +1608,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: fungusModule: 0 - stringTable: {fileID: 0} activeLanguage: en timeScale: -1 + stringTables: [] registerTypes: - {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3} --- !u!114 &1463630765 @@ -2044,6 +2051,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &1818482578 GameObject: m_ObjectHideFlags: 0 @@ -2113,6 +2121,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &1828947831 GameObject: m_ObjectHideFlags: 0 @@ -2250,6 +2259,10 @@ Prefab: m_Modification: m_TransformParent: {fileID: 1532103954} m_Modifications: + - target: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + propertyPath: stringTables.Array.size + value: 1 + objectReference: {fileID: 0} - target: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} propertyPath: m_LocalPosition.x value: 0 @@ -2290,6 +2303,10 @@ Prefab: propertyPath: stringTable value: objectReference: {fileID: 4900000, guid: 9900570d789fa4b29957e4b897af9e3b, type: 3} + - target: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + propertyPath: stringTables.Array.data[0] + value: + objectReference: {fileID: 4900000, guid: 9900570d789fa4b29957e4b897af9e3b, type: 3} m_RemovedComponents: - {fileID: 11403674, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} m_ParentPrefab: {fileID: 100100000, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} @@ -2495,6 +2512,7 @@ MonoBehaviour: repeatEveryFrame: 1 hasFailed: 0 executeMethods: 2 + executeMethodName: OnExecute --- !u!1 &1960220030 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt b/Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt deleted file mode 100644 index ca1578b2..00000000 --- a/Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt +++ /dev/null @@ -1,9 +0,0 @@ -local M = {} - -M.hello = -{ - en = "Hi there", - fr = "Bonjour" -} - -return M; \ No newline at end of file diff --git a/Assets/Tests/TestAssets/Text/teststringtable.txt b/Assets/Tests/TestAssets/Text/teststringtable.txt new file mode 100644 index 00000000..d14689da --- /dev/null +++ b/Assets/Tests/TestAssets/Text/teststringtable.txt @@ -0,0 +1,6 @@ +{ + "hello" : { + "en" : "Hi there", + "fr" : "Bonjour" + } +} \ No newline at end of file diff --git a/Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt.meta b/Assets/Tests/TestAssets/Text/teststringtable.txt.meta similarity index 100% rename from Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt.meta rename to Assets/Tests/TestAssets/Text/teststringtable.txt.meta