From c5d47148ff45582e75f037f12f663a2f29b20071 Mon Sep 17 00:00:00 2001 From: KAIYOHUGO Date: Fri, 18 Jun 2021 18:08:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=94add=20auto=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- README.md | 60 ++++++++-- input.go | 2 +- main.go | 236 +++++++++++++++++++++++++++++++-------- printer.go | 30 +++++ manifest.go => struct.go | 13 ++- 6 files changed, 284 insertions(+), 61 deletions(-) create mode 100644 printer.go rename manifest.go => struct.go (72%) diff --git a/.gitignore b/.gitignore index 9207185..605d703 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.mcpack \ No newline at end of file +*.mcpack +*.exe +dist \ No newline at end of file diff --git a/README.md b/README.md index c1e1ca4..58485ec 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![icon](https://i.imgur.com/yprFoFr.png) -_a better bedrock translate tool_ +_a java version translate to bedrock translate tool_ it will make bedrock version translate be the same as java version translate @@ -28,20 +28,60 @@ tobedrock.exe ## Example ```bash +# auto mode +$ tobedrock.exe + +( 1 ): true +( 2 ): false +choice build mode (auto mode?):1 +input version number (e.g. 1.17.0):1.17.0 +minecraft path https://minecraft.fandom.com/wiki/.minecraft/path:C:\Users\kymcm\AppData\Roaming\.minecraft +done ... +all done, exit +# or $ tobedrock.exe -input lang type you want to translate to:zh_TW +( 1 ): true +( 2 ): false +choice build mode (auto mode?):2 +input version number (e.g. 1.17.0):1.17.0 +( 1 ): en_US +( 2 ): en_GB +( 3 ): de_DE +( 4 ): es_ES +( 5 ): es_MX +( 6 ): fr_FR +( 7 ): fr_CA +( 8 ): it_IT +( 9 ): ja_JP +( 10 ): ko_KR +( 11 ): pt_BR +( 12 ): pt_PT +( 13 ): ru_RU +( 14 ): zh_CN +( 15 ): zh_TW +( 16 ): nl_NL +( 17 ): bg_BG +( 18 ): cs_CZ +( 19 ): da_DK +( 20 ): el_GR +( 21 ): fi_FI +( 22 ): hu_HU +( 23 ): id_ID +( 24 ): nb_NO +( 25 ): pl_PL +( 26 ): sk_SK +( 27 ): sv_SE +( 28 ): tr_TR +( 29 ): uk_UA +input lang type you want to translate to:15 input java version en_us lang file:example/en_us.json input bedrock version en_us lang file:example/en_us.lang -input java version lang file you want to translate to:example/zh_tw.json -input bedrock version lang file to compose (can omit):example/zh_tw.lang -pack up : to bedrock/template/ -pack up : to bedrock/template/manifest.json -pack up : to bedrock/template/pack_icon.png -pack up : to bedrock/template/texts -pack up : to bedrock/template/texts/zh_TW.lang -done +input java version zh_TW lang file:example/zh_tw.json +input bedrock version zh_TW lang file:example/zh_tw.lang +done zh_TW +all done, exit ```
java icons made by Freepik from www.flaticon.com
\ No newline at end of file diff --git a/input.go b/input.go index 49f30d7..54d9fb2 100644 --- a/input.go +++ b/input.go @@ -32,7 +32,7 @@ func InputSelect(input ...Input) { retry: fmt.Print(v.Message) fmt.Scanln(v.Var) - if (v.Var == nil || *v.Var.(*int) <= 0 || *v.Var.(*int) >= len(v.Select)) && !v.Omit { + if (v.Var == nil || *v.Var.(*int) <= 0 || *v.Var.(*int) > len(v.Select)) && !v.Omit { goto retry } *v.Var.(*int)-- diff --git a/main.go b/main.go index 32f1b56..3bb080a 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,13 @@ package main import ( + "archive/zip" "embed" "encoding/json" + "fmt" + "io" + "io/ioutil" + "net/http" "os" "strconv" "strings" @@ -13,10 +18,137 @@ var fs embed.FS func main() { var ( - lang int + lang, mode int // javaKey & translate is java json file,bedrockKey & compose is bedrock lang file - version, javaKey, translate, bedrockKey, compose string + version, javaKey, translate, bedrockKey, compose, javaPath string + // zipTemp *zip.Reader ) + javaList := make(map[string]string) + bedrockJavaMap := make(map[string]string) + tempDir, err := ioutil.TempDir("", "") + if err != nil { + panic(err) + } + defer func() { + os.RemoveAll(tempDir) + os.Remove(tempDir) + }() + fmt.Println(tempDir) + + // ture==0 + InputSelect(Input{ + Message: "choice build mode (auto mode?):", + Omit: false, + Var: &mode, + Select: []interface{}{ + "true", + "false", + }, + }) + + InputText(Input{ + Message: "input version number (e.g. 1.17.0):", + Omit: false, + Var: &version, + }) + var ( + versionArray []int64 + shortVersion string = strings.Join(strings.Split(version, ".")[:2], ".") + ) + for _, v := range strings.Split(version, ".") { + number, err := strconv.Atoi(v) + if err != nil { + panic(err) + } + versionArray = append(versionArray, int64(number)) + } + + if mode == 0 { + InputText(Input{ + Message: "minecraft path https://minecraft.fandom.com/wiki/.minecraft/path:", + Omit: false, + Var: &javaPath, + }) + printInPlace("parse java...") + func() { + file, err := os.Open(fmt.Sprintf("%s/assets/indexes/%s.json", javaPath, shortVersion)) + if err != nil { + panic(err) + } + defer file.Close() + var javaIndex JavaIndex + json.NewDecoder(file).Decode(&javaIndex) + for k, v := range javaIndex.Objects { + if strings.HasPrefix(k, "minecraft/lang/") { + javaList[strings.TrimSuffix(strings.TrimPrefix(k, "minecraft/lang/"), ".json")] = v.Hash + } + } + }() + func() { + printInPlace("download bedrock lang file...") + resp, err := http.Get(fmt.Sprintf("https://meedownloads.blob.core.windows.net/add-ons/Vanilla_Resource_Pack_%s.zip", version)) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + printInPlace("save bedrock lang file...") + temp, err := os.CreateTemp(tempDir, "tb") + if err != nil { + panic(err) + } + defer temp.Close() + io.CopyBuffer(temp, io.TeeReader(resp.Body, newCounter(func(u uint64) { + printInPlace(fmt.Sprintf("downloading :%d", u)) + })), make([]byte, 1024*1024)) + zf, err := zip.OpenReader(temp.Name()) + if err != nil { + panic(err) + } + defer zf.Close() + + // unzip lang file + for _, v := range langlist { + func() { + printInPlace(fmt.Sprintf("parse %s...", v)) + zipLangFile, err := zf.Open(fmt.Sprintf("texts/%s.lang", v)) + if err != nil { + panic(err) + } + defer zipLangFile.Close() + tempLangFile, err := os.Create(fmt.Sprintf("%s/%s", tempDir, v)) + if err != nil { + panic(err) + } + defer tempLangFile.Close() + io.Copy(tempLangFile, zipLangFile) + }() + } + }() + + func() { + printInPlace("reading java lang file...") + javaVersionFile, err := zip.OpenReader(fmt.Sprintf("%s/versions/%s/%s.jar", javaPath, shortVersion, shortVersion)) + if err != nil { + panic(err) + } + defer javaVersionFile.Close() + javaLangFile, err := javaVersionFile.Open("assets/minecraft/lang/en_us.json") + if err != nil { + panic(err) + } + defer javaLangFile.Close() + javaTempFile, err := os.Create(fmt.Sprintf("%s/%s", tempDir, "java_en_us")) + if err != nil { + panic(err) + } + defer javaTempFile.Close() + io.Copy(javaTempFile, javaLangFile) + }() + javaKey = fmt.Sprintf("%s/%s", tempDir, "java_en_us") + bedrockKey = fmt.Sprintf("%s/%s", tempDir, "en_US") + goto generate + } InputSelect(Input{ Message: "input lang type you want to translate to:", @@ -26,11 +158,6 @@ func main() { }) InputText([]Input{ - { - Message: "input version number (e.g. 1.17.0):", - Omit: false, - Var: &version, - }, { Message: "input java version en_us lang file:", Omit: false, @@ -42,19 +169,19 @@ func main() { Var: &bedrockKey, }, { - Message: "input java version lang file you want to translate to:", + Message: fmt.Sprintf("input java version %s lang file:", langlist[lang]), Omit: false, Var: &translate, }, { - Message: "input bedrock version lang file to compose (can omit):", + Message: fmt.Sprintf("input bedrock version %s lang file:", langlist[lang]), Omit: true, Var: &compose, }, }...) +generate: // generate bedrock:java key-value - bedrockJavaMap := make(map[string]string) func() { javaKeyFile, err := os.Open(javaKey) if err != nil { @@ -77,48 +204,61 @@ func main() { bedrockJavaMap = bedrockJavaKeyValue(bedrockMap, javaMap) }() - translated := make(map[string]string) - func() { - translateFile, err := os.Open(translate) - if err != nil { - panic(err) - } - defer translateFile.Close() - translateMap := make(map[string]string) + packup := func() { + translated := make(map[string]string) + func() { + translateFile, err := os.Open(translate) + if err != nil { + panic(err) + } + defer translateFile.Close() + translateMap := make(map[string]string) - if err := json.NewDecoder(translateFile).Decode(&translateMap); err != nil { - panic(err) - } - for k, v := range bedrockJavaMap { - translated[k] = translateMap[v] - } - }() + if err := json.NewDecoder(translateFile).Decode(&translateMap); err != nil { + panic(err) + } + for k, v := range bedrockJavaMap { + translated[k] = translateMap[v] + } + }() - func() { - if compose == "" { - return - } - composeFile, err := os.Open(compose) - if err != nil { - panic(err) - } - defer composeFile.Close() - composeMap, err := parseKeyValue(composeFile) - if err != nil { + func() { + if compose == "" { + return + } + composeFile, err := os.Open(compose) + if err != nil { + panic(err) + } + defer composeFile.Close() + composeMap, err := parseKeyValue(composeFile) + if err != nil { + panic(err) + } + translated = composeKeyValue(translated, composeMap) + }() + if err := packAddon(langlist[lang].(string), versionArray, translated); err != nil { panic(err) } - translated = composeKeyValue(translated, composeMap) - }() - var versionArray []int64 - for _, v := range strings.Split(version, ".") { - number, err := strconv.Atoi(v) - if err != nil { - panic(err) - } - versionArray = append(versionArray, int64(number)) + printInPlace(fmt.Sprintf("done %s", langlist[lang])) } - if err := packAddon(langlist[lang].(string), versionArray, translated); err != nil { - panic(err) + if mode == 0 { + for i, v := range langlist { + printInPlace(fmt.Sprintf("running %s", langlist[lang])) + if v == "en_US" { + continue + } + hash, ok := javaList[strings.ToLower(v.(string))] + if !ok { + continue + } + lang = i + translate = fmt.Sprintf("%s/assets/objects/%s/%s", javaPath, hash[:2], hash) + compose = fmt.Sprintf("%s/%s", tempDir, v) + packup() + } + } else { + packup() } - println("done") + fmt.Println("\nall done, exit") } diff --git a/printer.go b/printer.go new file mode 100644 index 0000000..331a08f --- /dev/null +++ b/printer.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strings" +) + +type counter struct { + count uint64 + fn func(uint64) +} + +func printInPlace(s string) { + fmt.Print(strings.Repeat(" ", 50)) + fmt.Printf("\r%s", s) +} + +func newCounter(s func(uint64)) *counter { + return &counter{ + count: 0, + fn: s, + } +} + +func (s *counter) Write(b []byte) (int, error) { + n := len(b) + s.count += uint64(n) + s.fn(s.count) + return n, nil +} diff --git a/manifest.go b/struct.go similarity index 72% rename from manifest.go rename to struct.go index 49b5be1..b80f729 100644 --- a/manifest.go +++ b/struct.go @@ -1,7 +1,7 @@ package main // Generated by https://quicktype.io - +// addon manifest json file type Manifest struct { FormatVersion int64 `json:"format_version"` Header Header `json:"header"` @@ -22,3 +22,14 @@ type Module struct { UUID string `json:"uuid"` Version []int64 `json:"version"` } + +// Generated by https://quicktype.io +// java index json file +type JavaIndex struct { + Objects map[string]Object `json:"objects"` +} + +type Object struct { + Hash string `json:"hash"` + Size int64 `json:"size"` +}