1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Dtf.Resources
{
using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// A subclass of Resource which provides specific methods for manipulating the resource data.
/// </summary>
/// <remarks>
/// The resource is of type <see cref="ResourceType.GroupIcon"/> (RT_GROUPICON).
/// </remarks>
public sealed class GroupIconResource : Resource
{
internal bool dirty;
private GroupIconInfo rawGroupIconInfo;
private List<Resource> icons;
/// <summary>
/// Creates a new GroupIconResource object without any data. The data can be later loaded from a file.
/// </summary>
/// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param>
/// <param name="locale">Locale of the resource</param>
public GroupIconResource(string name, int locale)
: this(name, locale, null)
{
}
/// <summary>
/// Creates a new GroupIconResource object with data. The data can be later saved to a file.
/// </summary>
/// <param name="name">Name of the resource. For a numeric resource identifier, prefix the decimal number with a "#".</param>
/// <param name="locale">Locale of the resource</param>
/// <param name="data">Raw resource data</param>
public GroupIconResource(string name, int locale, byte[] data)
: base(ResourceType.GroupIcon, name, locale, data)
{
this.RefreshIconGroupInfo(data);
}
/// <summary>
/// Gets or sets the raw data of the resource. The data is in the format of the RT_GROUPICON resource structure.
/// </summary>
public override byte[] Data
{
get
{
if (this.dirty)
{
base.Data = this.rawGroupIconInfo.GetResourceData();
this.dirty = false;
}
return base.Data;
}
set
{
this.RefreshIconGroupInfo(value);
base.Data = value;
this.dirty = false;
}
}
/// <summary>
/// Enumerates the the icons in the icon group.
/// </summary>
public IEnumerable<Resource> Icons { get { return this.icons; } }
/// <summary>
/// Reads the icon group from a .ico file.
/// </summary>
/// <param name="path">Path to an icon file (.ico).</param>
public void ReadFromFile(string path)
{
this.rawGroupIconInfo = new GroupIconInfo();
this.icons = new List<Resource>();
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
this.rawGroupIconInfo.ReadFromFile(fs);
// After reading the group icon info header from the file, read all the icons.
for (int i = 0; i < this.rawGroupIconInfo.DirectoryInfo.Length; ++i)
{
ushort index = this.rawGroupIconInfo.DirectoryInfo[i].imageIndex;
uint offset = this.rawGroupIconInfo.DirectoryInfo[i].imageOffset;
uint size = this.rawGroupIconInfo.DirectoryInfo[i].imageSize;
byte[] data = new byte[size];
fs.Seek(offset, SeekOrigin.Begin);
fs.Read(data, 0, data.Length);
Resource resource = new Resource(ResourceType.Icon, String.Concat("#", index), this.Locale, data);
this.icons.Add(resource);
}
}
this.dirty = true;
}
private void RefreshIconGroupInfo(byte[] refreshData)
{
this.rawGroupIconInfo = new GroupIconInfo();
this.icons = new List<Resource>();
if (refreshData != null)
{
this.rawGroupIconInfo.ReadFromResource(refreshData);
}
this.dirty = true;
}
}
}
|