Skip to content

Commit

Permalink
v2.6 - MD5 Support + Optimized SetImage
Browse files Browse the repository at this point in the history
1. Added new MD5 functions in the `Tools` helper class
2. Optimized SetImage to not resubmit an image that was just posted to the device. Can be overidden with new property in Connection.SetImage() function.
3. Updated dependency packages to latest versions
  • Loading branch information
BarRaider committed Sep 29, 2019
1 parent f5c6d0e commit 1096f40
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
24 changes: 20 additions & 4 deletions barraider-sdtools/SDConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace BarRaider.SdTools
/// </summary>
public class SDConnection
{
#region Private Methods

private string previousImageHash = null;

#endregion

#region Public Implementations

/// <summary>
Expand Down Expand Up @@ -77,19 +83,29 @@ public async Task GetGlobalSettingsAsync()
/// </summary>
/// <param name="base64Image">Base64 encoded image</param>
/// <returns></returns>
public async Task SetImageAsync(string base64Image)
public async Task SetImageAsync(string base64Image, bool forceSendToStreamdeck = false)
{
await StreamDeckConnection.SetImageAsync(base64Image, ContextId, streamdeck_client_csharp.SDKTarget.HardwareAndSoftware);
string hash = Tools.StringToMD5(base64Image);
if (forceSendToStreamdeck || hash != previousImageHash)
{
previousImageHash = hash;
await StreamDeckConnection.SetImageAsync(base64Image, ContextId, streamdeck_client_csharp.SDKTarget.HardwareAndSoftware);
}
}

/// <summary>
/// Sets an image on the StreamDeck key
/// </summary>
/// <param name="image">Image object</param>
/// <returns></returns>
public async Task SetImageAsync(Image image)
public async Task SetImageAsync(Image image, bool forceSendToStreamdeck = false)
{
await StreamDeckConnection.SetImageAsync(image, ContextId, streamdeck_client_csharp.SDKTarget.HardwareAndSoftware);
string hash = Tools.ImageToMD5(image);
if (forceSendToStreamdeck || hash != previousImageHash)
{
previousImageHash = hash;
await StreamDeckConnection.SetImageAsync(image, ContextId, streamdeck_client_csharp.SDKTarget.HardwareAndSoftware);
}
}

/// <summary>
Expand Down
63 changes: 62 additions & 1 deletion barraider-sdtools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;

namespace BarRaider.SdTools
{
Expand Down Expand Up @@ -54,6 +55,11 @@ public static string FileToBase64(string fileName, bool addHeaderPrefix)
/// <returns></returns>
public static string ImageToBase64(Image image, bool addHeaderPrefix)
{
if (image == null)
{
return null;
}

using (MemoryStream m = new MemoryStream())
{
image.Save(m, ImageFormat.Png);
Expand All @@ -74,6 +80,11 @@ public static Image Base64StringToImage(string base64String)
{
try
{
if (string.IsNullOrEmpty(base64String))
{
return null;
}

// Remove header
if (base64String.Substring(0, HEADER_PREFIX.Length) == HEADER_PREFIX)
{
Expand Down Expand Up @@ -182,7 +193,7 @@ private static Bitmap GenerateKeyImage(int height, int width, out Graphics graph
graphics = null;
return null;
}


/// <summary>
/// Extracts the actual filename from a file payload received from the Property Inspector
Expand All @@ -196,9 +207,59 @@ public static string FilenameFromPayload(Newtonsoft.Json.Linq.JToken payload)

private static string FilenameFromString(string filenameWithFakepath)
{
if (string.IsNullOrEmpty(filenameWithFakepath))
{
return null;
}

return Uri.UnescapeDataString(filenameWithFakepath.Replace("C:\\fakepath\\", ""));
}

/// <summary>
/// Returns MD5 Hash from an image object
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static string ImageToMD5(Image image)
{
if (image == null)
{
return null;
}

using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
return BytesToMD5(ms.ToArray());
}
}

/// <summary>
/// Returns MD5 Hash from a string
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static string StringToMD5(string str)
{
if (str == null)
{
return null;
}
return BytesToMD5(System.Text.Encoding.UTF8.GetBytes(str));
}

/// <summary>
/// Returns MD5 Hash from a byte stream
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static string BytesToMD5(byte[] byteStream)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(byteStream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}

#endregion

#region JObject Related
Expand Down
15 changes: 8 additions & 7 deletions barraider-sdtools/barraider-sdtools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Feel free to contact me for more information: https://barraider.github.io</Descr
<PackageTags>StreamDeck Elgato Library Plugin Stream Deck</PackageTags>
<PackageId>StreamDeck-Tools</PackageId>
<PackageIconUrl>https://raw.githubusercontent.com/BarRaider/barraider.github.io/master/images/BRLogo.png</PackageIconUrl>
<AssemblyVersion>2.5.0.0</AssemblyVersion>
<FileVersion>2.5.0.0</FileVersion>
<Version>2.5.0</Version>
<PackageReleaseNotes>2.5.0 - 1. Support for StreamDeckMobile device type
2. Added new `Tools.GenerateGenericKeyImage()` function that generates an image that fits all Stream Decks</PackageReleaseNotes>
<AssemblyVersion>2.6.0.0</AssemblyVersion>
<FileVersion>2.6.0.0</FileVersion>
<Version>2.6.0</Version>
<PackageReleaseNotes>2.6.0 - 1. Added new MD5 functions in the `Tools` helper class
2. Optimized SetImage to not resubmit an image that was just posted to the device. Can be overidden with new property in Connection.SetImage() function.
3. Updated dependency packages to latest versions</PackageReleaseNotes>
<RootNamespace>BarRaider.SdTools</RootNamespace>
<AssemblyName>StreamDeckTools</AssemblyName>
</PropertyGroup>
Expand All @@ -30,9 +31,9 @@ Feel free to contact me for more information: https://barraider.github.io</Descr
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NLog" Version="4.6.6" />
<PackageReference Include="NLog" Version="4.6.7" />
<PackageReference Include="streamdeck-client-csharp" Version="4.1.1" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="System.Drawing.Common" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down

0 comments on commit 1096f40

Please sign in to comment.