nori/decode.go

49 lines
976 B
Go
Raw Normal View History

2022-03-25 21:00:43 +00:00
package nori
import (
2023-01-05 06:41:21 +00:00
"bytes"
"fmt"
2022-03-25 21:00:43 +00:00
"io"
"os"
2023-01-05 07:08:48 +00:00
"github.com/spf13/afero"
"github.com/suapapa/go_hangul/encoding/cp949"
2022-03-25 21:00:43 +00:00
)
func New() *Nori {
return &Nori{}
}
func FromFile(fp string) (*Nori, error) {
file, err := os.Open(fp)
if err != nil {
return nil, err
}
defer file.Close()
2023-01-05 07:08:48 +00:00
fs := afero.NewMemMapFs()
f, _ := fs.OpenFile("_", os.O_TRUNC|os.O_CREATE, 0o740)
io.Copy(f, file)
f.Seek(0, io.SeekStart)
return Decode(f)
2022-03-25 21:00:43 +00:00
}
2023-01-05 07:08:48 +00:00
func Decode(r io.ReadSeeker) (*Nori, error) {
2022-03-25 21:00:43 +00:00
nori := New()
err := nori.Decode(r)
return nori, err
}
2023-01-05 06:41:21 +00:00
func GetAnimationNames(n *Nori) (map[string]int, error) {
names := make(map[string]int, n.AnimationCount)
for i := range n.Animations {
anim := n.Animations[i]
title, codepageError := cp949.From(anim.Title[:])
if codepageError != nil {
return nil, fmt.Errorf("couldn't convert title of animation %d", i)
}
cleanTitle := string(bytes.SplitN(title[:], []byte{0}, 2)[0])
names[cleanTitle] = i
}
return names, nil
}