Browse Source

Refactored StringSubstituter to use IStringSubstituter interface

master
Christopher 8 years ago
parent
commit
49278bf84c
  1. 3
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  2. 2
      Assets/Fungus/Narrative/Scripts/Localization.cs
  3. 46
      Assets/Fungus/Thirdparty/FungusLua/Scripts/IStringSubstituter.cs
  4. 12
      Assets/Fungus/Thirdparty/FungusLua/Scripts/IStringSubstituter.cs.meta
  5. 2
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaScript.cs
  6. 3
      Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaUtils.cs
  7. 33
      Assets/Fungus/Thirdparty/FungusLua/Scripts/StringSubstituter.cs

3
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -27,7 +27,7 @@ namespace Fungus
/// Flowchart objects may be edited visually using the Flowchart editor window. /// Flowchart objects may be edited visually using the Flowchart editor window.
/// </summary> /// </summary>
[ExecuteInEditMode] [ExecuteInEditMode]
public class Flowchart : MonoBehaviour, StringSubstituter.ISubstitutionHandler public class Flowchart : MonoBehaviour, ISubstitutionHandler
{ {
/// <summary> /// <summary>
/// The current version of the Flowchart. Used for updating components. /// The current version of the Flowchart. Used for updating components.
@ -1055,6 +1055,7 @@ 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.

2
Assets/Fungus/Narrative/Scripts/Localization.cs

@ -15,7 +15,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Multi-language localization support. /// Multi-language localization support.
/// </summary> /// </summary>
public class Localization : MonoBehaviour, ILocalization, StringSubstituter.ISubstitutionHandler public class Localization : MonoBehaviour, ILocalization, ISubstitutionHandler
{ {
[Tooltip("Language to use at startup, usually defined by a two letter language code (e.g DE = German)")] [Tooltip("Language to use at startup, usually defined by a two letter language code (e.g DE = German)")]
[SerializeField] protected string activeLanguage = ""; [SerializeField] protected string activeLanguage = "";

46
Assets/Fungus/Thirdparty/FungusLua/Scripts/IStringSubstituter.cs vendored

@ -0,0 +1,46 @@
using System.Text;
namespace Fungus
{
/// <summary>
/// Replaces special tokens in a string with substituted values (typically variables or localisation strings).
/// </summary>
public interface IStringSubstituter
{
/// <summary>
/// The internal StringBuilder object used to perform string substitution.
/// This is exposed publicly to allow for optimized string manipulation in client code.
/// </summary>
StringBuilder _StringBuilder { get; }
/// <summary>
/// Populates a cache of all components in the scene that implement ISubstitutionHandler.
/// </summary>
void CacheSubstitutionHandlers();
/// <summary>
/// Returns a new string that has been processed by all substitution handlers in the scene.
/// </summary>
string SubstituteStrings(string input);
/// <summary>
/// Returns a new string that has been processed by all substitution handlers in the scene.
/// </summary>
bool SubstituteStrings(StringBuilder input);
}
/// <summary>
/// Interface for components that support substituting strings.
/// </summary>
public interface ISubstitutionHandler
{
/// <summary>
/// Modifies a StringBuilder so that tokens are replaced by subsituted values.
/// It's up to clients how to implement substitution but the convention looks like:
/// "Hi {$VarName}" => "Hi John" where VarName == "John"
/// </summary>
/// <returns>True if the input was modified</returns>
bool SubstituteStrings(StringBuilder input);
}
}

12
Assets/Fungus/Thirdparty/FungusLua/Scripts/IStringSubstituter.cs.meta vendored

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b382917d791cc405f9a6bfdb5afed7a8
timeCreated: 1473692355
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

2
Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaScript.cs vendored

@ -14,7 +14,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Executes Lua script defined in a string property or in an external file. /// Executes Lua script defined in a string property or in an external file.
/// </summary> /// </summary>
public class LuaScript : MonoBehaviour public class LuaScript : MonoBehaviour, ILuaScript
{ {
/// <summary> /// <summary>
/// The Lua Environment to use when executing Lua script. /// The Lua Environment to use when executing Lua script.

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

@ -15,7 +15,7 @@ namespace Fungus
/// <summary> /// <summary>
/// A collection of utilites to use in Lua for common Unity / Fungus tasks. /// A collection of utilites to use in Lua for common Unity / Fungus tasks.
/// </summary> /// </summary>
public class LuaUtils : LuaEnvironment.Initializer, StringSubstituter.ISubstitutionHandler, ILuaUtils public class LuaUtils : LuaEnvironment.Initializer, ISubstitutionHandler, ILuaUtils
{ {
public enum FungusModuleOptions public enum FungusModuleOptions
{ {
@ -250,6 +250,7 @@ namespace Fungus
} }
stringSubstituter = new StringSubstituter(); stringSubstituter = new StringSubstituter();
stringSubstituter.CacheSubstitutionHandlers();
conversationManager = new ConversationManager(); conversationManager = new ConversationManager();
conversationManager.PopulateCharacterCache(); conversationManager.PopulateCharacterCache();

33
Assets/Fungus/Thirdparty/FungusLua/Scripts/StringSubstituter.cs vendored

@ -11,32 +11,16 @@ namespace Fungus
/// <summary> /// <summary>
/// Replaces special tokens in a string with substituted values (typically variables or localisation strings). /// Replaces special tokens in a string with substituted values (typically variables or localisation strings).
/// </summary> /// </summary>
public class StringSubstituter public class StringSubstituter : IStringSubstituter
{ {
/// <summary>
/// Interface for components that support substituting strings.
/// </summary>
public interface ISubstitutionHandler
{
/// <summary>
/// Modifies a StringBuilder so that tokens are replaced by subsituted values.
/// It's up to clients how to implement substitution but the convention looks like:
/// "Hi {$VarName}" => "Hi John" where VarName == "John"
/// <returns>True if the input was modified</returns>
/// </summary>
bool SubstituteStrings(StringBuilder input);
}
protected List<ISubstitutionHandler> substitutionHandlers = new List<ISubstitutionHandler>(); protected List<ISubstitutionHandler> substitutionHandlers = new List<ISubstitutionHandler>();
/// <summary> /// <summary>
/// The StringBuilder instance used to substitute strings optimally. /// The StringBuilder instance used to substitute strings optimally.
/// This property is public to support client code optimisations.
/// </summary> /// </summary>
protected StringBuilder stringBuilder; protected StringBuilder stringBuilder;
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
private int recursionDepth; protected int recursionDepth;
/// <summary> /// <summary>
/// Constructor which caches all components in the scene that implement ISubstitutionHandler. /// Constructor which caches all components in the scene that implement ISubstitutionHandler.
@ -44,14 +28,14 @@ namespace Fungus
/// </summary> /// </summary>
public StringSubstituter(int recursionDepth = 5) public StringSubstituter(int recursionDepth = 5)
{ {
CacheSubstitutionHandlers();
stringBuilder = new StringBuilder(1024); stringBuilder = new StringBuilder(1024);
this.recursionDepth = recursionDepth; this.recursionDepth = recursionDepth;
} }
/// <summary> #region IStringSubstituter implementation
/// Populates a cache of all components in the scene that implement ISubstitutionHandler.
/// </summary> public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
public virtual void CacheSubstitutionHandlers() public virtual void CacheSubstitutionHandlers()
{ {
// Use reflection to find all components in the scene that implement ISubstitutionHandler // Use reflection to find all components in the scene that implement ISubstitutionHandler
@ -74,9 +58,6 @@ namespace Fungus
} }
} }
/// <summary>
/// Returns a new string that has been processed by all substitution handlers in the scene.
/// </summary>
public virtual string SubstituteStrings(string input) public virtual string SubstituteStrings(string input)
{ {
stringBuilder.Length = 0; stringBuilder.Length = 0;
@ -120,5 +101,7 @@ namespace Fungus
return result; return result;
} }
#endregion
} }
} }
Loading…
Cancel
Save