diff --git a/Src/Workshell.PE.Resources/Menus/MenuResource.cs b/Src/Workshell.PE.Resources/Menus/MenuResource.cs index 24ecdc0..d7c9343 100644 --- a/Src/Workshell.PE.Resources/Menus/MenuResource.cs +++ b/Src/Workshell.PE.Resources/Menus/MenuResource.cs @@ -42,61 +42,20 @@ namespace Workshell.PE.Resources { - public enum MenuSaveFormat - { - Raw, - Resource - } - - public sealed class MenuResource : IEnumerable + public sealed class Menu : IEnumerable { - private Resource resource; + private MenuResource resource; private uint language_id; private MenuItem[] items; - internal MenuResource(Resource sourceResource, uint languageId, byte[] data) + internal Menu(MenuResource menuResource, uint languageId, MenuItem[] menuItems) { - resource = sourceResource; + resource = menuResource; language_id = languageId; - - MemoryStream mem = resource.Type.Resources.Image.MemoryStreamProvider.GetStream(data); - - using (mem) - { - ushort version = Utils.ReadUInt16(mem); - ushort header_size = Utils.ReadUInt16(mem); - List menu_items = new List(); - - LoadMenu(mem, menu_items); - - items = menu_items.ToArray(); - } - } - - #region Static Methods - - public static MenuResource Load(Resource resource) - { - return Load(resource, Resource.DEFAULT_LANGUAGE); - } - - public static MenuResource Load(Resource resource, uint language) - { - if (!resource.Languages.Contains(language)) - return null; - - if (resource.Type.Id != ResourceType.RT_MENU) - return null; - - byte[] data = resource.GetBytes(language); - MenuResource result = new MenuResource(resource, language, data); - - return result; + items = menuItems; } - #endregion - #region Methods public IEnumerator GetEnumerator() @@ -110,48 +69,88 @@ IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); } - public void Save(string fileName) + #endregion + + #region Properties + + public MenuResource Resource { - Save(fileName, MenuSaveFormat.Raw); + get + { + return resource; + } } - public void Save(Stream stream) + public uint Language { - Save(stream, MenuSaveFormat.Raw); + get + { + return language_id; + } } - public void Save(string fileName, MenuSaveFormat format) + public int Count { - using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) + get { - Save(file, format); - file.Flush(); + return items.Length; } } - public void Save(Stream stream, MenuSaveFormat format) + public MenuItem this[int index] { - switch (format) + get { - case MenuSaveFormat.Raw: - SaveRaw(stream); - break; - case MenuSaveFormat.Resource: - SaveResource(stream); - break; + return items[index]; } } - private void SaveRaw(Stream stream) + #endregion + + } + + public sealed class MenuResource : Resource + { + + public MenuResource(ResourceType owningType, ResourceDirectoryEntry directoryEntry) : base(owningType, directoryEntry) + { + } + + #region Static Methods + + public static bool Register() { - byte[] data = resource.GetBytes(language_id); + ResourceId resource_type = new ResourceId(ResourceType.RT_MENU); + + return ResourceType.Register(resource_type, typeof(MenuResource)); + } + + #endregion - stream.Write(data, 0, data.Length); + #region Methods + + public Menu ToMenu() + { + return ToMenu(DEFAULT_LANGUAGE); } - private void SaveResource(Stream stream) + public Menu ToMenu(uint languageId) { - throw new NotImplementedException(); + byte[] data = GetBytes(languageId); + + using (MemoryStream mem = new MemoryStream(data)) + { + ushort version = Utils.ReadUInt16(mem); + ushort header_size = Utils.ReadUInt16(mem); + List menu_items = new List(); + + LoadMenu(mem, menu_items); + + MenuItem[] items = menu_items.ToArray(); + Menu menu = new Resources.Menu(this, languageId, items); + + return menu; + } } private void LoadMenu(Stream stream, List items) @@ -210,42 +209,6 @@ private void LoadMenu(Stream stream, List items) #endregion - #region Properties - - public Resource Resource - { - get - { - return resource; - } - } - - public uint Language - { - get - { - return language_id; - } - } - - public int Count - { - get - { - return items.Length; - } - } - - public MenuItem this[int index] - { - get - { - return items[index]; - } - } - - #endregion - } }