From 458f4a673c75065a6d65239932044eeb48c660e1 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 16 Feb 2022 15:38:55 -0500 Subject: [PATCH] feat: add multi-client mode --- ChatTwo/Configuration.cs | 2 ++ ChatTwo/Resources/Language.Designer.cs | 29 +++++++++++++++++++++++++- ChatTwo/Resources/Language.resx | 9 ++++++++ ChatTwo/Store.cs | 28 ++++++++++++++++++------- ChatTwo/Ui/Settings.cs | 5 +++++ ChatTwo/Ui/SettingsTabs/Database.cs | 7 +++++++ ChatTwo/Util/ImGuiUtil.cs | 2 ++ 7 files changed, 73 insertions(+), 9 deletions(-) diff --git a/ChatTwo/Configuration.cs b/ChatTwo/Configuration.cs index b64b6d0..2467841 100755 --- a/ChatTwo/Configuration.cs +++ b/ChatTwo/Configuration.cs @@ -30,6 +30,7 @@ internal class Configuration : IPluginConfiguration { public bool DatabaseBattleMessages; public bool LoadPreviousSession; public bool FilterIncludePreviousSessions; + public bool SharedMode; public float FontSize = 17f; public float JapaneseFontSize = 17f; @@ -60,6 +61,7 @@ internal class Configuration : IPluginConfiguration { this.DatabaseBattleMessages = other.DatabaseBattleMessages; this.LoadPreviousSession = other.LoadPreviousSession; this.FilterIncludePreviousSessions = other.FilterIncludePreviousSessions; + this.SharedMode = other.SharedMode; this.FontSize = other.FontSize; this.JapaneseFontSize = other.JapaneseFontSize; this.SymbolsFontSize = other.SymbolsFontSize; diff --git a/ChatTwo/Resources/Language.Designer.cs b/ChatTwo/Resources/Language.Designer.cs index 66753cf..b8a9b93 100755 --- a/ChatTwo/Resources/Language.Designer.cs +++ b/ChatTwo/Resources/Language.Designer.cs @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by a tool. // Runtime Version:4.0.30319.42000 @@ -735,6 +735,33 @@ namespace ChatTwo.Resources { } } + /// + /// Looks up a localized string similar to Allow multiple clients to run {0} at the same time, sharing the same database.. + /// + internal static string Options_SharedMode_Description { + get { + return ResourceManager.GetString("Options_SharedMode_Description", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Enable multi-client mode. + /// + internal static string Options_SharedMode_Name { + get { + return ResourceManager.GetString("Options_SharedMode_Name", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to This option is not recommended. No support will be offered if you enable this option. This option will hurt the performance of {0}.. + /// + internal static string Options_SharedMode_Warning { + get { + return ResourceManager.GetString("Options_SharedMode_Warning", resourceCulture); + } + } + /// /// Looks up a localized string similar to Show the Novice Network join button next to the settings button if logged in as a mentor.. /// diff --git a/ChatTwo/Resources/Language.resx b/ChatTwo/Resources/Language.resx index 54f8a7a..a909fa4 100755 --- a/ChatTwo/Resources/Language.resx +++ b/ChatTwo/Resources/Language.resx @@ -464,4 +464,13 @@ Advanced + + Enable multi-client mode + + + Allow multiple clients to run {0} at the same time, sharing the same database. + + + This option is not recommended. No support will be offered if you enable this option. This option will hurt the performance of {0}. + diff --git a/ChatTwo/Store.cs b/ChatTwo/Store.cs index dc04067..313bea1 100755 --- a/ChatTwo/Store.cs +++ b/ChatTwo/Store.cs @@ -17,7 +17,7 @@ internal class Store : IDisposable { private ConcurrentQueue<(uint, Message)> Pending { get; } = new(); private Stopwatch CheckpointTimer { get; } = new(); - internal ILiteDatabase Database { get; } + internal ILiteDatabase Database { get; private set; } private ILiteCollection Messages => this.Database.GetCollection("messages"); private Dictionary Formats { get; } = new(); @@ -34,9 +34,6 @@ internal class Store : IDisposable { this.Plugin = plugin; this.CheckpointTimer.Start(); - var dir = this.Plugin.Interface.ConfigDirectory; - dir.Create(); - BsonMapper.Global = new BsonMapper { IncludeNonPublic = true, TrimWhitespace = false, @@ -116,10 +113,7 @@ internal class Store : IDisposable { dateTime => dateTime.Subtract(DateTime.UnixEpoch).TotalMilliseconds, bson => DateTime.UnixEpoch.AddMilliseconds(bson.AsInt64) ); - this.Database = new LiteDatabase(Path.Join(dir.FullName, "chat.db"), BsonMapper.Global) { - CheckpointSize = 1_000, - Timeout = TimeSpan.FromSeconds(1), - }; + this.Database = this.Connect(); this.Messages.EnsureIndex(msg => msg.Date); this.Messages.EnsureIndex(msg => msg.SortCode); @@ -138,6 +132,24 @@ internal class Store : IDisposable { this.Database.Dispose(); } + private ILiteDatabase Connect() { + var dir = this.Plugin.Interface.ConfigDirectory; + dir.Create(); + + var dbPath = Path.Join(dir.FullName, "chat.db"); + var connection = this.Plugin.Config.SharedMode ? "shared" : "direct"; + var connString = $"Filename='{dbPath}';Connection={connection}"; + return new LiteDatabase(connString, BsonMapper.Global) { + CheckpointSize = 1_000, + Timeout = TimeSpan.FromSeconds(1), + }; + } + + internal void Reconnect() { + this.Database.Dispose(); + this.Database = this.Connect(); + } + private void Logout(object? sender, EventArgs eventArgs) { this.LastContentId = 0; } diff --git a/ChatTwo/Ui/Settings.cs b/ChatTwo/Ui/Settings.cs index aadb82f..f821ca9 100755 --- a/ChatTwo/Ui/Settings.cs +++ b/ChatTwo/Ui/Settings.cs @@ -142,6 +142,7 @@ internal sealed class Settings : IUiComponent { || Math.Abs(this.Mutable.JapaneseFontSize - this.Ui.Plugin.Config.JapaneseFontSize) > 0.001 || Math.Abs(this.Mutable.SymbolsFontSize - this.Ui.Plugin.Config.SymbolsFontSize) > 0.001; var langChanged = this.Mutable.LanguageOverride != this.Ui.Plugin.Config.LanguageOverride; + var sharedChanged = this.Mutable.SharedMode != this.Ui.Plugin.Config.SharedMode; config.UpdateFrom(this.Mutable); @@ -159,6 +160,10 @@ internal sealed class Settings : IUiComponent { this.Ui.Plugin.LanguageChanged(this.Ui.Plugin.Interface.UiLanguage); } + if (sharedChanged) { + this.Ui.Plugin.Store.Reconnect(); + } + if (!this.Mutable.HideChat && hideChatChanged) { GameFunctions.GameFunctions.SetChatInteractable(true); } diff --git a/ChatTwo/Ui/SettingsTabs/Database.cs b/ChatTwo/Ui/SettingsTabs/Database.cs index 4df857a..97aa67b 100755 --- a/ChatTwo/Ui/SettingsTabs/Database.cs +++ b/ChatTwo/Ui/SettingsTabs/Database.cs @@ -39,6 +39,13 @@ internal sealed class Database : ISettingsTab { } } + ImGuiUtil.OptionCheckbox( + ref this.Mutable.SharedMode, + Language.Options_SharedMode_Name, + string.Format(Language.Options_SharedMode_Description, Plugin.PluginName) + ); + ImGuiUtil.WarningText(string.Format(Language.Options_SharedMode_Warning, Plugin.PluginName)); + ImGui.Spacing(); if (this._showAdvanced && ImGui.TreeNodeEx(Language.Options_Database_Advanced)) { diff --git a/ChatTwo/Util/ImGuiUtil.cs b/ChatTwo/Util/ImGuiUtil.cs index 7a3fa15..b9eb74c 100755 --- a/ChatTwo/Util/ImGuiUtil.cs +++ b/ChatTwo/Util/ImGuiUtil.cs @@ -202,7 +202,9 @@ internal static class ImGuiUtil { ImGui.PushStyleColor(ImGuiCol.Text, dalamudOrange.Value); } + ImGui.PushTextWrapPos(); ImGui.TextUnformatted(text); + ImGui.PopTextWrapPos(); if (dalamudOrange != null) { ImGui.PopStyleColor();