Files
ordoor/internal/sets/sets.go

146 lines
2.9 KiB
Go
Raw Normal View History

2018-03-18 05:34:14 +00:00
package sets
import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"ur.gs/chaos-gate/internal/util/asciiscan"
)
type MapSet struct {
Description string
2018-03-18 17:27:32 +00:00
SurfaceCount int
LeftCount int
RightCount int
CenterCount int
2018-03-18 05:34:14 +00:00
// TODO: is there any more structure than this? Should I preserve empty lines?
2018-03-18 17:27:32 +00:00
SurfacePalette []string
LeftPalette []string
RightPalette []string
CenterPalette []string
2018-03-18 05:34:14 +00:00
}
func LoadSets(dir string) (map[string]*MapSet, error) {
2018-03-18 05:34:14 +00:00
fis, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
out := make(map[string]*MapSet, len(fis))
2018-03-18 05:34:14 +00:00
for _, fi := range fis {
filename := filepath.Join(dir, fi.Name())
basename := filepath.Base(filename)
extname := filepath.Ext(filename)
// Don't try to load non-.obj files
if !strings.EqualFold(extname, ".set") {
continue
}
obj, err := LoadSet(filename)
if err != nil {
return nil, fmt.Errorf("%s: %v", filename, err)
}
out[basename] = obj
}
return out, nil
}
func LoadSet(filename string) (*MapSet, error) {
out := &MapSet{}
2018-03-18 05:34:14 +00:00
s, err := asciiscan.New(filename)
if err != nil {
return nil, err
2018-03-18 05:34:14 +00:00
}
defer s.Close()
out.Description, err = s.ConsumeString()
if err != nil {
return nil, err
2018-03-18 05:34:14 +00:00
}
2018-03-18 17:27:32 +00:00
if err := consumeDefs(s, out); err != nil {
return nil, err
2018-03-18 05:34:14 +00:00
}
2018-03-18 17:27:32 +00:00
if err := consumePalette(s, out.SurfaceCount, &out.SurfacePalette); err != nil {
return nil, fmt.Errorf("Failed to read surface palette: %v", err)
}
if err := consumePalette(s, out.LeftCount, &out.LeftPalette); err != nil {
return nil, fmt.Errorf("Failed to read left palette: %v", err)
}
if err := consumePalette(s, out.RightCount, &out.RightPalette); err != nil {
return nil, fmt.Errorf("Failed to read right palette: %v", err)
}
if err := consumePalette(s, out.CenterCount, &out.CenterPalette); err != nil {
return nil, fmt.Errorf("Failed to read center palette: %v", err)
2018-03-18 05:34:14 +00:00
}
return out, nil
}
2018-03-18 17:27:32 +00:00
func consumeDefs(scanner *asciiscan.Scanner, in *MapSet) error {
2018-03-18 05:34:14 +00:00
expectDefs, err := scanner.ConsumeString()
if err != nil {
2018-03-18 17:27:32 +00:00
return err
2018-03-18 05:34:14 +00:00
}
if expectDefs != "Defs" {
2018-03-18 17:27:32 +00:00
return fmt.Errorf("Couldn't find Defs section")
2018-03-18 05:34:14 +00:00
}
defs, err := scanner.ConsumeString()
if err != nil {
2018-03-18 17:27:32 +00:00
return err
2018-03-18 05:34:14 +00:00
}
parts := strings.SplitN(defs, " ", -1)
if len(parts) != 4 {
2018-03-18 17:27:32 +00:00
return fmt.Errorf("Defs section did not have 4 components")
2018-03-18 05:34:14 +00:00
}
2018-03-18 17:27:32 +00:00
ints := make([]int, 4)
2018-03-18 05:34:14 +00:00
for i, part := range parts {
n, err := strconv.ParseInt(part, 10, 8)
if err != nil {
2018-03-18 17:27:32 +00:00
return err
2018-03-18 05:34:14 +00:00
}
2018-03-18 17:27:32 +00:00
ints[i] = int(n) // safe as we specify 8 bits to ParseInt
2018-03-18 05:34:14 +00:00
}
2018-03-18 17:27:32 +00:00
in.SurfaceCount = ints[0]
in.LeftCount = ints[1]
in.RightCount = ints[2]
in.CenterCount = ints[3]
return nil
}
func consumePalette(scanner *asciiscan.Scanner, n int, in *[]string) error {
out := make([]string, 0, n)
for i := 0; i < n; i++ {
str, err := scanner.ConsumeString()
if err != nil {
return err // EOF is bad: the number of entries is explicit
}
out = append(out, str)
}
*in = out
return nil
2018-03-18 05:34:14 +00:00
}