Skip to content

Commit

Permalink
Merge pull request #16 from Myuuiii/trayicon-live12
Browse files Browse the repository at this point in the history
Tray Icon Application & Live 12 Support BETA
  • Loading branch information
Myuuiii authored Jul 27, 2024
2 parents dedeaa1 + bb62cee commit 9331587
Show file tree
Hide file tree
Showing 31 changed files with 871 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .run/Publish.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Publish" type="DotNetFolderPublish" factoryName="Publish to folder">
<riderPublish configuration="Release" platform="Any CPU" produce_single_file="true" runtime="win-x64" target_folder="G:/BUILD" target_framework="net8.0-windows" uuid_high="-6518831478130717984" uuid_low="-7892830845003068016" />
<method v="2" />
</configuration>
</component>
6 changes: 6 additions & 0 deletions DAWPresence.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DAWPresence", "DAWPresence\DAWPresence.csproj", "{F0349D0B-DCB7-4B14-812F-B52B8A945029}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DAWPresenceBackgroundApp", "DAWPresenceBackgroundApp\DAWPresenceBackgroundApp.csproj", "{A588754F-1362-4AE0-9277-082A1B588D90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +20,9 @@ Global
{F0349D0B-DCB7-4B14-812F-B52B8A945029}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0349D0B-DCB7-4B14-812F-B52B8A945029}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0349D0B-DCB7-4B14-812F-B52B8A945029}.Release|Any CPU.Build.0 = Release|Any CPU
{A588754F-1362-4AE0-9277-082A1B588D90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A588754F-1362-4AE0-9277-082A1B588D90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A588754F-1362-4AE0-9277-082A1B588D90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A588754F-1362-4AE0-9277-082A1B588D90}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 11 additions & 2 deletions DAWPresence/DAWPresence.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@

<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.1.3.18" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="12.3.1" />
</ItemGroup>

<ItemGroup>
<None Update="install_service.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="uninstall_service.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
16 changes: 12 additions & 4 deletions DAWPresence/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
using DAWPresence;

IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options => { options.ServiceName = "DAW Rich Presence"; })
.ConfigureServices(services => { services.AddHostedService<Worker>(); })
.Build();
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

// Require admin rights
// if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
// {
// Console.WriteLine("Please run this program as an administrator");
// Console.ReadKey();
// return;
// }

builder.Services.AddHostedService<Worker>();

IHost host = builder.Build();
host.Run();
7 changes: 6 additions & 1 deletion DAWPresence/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace DAWPresence;

