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