generated from karashiiro/DalamudPluginProjectTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
UiJournalHandler.cs
526 lines (460 loc) · 18.2 KB
/
UiJournalHandler.cs
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// <copyright file="UiJournalHandler.cs" company="lokinmodar">
// Copyright (c) lokinmodar. All rights reserved.
// Licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License license.
// </copyright>
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Game.Addon.Lifecycle.AddonArgTypes;
using Dalamud.Memory;
using Echoglossian.EFCoreSqlite.Models.Journal;
using FFXIVClientStructs.FFXIV.Component.GUI;
using Humanizer;
namespace Echoglossian
{
public partial class Echoglossian
{
// used to be sure we don't translate the same quest name twice
private readonly ConcurrentDictionary<string, bool> translatedQuestNames = new();
private unsafe List<SummaryQuest> TranslateSummaries(
AtkComponentBase* journalBox,
QuestPlate foundQuestPlate,
string summaryText)
{
List<SummaryQuest> summaries = new();
if (summaryText == string.Empty)
{
return summaries;
}
for (var i = 0; i < journalBox->UldManager.NodeListCount; i++)
{
if (journalBox->UldManager.NodeList[i]->NodeId < 480700 || journalBox->UldManager.NodeList[i]->NodeId > 480750)
{
continue;
}
if (!journalBox->UldManager.NodeList[i]->IsVisible())
{
continue;
}
var summaryItemNode = journalBox->UldManager.NodeList[i]->GetAsAtkComponentNode();
var summaryNode = summaryItemNode->Component->UldManager.SearchNodeById(2);
if (summaryNode == null || summaryNode->Type != NodeType.Text || !summaryNode->IsVisible())
{
continue;
}
var summaryTextNode = summaryNode->GetAsAtkTextNode();
if (summaryTextNode->NodeText.IsEmpty)
{
continue;
}
var originalText = MemoryHelper.ReadSeStringAsString(out _, (nint)summaryTextNode->NodeText.StringPtr);
if (foundQuestPlate != null && foundQuestPlate.Summaries.TryGetValue(originalText, out var storedSummaryText))
{
summaries.Add(new(originalText, storedSummaryText, summaryTextNode, false));
continue;
}
var translatedText = this.Translate(originalText);
summaries.Add(new(originalText, translatedText, summaryTextNode, true));
}
return summaries;
}
private unsafe void TranslateQuestOnJournalBox(
AtkComponentBase* journalBox,
QuestPlate foundQuestPlate,
string questName,
string questMessage,
string objectiveText,
string summaryText,
AtkTextNode* questNameNode,
AtkTextNode* descriptionNode,
AtkTextNode* objectiveNode,
AtkTextNode* summaryNode)
{
string translatedQuestName;
string translatedQuestMessage;
string translatedQuestObjective;
string translatedQuestSummary = string.Empty;
List<SummaryQuest> summaries;
if (foundQuestPlate != null)
{
translatedQuestName = foundQuestPlate.TranslatedQuestName;
translatedQuestMessage = foundQuestPlate.TranslatedQuestMessage;
var shouldUpdateQuest = false;
if (foundQuestPlate.Objectives.TryGetValue(objectiveText, out var storedObjectiveText))
{
translatedQuestObjective = storedObjectiveText;
}
else
{
translatedQuestObjective = this.Translate(objectiveText);
foundQuestPlate.Objectives.Add(objectiveText, translatedQuestObjective);
shouldUpdateQuest = true;
}
if (summaryText != string.Empty)
{
if (foundQuestPlate.Summaries.TryGetValue(summaryText, out var storedSummaryText))
{
translatedQuestSummary = storedSummaryText;
}
else
{
translatedQuestSummary = this.Translate(summaryText);
foundQuestPlate.Summaries.Add(summaryText, translatedQuestSummary);
shouldUpdateQuest = true;
}
}
summaries = this.TranslateSummaries(journalBox, foundQuestPlate, summaryText);
foreach (var summary in summaries)
{
if (summary.IsTranslated)
{
foundQuestPlate.Summaries.Add(summary.OriginalText, summary.TranslatedText);
shouldUpdateQuest = true;
}
}
if (shouldUpdateQuest)
{
string result = this.UpdateQuestPlate(foundQuestPlate);
#if DEBUG
PluginLog.Debug($"Using QuestPlate Replace - QuestPlate DB Update operation result: {result}");
#endif
}
#if DEBUG
PluginLog.Debug($"From database - Name: {foundQuestPlate.TranslatedQuestName}, Message: {foundQuestPlate.TranslatedQuestMessage}");
#endif
}
else
{
translatedQuestName = this.Translate(questName);
translatedQuestMessage = this.Translate(questMessage);
translatedQuestObjective = this.Translate(objectiveText);
QuestPlate translatedQuestPlate = new(
questName,
questMessage,
ClientStateInterface.ClientLanguage.Humanize(),
translatedQuestName,
translatedQuestMessage,
string.Empty,
langDict[languageInt].Code,
this.configuration.ChosenTransEngine,
DateTime.Now,
DateTime.Now);
if (summaryText != string.Empty)
{
translatedQuestSummary = this.Translate(summaryText);
translatedQuestPlate.Summaries.Add(summaryText, translatedQuestSummary);
}
summaries = this.TranslateSummaries(journalBox, foundQuestPlate, summaryText);
foreach (var summary in summaries)
{
translatedQuestPlate.Summaries.Add(summary.OriginalText, summary.TranslatedText);
}
translatedQuestPlate.Objectives.Add(objectiveText, translatedQuestObjective);
string result = this.InsertQuestPlate(translatedQuestPlate);
#if DEBUG
PluginLog.Debug($"Translated quest name: {translatedQuestName}");
PluginLog.Debug($"Translated quest message: {translatedQuestMessage}");
PluginLog.Debug($"Translated quest objective: {translatedQuestObjective}");
PluginLog.Debug($"Translated quest summary: {translatedQuestSummary}");
PluginLog.Debug($"Using QuestPlate Replace - QuestPlate DB Insert operation result: {result}");
#endif
}
if (this.configuration.RemoveDiacriticsWhenUsingReplacementQuest)
{
translatedQuestName = this.RemoveDiacritics(translatedQuestName, this.SpecialCharsSupportedByGameFont);
translatedQuestMessage = this.RemoveDiacritics(translatedQuestMessage, this.SpecialCharsSupportedByGameFont);
translatedQuestObjective = this.RemoveDiacritics(translatedQuestObjective, this.SpecialCharsSupportedByGameFont);
translatedQuestSummary = this.RemoveDiacritics(translatedQuestSummary, this.SpecialCharsSupportedByGameFont);
foreach (var summary in summaries)
{
summary.TranslatedText = this.RemoveDiacritics(summary.TranslatedText, this.SpecialCharsSupportedByGameFont);
}
}
questNameNode->SetText(translatedQuestName);
descriptionNode->SetText(translatedQuestMessage);
objectiveNode->SetText(translatedQuestObjective);
if (summaryText != string.Empty && summaryNode != null)
{
summaryNode->SetText(translatedQuestSummary);
}
foreach (var summary in summaries)
{
summary.Node->SetText(summary.TranslatedText);
}
}
private unsafe bool TranslateJournalBox(AtkUnitBase* journalDetail)
{
try
{
var questNameNode = journalDetail->GetTextNodeById(38);
if (questNameNode == null || questNameNode->NodeText.IsEmpty)
{
return false;
}
if (!journalDetail->GetNodeById(43)->IsVisible())
{
return false;
}
var journalBox = journalDetail->GetNodeById(43)->GetComponent();
var description = journalBox->UldManager.SearchNodeById(8);
if (description == null || description->Type != NodeType.Text)
{
return false;
}
var objectiveResNode = journalBox->UldManager.SearchNodeById(12)->GetComponent()->UldManager.SearchNodeById(3);
if (objectiveResNode == null || objectiveResNode->Type != NodeType.Text)
{
return true;
}
var summaryText = string.Empty;
AtkTextNode* summaryNode = null;
var summaryBox = journalBox->UldManager.SearchNodeById(48);
if (summaryBox != null && summaryBox->IsVisible())
{
var summaryResNode = summaryBox->GetComponent()->UldManager.SearchNodeById(2);
if (summaryResNode != null && summaryResNode->Type == NodeType.Text)
{
summaryNode = summaryResNode->GetAsAtkTextNode();
summaryText = MemoryHelper.ReadSeStringAsString(out _, (nint)summaryNode->NodeText.StringPtr);
}
}
var questName = MemoryHelper.ReadSeStringAsString(out _, (nint)questNameNode->NodeText.StringPtr);
var descriptionNode = description->GetAsAtkTextNode();
var questMessage = MemoryHelper.ReadSeStringAsString(out _, (nint)descriptionNode->NodeText.StringPtr);
var objectiveNode = objectiveResNode->GetAsAtkTextNode();
var objectiveText = MemoryHelper.ReadSeStringAsString(out _, (nint)objectiveNode->NodeText.StringPtr);
QuestPlate questPlate = this.FormatQuestPlate(questName, questMessage);
QuestPlate foundQuestPlate = this.FindQuestPlate(questPlate);
#if DEBUG
PluginLog.Debug($"Quest name: {questName}");
PluginLog.Debug($"Quest message: {questMessage}");
PluginLog.Debug($"Objective text: {objectiveText}");
PluginLog.Debug($"Summary text: {summaryText}");
#endif
this.TranslateQuestOnJournalBox(journalBox, foundQuestPlate, questName, questMessage, objectiveText, summaryText, questNameNode, descriptionNode, objectiveNode, summaryNode);
}
catch (Exception e)
{
PluginLog.Error($"Error in UIJournalHandler: {e}");
}
return true;
}
private unsafe void TranslateCompletedQuest(AtkUnitBase* journalDetail)
{
try
{
var questNameNode = journalDetail->GetTextNodeById(38);
if (questNameNode == null || questNameNode->NodeText.IsEmpty)
{
return;
}
if (!journalDetail->GetNodeById(46)->IsVisible())
{
return;
}
var description = journalDetail->GetNodeById(46);
if (description == null || description->Type != NodeType.Text)
{
return;
}
var questName = MemoryHelper.ReadSeStringAsString(out _, (nint)questNameNode->NodeText.StringPtr);
var descriptionNode = description->GetAsAtkTextNode();
var questMessage = MemoryHelper.ReadSeStringAsString(out _, (nint)descriptionNode->NodeText.StringPtr);
QuestPlate questPlate = this.FormatQuestPlate(questName, questMessage);
QuestPlate foundQuestPlate = this.FindQuestPlate(questPlate);
#if DEBUG
PluginLog.Debug($"Quest name: {questName}");
PluginLog.Debug($"Quest message: {questMessage}");
#endif
string translatedQuestName;
string translatedQuestMessage;
if (foundQuestPlate != null)
{
translatedQuestName = foundQuestPlate.TranslatedQuestName;
translatedQuestMessage = foundQuestPlate.TranslatedQuestMessage;
#if DEBUG
PluginLog.Debug($"From database - Name: {foundQuestPlate.TranslatedQuestName}, Message: {foundQuestPlate.TranslatedQuestMessage}");
#endif
}
else
{
translatedQuestName = this.Translate(questName);
translatedQuestMessage = this.Translate(questMessage);
#if DEBUG
PluginLog.Debug($"Translated quest name: {translatedQuestName}");
PluginLog.Debug($"Translated quest message: {translatedQuestMessage}");
#endif
QuestPlate translatedQuestPlate = new(
questName,
questMessage,
ClientStateInterface.ClientLanguage.Humanize(),
translatedQuestName,
translatedQuestMessage,
string.Empty,
langDict[languageInt].Code,
this.configuration.ChosenTransEngine,
DateTime.Now,
DateTime.Now);
string result = this.InsertQuestPlate(translatedQuestPlate);
#if DEBUG
PluginLog.Debug($"Using QuestPlate Replace - QuestPlate DB Insert operation result: {result}");
#endif
}
if (this.configuration.RemoveDiacriticsWhenUsingReplacementQuest)
{
translatedQuestName = this.RemoveDiacritics(translatedQuestName, this.SpecialCharsSupportedByGameFont);
translatedQuestMessage = this.RemoveDiacritics(translatedQuestMessage, this.SpecialCharsSupportedByGameFont);
}
questNameNode->SetText(translatedQuestName);
descriptionNode->SetText(translatedQuestMessage);
}
catch (Exception e)
{
PluginLog.Error($"Error in UiJournalHandler: {e}");
}
}
private unsafe void TranslateJournalDetail()
{
if (!this.configuration.TranslateJournal)
{
return;
}
var atkStage = AtkStage.Instance();
var journalDetail = atkStage->RaptureAtkUnitManager->GetAddonByName("JournalDetail");
if (journalDetail == null || !journalDetail->IsVisible)
{
return;
}
#if DEBUG
PluginLog.Debug($"Language: {ClientStateInterface.ClientLanguage.Humanize()}");
PluginLog.Debug($"Translate JournalDetail");
#endif
if (!this.TranslateJournalBox(journalDetail))
{
this.TranslateCompletedQuest(journalDetail);
}
}
private unsafe void TranslateJournalQuests()
{
if (!this.configuration.TranslateJournal)
{
return;
}
var atkStage = AtkStage.Instance();
var journal = atkStage->RaptureAtkUnitManager->GetAddonByName("Journal");
if (journal == null || !journal->IsVisible)
{
return;
}
#if DEBUG
PluginLog.Debug($"Language: {ClientStateInterface.ClientLanguage.Humanize()}");
PluginLog.Debug($"Translate JournalQuests");
#endif
try
{
var questListNode = journal->GetNodeById(25)->GetAsAtkComponentNode()->Component;
for (var i = 0; i < questListNode->UldManager.NodeListCount; i++)
{
if (!questListNode->UldManager.NodeList[i]->IsVisible() || questListNode->UldManager.NodeList[i]->NodeId == 5)
{
continue;
}
if (questListNode->UldManager.NodeList[i]->Type == NodeType.Collision || questListNode->UldManager.NodeList[i]->Type == NodeType.Res)
{
continue;
}
var questItemNode = questListNode->UldManager.NodeList[i]->GetAsAtkComponentNode();
var questNameNode = questItemNode->Component->UldManager.SearchNodeById(3);
if (questNameNode == null || !questNameNode->IsVisible() || questNameNode->Type != NodeType.Text)
{
continue;
}
var questName = questNameNode->GetAsAtkTextNode();
if (questName->NodeText.IsEmpty)
{
continue;
}
var questNameText = MemoryHelper.ReadSeStringAsString(out _, (nint)questName->NodeText.StringPtr);
if (this.translatedQuestNames.ContainsKey(questNameText))
{
continue;
}
QuestPlate questPlate = this.FormatQuestPlate(questNameText, string.Empty);
QuestPlate foundQuestPlate = this.FindQuestPlateByName(questPlate);
if (foundQuestPlate != null)
{
#if DEBUG
PluginLog.Debug($"Name from database: {questName->NodeText} -> {foundQuestPlate.TranslatedQuestName}");
#endif
var translQuestName = foundQuestPlate.TranslatedQuestName;
if (this.configuration.RemoveDiacriticsWhenUsingReplacementQuest)
{
translQuestName = this.RemoveDiacritics(foundQuestPlate.TranslatedQuestName, this.SpecialCharsSupportedByGameFont);
}
questName->SetText(translQuestName);
this.translatedQuestNames.TryAdd(foundQuestPlate.TranslatedQuestName, true);
continue;
}
var translatedNameText = this.Translate(questNameText);
#if DEBUG
PluginLog.Debug($"Name translated: {questNameText} -> {translatedNameText}");
#endif
QuestPlate translatedQuestPlate = new(
questNameText,
string.Empty,
ClientStateInterface.ClientLanguage.Humanize(),
translatedNameText,
string.Empty,
string.Empty,
langDict[languageInt].Code,
this.configuration.ChosenTransEngine,
DateTime.Now,
DateTime.Now);
string result = this.InsertQuestPlate(translatedQuestPlate);
#if DEBUG
PluginLog.Debug($"Using QuestPlate Replace - QuestPlate DB Insert operation result: {result}");
#endif
if (this.configuration.RemoveDiacriticsWhenUsingReplacementQuest)
{
translatedNameText = this.RemoveDiacritics(translatedNameText, this.SpecialCharsSupportedByGameFont);
}
questName->SetText(translatedNameText);
this.translatedQuestNames.TryAdd(translatedNameText, true);
}
}
catch (Exception e)
{
PluginLog.Error($"Error: {e}");
}
}
private unsafe void UiJournalDetailHandler(AddonEvent type, AddonArgs args)
{
#if DEBUG
PluginLog.Debug($"UiJournalDetailHandler AddonEvent: {type} {args.AddonName}");
#endif
this.TranslateJournalDetail();
}
private unsafe void UiJournalQuestHandler(AddonEvent type, AddonArgs args)
{
#if DEBUG
PluginLog.Debug($"UiJournalQuestHandler AddonEvent: {type} {args.AddonName}");
#endif
this.TranslateJournalQuests();
}
}
public unsafe class SummaryQuest
{
public string OriginalText { get; set; }
public string TranslatedText { get; set; }
public AtkTextNode* Node { get; set; }
public bool IsTranslated { get; set; }
public SummaryQuest(string originalText, string translatedText, AtkTextNode* node, bool isTranslated)
{
this.OriginalText = originalText;
this.TranslatedText = translatedText;
this.Node = node;
this.IsTranslated = isTranslated;
}
}
}