forked from lokinmodar/Echoglossian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEchoglossian.cs
473 lines (370 loc) · 16.9 KB
/
Echoglossian.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
// <copyright file="Echoglossian.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.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using Dalamud.Game.Addon.Lifecycle;
using Dalamud.Game.Command;
using Dalamud.Game.Text.Sanitizer;
using Dalamud.Interface.Internal;
using Dalamud.IoC;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using Echoglossian.EFCoreSqlite.Models;
using Echoglossian.Properties;
using XivCommon;
namespace Echoglossian
{
// TODO: implement multiple fallback translation engines.
public partial class Echoglossian : IDalamudPlugin
{
[PluginService]
public static IDataManager DManager { get; private set; }
[PluginService]
public static DalamudPluginInterface PluginInterface { get; private set; } = null!;
[PluginService]
public static ICommandManager CommandManager { get; private set; } = null!;
[PluginService]
public static IFramework Framework { get; private set; } = null!;
[PluginService]
public static IGameGui GameGui { get; private set; } = null!;
[PluginService]
public static IClientState ClientState { get; private set; } = null!;
[PluginService]
public static IToastGui ToastGui { get; private set; } = null!;
[PluginService]
public static IAddonEventManager EventManager { get; private set; } = null!;
[PluginService]
public static IAddonLifecycle AddonLifecycle { get; private set; } = null!;
[PluginService]
public static IPluginLog PluginLog { get; private set; } = null!;
[PluginService]
public static INotificationManager NotificationManager { get; private set; } = null!;
// private static XivCommonBase Common { get; set; }
public string Name => Resources.Name;
private const string SlashCommand = "/eglo";
private string configDir;
private static int languageInt = 28;
private static int fontSize = 24;
private static int chosenTransEngine;
private static string transEngineName;
public string LangToTranslateTo = string.Empty;
private bool pluginAssetsState;
private static Dictionary<int, LanguageInfo> langDict;
private bool config;
private Config configuration;
private readonly SemaphoreSlim toastTranslationSemaphore;
private readonly SemaphoreSlim talkTranslationSemaphore;
private readonly SemaphoreSlim nameTranslationSemaphore;
private readonly SemaphoreSlim battleTalkTranslationSemaphore;
private readonly SemaphoreSlim senderTranslationSemaphore;
private readonly SemaphoreSlim errorToastTranslationSemaphore;
private readonly SemaphoreSlim classChangeToastTranslationSemaphore;
private readonly SemaphoreSlim areaToastTranslationSemaphore;
private readonly SemaphoreSlim wideTextToastTranslationSemaphore;
private readonly SemaphoreSlim questToastTranslationSemaphore;
private readonly IDalamudTextureWrap pixImage;
private readonly IDalamudTextureWrap choiceImage;
private readonly IDalamudTextureWrap cutsceneChoiceImage;
private readonly IDalamudTextureWrap talkImage;
private readonly IDalamudTextureWrap logo;
private readonly CultureInfo cultureInfo;
private static Sanitizer sanitizer;
private UIAddonHandler uiBattleTalkAddonHandler;
private UIAddonHandler uiTalkAddonHandler;
private TranslationService translationService;
public List<ToastMessage> ErrorToastsCache { get; set; }
public List<ToastMessage> QuestToastsCache { get; set; }
public List<ToastMessage> OtherToastsCache { get; set; }
public Echoglossian()
{
this.configuration = PluginInterface.GetPluginConfig() as Config ?? new Config();
this.configDir = PluginInterface.GetPluginConfigDirectory() + Path.DirectorySeparatorChar;
CommandManager.AddHandler(SlashCommand, new CommandInfo(this.Command)
{
HelpMessage = Resources.HelpMessage,
});
sanitizer = PluginInterface.Sanitizer as Sanitizer;
// Resolver.Initialize();
// Resolver.GetInstance.SetupSearchSpace();
// Resolver.GetInstance.Resolve();
langDict = this.languagesDictionary;
identifier = Factory.Load($"{PluginInterface.AssemblyLocation.DirectoryName}{Path.DirectorySeparatorChar}Wiki82.profile.xml");
// Common = new XivCommonBase(PluginInterface, Hooks.Talk | Hooks.BattleTalk);
try
{
this.CreateOrUseDb();
}
catch (Exception e)
{
PluginLog.Error($"Error creating or using database: {e}");
}
finally
{
PluginLog.Information("Database created or used successfully.");
}
this.cultureInfo = new CultureInfo(this.configuration.DefaultPluginCulture);
this.assetsPath = $"{PluginInterface.AssemblyLocation.DirectoryName}{Path.DirectorySeparatorChar}Font{Path.DirectorySeparatorChar}";
this.AssetFiles.Add("NotoSansCJKhk-Regular.otf");
this.AssetFiles.Add("NotoSansCJKjp-Regular.otf");
this.AssetFiles.Add("NotoSansCJKkr-Regular.otf");
this.AssetFiles.Add("NotoSansCJKsc-Regular.otf");
this.AssetFiles.Add("NotoSansCJKtc-Regular.otf");
#if DEBUG
// PluginLog.Warning($"Assets state config: {JsonConvert.SerializeObject(this.configuration, Formatting.Indented)}");
#endif
this.configuration.PluginVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
if (this.configuration.Version < 5)
{
this.FixConfig();
}
this.pluginAssetsState = this.configuration.PluginAssetsDownloaded;
#if DEBUG
PluginLog.Warning($"Assets state config: {this.configuration.PluginAssetsDownloaded}");
PluginLog.Warning($"Assets state var: {this.pluginAssetsState}");
#endif
if (!this.pluginAssetsState)
{
this.PluginAssetsChecker();
}
PluginInterface.UiBuilder.BuildFonts += this.LoadLanguageComboFont;
PluginInterface.UiBuilder.BuildFonts += this.LoadFont;
// this.ListCultureInfos();
this.pixImage = PluginInterface.UiBuilder.LoadImage(Resources.pix);
this.choiceImage = PluginInterface.UiBuilder.LoadImage(Resources.choice);
this.cutsceneChoiceImage = PluginInterface.UiBuilder.LoadImage(Resources.cutscenechoice);
this.talkImage = PluginInterface.UiBuilder.LoadImage(Resources.prttws);
this.logo = PluginInterface.UiBuilder.LoadImage(Resources.logo);
PluginInterface.UiBuilder.DisableCutsceneUiHide = this.configuration.ShowInCutscenes;
PluginInterface.UiBuilder.OpenConfigUi += this.ConfigWindow;
languageInt = this.configuration.Lang;
fontSize = this.configuration.FontSize;
chosenTransEngine = this.configuration.ChosenTransEngine;
this.LangToTranslateTo = langDict[languageInt].Code;
TransEngines t = (TransEngines)chosenTransEngine;
transEngineName = t.ToString();
this.translationService = new TranslationService(this.configuration, PluginLog, sanitizer);
this.LoadAllErrorToasts();
this.LoadAllOtherToasts();
Framework.Update += this.Tick;
this.talkTranslationSemaphore = new SemaphoreSlim(1, 1);
this.nameTranslationSemaphore = new SemaphoreSlim(1, 1);
this.battleTalkTranslationSemaphore = new SemaphoreSlim(1, 1);
this.senderTranslationSemaphore = new SemaphoreSlim(1, 1);
this.toastTranslationSemaphore = new SemaphoreSlim(1, 1);
this.errorToastTranslationSemaphore = new SemaphoreSlim(1, 1);
this.classChangeToastTranslationSemaphore = new SemaphoreSlim(1, 1);
this.areaToastTranslationSemaphore = new SemaphoreSlim(1, 1);
this.wideTextToastTranslationSemaphore = new SemaphoreSlim(1, 1);
this.questToastTranslationSemaphore = new SemaphoreSlim(1, 1);
ToastGui.Toast += this.OnToast;
ToastGui.ErrorToast += this.OnErrorToast;
ToastGui.QuestToast += this.OnQuestToast;
// this.HandleTalkAsync();
// Common.Functions.ChatBubbles.OnChatBubble += this.ChatBubblesOnChatBubble;
// Common.Functions.Tooltips.OnActionTooltip += this.TooltipsOnActionTooltip;
// Common.Functions.Talk.OnTalk += this.GetTalk;
// Common.Functions.BattleTalk.OnBattleTalk += this.GetBattleTalk;
this.uiTalkAddonHandler = new UIAddonHandler(this.configuration, this.UiFont, this.FontLoaded, this.LangToTranslateTo);
this.uiBattleTalkAddonHandler = new UIAddonHandler(this.configuration, this.UiFont, this.FontLoaded, this.LangToTranslateTo);
this.EgloAddonHandler();
PluginInterface.UiBuilder.Draw += this.BuildUi;
/* if (ClientState.IsLoggedIn)
{
this.ParseUi();
}*/
}
/// <inheritdoc />
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
/*Common.Functions.Talk.OnTalk -= this.GetTalk;
Common.Functions.BattleTalk.OnBattleTalk -= this.GetBattleTalk;*/
// Common.Functions.ChatBubbles.OnChatBubble -= this.ChatBubblesOnChatBubble;
// Common.Functions.Tooltips.OnActionTooltip -= this.TooltipsOnActionTooltip;
/* Common.Functions.Talk.Dispose();
Common.Functions.BattleTalk.Dispose();*/
// Common.Functions.ChatBubbles.Dispose();
// Common.Functions.Tooltips.Dispose();
/*Common.Functions.Dispose();
Common?.Dispose();*/
ToastGui.Toast -= this.OnToast;
ToastGui.ErrorToast -= this.OnErrorToast;
ToastGui.QuestToast -= this.OnQuestToast;
PluginInterface.UiBuilder.OpenConfigUi -= this.ConfigWindow;
this.nameTranslationSemaphore?.Dispose();
this.talkTranslationSemaphore?.Dispose();
this.battleTalkTranslationSemaphore?.Dispose();
this.senderTranslationSemaphore?.Dispose();
this.toastTranslationSemaphore?.Dispose();
this.errorToastTranslationSemaphore?.Dispose();
this.areaToastTranslationSemaphore?.Dispose();
this.wideTextToastTranslationSemaphore?.Dispose();
this.questToastTranslationSemaphore?.Dispose();
PluginInterface.UiBuilder.Draw -= this.BuildUi;
this.pixImage?.Dispose();
this.choiceImage?.Dispose();
this.cutsceneChoiceImage?.Dispose();
this.talkImage?.Dispose();
Framework.Update -= this.Tick;
PluginInterface.UiBuilder.BuildFonts -= this.LoadFont;
PluginInterface.UiBuilder.BuildFonts -= this.LoadLanguageComboFont;
this.glyphRangeMainText?.Free();
this.glyphRangeConfigText?.Free();
this.glyphRangeMainText = null;
this.glyphRangeConfigText = null;
CommandManager.RemoveHandler(SlashCommand);
}
private void Tick(IFramework tFramework)
{
if (!this.configuration.Translate)
{
#if DEBUG
// PluginLog.Information("Translations are disabled!");
#endif
return;
}
switch (this.configuration.UseImGuiForTalk || this.configuration.UseImGuiForBattleTalk ||
this.configuration.UseImGuiForToasts)
{
case true when !this.FontLoaded || this.FontLoadFailed:
return;
case true:
{
switch (ClientState.IsLoggedIn)
{
case true:
/*this.uiTalkAddonHandler.EgloAddonHandler("Talk");
this.uiBattleTalkAddonHandler.EgloAddonHandler("_BattleTalk");*/
// this.TalkHandler("Talk", 1); tentative fix for stuttering
// this.BattleTalkHandler("_BattleTalk", 1); tentative fix for stuttering
this.TextErrorToastHandler("_TextError", 1);
this.ToastHandler("_WideText", 1);
// this.ClassChangeToastHandler("_WideText", 1);
//
// this.ClassChangeToastHandler("_WideText", 2);
this.ToastHandler("_TextClassChange", 1);
this.ToastHandler("_AreaText", 1);
// this.QuestToastHandler("_ScreenText", 1);
break;
}
break;
}
default:
// this.DisableAllTranslations();
break;
}
}
private void BuildUi()
{
if (!this.configuration.PluginAssetsDownloaded)
{
// this.PluginAssetsChecker();
return;
}
if (!this.LanguageComboFontLoaded && !this.LanguageComboFontLoadFailed)
{
PluginInterface.UiBuilder.RebuildFonts();
return;
}
if (!this.FontLoaded && !this.FontLoadFailed)
{
PluginInterface.UiBuilder.RebuildFonts();
return;
}
if (this.config)
{
this.EchoglossianConfigUi();
}
if (this.configuration.FontChangeTime > 0)
{
if (DateTime.Now.Ticks - 10000000 > this.configuration.FontChangeTime)
{
this.configuration.FontChangeTime = 0;
this.FontLoadFailed = false;
PluginInterface.UiBuilder.RebuildFonts();
}
}
if (!this.configuration.Translate)
{
return;
}
if (this.configuration.UseImGuiForBattleTalk && this.configuration.TranslateBattleTalk && this.battleTalkDisplayTranslation)
{
this.DrawTranslatedBattleDialogueWindow();
#if DEBUG
// PluginLog.Verbose("Showing BattleTalk Translation Overlay.");
#endif
}
if (this.configuration.UseImGuiForTalk && this.configuration.TranslateTalk && this.talkDisplayTranslation)
{
this.DrawTranslatedDialogueWindow();
#if DEBUG
// PluginLog.Verbose("Showing Talk Translation Overlay.");
#endif
}
#if DEBUG
/* PluginLog.Warning($"Toast Draw Vars: !UseImGuiForToasts - {!this.configuration.UseImGuiForToasts}" +
$", TranslateErrorToast - {this.configuration.TranslateErrorToast}" +
$", errorToastDisplayTranslation - {this.errorToastDisplayTranslation}" +
$" equals? {!this.configuration.UseImGuiForToasts && this.configuration.TranslateErrorToast && this.errorToastDisplayTranslation}");*/
#endif
if (this.configuration.UseImGuiForToasts && this.configuration.TranslateErrorToast && this.errorToastDisplayTranslation)
{
this.DrawTranslatedErrorToastWindow();
#if DEBUG
// PluginLog.Warning("Showing Error Toast Translation Overlay.");
#endif
}
if (this.configuration.UseImGuiForToasts && this.configuration.TranslateToast && this.toastDisplayTranslation)
{
this.DrawTranslatedToastWindow();
#if DEBUG
// PluginLog.Warning("Showing Error Toast Translation Overlay.");
#endif
}
/*if (!this.configuration.UseImGuiForToasts && this.configuration.TranslateClassChangeToast && this.classChangeToastDisplayTranslation)
{
this.DrawTranslatedClassChangeToastWindow();
*//*#if DEBUG
PluginLog.Warning("Showing Error Toast Translation Overlay.");
#endif*//*
}*/
}
private void ConfigWindow()
{
this.config = true;
}
private void Command(string command, string arguments)
{
this.config = true;
}
private void EgloAddonHandler()
{
Echoglossian.PluginLog.Information("EgloAddonHandler called.");
AddonLifecycle.RegisterListener(AddonEvent.PreSetup, "JournalResult", this.UiJournalResultHandler);
AddonLifecycle.RegisterListener(AddonEvent.PostReceiveEvent, "RecommendList", this.UiRecommendListHandler);
AddonLifecycle.RegisterListener(AddonEvent.PostRequestedUpdate, "RecommendList", this.UiRecommendListHandlerAsync);
AddonLifecycle.RegisterListener(AddonEvent.PreRefresh, "AreaMap", this.UiAreaMapHandler);
AddonLifecycle.RegisterListener(AddonEvent.PreRefresh, "ScenarioTree", this.UiScenarioTreeHandler);
AddonLifecycle.RegisterListener(AddonEvent.PreUpdate, "Journal", this.UiJournalQuestHandler);
AddonLifecycle.RegisterListener(AddonEvent.PostRequestedUpdate, "Journal", this.UiJournalDetailHandler);
AddonLifecycle.RegisterListener(AddonEvent.PreRequestedUpdate, "JournalDetail", this.UiJournalDetailHandler);
AddonLifecycle.RegisterListener(AddonEvent.PreSetup, "JournalAccept", this.UiJournalAcceptHandler);
AddonLifecycle.RegisterListener(AddonEvent.PostRequestedUpdate, "_ToDoList", this.UiToDoListHandler);
this.EgloNeutralAddonHandler("Talk", new string[] {/* "PreSetup", "PostSetup",*/ "PreUpdate", /* "PostUpdate", "PreDraw", "PostDraw", "PreFinalize", "PreReceiveEvent", "PostReceiveEvent", "PreRequestedUpdate", "PostRequestedUpdate", "PreRefresh", "PostRefresh" */});
// this.EgloNeutralAddonHandler("_BattleTalk", new string[] { "PreSetup", "PostSetup", "PreUpdate", "PostUpdate", "PreDraw", "PostDraw", "PreFinalize", "PreReceiveEvent", "PostReceiveEvent", "PreRequestedUpdate", "PostRequestedUpdate", "PreRefresh", "PostRefresh" });
/* AddonLifecycle.RegisterListener(AddonEvent.PreSetup, "TalkSubtitle", this.UpdateUI);
AddonLifecycle.RegisterListener(AddonEvent.PostSetup, "TalkSubtitle", this.UpdateUI);*/
}
}
}