Skip to content

Commit

Permalink
Merge pull request #12 from digital-pet/Dev
Browse files Browse the repository at this point in the history
v0.6.211221
  • Loading branch information
digital-pet authored Dec 22, 2021
2 parents 9908c56 + e667716 commit 38e762d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 72 deletions.
21 changes: 9 additions & 12 deletions AetherSenseRedux/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,27 @@ namespace AetherSenseRedux
[Serializable]
public class Configuration : IPluginConfiguration
{
public int Version { get; set; } = 1;
public bool Initialized = false;
public int Version { get; set; } = 2;
public bool FirstRun = true;
public bool LogChat { get; set; } = false;
public string Address { get; set; } = "ws://127.0.0.1:12345";
public List<string> SeenDevices { get; set; } = new();
public List<dynamic> Triggers { get; set; } = new List<dynamic>();

// the below exist just to make saving less cumbersome

[NonSerialized]
private DalamudPluginInterface? pluginInterface;

/// <summary>
///
/// Stores a reference to the plugin interface to allow us to save this configuration and reload it from disk.
/// </summary>
/// <param name="pluginInterface"></param>
/// <param name="pluginInterface">The DalamudPluginInterface instance in this plugin</param>
public void Initialize(DalamudPluginInterface pluginInterface)
{
this.pluginInterface = pluginInterface;
}

/// <summary>
///
/// Deep copies the trigger list while ensuring that everything has the correct type.
/// </summary>
public void FixDeserialization()
{
Expand All @@ -55,8 +53,8 @@ public void FixDeserialization()
/// </summary>
public void LoadDefaults()
{
Version = 1;
Initialized = true;
Version = 2;
FirstRun = false;
Triggers = new List<dynamic>() {
new ChatTriggerConfig()
{
Expand Down Expand Up @@ -103,12 +101,11 @@ public void Import(dynamic o)
{
try
{
if (o.Version != 1)
if (o.Version != 2)
{
return;
}
Version = o.Version;
Initialized = o.Initialized;
FirstRun = o.FirstRun;
LogChat = o.LogChat;
Address = o.Address;
SeenDevices = new List<string>(o.SeenDevices);
Expand Down
144 changes: 104 additions & 40 deletions AetherSenseRedux/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,79 @@ public sealed class Plugin : IDalamudPlugin
{
public string Name => "AetherSense Redux";

public bool Connected {
private const string commandName = "/asr";

public enum StatusTypes
{
Uninitialized,
Connected,
Connecting,
Disconnected,
Error
}

private StatusTypes _status;

public StatusTypes Status { get
{
if (Buttplug != null)
{
if (Buttplug.Connected && _status == StatusTypes.Connected)
{
return StatusTypes.Connected;
}
else if (_status == StatusTypes.Connecting)
{
return StatusTypes.Connecting;
}
else if (!Buttplug.Connected && _status == StatusTypes.Connected)
{
return StatusTypes.Error;
}
else if (LastException != null)
{
return StatusTypes.Error;
}
else
{
return StatusTypes.Disconnected;
}
} else
{
return StatusTypes.Uninitialized;
}
}
}

private bool Connected {
get {
if (Buttplug != null)
{
return Buttplug.Connected;
}
return false;
}
}
}

public bool Initialized
{
get
{
return Buttplug != null ? true : false;
private bool Initialized {
get {
return Buttplug != null;
}
}

public string[] ConnectedDevices {
public List<string> ConnectedDevices {
get
{
List<string> result = new();
foreach (Device device in DevicePool)
{
result.Add(device.Name);
}
return result.ToArray();
return result;
}
}

public Exception? LastException { get; set; }

private const string commandName = "/asr";

private DalamudPluginInterface PluginInterface { get; init; }
private CommandManager CommandManager { get; init; }
private Configuration Configuration { get; set; }
Expand Down Expand Up @@ -87,21 +126,30 @@ public Plugin(
Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration();
Configuration.Initialize(PluginInterface);
Configuration.FixDeserialization();
if (!Configuration.Initialized)

_status = StatusTypes.Disconnected;

// Update the configuration if it's an older version
if (Configuration.Version == 1)
{
Configuration.Version = 2;
Configuration.FirstRun = false;
Configuration.Save();
}

if (Configuration.FirstRun)
{
Configuration.LoadDefaults();
}

// you might normally want to embed resources and load them from the manifest stream
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
PluginUi = new PluginUI(Configuration, this);

CommandManager.AddHandler(commandName, new CommandInfo(OnShowUI)
{
HelpMessage = "Opens the Aether Sense Redux configuration window"
});

PluginInterface.UiBuilder.Draw += DrawUI;
this.PluginInterface.UiBuilder.Draw += DrawUI;
PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
}

Expand Down Expand Up @@ -181,6 +229,25 @@ private void OnScanComplete(object? sender, EventArgs e)
Task.Run(DoScan).ConfigureAwait(false);
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnServerDisconnect(object? sender, EventArgs e)
{
PluginLog.Debug("Server Disconnected");
DevicePool.Clear();
_status = StatusTypes.Disconnected;
Buttplug!.Dispose();
Buttplug = null;

if (ChatTriggerPool.Count > 0)
{
DestroyTriggers();
}
}

private void OnChatReceived(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled)
{
ChatMessage chatMessage = new(type, senderId, ref sender, ref message, ref isHandled);
Expand All @@ -193,6 +260,17 @@ private void OnChatReceived(XivChatType type, uint senderId, ref SeString sender
PluginLog.Debug(chatMessage.ToString());
}
}

/// <summary>
///
/// </summary>
/// <param name="command"></param>
/// <param name="args"></param>
private void OnShowUI(string command, string args)
{
// in response to the slash command, just display our main ui
PluginUi.SettingsVisible = true;
}
// END EVENT HANDLERS

// SOME FUNCTIONS THAT DO THINGS
Expand All @@ -202,7 +280,7 @@ private void OnChatReceived(XivChatType type, uint senderId, ref SeString sender
/// <param name="patternConfig">A pattern configuration.</param>
public void DoPatternTest(dynamic patternConfig)
{
if (!Connected)
if (Status != StatusTypes.Connected)
{
return;
}
Expand All @@ -222,7 +300,7 @@ public void DoPatternTest(dynamic patternConfig)
///
/// </summary>
/// <returns>The task associated with this method.</returns>
private async Task DoScan()
public async Task DoScan()
{
await Task.Delay(1000);
try
Expand All @@ -242,12 +320,15 @@ private async Task DoScan()
/// </summary>
private async Task InitButtplug()
{
LastException = null;
_status = StatusTypes.Connecting;
if (!Initialized)
{
Buttplug = new ButtplugClient("AetherSense Redux");
Buttplug.DeviceAdded += OnDeviceAdded;
Buttplug.DeviceRemoved += OnDeviceRemoved;
Buttplug.ScanningFinished += OnScanComplete;
Buttplug.ServerDisconnect += OnServerDisconnect;
}

if (!Connected)
Expand All @@ -269,7 +350,7 @@ private async Task InitButtplug()
if (Connected)
{
PluginLog.Debug("Buttplug initialized and connected.");
LastException = null;
_status = StatusTypes.Connected;
}

}
Expand All @@ -291,10 +372,11 @@ private void DestroyButtplug()

if (!Initialized)
{
_status = StatusTypes.Disconnected;
return;
}

if (Connected)
if (Status == StatusTypes.Connected)
{
try
{
Expand All @@ -307,9 +389,6 @@ private void DestroyButtplug()
}

}

Buttplug!.Dispose();
Buttplug = null;
PluginLog.Debug("Buttplug destroyed.");
}

Expand Down Expand Up @@ -371,7 +450,7 @@ public void Start()
/// <summary>
///
/// </summary>
public void Restart()
public void Reload()
{
if (Connected)
{
Expand All @@ -392,28 +471,13 @@ public void Stop()
// END START AND STOP FUNCTIONS

// UI FUNCTIONS
/// <summary>
///
/// </summary>
/// <param name="command"></param>
/// <param name="args"></param>
private void OnShowUI(string command, string args)
{
// in response to the slash command, just display our main ui
PluginUi.SettingsVisible = true;
}

/// <summary>
///
/// </summary>
private void DrawUI()
{
PluginUi.Draw();
this.PluginUi.Draw();
}

/// <summary>
///
/// </summary>
private void DrawConfigUI()
{
PluginUi.SettingsVisible = true;
Expand Down
Loading

0 comments on commit 38e762d

Please sign in to comment.