112 lines
3.2 KiB
C#
112 lines
3.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Resources;
|
|
using JD.LogService;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins.Description.FileReferences
|
|
{
|
|
/// <summary>
|
|
/// A reference to a resource file.
|
|
/// </summary>
|
|
/// FIXME: This class doesn't support loading/unloading well, i.e. once the resources are loaded, they remain loaded.
|
|
public class ResourceFileReference : FileReference
|
|
{
|
|
#region Variables
|
|
|
|
/// <summary>
|
|
/// Extension of recognized resource files.
|
|
/// </summary>
|
|
protected const string ResourcesExtension = ".resources";
|
|
|
|
private ResourceManager m_resources;
|
|
private bool m_resourcesLoaded;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
/// <param name="filename"></param>
|
|
public ResourceFileReference(string filename)
|
|
: base(filename)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
internal ResourceManager Resources
|
|
{
|
|
get
|
|
{
|
|
Load();
|
|
return m_resources;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
private void Load()
|
|
{
|
|
if (!m_resourcesLoaded)
|
|
{
|
|
m_resourcesLoaded = true;
|
|
m_resources = LoadImpl();
|
|
|
|
if (m_resources == null)
|
|
{
|
|
AddinManager.Log(new Error(LogCategory.Error, "Unable to create resource from file '{0}'", Filename));
|
|
m_resources = null;
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
m_resources.GetObject(string.Empty);
|
|
}
|
|
catch (MissingManifestResourceException)
|
|
{
|
|
// No resources available
|
|
AddinManager.Log(new Error(LogCategory.Error, "Could not load resources {0}.", Filename) { Origin = FileLocation.GetValueOrDefault() });
|
|
m_resources = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Loads the resources.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected virtual ResourceManager LoadImpl()
|
|
{
|
|
if (Addin != null)
|
|
{
|
|
string directory = Addin.FileLocation.GetValueOrDefault().FileName;
|
|
|
|
if (!string.IsNullOrEmpty(directory))
|
|
{
|
|
string fullname = Path.Combine(directory, Filename);
|
|
directory = Path.GetDirectoryName(fullname);
|
|
|
|
string baseName = Path.GetFileName(Filename);
|
|
if (baseName.EndsWith(ResourcesExtension, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
baseName = baseName.Substring(0, baseName.Length - ResourcesExtension.Length);
|
|
}
|
|
|
|
return ResourceManager.CreateFileBasedResourceManager(baseName, directory, null);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|