-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathQuestMacroHelper.cs
More file actions
380 lines (340 loc) · 17 KB
/
Copy pathQuestMacroHelper.cs
File metadata and controls
380 lines (340 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Game.Questing;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Game.Player;
using DaggerfallWorkshop.Game.UserInterfaceWindows;
namespace DaggerfallWorkshop.Utility
{
/// <summary>
/// Resolves in-place text replacement macros like %var, =var_, __var, etc. for various objects.
/// More information: http://www.dfworkshop.net/static_files/questing-source-docs.html#qrcsymbols
/// Caller will need to supply related objects (e.g. quest) as needed.
/// NOTE: This class will expand over time.
/// </summary>
public class QuestMacroHelper
{
#region Fields
#endregion
#region Structs and Enums
/// <summary>
/// A macro to resolve.
/// </summary>
struct Macro
{
public string token; // String token of any macro found (e.g. "__symbol_")
public MacroTypes type; // Type of macro found (MacroTypes.None if not found)
public string symbol; // Inner symbol of macro
public int index; // Index of first macro character
public int length; // Length of macro text from index
}
#endregion
#region Public Methods
#endregion
#region Quests
/// <summary>
/// Gets reference to all QuestResource objects referenced by message macros
/// </summary>
/// <param name="message">Message to extract resources from.</param>
/// <returns>QuestResource array or null.</returns>
public QuestResource[] GetMessageResources(Message message)
{
// Must have a message
if (message == null)
return null;
// Get raw message and enumerate resource tokens
List<QuestResource> resources = new List<QuestResource>();
TextFile.Token[] tokens = message.GetTextTokens(-1, false);
for (int token = 0; token < tokens.Length; token++)
{
string[] words = GetWords(tokens[token].text);
for (int word = 0; word < words.Length; word++)
{
Macro macro = GetMacro(words[word]);
QuestResource resource = message.ParentQuest.GetResource(macro.symbol);
if (resource != null)
resources.Add(resource);
}
}
return resources.ToArray();
}
/// <summary>
/// Expands any macros found inside quest message tokens.
/// </summary>
/// <param name="parentQuest">Parent quest of message.</param>
/// <param name="tokens">Array of message tokens to expand macros inside of.</param>
/// <param name="resolveDialogLinks">will reveal dialog linked resources in talk window (this must be false for all calls to this function except if caller is talk manager when expanding answers or for quest popups).</param>
public void ExpandQuestMessage(Quest parentQuest, ref TextFile.Token[] tokens, bool revealDialogLinks = false)
{
// Iterate message tokens
for (int token = 0; token < tokens.Length; token++)
{
// Split token text into individual words
string[] words = GetWords(tokens[token].text);
// Iterate words to find macros
for (int word = 0; word < words.Length; word++)
{
Macro macro = GetMacro(words[word]);
if (macro.type == MacroTypes.ContextMacro)
{
words[word] = words[word].Replace(macro.token, MacroHelper.GetValue(macro.token, parentQuest, parentQuest.ExternalMCP));
}
else
{
// fix for bug when parent quest is no longer available (http://forums.dfworkshop.net/viewtopic.php?f=24&t=1002) in case
// quest injected entries and rumors stay in list due to other bugs
// so parentQuest is no longer available
if (parentQuest == null)
return;
// Ask resource to expand macro if possible
QuestResource resource = parentQuest.GetResource(macro.symbol);
if (resource != null)
{
string result;
if (resource.ExpandMacro(macro.type, out result))
{
words[word] = words[word].Replace(macro.token, result);
}
// reveal dialog linked resources in talk window
if (revealDialogLinks && macro.type == MacroTypes.NameMacro1) // only resolve if their true name was expanded (given) which is MacroTypes.NameMacro1
{
System.Type t = resource.GetType();
if (t.Equals(typeof(DaggerfallWorkshop.Game.Questing.Place)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Location);
}
else if (t.Equals(typeof(DaggerfallWorkshop.Game.Questing.Person)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Person);
}
else if (t.Equals(typeof(DaggerfallWorkshop.Game.Questing.Item)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Thing);
}
}
}
}
// TODO: Need to store previous macro resource for pronomial context expansions
}
// Reassemble words and expanded macros back into final token text
string final = string.Empty;
for (int i = 0; i < words.Length; i++)
{
final += words[i];
if (i != words.Length - 1)
final += " ";
}
// Store result back into token
tokens[token].text = final;
}
}
public void ExpandQuestString(Quest parentQuest, ref string questString)
{
Macro macro = GetMacro(questString);
if (macro.type == MacroTypes.ContextMacro)
{
questString = questString.Replace(macro.token, MacroHelper.GetValue(macro.token, parentQuest));
}
}
/// <summary>
/// Expands name macros found inside letter message tokens and returns signoff.
/// </summary>
/// <param name="parentQuest">Parent quest of message.</param>
/// <param name="tokens">Array of message tokens to expand macros inside of.</param>
public string ExpandLetterSignoff(Quest parentQuest, TextFile.Token[] tokens)
{
string signoff = "";
int lines = 0;
for (int t = tokens.Length - 1; t >= 0; t--)
{
string[] words = GetWords(tokens[t].text);
for (int w = 0; w < words.Length; w++)
{
Macro macro = GetMacro(words[w]);
switch (macro.type)
{
// Expand simple name and other macros
case MacroTypes.NameMacro1:
case MacroTypes.DetailsMacro:
case MacroTypes.FactionMacro:
case MacroTypes.ContextMacro:
case MacroTypes.BindingMacro:
{
if (macro.type == MacroTypes.ContextMacro)
{
words[w] = words[w].Replace(macro.token, MacroHelper.GetValue(macro.token, parentQuest, parentQuest.ExternalMCP));
}
else
{
QuestResource resource = parentQuest.GetResource(macro.symbol);
if (resource != null)
{
string result;
if (resource.ExpandMacro(macro.type, out result))
{
words[w] = words[w].Replace(macro.token, result);
}
if (macro.type == MacroTypes.NameMacro1)
{
// reveal dialog linked resources in talk window
System.Type type = resource.GetType();
if (type.Equals(typeof(DaggerfallWorkshop.Game.Questing.Place)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Location);
}
else if (type.Equals(typeof(DaggerfallWorkshop.Game.Questing.Person)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Person);
}
else if (type.Equals(typeof(DaggerfallWorkshop.Game.Questing.Item)))
{
GameManager.Instance.TalkManager.AddDialogForQuestInfoResource(parentQuest.UID, macro.symbol, TalkManager.QuestInfoResourceType.Thing);
}
}
}
}
break;
}
// Replace location name macros with "..."
case MacroTypes.NameMacro2:
case MacroTypes.NameMacro3:
case MacroTypes.NameMacro4:
words[w] = "...";
break;
}
}
// Reassemble words and expanded macros back into final token text
string final = string.Empty;
for (int i = 0; i < words.Length; i++)
{
final += words[i];
if (i != words.Length - 1)
final += " ";
}
// Construct signoff
if (!string.IsNullOrEmpty(final))
{
signoff = final.Trim() + " " + signoff;
lines++;
}
if (lines >= 1)
break;
}
return TextManager.Instance.GetLocalizedText("letterPrefix") + signoff;
}
#endregion
#region Private Methods
/// <summary>
/// Splits text into words.
/// </summary>
string[] GetWords(string text)
{
return text.Split(' ');
}
/// <summary>
/// Attempts to get macro data from a word string.
/// Only a single macro will be matched per word.
/// </summary>
/// <param name="word">Source word to inspect for macro.</param>
/// <returns>Macro data, if a macro is found. Type will be be MacroTypes.None if no macro found in text.</returns>
Macro GetMacro(string word)
{
string pattern = @"(?<prefix>____)(?<NameMacro4_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>___)(?<NameMacro3_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>__)(?<NameMacro2_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>_)(?<NameMacro1_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>==)(?<FactionMacro_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>=#)(?<BindingMacro_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>=)(?<DetailsMacro_Symbol>[a-zA-Z0-9.]+)(?<suffix>_)|" +
@"(?<prefix>%)(?<ContextMacro_Symbol>\w+)";
// Mactch macro type and inner symbol
Macro macro = new Macro();
Match match = Regex.Match(word, pattern);
if (match.Success)
{
// Get possible groups from pattern to isolate match value
Group foundGroup = null;
Group NameMacro4_Group = match.Groups["NameMacro4_Symbol"];
Group NameMacro3_Group = match.Groups["NameMacro3_Symbol"];
Group NameMacro2_Group = match.Groups["NameMacro2_Symbol"];
Group NameMacro1_Group = match.Groups["NameMacro1_Symbol"];
Group FactionMacro_Group = match.Groups["FactionMacro_Symbol"];
Group BindingMacro_Group = match.Groups["BindingMacro_Symbol"];
Group DetailsMacro_Group = match.Groups["DetailsMacro_Symbol"];
Group ContextMacro_Group = match.Groups["ContextMacro_Symbol"];
// Check which match group (if any) was found
if (!string.IsNullOrEmpty(NameMacro4_Group.Value))
{
macro.type = MacroTypes.NameMacro4;
foundGroup = NameMacro4_Group;
}
else if (!string.IsNullOrEmpty(NameMacro3_Group.Value))
{
macro.type = MacroTypes.NameMacro3;
foundGroup = NameMacro3_Group;
}
else if (!string.IsNullOrEmpty(NameMacro2_Group.Value))
{
macro.type = MacroTypes.NameMacro2;
foundGroup = NameMacro2_Group;
}
else if (!string.IsNullOrEmpty(NameMacro1_Group.Value))
{
macro.type = MacroTypes.NameMacro1;
foundGroup = NameMacro1_Group;
}
else if (!string.IsNullOrEmpty(FactionMacro_Group.Value))
{
macro.type = MacroTypes.FactionMacro;
foundGroup = FactionMacro_Group;
}
else if (!string.IsNullOrEmpty(BindingMacro_Group.Value))
{
macro.type = MacroTypes.BindingMacro;
foundGroup = BindingMacro_Group;
}
else if (!string.IsNullOrEmpty(DetailsMacro_Group.Value))
{
macro.type = MacroTypes.DetailsMacro;
foundGroup = DetailsMacro_Group;
}
else if (!string.IsNullOrEmpty(ContextMacro_Group.Value))
{
macro.type = MacroTypes.ContextMacro;
foundGroup = ContextMacro_Group;
}
// Set macro data if found
if (foundGroup != null)
{
Group prefix = match.Groups["prefix"];
Group suffix = match.Groups["suffix"];
macro.symbol = foundGroup.Value;
macro.index = prefix.Index;
// Length is from first character to end of suffix (if present)
// This will exclude any other characters (like fullstop) adjacent to macro token in word
if (macro.type != MacroTypes.ContextMacro)
macro.length = suffix.Index + suffix.Length - macro.index;
else
macro.length = prefix.Length + macro.symbol.Length;
// Get substring of macro token alone
// This is used for replace later
macro.token = word.Substring(macro.index, macro.length);
}
}
return macro;
}
#endregion
}
}