/
generator.go
137 lines (118 loc) · 2.79 KB
/
generator.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
// BSD 3-Clause License in LICENSE file at the project root.
// Copyright (c) 2020, Hugues GUILLEUS
// All rights reserved.
package blache
import (
"bytes"
"encoding/json"
"fmt"
"github.com/HuguesGuilleus/blache/pkg/cpumutex"
"github.com/HuguesGuilleus/blache/web"
"image"
"image/png"
"io/fs"
"path"
"sort"
"sync"
)
type generator struct {
Option
cpu cpumutex.M
wg sync.WaitGroup
bar bar
// All the region coords.
allRegion []string
}
func (option Option) Gen() {
if option.Error == nil {
option.Error = func(error) {}
}
g := generator{Option: option}
g.cpu.Init(option.CPU)
if err := g.initOutput(); err != nil {
g.Error(err)
return
}
if !option.NoBar {
go g.bar.Start()
defer g.bar.Finish()
}
var (
root string
files []fs.DirEntry
err error
)
for _, p := range []string{"world/region", "region", "."} {
files, err = fs.ReadDir(option.In, p)
if err == nil {
root = p
break
}
}
if err != nil {
option.Error(fmt.Errorf("Read directory fail: %w", err))
return
}
g.bar.Total += (32*32 + 1) * int64(len(files))
g.cpu.Lock()
for _, f := range files {
n := path.Join(root, f.Name())
data, err := fs.ReadFile(option.In, n)
if err != nil {
option.Error(fmt.Errorf("Fail to read %q: %w", n, err))
break
}
x, z := 0, 0
if _, err := fmt.Sscanf(f.Name(), "r.%d.%d.mca", &x, &z); err != nil {
g.Error(fmt.Errorf("Error when read X end Z from file name %q: %w", f.Name(), err))
continue
}
if len(data) < 32*32*4 {
g.bar.Add(32*32 + 1)
continue
}
g.wg.Add(1)
go parseRegion(&g, x, z, data)
g.allRegion = append(g.allRegion, fmt.Sprintf("%d,%d", x, z))
}
g.cpu.Unlock()
g.wg.Wait()
g.saveRegionsList()
}
// Write directory and assets.
func (g *generator) initOutput() error {
for _, d := range [...]string{"bloc", "biome", "height", "structs"} {
if err := g.Out.MkdirAll(d); err != nil {
return fmt.Errorf("Write directory %q fail: %w", d, err)
}
}
for _, f := range web.List {
if err := g.Out.Create("", f.Name, f.Data); err != nil {
return fmt.Errorf("Fail to write web file %q: %v", f.Name, err)
}
}
return nil
}
// Save all the processed region coordonates into regions.json
func (g *generator) saveRegionsList() {
sort.Strings(g.allRegion)
data, err := json.Marshal(g.allRegion)
if err != nil {
g.Error(fmt.Errorf("Generated JSON regions fail: %w", err))
return
}
if err := g.Out.Create("", "regions.json", data); err != nil {
g.Error(fmt.Errorf("Write regions.json fail: %w", err))
}
}
// Save an image of one region.
func (g *generator) saveImage(dir, f string, img *image.RGBA) {
g.cpu.Lock()
defer g.cpu.Unlock()
defer g.wg.Done()
buff := bytes.Buffer{}
png.Encode(&buff, img)
if err := g.Out.Create(dir, f, buff.Bytes()); err != nil {
g.Error(fmt.Errorf("save image error: %v", err))
}
}