-
Notifications
You must be signed in to change notification settings - Fork 4
/
level_helpers.go
195 lines (176 loc) · 4.88 KB
/
level_helpers.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
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// LevelJSON is used to write the list of levels
type LevelJSON struct {
Name string `json:"name"`
Author string `json:"author"`
AuthorAccountID string `json:"account_id"`
LevelUUID string `json:"level_uuid"`
Date int64 `json:"date"`
PublishState int `json:"publish_state"`
Experimental bool `json:"experimental"`
LeaderboardKind int `json:"leaderboard_kind"`
Version int `json:"v"`
Difficulty int `json:"diff"`
Featured bool `json:"featured"`
}
// LevelManifest stores the manifest of a level.
type LevelManifest struct {
Name string `json:"name"`
Description string `json:"description"`
EntryPoint string `json:"entry_point"`
HasScoreLeaderboard bool `json:"has_score_leaderboard"`
}
// GetDir returns the path to the levels directory.
func GetDir() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
dir, err := filepath.Abs(cwd)
if err != nil {
panic(err)
}
dir = dir + string(os.PathSeparator) + "levels"
return dir
}
// ListLevels lists the levels.
// A level is a subdirectory that contains a properly formatted json file.
func ListLevels(dir string) []LevelJSON {
levels := []LevelJSON{}
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if the file is a symlink.
file_info, stat_err := os.Lstat(path)
if stat_err != nil {
log.Print("Failed to os.Lstat")
return stat_err
}
// log.Print("path: " + path + ", file_info.Mode():" + file_info.Mode().String())
fileIsASymLink := file_info.Mode()&os.ModeSymlink != 0
// If the file is a symlink, recursively walk it.
// TODO: prevent infinite loops.
if fileIsASymLink {
link, read_link_err := os.Readlink(path)
if read_link_err != nil {
log.Print("Failed to os.ReadLink")
return read_link_err
}
log.Print("followed sym link to " + link)
sub_levels := ListLevels(link)
levels = append(levels, sub_levels...)
return nil
}
directory, filename := filepath.Split(path)
if filename != "manifest.json" {
return nil
}
jsonContent, err := os.ReadFile(path)
if err != nil {
log.Print("Failed to read file at path " + path)
return nil
}
levelManifest := &LevelManifest{}
err = json.Unmarshal(jsonContent, levelManifest)
if err != nil {
log.Print("Failed to parse " + path)
log.Print("Error: " + err.Error())
// Do not return an error, otherwise a single malformed manifest
// will prevent the parsing of all the other manifests.
return nil
}
directory = strings.ReplaceAll(directory, string(os.PathSeparator), "/")
level := LevelJSON{
Name: levelManifest.Name,
Author: "Anonymous",
LevelUUID: directory,
Date: 0,
Experimental: true,
PublishState: 0,
LeaderboardKind: 0,
Version: 0,
}
if levelManifest.HasScoreLeaderboard {
level.LeaderboardKind = 1
}
levels = append(levels, level)
return nil
})
if err != nil {
log.Print("Failed to list levels: " + err.Error())
}
return levels
}
func canRead(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
// GetLevelData returns a zip containing the level.
// levelID is the path to the directory containing the level.
func GetLevelData(levelID string, disableFilter bool) (bytes.Buffer, error) {
var buffer bytes.Buffer
if !canRead(levelID) {
return buffer, errors.New("Can't read path")
}
zipWriter := zip.NewWriter(&buffer)
defer zipWriter.Close()
err := filepath.Walk(levelID,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !disableFilter && filepath.Ext(path) != ".lua" {
return nil
}
log.Print(path)
path = strings.ReplaceAll(path, string(os.PathSeparator), "/")
var header zip.FileHeader
header.Name = "level/" + strings.TrimPrefix(path, levelID)
header.Method = zip.Deflate
header.Modified = time.Now()
zipFileWriter, err := zipWriter.CreateHeader(&header)
if err != nil {
log.Print("Failed to create header")
return err
}
fileReader, err := os.Open(path)
if err != nil {
log.Print("Failed to create reader")
return err
}
defer fileReader.Close()
_, err = io.Copy(zipFileWriter, fileReader)
if err != nil {
log.Print("Failed to copy file:" + err.Error())
return err
}
return nil
})
if err != nil {
return buffer, err
}
// Tells `zipWriter` to finish writing the zip into `buffer`.
zipWriter.Close()
return buffer, nil
}
// GetLevelManifest returns the manifest of the level.
func GetLevelManifest(levelID string) ([]byte, error) {
return os.ReadFile(levelID + "manifest.json")
}