internal sealed class Worker : BackgroundService
{
private const string CONFIG_FILE_NAME = "config.yml";
// config in the appdata folder
private const string CONFIG_FILE_NAME = "C:\\Users\\%USERNAME%\\AppData\\Roaming\\DAWPresence\\config.yml";

private const string CREDIT = "DAWPresence by @Myuuiii#0001";
private readonly ILogger<Worker> _logger;
Expand All @@ -17,6 +18,10 @@ internal sealed class Worker : BackgroundService
public Worker(ILogger<Worker> logger)
{
_logger = logger;
// Make sure the entire directory exists
if (!Directory.Exists(Path.GetDirectoryName(CONFIG_FILE_NAME)))
Directory.CreateDirectory(Path.GetDirectoryName(CONFIG_FILE_NAME));

if (File.Exists(CONFIG_FILE_NAME))
{
_logger.LogInformation("Loading configuration file...");
Expand Down
15 changes: 15 additions & 0 deletions DAWPresence/install_service.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off
REM Change to the directory of the script
pushd "%~dp0"

REM get the full path of the DAWPresence.exe
for /f "tokens=*" %%a in ('dir /s /b DAWPresence.exe') do set DAWPresencePath=%%a

REM create a scheduled task to run DAWPresence.exe with highest privileges in the background
schtasks /create /tn "DAWPresence" /tr "\"%DAWPresencePath%\"" /sc onstart /rl highest /f

echo DAWPresence scheduled task created
pause

REM Revert to the previous directory
popd
7 changes: 7 additions & 0 deletions DAWPresence/uninstall_service.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@echo off

REM delete the scheduled task named "DAWPresence"
schtasks /delete /tn "DAWPresence" /f

echo DAWPresence scheduled task deleted
pause
39 changes: 39 additions & 0 deletions DAWPresenceBackgroundApp/AppConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace DAWPresence;

public class AppConfiguration
{
/// <summary>
/// Interval at which the app will check if the DAW is (still) running and update the presence accordingly.
/// </summary>
public TimeSpan UpdateInterval { get; set; } = new(0, 0, 3);

/// <summary>
/// Time offset to add to the current time when displaying the elapsed time.
/// </summary>
public TimeSpan Offset { get; set; } = new(0, 0, 0);

/// <summary>
/// Text to show when no project is open
/// </summary>
public string IdleText { get; set; } = "Not working on a project";

/// <summary>
/// Text to show before the project name when a project is open
/// </summary>
public string WorkingPrefixText { get; set; } = "Working on ";

/// <summary>
/// Overwrite the image key (for custom images)
/// </summary>
public bool UseCustomImage { get; set; } = false;

/// <summary>
/// Custom image key to use
/// </summary>
public string CustomImageKey { get; set; } = "custom";

/// <summary>
/// Enable hot reloading of the configuration file
/// </summary>
public bool Debug { get; set; } = false;
}
59 changes: 59 additions & 0 deletions DAWPresenceBackgroundApp/DAW.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Diagnostics;

namespace DAWPresence;

public abstract class Daw
{
/// <summary>
/// Pretty name that will be shown in the presence
/// </summary>
public string DisplayName { get; protected init; }

/// <summary>
/// Name of the DAWs process as seen in Task Manager on Windows
/// </summary>
public string ProcessName { get; protected init; }

/// <summary>
/// The text that needs to be trimmed from the Window title in order to get the project name (if it works that way for
/// the DAW)
/// </summary>
public string WindowTrim { get; protected init; }

/// <summary>
/// The amount of characters that should be trimmed to get the project name
/// </summary>
public int TitleOffset { get; protected init; }

/// <summary>
/// Discord Rich Presence Image Key
/// </summary>
public string ImageKey { get; protected init; }

/// <summary>
/// Discord Rich Presence Application Id
/// </summary>
public string ApplicationId { get; protected init; }

/// <summary>
/// Return the amount of processes with the name of the DAW
/// </summary>
public int ProcessCount => Process.GetProcessesByName(ProcessName).Length;

/// <summary>
/// Returns whether there is a running instance of the DAW or not
/// </summary>
public bool IsRunning => ProcessCount > 0;

/// <summary>
/// Retrieves the name of the currently open project or an empty string if no project is open
/// </summary>
/// <returns></returns>
public abstract string GetProjectNameFromProcessWindow();

/// <summary>
/// Get the first process with the name of the DAW
/// </summary>
/// <returns></returns>
protected Process GetProcess() => Process.GetProcessesByName(ProcessName).First();
}
26 changes: 26 additions & 0 deletions DAWPresenceBackgroundApp/DAWPresenceBackgroundApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PlatformTarget>x64</PlatformTarget>
<ApplicationIcon>appicon.ico</ApplicationIcon>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>

<ItemGroup>
<None Update="appicon.ico">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="DiscordRichPresence" Version="1.1.3.18"/>
<PackageReference Include="YamlDotNet" Version="15.1.4" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions DAWPresenceBackgroundApp/DAWs/AbletonLive10Intro.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace DAWPresence.DAWs;

public partial class AbletonLive10Intro : Daw
{
public AbletonLive10Intro()
{
ProcessName = "Ableton Live 10 Intro";
DisplayName = ProcessName;
ImageKey = "ableton-white";
ApplicationId = "1053952444859686983";
WindowTrim = " - " + DisplayName;
TitleOffset = 24;
}

public override string GetProjectNameFromProcessWindow()
{
Process? process = GetProcess();
if (process is null) return "";
string title = process.MainWindowTitle;
return title.Contains(WindowTrim)
? TitleRegex().Match(title[..^TitleOffset]).Value
: "";
}

[GeneratedRegex("[^\\[]*")]
private static partial Regex TitleRegex();
}
30 changes: 30 additions & 0 deletions DAWPresenceBackgroundApp/DAWs/AbletonLive10Standard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace DAWPresence.DAWs;

public partial class AbletonLive10Standard : Daw
{
public AbletonLive10Standard()
{
ProcessName = "Ableton Live 10 Standard";
DisplayName = ProcessName;
ImageKey = "ableton-white";
ApplicationId = "1053952444859686983";
WindowTrim = " - " + DisplayName;
TitleOffset = 27;
}

public override string GetProjectNameFromProcessWindow()
{
Process? process = GetProcess();
if (process is null) return "";
string title = process.MainWindowTitle;
return title.Contains(WindowTrim)
? TitleRegex().Match(title[..^TitleOffset]).Value
: "";
}

[GeneratedRegex("[^\\[]*")]
private static partial Regex TitleRegex();
}
30 changes: 30 additions & 0 deletions DAWPresenceBackgroundApp/DAWs/AbletonLive10Suite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace DAWPresence.DAWs;

public partial class AbletonLive10Suite : Daw
{
public AbletonLive10Suite()
{
ProcessName = "Ableton Live 10 Suite";
DisplayName = ProcessName;
ImageKey = "ableton-white";
ApplicationId = "1053952444859686983";
WindowTrim = " - " + DisplayName;
TitleOffset = 24;
}

public override string GetProjectNameFromProcessWindow()
{
Process? process = GetProcess();
if (process is null) return "";
string title = process.MainWindowTitle;
return title.Contains(WindowTrim)
? TitleRegex().Match(title[..^TitleOffset]).Value
: "";
}

[GeneratedRegex("[^\\[]*")]
private static partial Regex TitleRegex();
}
30 changes: 30 additions & 0 deletions DAWPresenceBackgroundApp/DAWs/AbletonLive11Intro.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace DAWPresence.DAWs;

public partial class AbletonLive11Intro : Daw
{
public AbletonLive11Intro()
{
ProcessName = "Ableton Live 11 Intro";
DisplayName = ProcessName;
ImageKey = "ableton-white";
ApplicationId = "1053952444859686983";
WindowTrim = " - " + DisplayName;
TitleOffset = 24;
}

public override string GetProjectNameFromProcessWindow()
{
Process? process = GetProcess();
if (process is null) return "";
string title = process.MainWindowTitle;
return title.Contains(WindowTrim)
? TitleRegex().Match(title[..^TitleOffset]).Value
: "";
}

[GeneratedRegex("[^\\[]*")]
private static partial Regex TitleRegex();
}
Loading

0 comments on commit 9331587

Please sign in to comment.