Skip to content

Commit

Permalink
Refactored dialog resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Aug 13, 2016
1 parent 3f05baa commit 641de7e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 117 deletions.
22 changes: 22 additions & 0 deletions Src/Workshell.PE.Resources/Dialogs/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ namespace Workshell.PE.Resources
public sealed class Dialog
{

internal Dialog(DialogResource dialogResource, uint languageId)
{
Resource = dialogResource;
Language = languageId;
}

#region Properties

public DialogResource Resource
{
get;
private set;
}

public uint Language
{
get;
private set;
}

#endregion

}

}
24 changes: 23 additions & 1 deletion Src/Workshell.PE.Resources/Dialogs/DialogEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ public byte[] ExtraData
public sealed class DialogEx : IEnumerable<DialogItemEx>
{

private DialogResource resource;
private uint language_id;

private uint help_id;
private uint ex_styles;
private uint styles;
Expand All @@ -243,8 +246,11 @@ public sealed class DialogEx : IEnumerable<DialogItemEx>
private CharacterSet char_set;
private DialogItemEx[] items;

internal DialogEx(Stream stream)
internal DialogEx(DialogResource dialogResource, uint languageId, Stream stream)
{
resource = dialogResource;
language_id = languageId;

ushort ver = Utils.ReadUInt16(stream);
ushort sig = Utils.ReadUInt16(stream);

Expand Down Expand Up @@ -359,6 +365,22 @@ public DialogStyle GetDialogStyles()

#region Properties

public DialogResource Resource
{
get
{
return resource;
}
}

public uint Language
{
get
{
return language_id;
}
}

public uint HelpId
{
get
Expand Down
155 changes: 39 additions & 116 deletions Src/Workshell.PE.Resources/Dialogs/DialogResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,152 +42,75 @@
namespace Workshell.PE.Resources
{

public enum DialogSaveFormat
{
Raw,
Resource
}

public sealed class DialogResource
public sealed class DialogResource : Resource
{

private Resource resource;
private uint language_id;
private bool is_extended;
private Dialog dialog;
private DialogEx dialog_ex;

internal DialogResource(Resource sourceResource, uint languageId, byte[] data)
public DialogResource(ResourceType owningType, ResourceDirectoryEntry directoryEntry) : base(owningType, directoryEntry)
{
resource = sourceResource;
language_id = languageId;

MemoryStream mem = resource.Type.Resources.Image.MemoryStreamProvider.GetStream(data);

using (mem)
{
ushort ver = Utils.ReadUInt16(mem);
ushort sig = Utils.ReadUInt16(mem);

is_extended = (ver == 1 && sig == 0xFFFF);

mem.Seek(0, SeekOrigin.Begin);

if (!is_extended)
{
dialog = null;
}
else
{
dialog_ex = new DialogEx(mem);
}
}
}

#region Static Methods

public static DialogResource Load(Resource resource)
public static bool Register()
{
return Load(resource, Resource.DEFAULT_LANGUAGE);
}

public static DialogResource Load(Resource resource, uint language)
{
if (!resource.Languages.Contains(language))
return null;

if (resource.Type.Id != ResourceType.RT_DIALOG)
return null;
ResourceId resource_type = new ResourceId(ResourceType.RT_DIALOG);

byte[] data = resource.GetBytes(language);
DialogResource result = new DialogResource(resource, language, data);

return result;
return ResourceType.Register(resource_type, typeof(DialogResource));
}

#endregion

#region Methods

public Dialog GetDialog()
public Dialog GetDialog(uint languageId)
{
return dialog;
}
byte[] data = GetBytes(languageId);

public DialogEx GetDialogEx()
{
return dialog_ex;
}
using (MemoryStream mem = new MemoryStream(data))
{
ushort ver = Utils.ReadUInt16(mem);
ushort sig = Utils.ReadUInt16(mem);

public void Save(string fileName)
{
Save(fileName, DialogSaveFormat.Raw);
}
bool is_extended = (ver == 1 && sig == 0xFFFF);

public void Save(Stream stream)
{
Save(stream, DialogSaveFormat.Raw);
}
mem.Seek(0, SeekOrigin.Begin);

public void Save(string fileName, DialogSaveFormat format)
{
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
Save(file, format);
file.Flush();
}
}
if (!is_extended)
{
Dialog dialog = new Dialog(this, languageId);

public void Save(Stream stream, DialogSaveFormat format)
{
switch (format)
{
case DialogSaveFormat.Raw:
SaveRaw(stream);
break;
case DialogSaveFormat.Resource:
SaveResource(stream);
break;
return dialog;
}
else
{
return null;
}
}
}

private void SaveRaw(Stream stream)
public DialogEx GetDialogEx(uint languageId)
{
byte[] data = resource.GetBytes(language_id);

stream.Write(data, 0, data.Length);
}
byte[] data = GetBytes(languageId);

private void SaveResource(Stream stream)
{
throw new NotImplementedException();
}

#endregion
using (MemoryStream mem = new MemoryStream(data))
{
ushort ver = Utils.ReadUInt16(mem);
ushort sig = Utils.ReadUInt16(mem);

#region Properties
bool is_extended = (ver == 1 && sig == 0xFFFF);

public Resource Resource
{
get
{
return resource;
}
}
mem.Seek(0, SeekOrigin.Begin);

public uint Language
{
get
{
return language_id;
}
}
if (!is_extended)
{
return null;
}
else
{
DialogEx dialog = new DialogEx(this, languageId, mem);

public bool IsExtended
{
get
{
return is_extended;
return dialog;
}
}
}

Expand Down

0 comments on commit 641de7e

Please sign in to comment.