-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
164 lines (141 loc) · 3.54 KB
/
client.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
package ngdp
import (
"encoding/hex"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/Gophercraft/core/format/ngdp/ccfg"
)
type Hash [16]byte
func (h Hash) String() string {
return hex.EncodeToString(h[:])
}
type Client struct {
*Agent
Opt uint64
Dir string
CDN *CDN
Build Hash
BuildConfig BuildConfig
}
type CDN struct {
Region string `ccfg:"Name"`
Path string `ccfg:"Path"`
Hosts []string `ccfg:"Hosts"`
Servers []string `ccfg:"Servers"`
ConfigPath string `ccfg:"ConfigPath"`
}
type Version struct {
Region string
BuildConfig Hash
CDNConfig Hash
KeyRing Hash
BuildId uint32
VersionsName string
ProductConfig Hash
}
// OpenOnline open a remote NGDP volume using the set CDN server
// Without setting additional options, the client will not touch the disk and will read directly from HTTP to memory.
func (ag *Agent) OpenOnline(programID string) (*Client, error) {
c := new(Client)
c.Agent = ag
c.Opt |= OptUseTACTNetwork
versionListFile, err := ag.DownloadFn(ag.HostServer + "/" + programID + "/versions")
if err != nil {
return nil, err
}
var versionList []Version
err = ccfg.NewDecoder(versionListFile).Decode(&versionList)
if err != nil {
return nil, err
}
if len(versionList) == 0 {
return nil, fmt.Errorf("no versions")
}
for _, version := range versionList {
if version.Region == ag.Region {
c.Build = version.BuildConfig
}
}
cdnListFile, err := ag.DownloadFn(ag.HostServer + "/" + programID + "/cdns")
if err != nil {
return nil, err
}
var cdnList []CDN
err = ccfg.NewDecoder(cdnListFile).Decode(&cdnList)
if err != nil {
return nil, err
}
if len(cdnList) == 0 {
return nil, fmt.Errorf("ngdp: no cdns")
}
var cdn *CDN
cdn = &cdnList[0]
for i, cdnListing := range cdnList {
if cdnListing.Region == ag.Region {
cdn = &cdnList[i]
break
}
}
if len(cdn.Servers) == 0 {
return nil, fmt.Errorf("ngdp: no servers")
}
c.CDN = cdn
return c, c.Init()
}
func (cl *Client) Init() error {
buildConfigFile, err := cl.openHash("config", cl.Build)
if err != nil {
return err
}
err = ccfg.NewDecoder(buildConfigFile).Decode(&cl.BuildConfig)
if err != nil {
return err
}
return nil
}
// // OpenCASC open an existing CASC archive and use its configuration files to figure out what to download
// func (ag *Agent) OpenCASC(path string) (*Client, error) {
// var c Client
// c.Agent = ag
// c.Opt |= OptUseCASContainer
// c.Dir = filepath.Clean(path)
// }
func (cl *Client) realPath(path string) string {
forwardSlash := strings.Split(path, "/")
return filepath.Join(append([]string{cl.Dir}, forwardSlash...)...)
}
func (cl *Client) exists(path string) bool {
if cl.Opt&OptUseCASContainer == 0 {
panic("cannot check existence")
}
_, err := os.Stat(cl.realPath(path))
return err == nil
}
func (cl *Client) openHash(kind string, h Hash) (io.ReadCloser, error) {
str := h.String()
p1, p2 := str[0:2], str[2:4]
return cl.openRawFile(fmt.Sprintf("%s/%s/%s/%s", kind, p1, p2, str))
}
// Retrieve a file from TACT and/or CASC
func (cl *Client) openRawFile(path string) (io.ReadCloser, error) {
if cl.Opt&OptUseCASContainer != 0 {
if cl.exists(path) {
return os.Open(cl.realPath(path))
}
}
// if cl.Opt&(OptUseCASContainer|OptTACTNetwork)!=0 {
// *download and return file as readerreader*
if cl.Opt&OptUseTACTNetwork != 0 {
u, err := url.Parse(cl.CDN.Servers[0])
if err != nil {
return nil, err
}
u.Path = "/" + cl.CDN.Path + "/" + path
return cl.DownloadFn(u.String())
}
panic("flags")
}