This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
172 lines (162 loc) · 3.52 KB
/
main.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
// +build !js
package main
import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"github.com/anaseto/gruid"
"github.com/anaseto/gruid/ui"
)
func main() {
optVersion := flag.Bool("v", false, "print version number")
optNoAnim := flag.Bool("n", false, "no animations")
optReplay := flag.String("r", "", "path to replay file (_ means default location)")
optLogFile := flag.String("o", "", "log to output file")
opt16colors := new(bool)
opt256colors := new(bool)
optFullscreen := new(bool)
if Terminal {
opt16colors = flag.Bool("s", false, "use 16-color simple palette")
opt256colors = flag.Bool("x", false, "use xterm 256-color palette (solarized approximation)")
} else {
optFullscreen = flag.Bool("F", false, "fullscreen")
}
flag.Parse()
if *optVersion {
fmt.Println(Version)
os.Exit(0)
}
if *optNoAnim {
DisableAnimations = true
}
if runtime.GOOS != "windows" {
Xterm256Color = true
} else {
Xterm256Color = false
Only8Colors = true
}
if *opt256colors {
Xterm256Color = true
Only8Colors = false
} else if *opt16colors {
Xterm256Color = false
Only8Colors = false
}
err := initConfig()
if err != nil {
log.Print(err)
}
applyThemeConf()
initDriver(*optFullscreen)
if *optReplay != "" {
RunReplay(*optReplay)
} else {
RunGame(*optLogFile)
}
}
func RunGame(logfile string) {
gd := gruid.NewGrid(UIWidth, UIHeight)
m := &model{gd: gd, g: &game{}}
var repw io.WriteCloser
dir, err := DataDir()
defer func() {
if repw != nil {
repw.Close()
}
if m.finished && dir != "" {
RemoveSaveFile()
_, err := os.Stat(filepath.Join(dir, "replay.part"))
if err != nil {
log.Printf("no replay file: %v", err)
return
}
if err := os.Rename(filepath.Join(dir, "replay.part"), filepath.Join(dir, "replay")); err != nil {
log.Printf("writing replay file: %v", err)
}
}
}()
if err == nil {
replay, err := os.OpenFile(filepath.Join(dir, "replay.part"), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err == nil {
repw = replay
} else {
log.Printf("writing to replay file: %v", err)
}
} else {
log.Print(err)
}
if logfile != "" {
f, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Printf("opening log file: %v", err)
} else {
defer f.Close()
LogGame = true
log.SetOutput(f)
}
}
if !Tiles && !LogGame {
log.SetOutput(ioutil.Discard)
}
app := gruid.NewApp(gruid.AppConfig{
Driver: driver,
Model: m,
FrameWriter: repw,
})
err = app.Start(context.Background())
if !Tiles && !LogGame {
log.SetOutput(os.Stderr)
}
if err != nil {
log.Fatal(err)
}
}
func RunReplay(file string) {
if file == "_" {
dir, err := DataDir()
if err == nil {
file = filepath.Join(dir, "replay")
} else {
log.Print(err)
}
}
replay, err := os.Open(file)
if err != nil {
log.Fatalf("loading replay file: %v", err)
}
defer replay.Close()
fd, err := gruid.NewFrameDecoder(replay)
if err != nil {
log.Printf("frame decoder: %v", err)
}
gd := gruid.NewGrid(UIWidth, UIHeight)
rep := ui.NewReplay(ui.ReplayConfig{
Grid: gd,
FrameDecoder: fd,
})
app := gruid.NewApp(gruid.AppConfig{
Driver: driver,
Model: rep,
})
if err := app.Start(context.Background()); err != nil {
log.Fatal(err)
}
}
func subSig(ctx context.Context, msgs chan<- gruid.Msg) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sig)
select {
case <-ctx.Done():
case <-sig:
msgs <- gruid.MsgQuit{}
}
}