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.
/// </summary>
[ExecuteInEditMode]
public class Flowchart : MonoBehaviour, StringSubstituter.ISubstitutionHandler
public class Flowchart : MonoBehaviour, ISubstitutionHandler
{
/// <summary>
/// The current version of the Flowchart. Used for updating components.
@ -1055,6 +1055,7 @@ namespace Fungus
if (stringSubstituer == null)
{
stringSubstituer = new StringSubstituter();
stringSubstituer.CacheSubstitutionHandlers();
}
// Use the string builder from StringSubstituter for efficiency.

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

@ -15,7 +15,7 @@ namespace Fungus
/// <summary>
/// Multi-language localization support.
/// </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)")]
[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>
/// Executes Lua script defined in a string property or in an external file.
/// </summary>
public class LuaScript : MonoBehaviour
public class LuaScript : MonoBehaviour, ILuaScript
{
/// <summary>
/// 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>
/// A collection of utilites to use in Lua for common Unity / Fungus tasks.
/// </summary>
public class LuaUtils : LuaEnvironment.Initializer, StringSubstituter.ISubstitutionHandler, ILuaUtils
public class LuaUtils : LuaEnvironment.Initializer, ISubstitutionHandler, ILuaUtils
{
public enum FungusModuleOptions
{
@ -250,6 +250,7 @@ namespace Fungus
}
stringSubstituter = new StringSubstituter();
stringSubstituter.CacheSubstitutionHandlers();
conversationManager = new ConversationManager();
conversationManager.PopulateCharacterCache();

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

@ -11,32 +11,16 @@ namespace Fungus
/// <summary>
/// Replaces special tokens in a string with substituted values (typically variables or localisation strings).
/// </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>();
/// <summary>
/// The StringBuilder instance used to substitute strings optimally.
/// This property is public to support client code optimisations.
/// </summary>
protected StringBuilder stringBuilder;
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
private int recursionDepth;
protected int recursionDepth;
/// <summary>
/// Constructor which caches all components in the scene that implement ISubstitutionHandler.
@ -44,14 +28,14 @@ namespace Fungus
/// </summary>
public StringSubstituter(int recursionDepth = 5)
{
CacheSubstitutionHandlers();
stringBuilder = new StringBuilder(1024);
this.recursionDepth = recursionDepth;
}
#region IStringSubstituter implementation
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
/// <summary>
/// Populates a cache of all components in the scene that implement ISubstitutionHandler.
/// </summary>
public virtual void CacheSubstitutionHandlers()
{
// 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)
{
stringBuilder.Length = 0;
@ -120,5 +101,7 @@ namespace Fungus
return result;
}
#endregion
}
}
Loading…
Cancel
Save