using System;
using System.Collections.Generic;
using UnityEngine;
namespace EasyTalk.Nodes.Variable
{
///
/// The NodeVariable class is used to store information about a dialogue variable, such as its type, name, initial value, and current value.
///
/// The variable type.
///
[SerializeField]
public Type variableType;
///
/// The variable name.
///
[SerializeField]
public string variableName;
///
/// The initial value of the variable.
///
[SerializeField]
public object initialValue;
///
/// The current value of the variable.
///
[SerializeField]
public object currentValue;
///
/// Whether the variable should be reset to its initial value each time the dialogue is entered.
///
[SerializeField]
public bool resetOnEntry;
///
/// Whether the variable is a global variable or not.
///
[SerializeField]
public bool isGlobal;
///
/// Strips all variable references from the provided string and returns a new string.
///
/// The string to remove variable references from.
/// A modified version of the provided string with all references to variables removed.
public static string RemoveVariables(string text)
{
string newText = text;
int startIndex = 0;
int endIndex = 0;
while ((startIndex = newText.IndexOf("(@", startIndex)) != -1 &&
(endIndex = newText.IndexOf(')', startIndex + 2)) != -1)
{
newText = newText.Substring(0, startIndex) + newText.Substring(endIndex + 1);
startIndex = endIndex;
}
return newText;
}
///
/// Takes each variable in the provided string and replaces it with a numeric variable reference. The nameReplacementMap is populated with the new variable reference
/// names as the keys, and the old variable reference names as the values. This method is useful for translations of text which contain variable references since automatic
/// translation could potentially rename variable references; nameing the variables numerically prevents this and allows the original variable names to be swapped back
/// into the string later.
///
/// The string to index variable references for.
/// A Dictionary which maps the new variable reference names to the old names.
/// A modified version of the original string with indexed variable references.
public static string IndexVariablesNames(string text, Dictionary nameReplacementMap)
{
string newText = text;
int startIndex = 0;
int endIndex = 0;
string passString = "*";
while (startIndex < newText.Length &&
(startIndex = newText.IndexOf("(@", startIndex)) != -1 &&
startIndex + 2 < newText.Length &&
(endIndex = newText.IndexOf(')', startIndex + 2)) != -1)
{
string oldVariableName = newText.Substring(startIndex + 2, endIndex - startIndex - 2);
string newVariableName = "(@" + passString + ")";
newText = newText.Substring(0, startIndex) + newVariableName + newText.Substring(endIndex + 1);
nameReplacementMap.Add(newVariableName, oldVariableName);
startIndex = endIndex;
passString += "*";
}
return newText;
}
///
/// Given a string, this method replaces the names of each variable reference in the string with the value attributed to the key of the same name in the provided
/// nameReplacementMap.
///
/// The string to replace variable name references in.
/// A mapping of current variable names to replacement names.
/// A modified version of the original string where the original variable name references have been replaced based upon the values in the nameReplacementMap.
public static string ReplaceVariablesNames(string text, Dictionary nameReplacementMap)
{
string newText = text;
foreach (string currentName in nameReplacementMap.Keys)
{
if (newText.Contains(currentName))
{
newText = newText.Replace(currentName, "(@" + nameReplacementMap[currentName] + ")");
}
}
return newText;
}
}
}