Skip to content

Commit

Permalink
Refactored accelerator resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Aug 13, 2016
1 parent 8ac2321 commit 3f05baa
Showing 1 changed file with 77 additions and 90 deletions.
167 changes: 77 additions & 90 deletions Src/Workshell.PE.Resources/Accelerators/AcceleratorResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,66 +123,20 @@ public ushort Id

}

public enum AcceleratorSaveFormat
{
Raw,
Resource
}

public sealed class AcceleratorResource : IEnumerable<AcceleratorEntry>
public sealed class Accelerators : IEnumerable<AcceleratorEntry>
{

private Resource resource;
private AcceleratorResource resource;
private uint language_id;
private AcceleratorEntry[] accelerators;

internal AcceleratorResource(Resource sourceResource, uint languageId, byte[] data)
internal Accelerators(AcceleratorResource acceleratorResource, uint languageId, AcceleratorEntry[] acceleratorEntries)
{
resource = sourceResource;
resource = acceleratorResource;
language_id = languageId;

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

using (mem)
{
int size = Utils.SizeOf<ACCELTABLEENTRY>();
long count = mem.Length / size;

accelerators = new AcceleratorEntry[count];

for (var i = 0; i < count; i++)
{
ACCELTABLEENTRY table_entry = Utils.Read<ACCELTABLEENTRY>(mem, size);
AcceleratorEntry entry = new AcceleratorEntry(table_entry.fFlags, table_entry.wAnsi, table_entry.wId);

accelerators[i] = entry;
}
}
}

#region Static Methods

public static AcceleratorResource Load(Resource resource)
{
return Load(resource, Resource.DEFAULT_LANGUAGE);
}

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

if (resource.Type.Id != ResourceType.RT_ACCELERATOR)
return null;

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

return result;
accelerators = acceleratorEntries;
}

#endregion

#region Methods

public IEnumerator<AcceleratorEntry> GetEnumerator()
Expand All @@ -196,85 +150,118 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}

public void Save(string fileName)
#endregion

#region Properties

public AcceleratorResource Resource
{
Save(fileName, AcceleratorSaveFormat.Raw);
get
{
return resource;
}
}

public void Save(Stream stream)
public uint Language
{
Save(stream, AcceleratorSaveFormat.Raw);
get
{
return language_id;
}
}

public void Save(string fileName, AcceleratorSaveFormat format)
public int Count
{
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
get
{
Save(file, format);
file.Flush();
return accelerators.Length;
}
}

public void Save(Stream stream, AcceleratorSaveFormat format)
public AcceleratorEntry this[int index]
{
switch (format)
get
{
case AcceleratorSaveFormat.Raw:
SaveRaw(stream);
break;
case AcceleratorSaveFormat.Resource:
SaveResource(stream);
break;
return accelerators[index];
}
}

private void SaveRaw(Stream stream)
{
byte[] data = resource.GetBytes(language_id);
#endregion

}

/*
public enum AcceleratorSaveFormat
{
Raw
}
*/

public sealed class AcceleratorResource : Resource
{

stream.Write(data, 0, data.Length);
public AcceleratorResource(ResourceType owningType, ResourceDirectoryEntry directoryEntry) : base(owningType, directoryEntry)
{
}

private void SaveResource(Stream stream)
#region Static Methods

public static bool Register()
{
throw new NotImplementedException();
ResourceId resource_type = new ResourceId(ResourceType.RT_ACCELERATOR);

return ResourceType.Register(resource_type, typeof(AcceleratorResource));
}

#endregion

#region Properties
#region Methods

public Resource Resource
public Accelerators Get(uint languageId)
{
get
{
return resource;
}
}
byte[] data = GetBytes(languageId);

public uint Language
{
get
using (MemoryStream mem = new MemoryStream(data))
{
return language_id;
int size = Utils.SizeOf<ACCELTABLEENTRY>();
long count = mem.Length / size;

AcceleratorEntry[] accelerators = new AcceleratorEntry[count];

for (var i = 0; i < count; i++)
{
ACCELTABLEENTRY table_entry = Utils.Read<ACCELTABLEENTRY>(mem, size);
AcceleratorEntry entry = new AcceleratorEntry(table_entry.fFlags, table_entry.wAnsi, table_entry.wId);

accelerators[i] = entry;
}

Accelerators result = new Accelerators(this, languageId, accelerators);

return result;
}
}

public int Count
/*
public void Save(string fileName, uint languageId, AcceleratorSaveFormat format)
{
get
using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
return accelerators.Length;
Save(file, languageId, format);
file.Flush();
}
}
public AcceleratorEntry this[int index]
public void Save(Stream stream, uint languageId, AcceleratorSaveFormat format)
{
get
if (format == AcceleratorSaveFormat.Raw)
{
return accelerators[index];
byte[] data = GetBytes(languageId);
stream.Write(data, 0, data.Length);
}
}
*/

#endregion

Expand Down

0 comments on commit 3f05baa

Please sign in to comment.