generated from karashiiro/DalamudPluginProjectTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
UiBattleTalkHandler.cs
238 lines (217 loc) · 9.39 KB
/
UiBattleTalkHandler.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
// <copyright file="UiTalkHandler.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.Threading;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Echoglossian
{
public partial class Echoglossian
{
private unsafe void BattleTalkHandler(string addonName, int index)
{
IntPtr battleTalk = GameGuiInterface.GetAddonByName(addonName, index);
if (battleTalk != IntPtr.Zero)
{
AtkUnitBase* battleTalkMaster = (AtkUnitBase*)battleTalk;
while (battleTalkMaster->IsVisible)
{
this.battleTalkDisplayTranslation = true;
this.battleTalkTextDimensions.X = battleTalkMaster->RootNode->Width * battleTalkMaster->Scale;
this.battleTalkTextDimensions.Y = battleTalkMaster->RootNode->Height * battleTalkMaster->Scale;
this.battleTalkTextPosition.X = battleTalkMaster->RootNode->X;
this.battleTalkTextPosition.Y = battleTalkMaster->RootNode->Y;
Thread.Sleep(this.delayBetweenVisibilityCheckForOverlay);
}
this.battleTalkDisplayTranslation = false;
}
this.battleTalkDisplayTranslation = false;
}
/*private void GetBattleTalk(ref SeString sender, ref SeString message, *//*ref BattleTalkOptions options*//*
ref bool ishandled)
{
if (!this.configuration.TranslateBattleTalk)
{
return;
}
#if DEBUG
using StreamWriter logStream = new(this.configDir + "GetBattleTalkLog.txt", append: true);
#endif
try
{
#if DEBUG
PluginLog.Debug(sender.TextValue + ": " + message.TextValue);
#endif
string senderToTranslate = !sender.TextValue.IsNullOrEmpty() ? sender.TextValue : "System Message";
string battleTextToTranslate = message.TextValue;
BattleTalkMessage battleTalkMessage = this.FormatBattleTalkMessage(senderToTranslate, battleTextToTranslate);
#if DEBUG
PluginLog.Fatal($"Before DB Query attempt: {battleTalkMessage}");
#endif
bool findings = FindBattleTalkMessage(battleTalkMessage);
#if DEBUG
PluginLog.Fatal(
$"After DB Query attempt: {(findings ? "Message found in Db." : "Message not found in Db")}");
#endif
// If the dialogue is not saved
if (!findings)
{
if (!this.configuration.UseImGuiForBattleTalk)
{
string translatedBattleTalkMessage = this.Translate(battleTextToTranslate);
string senderTranslation = this.Translate(senderToTranslate);
#if DEBUG
PluginLog.Debug(translatedBattleTalkMessage);
#endif
if (this.configuration.TranslateNpcNames)
{
sender = senderTranslation == string.Empty ? sender : senderTranslation;
message = translatedBattleTalkMessage;
BattleTalkMessage translatedBattleTalkData = new BattleTalkMessage(senderToTranslate, battleTextToTranslate,
LangIdentify(battleTextToTranslate),
LangIdentify(senderToTranslate), senderTranslation, translatedBattleTalkMessage, langDict[languageInt].Code,
this.configuration.ChosenTransEngine, DateTime.Now, DateTime.Now);
#if DEBUG
logStream.WriteLineAsync($"Before Talk Messages table data insertion: {translatedBattleTalkData}");
#endif
string result = InsertBattleTalkData(translatedBattleTalkData);
#if DEBUG
PluginLog.Debug($"BattleTalk Message DB Insert operation result: {result}");
#endif
}
else
{
message = translatedBattleTalkMessage;
BattleTalkMessage translatedBattleTalkData = new BattleTalkMessage(senderToTranslate, battleTextToTranslate,
LangIdentify(battleTextToTranslate),
LangIdentify(senderToTranslate), string.Empty, translatedBattleTalkMessage, langDict[languageInt].Code,
this.configuration.ChosenTransEngine, DateTime.Now, DateTime.Now);
string result = InsertBattleTalkData(translatedBattleTalkData);
#if DEBUG
PluginLog.Debug($"Using BattleTalk Overlay - BattleTalk Message DB Insert operation result: {result}");
#endif
}
#if DEBUG
PluginLog.Debug($"Using BattleTalk Replace - {sender.TextValue}: {message.TextValue}");
#endif
}
else
{
if (this.configuration.TranslateNpcNames)
{
this.currentSenderTranslationId = Environment.TickCount;
this.currentSenderTranslation = Resources.WaitingForTranslation;
Task.Run(() =>
{
int nameId = this.currentSenderTranslationId;
string senderTranslation = this.Translate(senderToTranslate);
this.senderTranslationSemaphore.Wait();
if (nameId == this.currentSenderTranslationId)
{
this.currentSenderTranslation = senderTranslation;
}
this.senderTranslationSemaphore.Release();
});
}
this.currentBattleTalkTranslationId = Environment.TickCount;
this.currentBattleTalkTranslation = Resources.WaitingForTranslation;
Task.Run(() =>
{
int id = this.currentBattleTalkTranslationId;
string translation = this.Translate(battleTextToTranslate);
this.battleTalkTranslationSemaphore.Wait();
if (id == this.currentBattleTalkTranslationId)
{
this.currentBattleTalkTranslation = translation;
}
this.battleTalkTranslationSemaphore.Release();
#if DEBUG
PluginLog.Debug($"Before if BattleTalk translation: {this.currentBattleTalkTranslation}");
#endif
if (this.currentSenderTranslation != Resources.WaitingForTranslation && this.currentBattleTalkTranslation != Resources.WaitingForTranslation)
{
BattleTalkMessage translatedBattleTalkData = new BattleTalkMessage(senderToTranslate, battleTextToTranslate,
LangIdentify(battleTextToTranslate),
LangIdentify(senderToTranslate),
this.configuration.TranslateNpcNames ? this.currentSenderTranslation : string.Empty,
this.currentBattleTalkTranslation, langDict[languageInt].Code,
this.configuration.ChosenTransEngine, DateTime.Now, DateTime.Now);
string result = InsertBattleTalkData(translatedBattleTalkData);
#if DEBUG
PluginLog.Debug($"BattleTalk Message DB Insert operation result: {result}");
#endif
}
});
}
}
else
{ // if the data is already in the DB
if (!this.configuration.UseImGuiForBattleTalk)
{
string translatedBattleMessage = FoundBattleTalkMessage.TranslatedBattleTalkMessage;
string senderTranslation = FoundBattleTalkMessage.TranslatedSenderName;
#if DEBUG
PluginLog.Debug($"From database - Name: {senderTranslation}, Message: {translatedBattleMessage}");
#endif
if (this.configuration.TranslateNpcNames)
{
sender = senderTranslation == string.Empty || senderTranslation == null || senderTranslation == string.Empty ? sender : senderTranslation;
message = translatedBattleMessage;
}
else
{
message = translatedBattleMessage;
}
#if DEBUG
PluginLog.Debug(sender.TextValue + ": " + message.TextValue);
#endif
}
else
{
if (this.configuration.TranslateNpcNames)
{
this.currentSenderTranslationId = Environment.TickCount;
this.currentSenderTranslation = Resources.WaitingForTranslation;
Task.Run(() =>
{
int nameId = this.currentSenderTranslationId;
string senderTranslation = FoundBattleTalkMessage.TranslatedSenderName;
this.senderTranslationSemaphore.Wait();
if (nameId == this.currentSenderTranslationId)
{
this.currentSenderTranslation = senderTranslation;
#if DEBUG
PluginLog.Error($"Using overlay - name found in DB: {senderTranslation} ");
#endif
}
this.senderTranslationSemaphore.Release();
});
}
this.currentBattleTalkTranslationId = Environment.TickCount;
this.currentBattleTalkTranslation = Resources.WaitingForTranslation;
Task.Run(() =>
{
int id = this.currentBattleTalkTranslationId;
string translatedBattleTalkMessage = FoundBattleTalkMessage.TranslatedBattleTalkMessage;
this.battleTalkTranslationSemaphore.Wait();
if (id == this.currentBattleTalkTranslationId)
{
this.currentBattleTalkTranslation = translatedBattleTalkMessage;
#if DEBUG
PluginLog.Error($"Using overlay - message found in DB: {translatedBattleTalkMessage} ");
#endif
}
this.battleTalkTranslationSemaphore.Release();
});
}
}
}
catch (Exception e)
{
PluginLog.Debug("Exception: " + e.StackTrace);
throw;
}
}*/
}
}