mlp-server-tools/tools/convertsets/main.go

94 lines
2.4 KiB
Go

package main // import "git.fromouter.space/mcg/mlp-server-tools/tools/convertsets"
import (
"encoding/json"
"flag"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows/registry"
)
var setfilenames = map[string]string{
"Tokens": "tokens",
"Premiere": "pr",
"Promos": "promo",
"High Magic": "hm",
"Equestrian Odysseys": "eo",
"Marks in Time": "mt",
"Absolute Discord": "ad",
"Crystal Games": "cg",
"Rock and Rave": "rr",
"Celestial Solstice": "cs",
"Canterlot Nights": "cn",
"Defenders of Equestria": "de",
"Seaquestria and Beyond": "sb",
"Friends Forever": "ff",
"Sands in Time": "st",
"Villain Challenge": "villain",
}
func main() {
doc := getDocumentDir()
defaultSetPath := filepath.Join(doc, "OCTGN", "GameDatabase", "65656467-b709-43b2-a5c6-80c2f216adf9", "Sets")
setpath := flag.String("set-path", defaultSetPath, "Path to sets")
format := flag.Bool("format", false, "Format the output")
outdir := flag.String("out-dir", "setdata", "Output directory")
flag.Parse()
// Load sets
sets, err := loadSets(*setpath)
if err != nil {
log.Fatalf("Could not load all sets: %s\nClosing...\n", err.Error())
}
err = os.MkdirAll(*outdir, 0755)
if err != nil {
log.Fatalf("Error while creating output directory: %s\n", err.Error())
}
// Iterate for each set
for _, set := range sets {
// Get set shorthand
setname, ok := setfilenames[set.Name]
if !ok {
setname = strings.ToLower(set.Name)
}
// Convert to MCG's JSON format
setdata := convert(set)
// Create file
outpath := filepath.Join(*outdir, setname+".json")
file, err := os.Create(outpath)
if err != nil {
log.Fatalf("Error while creating output file for set \"%s\": %s\n", set.Name, err.Error())
}
enc := json.NewEncoder(file)
if *format {
enc.SetIndent("", " ")
}
err = enc.Encode(setdata)
if err != nil {
log.Fatalf("Error while encoding to JSON: %s\n", err.Error())
}
log.Printf("Saved converted set \"%s\" to: %s\n", set.Name, outpath)
}
}
func getDocumentDir() string {
k, err := registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders`, registry.QUERY_VALUE)
if err != nil {
return ""
}
defer k.Close()
s, _, err := k.GetStringValue("Personal")
if err != nil {
return ""
}
return s
}