mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-06-18 02:59:04 +00:00
Reduce complexity: Splitting the GetResource method into separate methods
This commit is contained in:
parent
7c19dd366f
commit
9213d18c90
|
@ -102,9 +102,18 @@ namespace WelsonJS.Launcher
|
||||||
|
|
||||||
private byte[] GetResource(string resourceName)
|
private byte[] GetResource(string resourceName)
|
||||||
{
|
{
|
||||||
// Find a resource from Embedded Resource
|
// Try to fetch embedded resource.
|
||||||
|
byte[] data = GetEmbeddedResource(typeof(ResourceServer).Namespace + "." + resourceName);
|
||||||
|
if (data != null) return data;
|
||||||
|
|
||||||
|
// Fallback: Try to fetch resource from ResourceManager.
|
||||||
|
return GetResourceFromManager(resourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] GetEmbeddedResource(string fullResourceName)
|
||||||
|
{
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||||
using (Stream stream = assembly.GetManifestResourceStream(typeof(ResourceServer).Namespace + "." + resourceName))
|
using (Stream stream = assembly.GetManifestResourceStream(fullResourceName))
|
||||||
{
|
{
|
||||||
if (stream != null)
|
if (stream != null)
|
||||||
{
|
{
|
||||||
|
@ -115,21 +124,22 @@ namespace WelsonJS.Launcher
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Find a resource from Resources.resx
|
private byte[] GetResourceFromManager(string resourceName)
|
||||||
|
{
|
||||||
object resourceObject = Properties.Resources.ResourceManager.GetObject(resourceName);
|
object resourceObject = Properties.Resources.ResourceManager.GetObject(resourceName);
|
||||||
switch (resourceObject)
|
switch (resourceObject)
|
||||||
{
|
{
|
||||||
case byte[] resourceBytes: // if the matched type is byte[]
|
case byte[] resourceBytes:
|
||||||
return resourceBytes;
|
return resourceBytes;
|
||||||
|
case System.Drawing.Icon icon:
|
||||||
case System.Drawing.Icon icon: // if the matched type is System.Drawing.Icon
|
|
||||||
return ConvertIconToBytes(icon);
|
return ConvertIconToBytes(icon);
|
||||||
}
|
default:
|
||||||
|
|
||||||
// If not found
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private byte[] ConvertIconToBytes(System.Drawing.Icon icon)
|
private byte[] ConvertIconToBytes(System.Drawing.Icon icon)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user