-
Notifications
You must be signed in to change notification settings - Fork 0
/
pml.go
146 lines (140 loc) · 3.69 KB
/
pml.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"embed"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"text/template"
)
//go:embed template/*.mac
var Templates embed.FS
const (
WRK_INPUT = "input.txt"
WRK_OUTPUT = "output.nwd"
WRK_E3DLOG = "e3d.log"
WRK_NAVLOG = "nav.log"
WRK_BAT = "makenwd.bat"
)
// MakePml makes a PML macro text from RvmBuilder
func MakePml(b *RvmBuilder) (string, error) {
var buf bytes.Buffer
if err := writeWithTemplate(&buf, "header.mac", map[string]any{
"wd": b.WorkingDir,
"log": filepath.Join(b.WorkingDir, WRK_E3DLOG),
}); err != nil {
return "", err
}
for _, v := range b.ExportRvms {
e := &ExportOption{
RvmType: "binary",
Encoding: "utfeight",
ImpliedTube: "separate",
Holes: "on",
Repr: "on",
ReprCl: "off",
ReprTube: "on",
ReprInsu: "off",
ReprObst: "off",
ReprLevel: 6,
ReprLevelPipe: 6,
ReprLevelNozz: 6,
ReprLevelStru: 6,
AutoColor: "on",
AutoColorDisplayExport: "on",
}
e.Merge(&b.ExportDefault)
e.Merge(&v)
if e.Filename == "" {
return "", errors.New("RVM has no filename")
}
if err := writeWithTemplate(&buf, "exportrvm.mac", map[string]any{
"file": filepath.Join(b.WorkingDir, e.Filename),
"text": e.RvmType == "text",
"note": strings.ReplaceAll(e.Filenote, "'", "''"),
"enc": e.Encoding,
"impl": e.ImpliedTube,
"holes": e.Holes,
"repr": e.Repr,
"cent": e.ReprCl,
"tube": e.ReprTube,
"insu": e.ReprInsu,
"obst": e.ReprObst,
"lv": e.ReprLevel,
"lvpipe": e.ReprLevelPipe,
"lvnozz": e.ReprLevelNozz,
"lvstru": e.ReprLevelStru,
"ac": e.AutoColor,
"acdisp": e.AutoColorDisplayExport,
"aclist": makeColorRule(e.AutoColorRules, e.GlobalTranslucency),
"incl": e.ExportInclude,
"excl": e.ExportExclude,
"input": filepath.Join(b.WorkingDir, WRK_INPUT),
}); err != nil {
return "", err
}
}
if b.DumpAtt {
if err := writeWithTemplate(&buf, "dumpatt.mac", map[string]any{
"file": filepath.Join(b.WorkingDir, b.DumpAttOption.Filename),
"exatt": b.DumpAttOption.AdditionalAtts,
"noval": b.DumpAttOption.HasNoData,
"uda": b.DumpAttOption.HasUda,
"refs": b.DumpAttOption.Selection,
}); err != nil {
return "", err
}
}
if err := writeWithTemplate(&buf, "footer.mac", map[string]any{
"bat": filepath.Join(b.WorkingDir, WRK_BAT),
"nav": b.FiletoolsTaskRunner,
"relin": WRK_INPUT,
"relout": WRK_OUTPUT,
"rellog": WRK_NAVLOG,
"ver": b.NwdVersion,
"out": filepath.Join(b.WorkingDir, WRK_OUTPUT),
"final": b.Filename,
}); err != nil {
return "", err
}
return buf.String(), nil
}
func writeWithTemplate(w io.Writer, n string, d map[string]any) error {
t, err := template.New(n).ParseFS(Templates, "template/"+n)
if err != nil {
return err
}
if err := t.Execute(w, d); err != nil {
return err
}
return nil
}
func makeColorRule(acr []AutoColorRule, gt int) string {
var buf bytes.Buffer
cols := map[string]int{}
for _, rule := range acr {
hex := strings.ToLower(rule.Color)
no, ok := cols[hex]
if !ok {
no = len(cols) + 1
cols[hex] = no
var r, g, b uint8
// Scan hex color
fmt.Sscanf(hex, "#%02x%02x%02x", &r, &g, &b)
fmt.Fprintf(&buf, "colour %d rgb %d, %d, %d\n", no, r, g, b)
}
trans := gt
if rule.Translucency != 0 {
trans = rule.Translucency
}
buf.WriteString("export autocolour " + rule.Selection)
fmt.Fprintf(&buf, " colour %d", no)
if trans != 0 {
fmt.Fprintf(&buf, " translucency %d", trans)
}
buf.WriteString("\n")
}
return strings.TrimSpace(buf.String())
}