Skip to content

Commit e3f2697

Browse files
committed
Expose package installed/action lua in struct
1 parent 1ca80e3 commit e3f2697

3 files changed

Lines changed: 69 additions & 27 deletions

File tree

common/package.go

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,30 @@ import (
2626
)
2727

2828
type Package struct {
29-
ID int `json:"id"`
30-
Revision int `json:"rev"`
31-
Type string `json:"type"`
32-
Name string `json:"name"`
33-
Dataname string `json:"dataname,omitempty"`
29+
ID int `json:"id"`
30+
Revision int `json:"rev"`
31+
Type string `json:"type"`
32+
Name string `json:"name"`
33+
Dataname string `json:"dataname,omitempty"`
34+
Content []Content `json:"content,omitempty"`
35+
Includes []Include `json:"includes,omitempty"`
36+
Data []byte `json:"data,omitempty"`
37+
38+
// only used by Install packages
39+
40+
LuaMenuInstalled string `json:"-"`
41+
LuaMenuAction string `json:"-"`
42+
LuaClientInstalled string `json:"-"`
43+
LuaClientAction string `json:"-"`
44+
LuaServerInstalled string `json:"-"`
45+
LuaServerAction string `json:"-"`
46+
47+
// metadata
48+
3449
Author string `json:"author,omitempty"`
3550
AuthorName string `json:"authorname,omitempty"`
3651
AuthorIcon string `json:"authoricon,omitempty"`
3752
Description string `json:"description,omitempty"`
38-
Data []byte `json:"data,omitempty"`
39-
Content []Content `json:"content,omitempty"`
40-
Includes []Include `json:"includes,omitempty"`
4153
Uploaded time.Time `json:"uploaded,omitempty"`
4254

4355
Downloads int `json:"downloads,omitempty"`
@@ -47,10 +59,11 @@ type Package struct {
4759
}
4860

4961
type Content struct {
50-
ID int `json:"id"`
51-
Path string `json:"path"`
52-
Size int `json:"size"`
53-
PSize int `json:"psize"`
62+
ID int `json:"id"`
63+
Revision int `json:"rev"` // not stored by cloudbox, always 1
64+
Path string `json:"path"`
65+
Size int `json:"size"` // raw size
66+
PSize int `json:"psize"` // compressed size
5467
}
5568

5669
type Include struct {
@@ -59,7 +72,7 @@ type Include struct {
5972
Type string `json:"type"`
6073
}
6174

62-
func (pkg Package) Marshal() []byte {
75+
func (pkg Package) Marshal(install bool) []byte {
6376
script := make(VDF)
6477

6578
script["scriptid"] = pkg.ID
@@ -68,11 +81,26 @@ func (pkg Package) Marshal() []byte {
6881
script["dataname"] = pkg.Dataname
6982
script["name"] = pkg.Name
7083

71-
// maps have extra stuff
72-
if pkg.Type == "map" {
73-
script["uid"] = fmt.Sprintf("map_%d", pkg.ID)
74-
script["luamenu_installed"] = "OnMapDownloaded();"
75-
script["luamenu_action"] = fmt.Sprintf("OnMapSelected('%s');", pkg.RealMapName())
84+
if install {
85+
script["uid"] = pkg.UID()
86+
if pkg.LuaMenuInstalled != "" {
87+
script["luamenu_installed"] = pkg.LuaMenuInstalled
88+
}
89+
if pkg.LuaMenuAction != "" {
90+
script["luamenu_action"] = pkg.LuaMenuAction
91+
}
92+
if pkg.LuaClientInstalled != "" {
93+
script["luaclient_installed"] = pkg.LuaClientInstalled
94+
}
95+
if pkg.LuaClientAction != "" {
96+
script["luaclient_action"] = pkg.LuaClientAction
97+
}
98+
if pkg.LuaServerInstalled != "" {
99+
script["luaserver_installed"] = pkg.LuaServerInstalled
100+
}
101+
if pkg.LuaServerAction != "" {
102+
script["luaserver_action"] = pkg.LuaServerAction
103+
}
76104
}
77105

78106
if len(pkg.Content) != 0 {
@@ -82,11 +110,12 @@ func (pkg Package) Marshal() []byte {
82110
item := make(VDF)
83111

84112
item["id"] = c.ID
85-
item["rev"] = 1
113+
item["rev"] = c.Revision
86114
item["name"] = c.Path
87115
item["url"] = fmt.Sprintf("http://api.cl0udb0x.com/content/getzip?id=%d", c.ID)
88116
item["size"] = c.PSize
89117

118+
// name doesn't matter
90119
content[fmt.Sprintf("content_%d", c.ID)] = item
91120
}
92121

@@ -103,6 +132,7 @@ func (pkg Package) Marshal() []byte {
103132
item["rev"] = i.Revision
104133
item["type"] = i.Type
105134

135+
// name doesn't matter
106136
includes[fmt.Sprintf("include_%d", i.ID)] = item
107137
}
108138

@@ -119,15 +149,17 @@ func (pkg Package) Marshal() []byte {
119149
return []byte(root.Marshal())
120150
}
121151

122-
func (pkg Package) RealMapName() string {
123-
mapname := pkg.Name
152+
func (pkg Package) BSPName() string {
124153
for _, c := range pkg.Content {
125-
if filepath.Ext(c.Path) != ".bsp" {
126-
continue
154+
if filepath.Ext(c.Path) == ".bsp" {
155+
return strings.TrimSuffix(filepath.Base(c.Path), filepath.Ext(c.Path))
127156
}
128-
129-
mapname = strings.TrimSuffix(filepath.Base(c.Path), filepath.Ext(c.Path))
130157
}
131158

132-
return mapname
159+
return pkg.Name
160+
}
161+
162+
// used by Install packages only
163+
func (pkg Package) UID() string {
164+
return fmt.Sprintf("%s_%d", pkg.Type, pkg.ID)
133165
}

db/package.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func FetchPackage(id int, rev int) (common.Package, error) {
8181
return pkg, err
8282
}
8383

84+
content.Revision = 1
85+
8486
pkg.Content = append(pkg.Content, content)
8587
}
8688

ingame/toyboxapi/getpackage.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ func GetPackage(w http.ResponseWriter, r *http.Request) {
6868
return
6969
}
7070

71-
w.Write(pkg.Marshal())
71+
var install bool
72+
if pkg.Type == "map" {
73+
install = true
74+
75+
pkg.LuaMenuInstalled = "OnMapDownloaded();"
76+
pkg.LuaMenuAction = fmt.Sprintf("OnMapSelected('%s');", pkg.BSPName())
77+
}
78+
79+
w.Write(pkg.Marshal(install))
7280

7381
// webhook related
7482
err = utils.SendDiscordMessage(utils.DiscordStatsWebhookURL, utils.DiscordWebhookRequest{

0 commit comments

Comments
 (0)