Skip to content

Commit c09f1d3

Browse files
committed
fix: foundryvtt handling
1 parent 2646b96 commit c09f1d3

File tree

1 file changed

+73
-18
lines changed

1 file changed

+73
-18
lines changed

imexport/vtt/vtt.go

+73-18
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import (
44
"encoding/json"
55
"fmt"
66
"io/ioutil"
7+
"os"
78
"path/filepath"
89
"strings"
910

1011
"github.com/BigJk/snd"
1112
)
1213

1314
type Module struct {
14-
Name string `json:"name"`
15-
Title string `json:"title"`
16-
Description string `json:"description"`
17-
Version string `json:"version"`
18-
Systems []string `json:"systems"`
19-
Author interface{} `json:"author"`
20-
Scripts []interface{} `json:"scripts"`
21-
Esmodules []interface{} `json:"esmodules"`
22-
Styles []interface{} `json:"styles"`
23-
Packs []struct {
15+
Name string `json:"name"`
16+
Title string `json:"title"`
17+
Description string `json:"description"`
18+
Version string `json:"version"`
19+
Systems []string `json:"systems"`
20+
Author interface{} `json:"author"`
21+
Authors []struct {
22+
Name string `json:"name"`
23+
} `json:"authors"`
24+
Scripts []interface{} `json:"scripts"`
25+
Esmodules []interface{} `json:"esmodules"`
26+
Styles []interface{} `json:"styles"`
27+
Packs []struct {
2428
Name string `json:"name"`
2529
Label string `json:"label"`
2630
Package string `json:"package"`
@@ -40,7 +44,6 @@ type PackEntry struct {
4044
Permission struct {
4145
Default int `json:"default"`
4246
} `json:"permission"`
43-
Data map[string]interface{} `json:"data"`
4447
Flags struct {
4548
} `json:"flags"`
4649
Type string `json:"type"`
@@ -49,12 +52,46 @@ type PackEntry struct {
4952

5053
// ConvertPackEntries converts a .db FoundryVTT file to S&D entries.
5154
func ConvertPackEntries(packFile string) ([]snd.Entry, error) {
55+
// Check if packFile is a directory
56+
fi, err := os.Stat(packFile)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
if fi.IsDir() {
62+
var entries []snd.Entry
63+
return entries, filepath.Walk(packFile, func(path string, info os.FileInfo, err error) error {
64+
if filepath.Ext(path) != ".json" || filepath.Base(path)[0] == '_' {
65+
return nil
66+
}
67+
68+
fmt.Println(path)
69+
70+
e, err := ConvertPackEntries(path)
71+
if err == nil {
72+
entries = append(entries, e...)
73+
} else {
74+
fmt.Println("error while converting pack entries:", err)
75+
}
76+
return nil
77+
})
78+
}
79+
5280
packBytes, err := ioutil.ReadFile(packFile)
5381
if err != nil {
5482
return nil, err
5583
}
5684

57-
packLines := strings.Split(string(packBytes), "\n")
85+
packLines := []string{string(packBytes)}
86+
87+
// Check if each line is it's own json object
88+
split := strings.Split(string(packBytes), "\n")
89+
if len(split) >= 1 {
90+
test := map[string]any{}
91+
if err := json.Unmarshal([]byte(split[0]), &test); err == nil {
92+
packLines = split
93+
}
94+
}
5895

5996
var entries []snd.Entry
6097
for i := range packLines {
@@ -63,27 +100,35 @@ func ConvertPackEntries(packFile string) ([]snd.Entry, error) {
63100
}
64101

65102
pack := PackEntry{}
103+
packRaw := map[string]any{}
66104
if err := json.Unmarshal([]byte(packLines[i]), &pack); err != nil {
67105
return nil, err
68106
}
69-
70-
if pack.Data == nil {
71-
continue
107+
if err := json.Unmarshal([]byte(packLines[i]), &packRaw); err != nil {
108+
return nil, err
72109
}
73110

74-
pack.Data["name"] = pack.Name
75-
pack.Data["vtt_meta"] = map[string]interface{}{
111+
data := map[string]any{}
112+
data["name"] = pack.Name
113+
data["vtt_meta"] = map[string]interface{}{
76114
"ID": pack.ID,
77115
"Img": pack.Img,
78116
"Permission": pack.Permission,
79117
"Flags": pack.Flags,
80118
"Type": pack.Type,
81119
}
82120

121+
for k, v := range packRaw {
122+
if k == "_id" || k == "name" || k == "permission" || k == "flags" || k == "type" || k == "img" {
123+
continue
124+
}
125+
data[k] = v
126+
}
127+
83128
entries = append(entries, snd.Entry{
84129
ID: pack.ID,
85130
Name: pack.Name,
86-
Data: pack.Data,
131+
Data: data,
87132
})
88133
}
89134

@@ -112,9 +157,19 @@ func ConvertDataSources(moduleFile string) ([]snd.DataSource, [][]snd.Entry, err
112157
author = strings.Join(mod.Author.([]string), ", ")
113158
}
114159

160+
if len(mod.Authors) > 0 {
161+
var authors []string
162+
for i := range mod.Authors {
163+
authors = append(authors, mod.Authors[i].Name)
164+
}
165+
author = strings.Join(authors, ", ")
166+
}
167+
115168
var sources []snd.DataSource
116169
var sourceEntries [][]snd.Entry
117170
for i := range mod.Packs {
171+
fmt.Println(mod.Packs[i].Path)
172+
118173
entries, err := ConvertPackEntries(filepath.Join(filepath.Dir(moduleFile), "/", mod.Packs[i].Path))
119174
if err != nil {
120175
return nil, nil, err

0 commit comments

Comments
 (0)