You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.5 KiB
107 lines
3.5 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
9 years ago
|
using UnityEngine;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
9 years ago
|
using System.Text;
|
||
9 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Replaces special tokens in a string with substituted values (typically variables or localisation strings).
|
||
|
/// </summary>
|
||
8 years ago
|
public class StringSubstituter : IStringSubstituter
|
||
8 years ago
|
{
|
||
|
protected List<ISubstitutionHandler> substitutionHandlers = new List<ISubstitutionHandler>();
|
||
9 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// The StringBuilder instance used to substitute strings optimally.
|
||
|
/// </summary>
|
||
8 years ago
|
protected StringBuilder stringBuilder;
|
||
9 years ago
|
|
||
8 years ago
|
protected int recursionDepth;
|
||
9 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Constructor which caches all components in the scene that implement ISubstitutionHandler.
|
||
9 years ago
|
/// <param name="recursionDepth">Number of levels of recursively embedded keys to resolve.</param>
|
||
8 years ago
|
/// </summary>
|
||
9 years ago
|
public StringSubstituter(int recursionDepth = 5)
|
||
8 years ago
|
{
|
||
9 years ago
|
stringBuilder = new StringBuilder(1024);
|
||
|
this.recursionDepth = recursionDepth;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
#region IStringSubstituter implementation
|
||
|
|
||
|
public virtual StringBuilder _StringBuilder { get { return stringBuilder; } }
|
||
8 years ago
|
|
||
|
public virtual void CacheSubstitutionHandlers()
|
||
|
{
|
||
|
// Use reflection to find all components in the scene that implement ISubstitutionHandler
|
||
|
var types = this.GetType().Assembly.GetTypes().Where(type => type.IsClass &&
|
||
|
!type.IsAbstract &&
|
||
|
typeof(ISubstitutionHandler).IsAssignableFrom(type));
|
||
|
|
||
|
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)
|
||
|
{
|
||
9 years ago
|
stringBuilder.Length = 0;
|
||
|
stringBuilder.Append(input);
|
||
|
|
||
|
if (SubstituteStrings(stringBuilder))
|
||
|
{
|
||
|
return stringBuilder.ToString();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return input; // String wasn't modified
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
|
public virtual bool SubstituteStrings(StringBuilder input)
|
||
|
{
|
||
|
bool result = false;
|
||
9 years ago
|
|
||
|
// Perform the substitution multiple times to expand nested keys
|
||
|
int loopCount = 0; // Avoid infinite recursion loops
|
||
9 years ago
|
while (loopCount < recursionDepth)
|
||
9 years ago
|
{
|
||
9 years ago
|
bool modified = false;
|
||
|
foreach (ISubstitutionHandler handler in substitutionHandlers)
|
||
|
{
|
||
|
if (handler.SubstituteStrings(input))
|
||
|
{
|
||
|
modified = true;
|
||
|
result = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!modified)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
|
||
|
loopCount++;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return result;
|
||
|
}
|
||
8 years ago
|
|
||
|
#endregion
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|