-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
204 lines (158 loc) · 4.22 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
type release struct {
Assets []asset `json:"assets"`
}
type asset struct {
URL string `json:"browser_download_url"`
}
type nacp struct {
Name string `json:"name"`
Author string `json:"author"`
Version string `json:"version"`
TitleID string `json:"title_id"`
}
func printHelpAndExit() {
fmt.Printf("usage: nspbuild <path/to/nso> <name> <author> <version> <path/to/icon/jpg> <tid>\n")
os.Exit(0)
}
func getRelease(repo string) (*release, error) {
resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
r := &release{}
err = json.Unmarshal(body, r)
if err != nil {
return nil, err
}
return r, nil
}
func main() {
os.Args = del(os.Args, 0)
if isEverythingNil(os.Args) {
printHelpAndExit()
}
args := org(parse(os.Args), []string{
"nso",
"name",
"author",
"version",
"icon",
"tid",
})
if len(args) != 6 {
printHelpAndExit()
}
if !fileExists(args["nso"]) {
fmt.Printf("error: the file at %s does not exist!\n", args["nso"])
os.Exit(1)
} else if args["icon"] != "none" && !fileExists(args["icon"]) {
fmt.Printf("error: the file at %s does not exist!\n", args["icon"])
os.Exit(1)
}
if !isHex(args["tid"]) {
fmt.Printf("error: the title id %s is not valid hex!\n", args["tid"])
os.Exit(1)
} else if len(args["tid"]) != 16 {
fmt.Printf("error: the title id %s is not 16 characters!\n", args["tid"])
os.Exit(1)
}
chkErr(os.MkdirAll("build/", 0700))
linkle, err := getRelease("MegatonHammer/linkle")
chkErr(err)
url := ""
for _, v := range linkle.Assets {
if strings.HasSuffix(v.URL, "x86_64-pc-windows-msvc.zip") {
url = v.URL
}
}
chkErr(download(url, "build/linkle.zip"))
chkErr(unzipFile("build/linkle.zip", "linkle.exe", "build/linkle.exe"))
hbp, err := getRelease("The-4n/hacBrewPack")
chkErr(err)
chkErr(download(hbp.Assets[1].URL, "build/hbp.zip"))
chkErr(unzipFile("build/hbp.zip", "hacbrewpack.exe", "build/hbp.exe"))
chkErr(download("https://raw.githubusercontent.com/ThatNerdyPikachu/nspbuild/master/npdmtool.exe",
"build/npdmtool.exe"))
chkErr(os.MkdirAll("build/exefs", 0700))
chkErr(copyFile(args["nso"], "build/exefs/main"))
resp, err := http.Get("https://raw.githubusercontent.com/switchbrew/nx-hbloader/master/hbl.json")
chkErr(err)
npdm, err := os.Create("build/npdm.json")
chkErr(err)
replacer := strings.NewReplacer("hbloader", args["name"], "0x010000000000100D",
"0x"+strings.ToLower(args["tid"]), "\"application_type\" : 2", "\"application_type\" : 1")
body, err := ioutil.ReadAll(resp.Body)
chkErr(err)
resp.Body.Close()
for _, v := range strings.Split(string(body), "\n") {
_, err = npdm.WriteString(replacer.Replace(v))
chkErr(err)
}
npdm.Close()
cmd := exec.Command(".\\npdmtool", "npdm.json", "exefs/main.npdm")
cmd.Dir = "build/"
chkErr(cmd.Run())
chkErr(os.MkdirAll("build/control", 0700))
gen := nacp{
args["name"],
args["author"],
args["version"],
strings.ToLower(args["tid"]),
}
nacp, err := os.Create("build/nacp.json")
chkErr(err)
j, err := json.Marshal(gen)
chkErr(err)
nacp.WriteString(string(j))
nacp.Close()
cmd = exec.Command(".\\linkle", "nacp", "nacp.json", "control/control.nacp")
cmd.Dir = "build/"
chkErr(cmd.Run())
if args["icon"] != "none" {
languages := []string{
"Japanese",
"AmericanEnglish",
"French",
"German",
"Italian",
"Spanish",
"Chinese",
"Korean",
"Dutch",
"Portuguese",
"Russian",
"Taiwanese",
"BritishEnglish",
"CanadianFrench",
"LatinAmericanSpanish",
"SimplifiedChinese",
"TraditionalChinese",
}
for _, v := range languages {
chkErr(copyFile(args["icon"], fmt.Sprintf("build/control/icon_%s.dat", v)))
}
}
chkErr(copyFile("keys.txt", "build/keys.dat"))
cmd = exec.Command(".\\hbp", "--noromfs", "--nologo")
cmd.Dir = "build/"
chkErr(cmd.Run())
chkErr(os.MkdirAll("out/", 0700))
chkErr(copyFile(fmt.Sprintf("build/hacbrewpack_nsp/%s.nsp", strings.ToLower(args["tid"])),
fmt.Sprintf("out/%s.nsp", args["name"])))
chkErr(os.RemoveAll("build/"))
}