2022-04-14 18:39:18 +03:00
|
|
|
package model
|
2022-04-13 15:01:20 +03:00
|
|
|
|
|
|
|
import (
|
2022-04-18 11:36:29 +03:00
|
|
|
"encoding/json"
|
2022-04-13 15:01:20 +03:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2022-04-18 11:36:29 +03:00
|
|
|
"io/ioutil"
|
2022-04-15 16:27:51 +03:00
|
|
|
"log"
|
2022-04-13 15:01:20 +03:00
|
|
|
"os"
|
2022-04-15 16:27:51 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-04-13 15:01:20 +03:00
|
|
|
|
2022-04-18 13:13:38 +03:00
|
|
|
"github.com/cheggaaa/pb/v3"
|
2022-04-14 18:39:18 +03:00
|
|
|
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
|
|
|
|
)
|
2022-04-14 16:49:09 +03:00
|
|
|
|
|
|
|
func (e *HistoricalEvent) Name() string { return "" }
|
|
|
|
func (e *HistoricalEventCollection) Name() string { return "" }
|
2022-04-13 15:01:20 +03:00
|
|
|
|
2022-04-18 13:13:38 +03:00
|
|
|
func NewLegendsDecoder(file string) (*xml.Decoder, *os.File, *pb.ProgressBar, error) {
|
|
|
|
fi, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
size := fi.Size()
|
|
|
|
bar := pb.Full.Start64(size)
|
2022-04-18 11:36:29 +03:00
|
|
|
|
2022-04-13 15:01:20 +03:00
|
|
|
xmlFile, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Successfully Opened", file)
|
|
|
|
|
|
|
|
converter := util.NewConvertReader(xmlFile)
|
2022-04-18 13:13:38 +03:00
|
|
|
barReader := bar.NewProxyReader(converter)
|
|
|
|
d := xml.NewDecoder(barReader)
|
|
|
|
|
|
|
|
return d, xmlFile, bar, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(file string) (*DfWorld, error) {
|
|
|
|
InitSameFields()
|
|
|
|
|
|
|
|
d, xmlFile, bar, err := NewLegendsDecoder(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer xmlFile.Close()
|
2022-04-13 15:01:20 +03:00
|
|
|
|
2022-04-15 16:27:51 +03:00
|
|
|
var world *DfWorld
|
|
|
|
BaseLoop:
|
2022-04-13 15:01:20 +03:00
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
if t.Name.Local == "df_world" {
|
2022-04-15 16:27:51 +03:00
|
|
|
world, err = parseDfWorld(d, &t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break BaseLoop
|
2022-04-13 15:01:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 16:27:51 +03:00
|
|
|
|
2022-04-18 13:13:38 +03:00
|
|
|
bar.Finish()
|
|
|
|
|
2022-04-15 16:27:51 +03:00
|
|
|
plus := true
|
|
|
|
|
|
|
|
if plus {
|
|
|
|
file = strings.Replace(file, "-legends.xml", "-legends_plus.xml", 1)
|
2022-04-18 13:13:38 +03:00
|
|
|
|
|
|
|
d, xmlFile, bar, err := NewLegendsDecoder(file)
|
2022-04-15 16:27:51 +03:00
|
|
|
if err != nil {
|
2022-04-18 13:13:38 +03:00
|
|
|
return nil, err
|
2022-04-15 16:27:51 +03:00
|
|
|
}
|
|
|
|
defer xmlFile.Close()
|
|
|
|
|
|
|
|
PlusLoop:
|
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
if t.Name.Local == "df_world" {
|
|
|
|
world, err = parseDfWorldPlus(d, &t, world)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break PlusLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 13:13:38 +03:00
|
|
|
|
|
|
|
bar.Finish()
|
2022-04-15 16:27:51 +03:00
|
|
|
}
|
|
|
|
|
2022-04-18 11:36:29 +03:00
|
|
|
same, err := json.MarshalIndent(exportSameFields(), "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return world, err
|
|
|
|
}
|
|
|
|
ioutil.WriteFile("same.json", same, 0644)
|
|
|
|
|
2022-04-15 16:27:51 +03:00
|
|
|
return world, nil
|
2022-04-13 15:01:20 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 16:49:09 +03:00
|
|
|
func parseArray[T any](d *xml.Decoder, dest *[]T, creator func(*xml.Decoder, *xml.StartElement) (T, error)) {
|
2022-04-13 15:01:20 +03:00
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return // nil, err
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
2022-04-14 16:49:09 +03:00
|
|
|
x, _ := creator(d, &t)
|
2022-04-13 15:01:20 +03:00
|
|
|
*dest = append(*dest, x)
|
|
|
|
|
|
|
|
case xml.EndElement:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:49:09 +03:00
|
|
|
func parseMap[T Identifiable](d *xml.Decoder, dest *map[int]T, creator func(*xml.Decoder, *xml.StartElement) (T, error)) {
|
2022-04-13 15:01:20 +03:00
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return // nil, err
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
2022-04-14 16:49:09 +03:00
|
|
|
x, _ := creator(d, &t)
|
2022-04-13 15:01:20 +03:00
|
|
|
(*dest)[x.Id()] = x
|
|
|
|
|
|
|
|
case xml.EndElement:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 16:27:51 +03:00
|
|
|
|
|
|
|
func parseMapPlus[T Identifiable](d *xml.Decoder, dest *map[int]T, creator func(*xml.Decoder, *xml.StartElement, T) (T, error)) {
|
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
id, err := parseId(d)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
x, err := creator(d, &t, (*dest)[id])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
(*dest)[id] = x
|
|
|
|
|
|
|
|
case xml.EndElement:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func parseId(d *xml.Decoder) (int, error) {
|
|
|
|
var data []byte
|
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
switch t := tok.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
data = nil
|
|
|
|
if t.Name.Local != "id" {
|
|
|
|
d.Skip()
|
|
|
|
// return -1, fmt.Errorf("expected id at: %d", d.InputOffset())
|
|
|
|
}
|
|
|
|
|
|
|
|
case xml.CharData:
|
|
|
|
data = append(data, t...)
|
|
|
|
|
|
|
|
case xml.EndElement:
|
|
|
|
if t.Name.Local == "id" {
|
|
|
|
return strconv.Atoi(string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 11:36:29 +03:00
|
|
|
|
|
|
|
var sameFields map[string]map[string]map[string]bool
|
|
|
|
|
|
|
|
func exportSameFields() map[string]map[string]string {
|
|
|
|
export := make(map[string]map[string]string)
|
|
|
|
|
|
|
|
for objectType, v := range sameFields {
|
|
|
|
fields := make(map[string]string)
|
|
|
|
for field, v2 := range v {
|
|
|
|
c := 0
|
|
|
|
f := ""
|
|
|
|
for field2, same := range v2 {
|
|
|
|
if same {
|
|
|
|
c++
|
|
|
|
f = field2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c == 1 {
|
|
|
|
fields[field] = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(fields) > 0 {
|
|
|
|
export[objectType] = fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return export
|
|
|
|
}
|