@@ -4,23 +4,27 @@ import (
4
4
"encoding/json"
5
5
"fmt"
6
6
"io/ioutil"
7
+ "os"
7
8
"path/filepath"
8
9
"strings"
9
10
10
11
"github.com/BigJk/snd"
11
12
)
12
13
13
14
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 {
24
28
Name string `json:"name"`
25
29
Label string `json:"label"`
26
30
Package string `json:"package"`
@@ -40,7 +44,6 @@ type PackEntry struct {
40
44
Permission struct {
41
45
Default int `json:"default"`
42
46
} `json:"permission"`
43
- Data map [string ]interface {} `json:"data"`
44
47
Flags struct {
45
48
} `json:"flags"`
46
49
Type string `json:"type"`
@@ -49,12 +52,46 @@ type PackEntry struct {
49
52
50
53
// ConvertPackEntries converts a .db FoundryVTT file to S&D entries.
51
54
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
+
52
80
packBytes , err := ioutil .ReadFile (packFile )
53
81
if err != nil {
54
82
return nil , err
55
83
}
56
84
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
+ }
58
95
59
96
var entries []snd.Entry
60
97
for i := range packLines {
@@ -63,27 +100,35 @@ func ConvertPackEntries(packFile string) ([]snd.Entry, error) {
63
100
}
64
101
65
102
pack := PackEntry {}
103
+ packRaw := map [string ]any {}
66
104
if err := json .Unmarshal ([]byte (packLines [i ]), & pack ); err != nil {
67
105
return nil , err
68
106
}
69
-
70
- if pack .Data == nil {
71
- continue
107
+ if err := json .Unmarshal ([]byte (packLines [i ]), & packRaw ); err != nil {
108
+ return nil , err
72
109
}
73
110
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 {}{
76
114
"ID" : pack .ID ,
77
115
"Img" : pack .Img ,
78
116
"Permission" : pack .Permission ,
79
117
"Flags" : pack .Flags ,
80
118
"Type" : pack .Type ,
81
119
}
82
120
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
+
83
128
entries = append (entries , snd.Entry {
84
129
ID : pack .ID ,
85
130
Name : pack .Name ,
86
- Data : pack . Data ,
131
+ Data : data ,
87
132
})
88
133
}
89
134
@@ -112,9 +157,19 @@ func ConvertDataSources(moduleFile string) ([]snd.DataSource, [][]snd.Entry, err
112
157
author = strings .Join (mod .Author .([]string ), ", " )
113
158
}
114
159
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
+
115
168
var sources []snd.DataSource
116
169
var sourceEntries [][]snd.Entry
117
170
for i := range mod .Packs {
171
+ fmt .Println (mod .Packs [i ].Path )
172
+
118
173
entries , err := ConvertPackEntries (filepath .Join (filepath .Dir (moduleFile ), "/" , mod .Packs [i ].Path ))
119
174
if err != nil {
120
175
return nil , nil , err
0 commit comments