feat: add multi-client mode

This commit is contained in:
Anna 2022-02-16 15:38:55 -05:00
parent eea455c51b
commit 458f4a673c
7 changed files with 73 additions and 9 deletions

View File

@ -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;

View File

@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
@ -735,6 +735,33 @@ namespace ChatTwo.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Allow multiple clients to run {0} at the same time, sharing the same database..
/// </summary>
internal static string Options_SharedMode_Description {
get {
return ResourceManager.GetString("Options_SharedMode_Description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Enable multi-client mode.
/// </summary>
internal static string Options_SharedMode_Name {
get {
return ResourceManager.GetString("Options_SharedMode_Name", resourceCulture);
}
}
/// <summary>
/// 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}..
/// </summary>
internal static string Options_SharedMode_Warning {
get {
return ResourceManager.GetString("Options_SharedMode_Warning", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Show the Novice Network join button next to the settings button if logged in as a mentor..
/// </summary>

View File

@ -464,4 +464,13 @@
<data name="Options_Database_Advanced" xml:space="preserve">
<value>Advanced</value>
</data>
<data name="Options_SharedMode_Name" xml:space="preserve">
<value>Enable multi-client mode</value>
</data>
<data name="Options_SharedMode_Description" xml:space="preserve">
<value>Allow multiple clients to run {0} at the same time, sharing the same database.</value>
</data>
<data name="Options_SharedMode_Warning" xml:space="preserve">
<value>This option is not recommended. No support will be offered if you enable this option. This option will hurt the performance of {0}.</value>
</data>
</root>

View File

@ -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<Message> Messages => this.Database.GetCollection<Message>("messages");
private Dictionary<ChatType, NameFormatting> 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;
}

View File

@ -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);
}

View File

@ -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)) {

View File

@ -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();