Browse Source

Fixed Localization only localizes character name, not story text #611, #614

Wasn’t able to replicate this, but I think it was an issue around
caching SubstitutionHandlers. I’ve changed this system to use a static
list that each substituter registers / unregisters with on OnEnable /
OnDisable. Should be more robust now.
master
Christopher 8 years ago
parent
commit
b8a96bef8a
  1. 15
      Assets/Fungus/Scripts/Components/Flowchart.cs
  2. 10
      Assets/Fungus/Scripts/Components/Localization.cs
  3. 8
      Assets/Fungus/Scripts/Components/SayDialog.cs
  4. 11
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs
  5. 5
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs
  6. 43
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs

15
Assets/Fungus/Scripts/Components/Flowchart.cs

@ -147,6 +147,15 @@ namespace Fungus
CheckItemIds(); CheckItemIds();
CleanupComponents(); CleanupComponents();
UpdateVersion(); UpdateVersion();
StringSubstituter.RegisterHandler(this);
}
protected virtual void OnDisable()
{
cachedFlowcharts.Remove(this);
StringSubstituter.UnregisterHandler(this);
} }
protected virtual void UpdateVersion() protected virtual void UpdateVersion()
@ -172,11 +181,6 @@ namespace Fungus
version = FungusConstants.CurrentVersion; version = FungusConstants.CurrentVersion;
} }
protected virtual void OnDisable()
{
cachedFlowcharts.Remove(this);
}
protected virtual void CheckItemIds() protected virtual void CheckItemIds()
{ {
// Make sure item ids are unique and monotonically increasing. // Make sure item ids are unique and monotonically increasing.
@ -1181,7 +1185,6 @@ namespace Fungus
if (stringSubstituer == null) if (stringSubstituer == null)
{ {
stringSubstituer = new StringSubstituter(); stringSubstituer = new StringSubstituter();
stringSubstituer.CacheSubstitutionHandlers();
} }
// Use the string builder from StringSubstituter for efficiency. // Use the string builder from StringSubstituter for efficiency.

10
Assets/Fungus/Scripts/Components/Localization.cs

@ -65,6 +65,16 @@ namespace Fungus
} }
} }
protected virtual void OnEnable()
{
StringSubstituter.RegisterHandler(this);
}
protected virtual void OnDisable()
{
StringSubstituter.UnregisterHandler(this);
}
protected virtual void Start() protected virtual void Start()
{ {
Init(); Init();

8
Assets/Fungus/Scripts/Components/SayDialog.cs

@ -149,14 +149,6 @@ namespace Fungus
} }
} }
protected virtual void OnEnable()
{
// We need to update the cached list every time the Say Dialog is enabled
// due to an initialization order issue after loading scenes.
stringSubstituter.CacheSubstitutionHandlers();
}
protected virtual void LateUpdate() protected virtual void LateUpdate()
{ {
UpdateAlpha(); UpdateAlpha();

11
Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaUtils.cs vendored

@ -68,6 +68,16 @@ namespace Fungus
protected ConversationManager conversationManager; protected ConversationManager conversationManager;
#endif #endif
protected virtual void OnEnable()
{
StringSubstituter.RegisterHandler(this);
}
protected virtual void OnDisable()
{
StringSubstituter.UnregisterHandler(this);
}
/// <summary> /// <summary>
/// Registers all listed c# types for interop with Lua. /// Registers all listed c# types for interop with Lua.
/// You can also register types directly in the Awake method of any /// You can also register types directly in the Awake method of any
@ -245,7 +255,6 @@ namespace Fungus
} }
stringSubstituter = new StringSubstituter(); stringSubstituter = new StringSubstituter();
stringSubstituter.CacheSubstitutionHandlers();
#if !FUNGUSLUA_STANDALONE #if !FUNGUSLUA_STANDALONE
conversationManager = new ConversationManager(); conversationManager = new ConversationManager();

5
Assets/Fungus/Thirdparty/FungusLua/Scripts/Interfaces/IStringSubstituter.cs vendored

@ -16,11 +16,6 @@ namespace Fungus
/// </summary> /// </summary>
StringBuilder _StringBuilder { get; } StringBuilder _StringBuilder { get; }
/// <summary>
/// Populates a cache of all components in the scene that implement ISubstitutionHandler.
/// </summary>
void CacheSubstitutionHandlers();
/// <summary> /// <summary>
/// Returns a new string that has been processed by all substitution handlers in the scene. /// Returns a new string that has been processed by all substitution handlers in the scene.
/// </summary> /// </summary>

43
Assets/Fungus/Thirdparty/FungusLua/Scripts/Utils/StringSubstituter.cs vendored

@ -16,7 +16,7 @@ namespace Fungus
/// </summary> /// </summary>
public class StringSubstituter : IStringSubstituter public class StringSubstituter : IStringSubstituter
{ {
protected List<ISubstitutionHandler> substitutionHandlers = new List<ISubstitutionHandler>(); protected static List<ISubstitutionHandler> substitutionHandlers = new List<ISubstitutionHandler>();
/// <summary> /// <summary>
/// The StringBuilder instance used to substitute strings optimally. /// The StringBuilder instance used to substitute strings optimally.
@ -27,6 +27,19 @@ namespace Fungus
#region Public members #region Public members
public static void RegisterHandler(ISubstitutionHandler handler)
{
if (!substitutionHandlers.Contains(handler))
{
substitutionHandlers.Add(handler);
}
}
public static void UnregisterHandler(ISubstitutionHandler handler)
{
substitutionHandlers.Remove(handler);
}
/// <summary> /// <summary>
/// Constructor which caches all components in the scene that implement ISubstitutionHandler. /// Constructor which caches all components in the scene that implement ISubstitutionHandler.
/// <param name="recursionDepth">Number of levels of recursively embedded keys to resolve.</param> /// <param name="recursionDepth">Number of levels of recursively embedded keys to resolve.</param>
@ -43,34 +56,6 @@ namespace Fungus
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } } public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
public virtual void CacheSubstitutionHandlers()
{
// Use reflection to find all components in the scene that implement ISubstitutionHandler
#if NETFX_CORE
var types = this.GetType().GetAssembly().GetTypes().Where(type => type.IsClass() &&
!type.IsAbstract() &&
typeof(ISubstitutionHandler).IsAssignableFrom(type));
#else
var types = this.GetType().Assembly.GetTypes().Where(type => type.IsClass &&
!type.IsAbstract &&
typeof(ISubstitutionHandler).IsAssignableFrom(type));
#endif
substitutionHandlers.Clear();
foreach (System.Type t in types)
{
Object[] objects = GameObject.FindObjectsOfType(t);
foreach (Object o in objects)
{
ISubstitutionHandler handler = o as ISubstitutionHandler;
if (handler != null)
{
substitutionHandlers.Add(handler);
}
}
}
}
public virtual string SubstituteStrings(string input) public virtual string SubstituteStrings(string input)
{ {
stringBuilder.Length = 0; stringBuilder.Length = 0;

Loading…
Cancel
Save