255 lines
9.0 KiB
C#
255 lines
9.0 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
namespace Sarand.Net.Standard.Base.Helpers
|
|
{
|
|
//=======================================================================
|
|
public static class SacsResourceHelper
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
public enum TextResource
|
|
{
|
|
SaBaseCss,
|
|
SaBaseJs,
|
|
JQueryJs,
|
|
JQueryUiCss,
|
|
JQueryUiJs,
|
|
}
|
|
public enum ImageResource
|
|
{
|
|
// Burgers.
|
|
Icon_Burger_Black_48_png,
|
|
Icon_Burger_Black_64_png,
|
|
// Arrows.
|
|
Arrow_Left_Black_16_png,
|
|
Arrow_Left_Double_Black_16_png,
|
|
Arrow_Right_Black_16_png,
|
|
Arrow_Right_Double_Black_16_png,
|
|
Arrow_Left_Black_24_png,
|
|
Arrow_Left_Double_Black_24_png,
|
|
Arrow_Right_Black_24_png,
|
|
Arrow_Right_Double_Black_24_png,
|
|
// IconShock.
|
|
IconShock_Arrow_Down_Purple_72_png,
|
|
IconShock_Arrow_Right_Purple_72_png,
|
|
IconShock_Add_Green_72_png,
|
|
IconShock_Pencil_Orange_72_png,
|
|
IconShock_Close_Red_72_png,
|
|
IconShock_TrashCan_Grey_72_png,
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public static Assembly MyAssembly { get; } = typeof(SacsResourceHelper).GetTypeInfo().Assembly;
|
|
|
|
//-------------------------------------------------------------------
|
|
// NB 'Properties' not available in Core .Net project.
|
|
// Resources must be loaded via csproj so that they are available in the Manifest Resources:
|
|
// <ItemGroup>
|
|
// <EmbeddedResource Include="Properties\Resources.resx">
|
|
// <Generator>ResXFileCodeGenerator</Generator>
|
|
// <LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
|
// </EmbeddedResource>
|
|
// <EmbeddedResource Include="Resources\html\*.css" />
|
|
// <EmbeddedResource Include="Resources\html\*.js" />
|
|
// </ItemGroup>
|
|
// REf. https://stackoverflow.com/questions/38762368/embedded-resource-in-net-core-libraries
|
|
//-------------------------------------------------------------------
|
|
public static String GetResourceText(TextResource textResource)
|
|
{
|
|
String partialPath = "";
|
|
String resourceFilename = "";
|
|
switch (textResource)
|
|
{
|
|
case TextResource.SaBaseCss:
|
|
//return Properties.Resources.sa_base_css;
|
|
resourceFilename = "sa-base.css";
|
|
break;
|
|
case TextResource.SaBaseJs:
|
|
//return Properties.Resources.sa_base_js;
|
|
resourceFilename = "sa-base.js";
|
|
break;
|
|
case TextResource.JQueryJs:
|
|
//return Properties.Resources.jquery_1_12_1_min_js;
|
|
resourceFilename = "jquery-1.12.1.min.js";
|
|
break;
|
|
case TextResource.JQueryUiCss:
|
|
//return Properties.Resources.jquery_ui_1_12_1_min_css;
|
|
resourceFilename = "jquery-ui-1.12.1.min.css";
|
|
break;
|
|
case TextResource.JQueryUiJs:
|
|
//return Properties.Resources.jquery_ui_1_12_1_min_js;
|
|
resourceFilename = "jquery-ui-1.12.1.min.js";
|
|
break;
|
|
}
|
|
if (!StringUtil.IsBlank(resourceFilename))
|
|
{
|
|
String resourceText = GetManifestResourceAsText(MyAssembly, partialPath, resourceFilename);
|
|
if (!StringUtil.IsBlank(resourceText))
|
|
return resourceText;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
public static byte[] GetResourceImage(ImageResource imageResource)
|
|
{
|
|
String partialPath = "";
|
|
String resourceFilename = "";
|
|
switch (imageResource)
|
|
{
|
|
//-----------------------------------------------------------
|
|
// Burgers.
|
|
case ImageResource.Icon_Burger_Black_48_png:
|
|
resourceFilename = "burger_black_48.png";
|
|
break;
|
|
case ImageResource.Icon_Burger_Black_64_png:
|
|
resourceFilename = "burger_black_64.png";
|
|
break;
|
|
//-----------------------------------------------------------
|
|
// Arrows.
|
|
case ImageResource.Arrow_Left_Black_16_png:
|
|
resourceFilename = "arrow_left_black_16.png";
|
|
break;
|
|
case ImageResource.Arrow_Left_Double_Black_16_png:
|
|
resourceFilename = "arrow_left_double_black_16.png";
|
|
break;
|
|
case ImageResource.Arrow_Right_Black_16_png:
|
|
resourceFilename = "arrow_right_black_16.png";
|
|
break;
|
|
case ImageResource.Arrow_Right_Double_Black_16_png:
|
|
resourceFilename = "arrow_right_double_black_16.png";
|
|
break;
|
|
case ImageResource.Arrow_Left_Black_24_png:
|
|
resourceFilename = "arrow_left_black_24.png";
|
|
break;
|
|
case ImageResource.Arrow_Left_Double_Black_24_png:
|
|
resourceFilename = "arrow_left_double_black_24.png";
|
|
break;
|
|
case ImageResource.Arrow_Right_Black_24_png:
|
|
resourceFilename = "arrow_right_black_24.png";
|
|
break;
|
|
case ImageResource.Arrow_Right_Double_Black_24_png:
|
|
resourceFilename = "arrow_right_double_black_24.png";
|
|
break;
|
|
//-----------------------------------------------------------
|
|
// IconShock.
|
|
case ImageResource.IconShock_Arrow_Down_Purple_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "arrow_down_purple_72.png";
|
|
break;
|
|
case ImageResource.IconShock_Arrow_Right_Purple_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "arrow_right_purple_72.png";
|
|
break;
|
|
case ImageResource.IconShock_Add_Green_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "add_green_72.png";
|
|
break;
|
|
case ImageResource.IconShock_Pencil_Orange_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "pencil_orange_72.png";
|
|
break;
|
|
case ImageResource.IconShock_Close_Red_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "close_red_72.png";
|
|
break;
|
|
case ImageResource.IconShock_TrashCan_Grey_72_png:
|
|
partialPath = "IconShock";
|
|
resourceFilename = "trashcan_grey_72.png";
|
|
break;
|
|
//-----------------------------------------------------------
|
|
}
|
|
if (!StringUtil.IsBlank(resourceFilename))
|
|
{
|
|
byte[] resourceData = GetManifestResourceAsBinary(MyAssembly, partialPath, resourceFilename);
|
|
if (resourceData != null)
|
|
return resourceData;
|
|
}
|
|
return new byte[0];
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Text resources.
|
|
|
|
/// <summary>Retrieve resource as String based on assembly and partial resource name.</summary>
|
|
public static String GetManifestResourceAsText(Assembly assembly, String partialPath, String resourceNameEndsWith)
|
|
{
|
|
if (StringUtil.IsBlank(resourceNameEndsWith)) return "";
|
|
|
|
using (Stream resourceStream = GetManifestResourceStream(assembly, partialPath, resourceNameEndsWith))
|
|
{
|
|
if (resourceStream != null)
|
|
{
|
|
using (StreamReader resourceReader = new StreamReader(resourceStream, Encoding.UTF8))
|
|
{
|
|
return resourceReader.ReadToEnd();
|
|
}
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Binary resources.
|
|
|
|
/// <summary>Retrieve resource as byte array based on assembly and partial resource name.</summary>
|
|
public static byte[] GetManifestResourceAsBinary(Assembly assembly, String partialPath, String resourceNameEndsWith)
|
|
{
|
|
if (
|
|
StringUtil.IsBlank(resourceNameEndsWith)) return null;
|
|
using (Stream resourceStream = GetManifestResourceStream(assembly, partialPath, resourceNameEndsWith))
|
|
{
|
|
if (resourceStream != null)
|
|
{
|
|
using (BinaryReader resourceReader = new BinaryReader(resourceStream, Encoding.UTF8))
|
|
{
|
|
return resourceReader.ReadBytes((int)resourceStream.Length);
|
|
}
|
|
}
|
|
}
|
|
SaLogger.Warn($"{SysUtil.Fn} Failed to locate text resource: {resourceNameEndsWith}");
|
|
return null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Resource stream.
|
|
|
|
/// <summary>Retrieve resource as stream based on assembly and partial resource name.</summary>
|
|
public static Stream GetResourceStream(String partialPath, String resourceNameEndsWith)
|
|
{
|
|
return GetManifestResourceStream(MyAssembly, partialPath, resourceNameEndsWith);
|
|
}
|
|
|
|
/// <summary>Retrieve resource as stream based on assembly and partial resource name.</summary>
|
|
public static Stream GetManifestResourceStream(Assembly assembly, String partialPath, String resourceNameEndsWith)
|
|
{
|
|
//SaLogger.Debug($"{SysUtil.Fn} Manifest resource names: {assembly.GetManifestResourceNames().Concat("","\n\t")}");
|
|
foreach (String resourceName in assembly.GetManifestResourceNames())
|
|
{
|
|
//SaLogger.Debug($"{SysUtil.Fn} Resource: '{resourceName}'.");
|
|
|
|
// Match the partial path.
|
|
if (!StringUtil.IsBlank(partialPath) && !resourceName.ContainsCI(partialPath)) continue;
|
|
// Match the resource filename to the end of the resource name - resource names start with default namespace (eg. 'Sarand').
|
|
if (!resourceName.EndsWithCI(resourceNameEndsWith)) continue;
|
|
|
|
//SaLogger.Debug($"{SysUtil.Fn} Resource filename: '{resourceNameEndsWith}'. Matched to resource: {resourceName}");
|
|
return assembly.GetManifestResourceStream(resourceName);
|
|
}
|
|
SaLogger.Warn($"{SysUtil.Fn} Resource filename not found: {resourceNameEndsWith}.");
|
|
return null;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|