using System; using System.IO; using System.Resources; using JD.LogService; namespace JD.TeaBoxAddinSystem.Addins.Description.FileReferences { /// /// A reference to a resource file. /// /// 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 /// /// Extension of recognized resource files. /// protected const string ResourcesExtension = ".resources"; private ResourceManager m_resources; private bool m_resourcesLoaded; #endregion #region Constructor /// /// Constructor. /// /// 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; } } } } /// /// Loads the resources. /// /// 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 } }