Skip to content

Commit

Permalink
✔add auto mode
Browse files Browse the repository at this point in the history
  • Loading branch information
KAIYOHUGO authored and KAIYOHUGO committed Jun 18, 2021
1 parent a82d097 commit c5d4714
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 61 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.mcpack
*.mcpack
*.exe
dist
60 changes: 50 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
```

<div>java icons made by <a href="https://www.freepik.com" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a></div>
2 changes: 1 addition & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)--
Expand Down
236 changes: 188 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package main

import (
"archive/zip"
"embed"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
Expand All @@ -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:",
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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")
}
Loading

0 comments on commit c5d4714

Please sign in to comment.