aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Decompile/DecompilerCore.cs
blob: 17c97e0988fe62a6d00da34dbd13e26183bfc48f (plain)
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
// 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
{
    using System;
    using System.Collections;
    using WixToolset.Data.WindowsInstaller;
    using WixToolset.Extensibility;
    using Wix = WixToolset.Data.Serialize;

    /// <summary>
    /// The base of the decompiler. Holds some variables used by the decompiler and extensions,
    /// as well as some utility methods.
    /// </summary>
    internal class DecompilerCore
    {
        private readonly Hashtable elements;
        private Wix.UI uiElement;

        /// <summary>
        /// Instantiate a new decompiler core.
        /// </summary>
        /// <param name="rootElement">The root element of the decompiled database.</param>
        /// <param name="messageHandler">The message handler.</param>
        internal DecompilerCore(Wix.IParentElement rootElement)
        {
            this.elements = new Hashtable();
            this.RootElement = rootElement;
        }

        /// <summary>
        /// Gets the root element of the decompiled output.
        /// </summary>
        /// <value>The root element of the decompiled output.</value>
        public Wix.IParentElement RootElement { get; }

        /// <summary>
        /// Gets the UI element.
        /// </summary>
        /// <value>The UI element.</value>
        public Wix.UI UIElement
        {
            get
            {
                if (null == this.uiElement)
                {
                    this.uiElement = new Wix.UI();
                    this.RootElement.AddChild(this.uiElement);
                }

                return this.uiElement;
            }
        }

        /// <summary>
        /// Verifies if a filename is a valid short filename.
        /// </summary>
        /// <param name="filename">Filename to verify.</param>
        /// <param name="allowWildcards">true if wildcards are allowed in the filename.</param>
        /// <returns>True if the filename is a valid short filename</returns>
        public virtual bool IsValidShortFilename(string filename, bool allowWildcards)
        {
            return false;
        }

        /// <summary>
        /// Convert an Int32 into a DateTime.
        /// </summary>
        /// <param name="value">The Int32 value.</param>
        /// <returns>The DateTime.</returns>
        public DateTime ConvertIntegerToDateTime(int value)
        {
            var date = value / 65536;
            var time = value % 65536;

            return new DateTime(1980 + (date / 512), (date % 512) / 32, date % 32, time / 2048, (time % 2048) / 32, (time % 32) * 2);
        }

        /// <summary>
        /// Gets the element corresponding to the row it came from.
        /// </summary>
        /// <param name="row">The row corresponding to the element.</param>
        /// <returns>The indexed element.</returns>
        public Wix.ISchemaElement GetIndexedElement(Row row)
        {
            return this.GetIndexedElement(row.TableDefinition.Name, row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter));
        }

        /// <summary>
        /// Gets the element corresponding to the primary key of the given table.
        /// </summary>
        /// <param name="table">The table corresponding to the element.</param>
        /// <param name="primaryKey">The primary key corresponding to the element.</param>
        /// <returns>The indexed element.</returns>
        public Wix.ISchemaElement GetIndexedElement(string table, params string[] primaryKey)
        {
            return (Wix.ISchemaElement)this.elements[String.Concat(table, ':', String.Join(DecompilerConstants.PrimaryKeyDelimiterString, primaryKey))];
        }

        /// <summary>
        /// Index an element by its corresponding row.
        /// </summary>
        /// <param name="row">The row corresponding to the element.</param>
        /// <param name="element">The element to index.</param>
        public void IndexElement(Row row, Wix.ISchemaElement element)
        {
            this.elements.Add(String.Concat(row.TableDefinition.Name, ':', row.GetPrimaryKey(DecompilerConstants.PrimaryKeyDelimiter)), element);
        }
    }
}