-
Notifications
You must be signed in to change notification settings - Fork 5
/
languages.go
130 lines (109 loc) · 3.26 KB
/
languages.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"github.com/dofusdude/ankabuffer"
"github.com/dofusdude/doduda/ui"
)
// TODO remove release when native implementation for .bin files exists.
func DownloadLanguageFiles(release string, hashJson *ankabuffer.Manifest, bin int, version int, lang string, dir string, indent string, headless bool) error {
destPath := filepath.Join(dir, "languages")
if version == 2 {
var langFile HashFile
langFile.Filename = "data/i18n/i18n_" + lang + ".d2i"
langFile.FriendlyName = lang + ".d2i"
err := DownloadUnpackFiles(lang, bin, hashJson, "lang_"+lang, []HashFile{langFile}, dir, destPath, true, indent, headless, false)
return err
} else if version == 3 {
// TODO remove gh api part when native implementation for .json files exists.
feedbacks := make(chan string)
var feedbackWg sync.WaitGroup
feedbackWg.Add(1)
go func() {
defer feedbackWg.Done()
ui.Spinner("Languages", feedbacks, false, headless)
}()
feedbacks <- "searching"
if release == "dofus3" {
release = "main"
}
ghUrl := fmt.Sprintf("https://api.github.com/repos/dofusdude/dofus3-lang-%s/releases/latest", release)
releaseApiResponse, err := http.Get(ghUrl)
if err != nil {
return err
}
releaseApiResponseBody, err := io.ReadAll(releaseApiResponse.Body)
if err != nil {
return err
}
var v map[string]interface{}
err = json.Unmarshal(releaseApiResponseBody, &v)
if err != nil {
return err
}
// check if assets exists
if _, ok := v["assets"]; !ok {
return errors.New("Could not find assets in the latest release.")
}
assets := v["assets"].([]interface{})
found := false
for _, asset := range assets {
assetMap := asset.(map[string]interface{})
if assetMap["name"].(string) == fmt.Sprintf("%s.i18n.json", lang) {
found = true
assetUrl := assetMap["browser_download_url"].(string)
feedbacks <- "loading " + lang
imagesResponse, err := http.Get(assetUrl)
if err != nil {
return err
}
file := filepath.Join(destPath, lang+".json")
out, err := os.Create(file)
if err != nil {
return err
}
_, err = io.Copy(out, imagesResponse.Body)
if err != nil {
return err
}
}
}
if !found {
close(feedbacks)
feedbackWg.Wait()
return errors.New("Could not find the specified file in the latest release")
}
close(feedbacks)
feedbackWg.Wait()
/*langFile := HashFile{
Filename: "Dofus_Data/StreamingAssets/Content/I18n/" + lang + ".bin",
FriendlyName: lang + ".bin",
}
err := DownloadUnpackFiles(lang, bin, hashJson, "i18n", []HashFile{langFile}, dir, destPath, false, indent, headless, false)*/
return nil
} else {
return errors.New("unsupported version: " + strconv.Itoa(version))
}
}
func DownloadLanguages(release string, hashJson *ankabuffer.Manifest, bin int, version int, dir string, indent string, headless bool) error {
var langs []string
if version == 2 {
langs = []string{"fr", "en", "es", "de", "it", "pt"}
} else if version == 3 {
langs = []string{"fr", "en", "es", "de", "pt"}
}
for _, lang := range langs {
err := DownloadLanguageFiles(release, hashJson, bin, version, lang, dir, indent, headless)
if err != nil {
return err
}
}
return nil
}