Skip to content

Commit

Permalink
Refactor of menu resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Aug 13, 2016
1 parent 641de7e commit 598150e
Showing 1 changed file with 66 additions and 103 deletions.
169 changes: 66 additions & 103 deletions Src/Workshell.PE.Resources/Menus/MenuResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,61 +42,20 @@
namespace Workshell.PE.Resources
{

public enum MenuSaveFormat
{
Raw,
Resource
}

public sealed class MenuResource : IEnumerable<MenuItem>
public sealed class Menu : IEnumerable<MenuItem>
{

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<MenuItem> menu_items = new List<MenuItem>();

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<MenuItem> GetEnumerator()
Expand All @@ -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<MenuItem> menu_items = new List<MenuItem>();

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<MenuItem> items)
Expand Down Expand Up @@ -210,42 +209,6 @@ private void LoadMenu(Stream stream, List<MenuItem> 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

}

}

0 comments on commit 598150e

Please sign in to comment.