Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text;
- using System.Collections.Generic;
- using SimpleJSON;
- using System.Text.RegularExpressions;
- using XRL.Language;
- namespace HistoryKit
- {
- public static class HistoricStringExpander
- {
- public static void SetVar(Dictionary<string, JSONNode> vars, string key, JSONNode val)
- {
- if (vars.ContainsKey(key))
- {
- vars[key] = val;
- }
- else
- {
- vars.Add(key, val);
- }
- }
- public static History nullHistory = new History(0);
- public static string ExpandQuery(string query, HistoricEntitySnapshot entity, History history, Dictionary<string, string> vars, Dictionary<string, JSONNode> nodeVars)
- {
- string origQuery = query;
- string ret = query;
- string var = null;
- bool bArticle = false;
- bool bCapitalize = false;
- bool bPossessive = false;
- if (history == null)
- {
- if (nullHistory == null) nullHistory = new History(0);
- history = nullHistory;
- }
- if (query.Length > 0 && query[0] == '<')
- {
- query = query.Substring(1, query.Length - 2);
- }
- if (query.EndsWith(".capitalize"))
- {
- query = query.Remove(query.Length - ".capitalize".Length);
- bCapitalize = true;
- }
- if (query.EndsWith(".article"))
- {
- query = query.Remove(query.Length - ".article".Length);
- bArticle = true;
- }
- if (query.EndsWith(".possessive"))
- {
- query = query.Remove(query.Length - ".possessive".Length);
- bPossessive = true;
- }
- string[] parts = query.Split('.');
- for (int x = 0; x < parts.Length; x++)
- {
- if (vars.ContainsKey(parts[x])) parts[x] = vars[parts[x]];
- }
- if (parts.Length < 2)
- {
- string target = null;
- if (parts[0].Contains("="))
- {
- string[] varparts = parts[0].Split('=');
- parts[0] = varparts[1];
- var = varparts[0];
- }
- else
- {
- var = null;
- }
- target = parts[0];
- if (nodeVars.ContainsKey(target))
- {
- ret = nodeVars[target].Value;
- goto done;
- }
- if (vars.ContainsKey(target))
- {
- ret = vars[target];
- goto done;
- }
- ret = target;
- goto done;
- }
- else
- {
- string target = null;
- if (query.Contains("="))
- {
- string[] varparts = query.Split('=');
- var = varparts[0];
- parts[0] = varparts[1].Split('.')[0];
- }
- else
- {
- var = null;
- }
- target = parts[0];
- if (target.StartsWith("spice") || target[0] == '$')
- {
- JSONNode spiceRoot;
- JSONNode originalSpiceRoot;
- if (target == "spice")
- {
- spiceRoot = HistoricSpice.root;
- }
- else
- {
- spiceRoot = nodeVars[target];
- }
- // resolve spice link
- for (int x = 1; x < parts.Length; x++)
- {
- originalSpiceRoot = spiceRoot;
- // TODO
- if (parts[x] == "!random")
- {
- if (x == parts.Length - 1)
- {
- JSONClass cl = spiceRoot as JSONClass;
- if (cl != null)
- {
- List<string> keys = cl.GetKeys();
- ret = keys[history.Random(0, keys.Count - 1)];
- if (!string.IsNullOrEmpty(var) && var[0] == '$')
- {
- SetVar(nodeVars, var, keys[history.Random(0, keys.Count - 1)]);
- return "";
- }
- goto done;
- }
- JSONNode node = spiceRoot[history.Random(0, spiceRoot.Count - 1)];
- if (!string.IsNullOrEmpty(var) && var[0] == '$')
- {
- SetVar(nodeVars, var, node);
- return "";
- }
- ret = node.Value;
- goto done;
- }
- else
- {
- int n = 0;
- n = history.Random(0, spiceRoot.Count - 1);
- spiceRoot = spiceRoot[n] as JSONNode;
- if (spiceRoot == null)
- {
- UnityEngine.Debug.LogError("no spice root after random in " + origQuery);
- return "";
- }
- }
- }
- else
- {
- if (parts[x].StartsWith("entity$"))
- {
- string[] subparts = parts[x].Split('$');
- if (subparts[1].Contains("["))
- {
- string[] listpropertyparts = subparts[1].Split('[');
- listpropertyparts[1] = listpropertyparts[1].Replace("]", "");
- if (!entity.listProperties.ContainsKey(listpropertyparts[0]))
- {
- if (!entity.properties.ContainsKey(subparts[1]))
- {
- if (originalSpiceRoot["_failureredirect"] != null)
- {
- StringBuilder newQuery = new StringBuilder(originalSpiceRoot["_failureredirect"]);
- for (int p = x; p < parts.Length; p++)
- {
- newQuery.Append(".");
- newQuery.Append(parts[p]);
- }
- return ExpandQuery(newQuery.ToString(), entity, history, vars, nodeVars);
- }
- if (originalSpiceRoot["_staticfailureredirect"] != null)
- {
- return ExpandQuery(originalSpiceRoot["_staticfailureredirect"], entity, history, vars, nodeVars);
- }
- return "<undefined entity property " + subparts[1] + ">";
- }
- return "<undefined entity list " + listpropertyparts[0] + ">";
- }
- if (entity.GetList(listpropertyparts[0]).Count == 0) return "<empty entity list " + listpropertyparts[0] + ">";
- int n = 0;
- if (listpropertyparts[1] == "random")
- {
- n = history.Random(0, entity.GetList(listpropertyparts[0]).Count - 1);
- }
- else
- {
- n = int.Parse(listpropertyparts[1]);
- }
- spiceRoot = spiceRoot[entity.GetList(listpropertyparts[0])[n]] as JSONNode;
- }
- else
- {
- if (!entity.properties.ContainsKey(subparts[1]))
- {
- if (originalSpiceRoot["_failureredirect"] != null)
- {
- StringBuilder newQuery = new StringBuilder(originalSpiceRoot["_failureredirect"]);
- for (int p = x; p < parts.Length; p++)
- {
- newQuery.Append(".");
- newQuery.Append(parts[p]);
- }
- return ExpandQuery(newQuery.ToString(), entity, history, vars, nodeVars);
- }
- if (originalSpiceRoot["_staticfailureredirect"] != null)
- {
- return ExpandQuery(originalSpiceRoot["_staticfailureredirect"], entity, history, vars, nodeVars);
- }
- return "<undefined entity property " + subparts[1] + ">";
- }
- spiceRoot = spiceRoot[entity.GetProperty(subparts[1])] as JSONNode;
- }
- }
- else
- {
- spiceRoot = spiceRoot[parts[x]] as JSONNode;
- if (spiceRoot == null)
- {
- if (originalSpiceRoot["_failureredirect"] != null)
- {
- StringBuilder newQuery = new StringBuilder(originalSpiceRoot["_failureredirect"]);
- for (int p = x; p < parts.Length; p++)
- {
- newQuery.Append(".");
- newQuery.Append(parts[p]);
- }
- return ExpandQuery(newQuery.ToString(), entity, history, vars, nodeVars);
- }
- if (originalSpiceRoot["_staticfailureredirect"] != null)
- {
- return ExpandQuery(originalSpiceRoot["_staticfailureredirect"], entity, history, vars, nodeVars);
- }
- }
- }
- if (spiceRoot == null)
- {
- UnityEngine.Debug.LogError("spice reference " + parts[x] + " in " + origQuery + " wasn't a node");
- return "";
- }
- if (x == parts.Length - 1)
- {
- ret = spiceRoot.Value;
- if (!string.IsNullOrEmpty(var) && var[0] == '$')
- {
- SetVar(nodeVars, var, spiceRoot);
- return "";
- }
- goto done;
- }
- }
- }
- }
- else
- if (target.StartsWith("entity"))
- {
- HistoricEntitySnapshot targetEntity = entity;
- if (target.StartsWith("entity["))
- {
- targetEntity = history.GetEntity(target.Split('[')[1].Replace("]", "")).GetSnapshotAtYear(history.currentYear);
- }
- if (targetEntity == null) return "<unknown entity>";
- // just a property
- if (parts.Length == 2)
- {
- if (parts[1].Contains("["))
- {
- string[] listpropertyparts = parts[1].Split('[');
- listpropertyparts[1] = listpropertyparts[1].Replace("]", "");
- if (!entity.listProperties.ContainsKey(listpropertyparts[0])) return "<undefined entity list " + listpropertyparts[0] + ">";
- if (entity.GetList(listpropertyparts[0]).Count == 0) return "<empty entity list " + listpropertyparts[0] + ">";
- int n = 0;
- if (listpropertyparts[1] == "random")
- {
- n = history.Random(0, entity.GetList(listpropertyparts[0]).Count - 1);
- }
- else
- {
- n = int.Parse(listpropertyparts[1]);
- }
- ret = targetEntity.GetList(listpropertyparts[0])[n];
- }
- else
- {
- ret = targetEntity.GetProperty(parts[1]);
- }
- goto done;
- }
- else
- {
- return "<unknown format " + query + ">";
- }
- }
- else
- if (target.StartsWith("history"))
- {
- }
- }
- done:
- if (ret == " ") ret = "";
- if (bArticle && ret.Length > 0)
- {
- if (ret[0] == '=')
- {
- ret = (bCapitalize ? "=Article=" : "=article=") + ret;
- }
- else
- {
- ret = Grammar.A(ret, bCapitalize);
- }
- bCapitalize = false;
- }
- if (bCapitalize && ret.Length > 0)
- {
- if( ret[0] == '<')
- {
- if( ret.Substring(ret.IndexOf('>')-".capitalize".Length) != ".capitalize")
- {
- ret = ret.Insert(ret.IndexOf('>'), ".capitalize");
- }
- }
- else
- if (ret[0] == '=')
- {
- ret = "=capitalize=" + ret;
- }
- else
- {
- ret = char.ToUpper(ret[0]) + ret.Substring(1);
- }
- }
- if (var != null)
- {
- if (vars.ContainsKey(var))
- {
- vars[var] = ret;
- }
- else
- {
- vars.Add(var, ret);
- }
- return "";
- }
- if (vars != null)
- {
- foreach (var pair in vars)
- {
- int n = 0;
- while (ret.Contains(pair.Key) && n < 5)
- {
- ret.Replace(pair.Key, pair.Value);
- n++;
- }
- }
- }
- return ret;
- }
- public static string ExpandString(string input)
- {
- return ExpandString(input, null, null);
- }
- public static string ExpandString(string input, HistoricEntitySnapshot entity, History history, Dictionary<string, string> vars = null)
- {
- if (string.IsNullOrEmpty(input)) return "";
- if (history == null)
- {
- if (nullHistory == null) nullHistory = new History(0);
- history = nullHistory;
- }
- HistoricSpice.Init();
- StringBuilder sb = new StringBuilder(input);
- if (vars == null) vars = new Dictionary<string, string>();
- Dictionary<string, JSONNode> nodeVars = new Dictionary<string, JSONNode>();
- Match HeaderMatches = Regex.Match(input, @"<.*?>");
- int n = 0;
- while (HeaderMatches != null && !string.IsNullOrEmpty(HeaderMatches.Value))
- {
- int pos = HeaderMatches.Groups[0].Index;
- int len = HeaderMatches.Groups[0].Length;
- sb.Remove(pos, len);
- sb.Insert(pos, ExpandQuery(HeaderMatches.Groups[0].Value, entity, history, vars, nodeVars));
- if (sb.Equals(input)) break;
- n++;
- if (n > 25)
- {
- UnityEngine.Debug.LogError("Maximum recursion reached on " + input + " current " + sb.ToString());
- break;
- }
- HeaderMatches = Regex.Match(sb.ToString(), @"<.*?>");
- }
- if (vars != null)
- {
- foreach (var pair in vars)
- {
- n = 0;
- while (sb.Contains(pair.Key) && n < 5)
- {
- sb.Replace(pair.Key, pair.Value);
- n++;
- }
- }
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement