Browse Source

Merge pull request #456 from snozbot/JSON-string-tables

Changed format of string tables to JSON
master
Chris Gregan 9 years ago
parent
commit
b9f94f18e3
  1. 49
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs
  2. 93
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs
  3. 26
      Assets/Tests/Lua/LuaEnvironmentTests.unity
  4. 9
      Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt
  5. 6
      Assets/Tests/TestAssets/Text/teststringtable.txt
  6. 0
      Assets/Tests/TestAssets/Text/teststringtable.txt.meta

49
Assets/Fungus/Thirdparty/FungusLua/Scripts/Editor/LuaUtilsEditor.cs vendored

@ -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();
}

93
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs vendored

@ -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;
/// <summary>
/// Lua script file which defines the global string table used for localisation.
/// </summary>
[Tooltip("Lua script file which defines the global string table used for localisation.")]
public TextAsset stringTable;
/// <summary>
/// The currently selected language in the string table. Affects variable substitution.
/// </summary>
@ -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;
/// <summary>
/// Lua script file which defines the global string table used for localisation.
/// </summary>
[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<TextAsset> stringTables = new List<TextAsset>();
/// <summary>
/// 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.
/// </summary>
[HideInInspector]
[Tooltip("JSON text files listing the c# types that can be accessed from Lua.")]
public List<TextAsset> registerTypes = new List<TextAsset>();
/// <summary>
@ -64,7 +66,7 @@ namespace Fungus
/// <summary>
/// Cached reference to the string table (if loaded).
/// </summary>
protected Table stringTableCached;
protected Table stringTable;
/// <summary>
/// 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
/// </summary>
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);

26
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

9
Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt

@ -1,9 +0,0 @@
local M = {}
M.hello =
{
en = "Hi there",
fr = "Bonjour"
}
return M;

6
Assets/Tests/TestAssets/Text/teststringtable.txt

@ -0,0 +1,6 @@
{
"hello" : {
"en" : "Hi there",
"fr" : "Bonjour"
}
}

0
Assets/Tests/TestAssets/Resources/Lua/teststringtable.txt.meta → Assets/Tests/TestAssets/Text/teststringtable.txt.meta

Loading…
Cancel
Save