/
render.go
235 lines (208 loc) · 7 KB
/
render.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/Palats/mapshot/embed"
"github.com/Palats/mapshot/factorio"
"github.com/golang/glog"
"github.com/google/uuid"
"github.com/otiai10/copy"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// RenderFlags holds parameters to the rendering.
type RenderFlags struct {
area string
tilemin int64
tilemax int64
prefix string
resolution int64
jpgquality int64
minjpgquality int64
surface string
}
// Register creates flags for the rendering parameters.
func (rf *RenderFlags) Register(flags *pflag.FlagSet, prefix string) *RenderFlags {
flags.StringVar(&rf.area, prefix+"area", "", "How to pick the area to render. all=all existing chunks; entities=chunks including artifical build. If empty, use value from the game.")
flags.Int64Var(&rf.tilemin, prefix+"tilemin", 0, "Size in in-game units of a tile for the most zoomed layer. If 0, use value from the game.")
flags.Int64Var(&rf.tilemax, prefix+"tilemax", 0, "Size in in-game units of a tile for the least zoomed layer. If 0, use value from the game.")
flags.StringVar(&rf.prefix, prefix+"prefix", "", "Prefix to add to all generated filenames. If empty, use value from the game.")
flags.Int64Var(&rf.resolution, prefix+"resolution", 0, "Pixel size for generated tiles. If 0, use value from the game.")
flags.Int64Var(&rf.jpgquality, prefix+"jpgquality", 0, "Compression quality for jpg files. If 0, use value from the game.")
flags.Int64Var(&rf.minjpgquality, prefix+"minjpgquality", -1, "Compression quality for jpg files when no player entities are present. Set to 0 to skip the tile entirely.")
flags.StringVar(&rf.surface, prefix+"surface", "", "Game surface to render. If empty, use value from the game. Use _all_ for render all surfaces (default behavior).")
return rf
}
func (rf *RenderFlags) genOverrides() map[string]interface{} {
ov := map[string]interface{}{}
if rf.area != "" {
ov["area"] = rf.area
}
if rf.tilemin != 0 {
ov["tilemin"] = rf.tilemin
}
if rf.tilemax != 0 {
ov["tilemax"] = rf.tilemax
}
if rf.prefix != "" {
ov["prefix"] = rf.prefix
}
if rf.resolution != 0 {
ov["resolution"] = rf.resolution
}
if rf.jpgquality != 0 {
ov["jpgquality"] = rf.jpgquality
}
if rf.jpgquality != -1 {
ov["minjpgquality"] = rf.minjpgquality
}
if rf.surface != "" {
ov["surface"] = rf.surface
}
return ov
}
func copyMod(dstMapshot string) error {
if err := os.MkdirAll(dstMapshot, 0755); err != nil {
return fmt.Errorf("unable to create dir %q: %w", dstMapshot, err)
}
for name, content := range embed.ModFiles {
dst := filepath.Join(dstMapshot, name)
if err := ioutil.WriteFile(dst, []byte(content), 0644); err != nil {
return fmt.Errorf("unable to write file %q: %w", dst, err)
}
}
return nil
}
func writeOverrides(data map[string]interface{}, dstPath string) error {
inline, err := json.Marshal(data)
if err != nil {
return err
}
overrides := "return [===[" + string(inline) + "]===]\n"
overridesFilename := filepath.Join(dstPath, "overrides.lua")
if err := ioutil.WriteFile(overridesFilename, []byte(overrides), 0644); err != nil {
return fmt.Errorf("unable to write overrides file %q: %w", overridesFilename, err)
}
glog.Infof("overrides file created at %q", overridesFilename)
return nil
}
func render(ctx context.Context, factorioSettings *factorio.Settings, rf *RenderFlags, rawname string) error {
fact, err := factorio.New(factorioSettings)
if err != nil {
return err
}
runID := uuid.New().String()
glog.Infof("runid: %s", runID)
// The parameter can be a filename, so extract a name.
name := filepath.Base(rawname)
name = name[:len(name)-len(filepath.Ext(name))]
tmpdir, cleanup := getWorkDir()
defer cleanup()
// Copy game save
srcSavegame, err := fact.FindSaveFile(rawname)
if err != nil {
return fmt.Errorf("unable to find savegame %q: %w", rawname, err)
}
fmt.Printf("Generating mapshot %q using file %s\n", name, srcSavegame)
dstSavegame := filepath.Join(tmpdir, name+".zip")
if err := copy.Copy(srcSavegame, dstSavegame); err != nil {
return fmt.Errorf("unable to copy file %q: %w", srcSavegame, err)
}
glog.Infof("copied save from %q to %q", srcSavegame, dstSavegame)
// Copy mods
dstMods := filepath.Join(tmpdir, "mods")
if err := fact.CopyMods(dstMods, []string{"mapshot"}); err != nil {
return err
}
// Add the mod itself.
dstMapshot := filepath.Join(dstMods, "mapshot")
if err := copyMod(dstMapshot); err != nil {
return err
}
if err := factorio.EnableMod(dstMods, "mapshot"); err != nil {
return err
}
glog.Infof("mod created at %q", dstMapshot)
// Generates overrides to the parameters. This is done by creating a Lua
// file, as mods don't have any way of loading data.
overridesData := rf.genOverrides()
overridesData["onstartup"] = runID
overridesData["savename"] = name
if err := writeOverrides(overridesData, dstMapshot); err != nil {
return err
}
// Remove done marker if still present
doneFile := filepath.Join(fact.ScriptOutput(), "mapshot-done-"+runID)
err = os.Remove(doneFile)
glog.Infof("removed done-file %q: %v", doneFile, err)
factorioArgs := []string{
"--disable-audio",
"--load-game", dstSavegame,
"--mod-directory", dstMods,
}
execCtx, cancel := context.WithCancel(ctx)
defer cancel()
errCh := make(chan error)
fmt.Println("Starting Factorio...")
go func() {
errCh <- fact.Run(execCtx, factorioArgs)
}()
// Wait for the `done` file to be created, indicating that the work is
// done.
for {
_, err := os.Stat(doneFile)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unable to stat file %q: %w", doneFile, err)
}
if err == nil {
cancel()
break
}
// Context cancellation should terminate Factorio, which is detected
// through errCh, so no need to wait on context.
select {
case <-time.After(time.Second):
case err := <-errCh:
if err == nil {
return errors.New("factorio exited early")
}
return fmt.Errorf("factorio exited early: %w", err)
}
}
glog.Infof("done file %q now exists", doneFile)
rawDone, err := ioutil.ReadFile(doneFile)
if err != nil {
return fmt.Errorf("unable to read file %q: %w", doneFile, err)
}
resultPrefix := string(rawDone)
glog.Infof("output at %s", resultPrefix)
fmt.Println("Output:", filepath.Join(fact.ScriptOutput(), resultPrefix))
// Cleaning up done file now that we've read it.
err = os.Remove(doneFile)
glog.Infof("removed done-file %q: %v", doneFile, err)
// Wait for Factorio to terminate.
err = <-errCh
if err != nil {
glog.Warningf("Factorio finished with an error; ignoring as rendering was done. Error: %v", err)
}
return nil
}
var cmdRender = &cobra.Command{
Use: "render",
Short: "Create a screenshot from a save.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return render(cmd.Context(), factorioSettings, renderFlags, args[0])
},
}
var renderFlags = &RenderFlags{}
func init() {
renderFlags.Register(cmdRender.PersistentFlags(), "")
cmdRoot.AddCommand(cmdRender)
}