/
winmd.go
61 lines (50 loc) · 1.56 KB
/
winmd.go
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
package winmd
import (
"bytes"
"debug/pe"
"embed"
"io/fs"
"io/ioutil"
"github.com/tdakkota/win32metadata/types"
)
// Custom Attributes
const (
AttributeTypeGUID = "Windows.Foundation.Metadata.GuidAttribute"
AttributeTypeExclusiveTo = "Windows.Foundation.Metadata.ExclusiveToAttribute"
AttributeTypeStaticAttribute = "Windows.Foundation.Metadata.StaticAttribute"
AttributeTypeActivatableAttribute = "Windows.Foundation.Metadata.ActivatableAttribute"
AttributeTypeDefaultAttribute = "Windows.Foundation.Metadata.DefaultAttribute"
AttributeTypeOverloadAttribute = "Windows.Foundation.Metadata.OverloadAttribute"
)
// HasContext is a helper struct that holds the original context of a metadata element.
type HasContext struct {
originalCtx *types.Context
}
// Ctx return the original context of the element.
func (hctx *HasContext) Ctx() *types.Context {
return hctx.originalCtx
}
//go:embed metadata/*.winmd
var files embed.FS
// allFiles returns all winmd files embedded in the binary.
func allFiles() ([]fs.DirEntry, error) {
return files.ReadDir("metadata")
}
// open reads the given file and returns a pe.File instance.
// The user should close the returned instance once he is done working with it.
func open(path string) (*pe.File, error) {
f, err := files.Open("metadata/" + path)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
pef, err := pe.NewFile(bytes.NewReader(data))
if err != nil {
return nil, err
}
return pef, nil
}