2 Commits

Author SHA1 Message Date
561811cd1f Enhance command handling and add namespace imports
- Added `using` directives for essential namespaces in `Plugin.cs`.
- Implemented a "set" subcommand for configuring activity options, including color and prefix/suffix settings.
- Added error handling for duplicate and invalid inputs, with user feedback via chat.
- Updated the configuration for the first activity based on user input.
2025-06-22 21:44:51 +03:00
0c0c72be9f Enhance Spotify track retrieval in Updater.cs
Updated `Updater.cs` to improve functionality for retrieving the currently playing track in Spotify. Added new methods for window title retrieval and utilized the `EnumWindows` function to filter and identify relevant Spotify track titles. Included P/Invoke declarations for Windows API functions to support these enhancements, along with a new method `GetWindowTitle` for better code organization.
2025-06-11 20:39:08 +03:00
2 changed files with 134 additions and 7 deletions

View File

@@ -6,6 +6,12 @@ using Dalamud.Plugin.Services;
using SpotifyHonorific.Windows; using SpotifyHonorific.Windows;
using SpotifyHonorific.Activities; using SpotifyHonorific.Activities;
using SpotifyHonorific.Updaters; using SpotifyHonorific.Updaters;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using System.Numerics;
namespace SpotifyHonorific; namespace SpotifyHonorific;
@@ -70,6 +76,87 @@ public sealed class Plugin : IDalamudPlugin
Config.Save(); Config.Save();
Updater.Stop(); Updater.Stop();
} }
else if (subcommand == "set")
{
var splitArgs = args.Trim().Split(' ', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (splitArgs.Length != 2)
{
return;
}
var setArgs = splitArgs[1].Split('|', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
bool? prefix = null;
Vector3? color = null;
Vector3? glow = null;
foreach (var a in setArgs)
{
var arg = a.ToLower();
var colorArg = arg.Skip(arg.StartsWith('#') ? 1 : 0).ToArray();
if (colorArg.Length == 6 && colorArg.All(chr => chr is >= '0' and <= '9' or >= 'a' and <= 'f'))
{
if (color != null && glow != null)
{
ChatGui.PrintError($"Duplicate Option in Set: '{a}'", "SpotifyHonorific");
return;
}
var rHex = string.Join(null, colorArg.Skip(0).Take(2));
var gHex = string.Join(null, colorArg.Skip(2).Take(2));
var bHex = string.Join(null, colorArg.Skip(4).Take(2));
if (byte.TryParse(rHex, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out var r) &&
byte.TryParse(gHex, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out var g) &&
byte.TryParse(bHex, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out var b))
{
var c = new Vector3(r / 255f, g / 255f, b / 255f);
if (color == null)
{
color = c;
continue;
}
glow = c;
continue;
}
}
if (arg is "p" or "prefix" or "pre")
{
if (prefix != null)
{
ChatGui.PrintError($"Duplicate Option in Set: '{a}'", "SpotifyHonorific");
return;
}
prefix = true;
continue;
}
if (arg is "s" or "suffix")
{
if (prefix != null)
{
ChatGui.PrintError($"Duplicate Option in Set: '{a}'", "SpotifyHonorific");
return;
}
prefix = false;
continue;
}
ChatGui.PrintError($"Invalid Option in Set: '{a}'", "SpotifyHonorific");
return;
}
Config.ActivityConfigs.First().Color = color;
Config.ActivityConfigs.First().Glow = glow;
Config.ActivityConfigs.First().IsPrefix = prefix ?? false;
ChatGui.Print("Updated Title", "SpotifyHonorific");
}
else else
{ {
ChatGui.Print(CommandHelpMessage); ChatGui.Print(CommandHelpMessage);

View File

@@ -8,6 +8,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
@@ -147,25 +149,63 @@ public class Updater : IDisposable
try try
{ {
var spotifyProcesses = Process.GetProcessesByName("Spotify"); var spotifyProcesses = Process.GetProcessesByName("Spotify");
var trackTitles = new List<string>();
foreach (var process in spotifyProcesses) foreach (var process in spotifyProcesses)
{ {
if (!string.IsNullOrEmpty(process.MainWindowTitle) && EnumWindows((hWnd, lParam) =>
process.MainWindowTitle != "Spotify" &&
process.MainWindowTitle != "Spotify Premium" &&
process.MainWindowTitle != "Spotify Free")
{ {
return process.MainWindowTitle; uint processId;
GetWindowThreadProcessId(hWnd, out processId);
if (processId == process.Id)
{
var title = GetWindowTitle(hWnd);
if (!string.IsNullOrEmpty(title) &&
title != "Spotify" &&
title != "Spotify Premium" &&
title != "Spotify Free" &&
title.Contains(" - "))
{
trackTitles.Add(title);
} }
} }
return true;
}, IntPtr.Zero);
}
return trackTitles.OrderByDescending(t => t.Length).FirstOrDefault();
} }
catch (Exception ex) catch (Exception ex)
{ {
PluginLog.Debug($"Error getting Spotify track: {ex.Message}"); PluginLog.Debug($"Error getting Spotify track: {ex.Message}");
} }
return null; return null;
} }
private string GetWindowTitle(IntPtr hWnd)
{
const int nChars = 256;
var buff = new StringBuilder(nChars);
if (GetWindowTextW(hWnd, buff, nChars) > 0)
{
return buff.ToString();
}
return string.Empty;
}
[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowTextW(IntPtr hWnd, StringBuilder text, int count);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
private bool MediaActivitiesEqual(MediaActivity? a, MediaActivity? b) private bool MediaActivitiesEqual(MediaActivity? a, MediaActivity? b)
{ {
if (a == null && b == null) return true; if (a == null && b == null) return true;