forked from dizzyd/mcdex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curseforge_file.go
191 lines (161 loc) · 5.71 KB
/
curseforge_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ***************************************************************************
//
// Copyright 2017 David (Dizzy) Smith, [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ***************************************************************************
package main
import (
"fmt"
"math"
"github.com/Jeffail/gabs"
)
type CurseForgeModFile struct {
projectID int
fileID int
desc string
name string
clientOnly bool
}
func SelectCurseForgeModFile(pack *ModPack, mod string, url string, clientOnly bool) error {
// Try to find the project ID using the mod name as a slug
projectID, err := pack.db.findModBySlug(mod)
if err != nil {
return fmt.Errorf("unknown mod %s", mod)
}
// Look up the slug, name and description
_, name, desc, err := pack.db.getProjectInfo(projectID)
if err != nil {
return fmt.Errorf("no name/description available for %s (%d): %+v", mod, projectID, err)
}
// Setup a mod file entry and then pull the latest file info
modFile := CurseForgeModFile{projectID: projectID, desc: desc, name: name, clientOnly: clientOnly}
fileId, err := modFile.getLatestFile(pack.minecraftVersion())
if err != nil {
return fmt.Errorf("failed to get latest file for %s (%d): %+v", mod, projectID, err)
}
// If we found a newer file, update entry and then the pack
if fileId > modFile.fileID {
modFile.fileID = fileId
err = pack.selectMod(&modFile)
if err != nil {
return err
}
}
return nil
}
func NewCurseForgeModFile(modJson *gabs.Container) *CurseForgeModFile {
projectID, _ := intValue(modJson, "projectID")
fileID, _ := intValue(modJson, "fileID")
name, ok := modJson.Path("desc").Data().(string)
if !ok {
name = fmt.Sprintf("Curseforge project %d: %d", projectID, fileID)
}
clientOnly, ok := modJson.S("clientOnly").Data().(bool)
return &CurseForgeModFile{projectID, fileID, name, name, ok && clientOnly}
}
func (f CurseForgeModFile) install(pack *ModPack) error {
// Check the mod cache to see if we already have the right file ID installed
lastFileId, lastFilename := pack.modCache.GetLastModFile(f.projectID)
if lastFileId == f.fileID {
// Nothing to do; we can skip this installed file
fmt.Printf("Skipping %s\n", lastFilename)
return nil
} else if lastFileId > 0 {
// A different version of the file is installed; clean it up
pack.modCache.CleanupModFile(f.projectID)
}
// Resolve the project ID into a slug
slug, err := pack.db.findSlugByProject(f.projectID)
if err != nil {
return fmt.Errorf("failed to find slug for project %d: %+v", f.projectID, err)
}
// Now, retrieve the JSON descriptor for this file so we can get the CDN url
descriptorUrl := fmt.Sprintf("https://addons-ecs.forgesvc.net/api/v2/addon/%d/file/%d", f.projectID, f.fileID)
descriptor, err := getJSONFromURL(descriptorUrl)
if err != nil {
return fmt.Errorf("failed to retrieve descriptor for %s: %+v", slug, err)
}
// Download the file to the pack mod directory
finalUrl := descriptor.Path("downloadUrl").Data().(string)
filename, err := downloadHttpFileToDir(finalUrl, pack.modPath(), true)
if err != nil {
return err
}
// Download succeeded; register this mod as installed in the cache
pack.modCache.AddModFile(f.projectID, f.fileID, filename)
return nil
}
func (f *CurseForgeModFile) update(pack *ModPack) (bool, error) {
latestFile, err := f.getLatestFile(pack.minecraftVersion())
if err != nil {
return false, err
}
if latestFile > f.fileID {
f.fileID = latestFile
return true, nil
}
return false, nil
}
func (f CurseForgeModFile) getName() string {
return f.name
}
func (f CurseForgeModFile) isClientOnly() bool {
return f.clientOnly
}
func (f CurseForgeModFile) equalsJson(modJson *gabs.Container) bool {
projectID, ok := modJson.Path("projectID").Data().(float64)
return ok && int(projectID) == f.projectID
}
func (f CurseForgeModFile) toJson() map[string]interface{} {
result := map[string]interface{}{
"projectID": f.projectID,
"fileID": f.fileID,
"required": true,
"desc": f.name,
}
if f.clientOnly {
result["clientOnly"] = true
}
return result
}
func (f CurseForgeModFile) getLatestFile(minecraftVersion string) (int, error) {
// Pull the project's descriptor, which has a list of the latest files for each version of Minecraft
projectUrl := fmt.Sprintf("https://addons-ecs.forgesvc.net/api/v2/addon/%d", f.projectID)
project, err := getJSONFromURL(projectUrl)
if err != nil {
return -1, fmt.Errorf("failed to retrieve project for %s: %+v", f.name, err)
}
selectedFileType := math.MaxInt8
selectedFileId := 0
// Look for the file with the matching version
files, _ := project.Path("gameVersionLatestFiles").Children()
for _, file := range files {
fileType, _ := intValue(file, "fileType") // 1 = release, 2 = beta, 3 = alpha
fileId, _ := intValue(file, "projectFileId")
targetVsn := file.Path("gameVersion").Data().(string)
if targetVsn != minecraftVersion {
continue
}
// Matched on version; prefer releases over beta/alpha
if fileType < selectedFileType {
selectedFileType = fileType
selectedFileId = fileId
}
}
if selectedFileId == 0 {
return -1, fmt.Errorf("no version found for Minecraft %s\n", minecraftVersion)
}
// TODO: Pull file descriptor and check for deps
return selectedFileId, nil
}