Skip to content

Commit

Permalink
Decouple configs into OpenTabletDriver.Configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
X9VoiD committed Oct 6, 2021
1 parent f6b13e7 commit d8640ac
Show file tree
Hide file tree
Showing 147 changed files with 104 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using OpenTabletDriver.Plugin.Components;
using OpenTabletDriver.Plugin.Tablet;

namespace OpenTabletDriver.ComponentProviders
namespace OpenTabletDriver.Configurations
{
public class DeviceConfigurationProvider : IDeviceConfigurationProvider
{
public DeviceConfigurationProvider()
{
var asm = typeof(Driver).Assembly;
var asm = typeof(DeviceConfigurationProvider).Assembly;
var jsonSerializer = new JsonSerializer();

TabletConfigurations = asm.GetManifestResourceNames()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Project Properties">
<TargetFrameworks>net5</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<VersionPrefix>0.6.0.0</VersionPrefix>
<NoWarn>VSTHRD100; VSTHRD101; VSTHRD110; VSTHRD200</NoWarn>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<PropertyGroup Label="NuGet Package Information">
<PackageId>OpenTabletDriver.Configurations</PackageId>
<Version>0.6.0.0</Version>
<Authors>InfinityGhost</Authors>
<Description>OpenTabletDriver tablet configurations</Description>
<PackageOutputPath>../nupkg</PackageOutputPath>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup Label="NuGet Packages">
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup Label="Project References">
<ProjectReference Include="..\OpenTabletDriver.Plugin\OpenTabletDriver.Plugin.csproj" />
</ItemGroup>

<ItemGroup Label="Configurations">
<EmbeddedResource Include=".\Configurations\*\*.json">
<IncludeInPackage>true</IncludeInPackage>
</EmbeddedResource>
</ItemGroup>

</Project>
26 changes: 8 additions & 18 deletions OpenTabletDriver.Daemon/DriverDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,18 @@ public Task<IEnumerable<TabletReference>> GetTablets()

public async Task<IEnumerable<TabletReference>> DetectTablets()
{
var configDir = new DirectoryInfo(AppInfo.Current.ConfigurationDirectory);
if (configDir.Exists)
{
Driver.Detect();
Driver.Detect();

foreach (var tablet in Driver.InputDevices)
foreach (var tablet in Driver.InputDevices)
{
foreach (var dev in tablet.InputDevices)
{
foreach (var dev in tablet.InputDevices)
{
dev.RawReport += (_, report) => PostDebugReport(tablet, report);
dev.RawClone = debugging;
}
dev.RawReport += (_, report) => PostDebugReport(tablet, report);
dev.RawClone = debugging;
}

return await GetTablets();
}
else
{
Log.Write("Detect", $"The configuration directory '{configDir.FullName}' does not exist.", LogLevel.Error);
}
Log.Write("Detect", "No tablet found.");
return Array.Empty<TabletReference>();

return await GetTablets();
}

public Task SetSettings(Settings? settings)
Expand Down
44 changes: 42 additions & 2 deletions OpenTabletDriver.Desktop/DesktopDeviceConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenTabletDriver.Configurations;
using OpenTabletDriver.Plugin;
using OpenTabletDriver.Plugin.Components;
using OpenTabletDriver.Plugin.Tablet;

#nullable enable

namespace OpenTabletDriver.Desktop
{
public class DesktopDeviceConfigurationProvider : IDeviceConfigurationProvider
{
private readonly DeviceConfigurationProvider _inAssemblyConfigurationProvider = new();

public IEnumerable<TabletConfiguration> TabletConfigurations
{
get
{
var files = Directory.EnumerateFiles(AppInfo.Current.ConfigurationDirectory, "*.json", SearchOption.AllDirectories);
return files.Select(path => Serialization.Deserialize<TabletConfiguration>(File.OpenRead(path)));
IEnumerable<(ConfigurationSource, TabletConfiguration)> jsonConfigurations = Array.Empty<(ConfigurationSource, TabletConfiguration)>();

if (Directory.Exists(AppInfo.Current.ConfigurationDirectory))
{
var files = Directory.EnumerateFiles(AppInfo.Current.ConfigurationDirectory, "*.json", SearchOption.AllDirectories);

jsonConfigurations = files.Select(path => Serialization.Deserialize<TabletConfiguration>(File.OpenRead(path)))
.Select(jsonConfig => (ConfigurationSource.File, jsonConfig));
}
else
{
Log.Write("Detect", $"The configuration directory '{AppInfo.Current.ConfigurationDirectory}' does not exist.", LogLevel.Warning);
}

return _inAssemblyConfigurationProvider.TabletConfigurations
.Select(asmConfig => (ConfigurationSource.Assembly, asmConfig))
.Concat(jsonConfigurations)
.GroupBy(sourcedConfig => sourcedConfig.Item2.Name)
.Select(multiSourcedConfig =>
{
var asmConfig = multiSourcedConfig.Where(m => m.Item1 == ConfigurationSource.Assembly)
.Select(m => m.Item2)
.FirstOrDefault();
var jsonConfig = multiSourcedConfig.Where(m => m.Item1 == ConfigurationSource.File)
.Select(m => m.Item2)
.FirstOrDefault();

return jsonConfig ?? asmConfig!;
});
}
}

private enum ConfigurationSource
{
Assembly,
File
}
}
}
1 change: 1 addition & 0 deletions OpenTabletDriver.Desktop/OpenTabletDriver.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ItemGroup Label="Project References">
<ProjectReference Include="..\OpenTabletDriver.Native\OpenTabletDriver.Native.csproj" />
<ProjectReference Include="..\OpenTabletDriver\OpenTabletDriver.csproj" />
<ProjectReference Include="..\OpenTabletDriver.Configurations\OpenTabletDriver.Configurations.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion OpenTabletDriver.Desktop/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using OpenTabletDriver.Desktop.Migration;
using OpenTabletDriver.Desktop.Profiles;
using OpenTabletDriver.Desktop.Reflection;
Expand Down
14 changes: 14 additions & 0 deletions OpenTabletDriver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTabletDriver.Benchmarks
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTabletDriver.Tools.udev", "OpenTabletDriver.Tools.udev\OpenTabletDriver.Tools.udev.csproj", "{E1894A5A-B7D8-4F6B-8816-DBF7C5E1AE10}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTabletDriver.Configurations", "OpenTabletDriver.Configurations\OpenTabletDriver.Configurations.csproj", "{EF54AA9B-A725-44B5-96B5-AE526EF431CF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -198,5 +200,17 @@ Global
{E1894A5A-B7D8-4F6B-8816-DBF7C5E1AE10}.Release|x64.Build.0 = Release|Any CPU
{E1894A5A-B7D8-4F6B-8816-DBF7C5E1AE10}.Release|x86.ActiveCfg = Release|Any CPU
{E1894A5A-B7D8-4F6B-8816-DBF7C5E1AE10}.Release|x86.Build.0 = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|x64.ActiveCfg = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|x64.Build.0 = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|x86.ActiveCfg = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Debug|x86.Build.0 = Debug|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|Any CPU.Build.0 = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|x64.ActiveCfg = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|x64.Build.0 = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|x86.ActiveCfg = Release|Any CPU
{EF54AA9B-A725-44B5-96B5-AE526EF431CF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions OpenTabletDriver/DriverServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenTabletDriver.ComponentProviders;
using OpenTabletDriver.Configurations;
using OpenTabletDriver.Devices;
using OpenTabletDriver.Plugin.Components;

Expand Down
1 change: 1 addition & 0 deletions OpenTabletDriver/OpenTabletDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ItemGroup Label="Project References">
<ProjectReference Include="..\OpenTabletDriver.Plugin\OpenTabletDriver.Plugin.csproj" />
<ProjectReference Include="..\OpenTabletDriver.Native\OpenTabletDriver.Native.csproj" />
<ProjectReference Include="..\OpenTabletDriver.Configurations\OpenTabletDriver.Configurations.csproj" />
</ItemGroup>

<ItemGroup Label="Configurations">
Expand Down

0 comments on commit d8640ac

Please sign in to comment.