-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from carrot-hu23/多层版本-1.4.0
多层版本 1.4.0
- Loading branch information
Showing
14 changed files
with
821 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package api | ||
|
||
import ( | ||
"dst-admin-go/service" | ||
"dst-admin-go/utils/dstConfigUtils" | ||
"dst-admin-go/utils/dstUtils" | ||
"dst-admin-go/utils/fileUtils" | ||
"dst-admin-go/vo" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type DstMapApi struct { | ||
} | ||
|
||
func (d *DstMapApi) GenDstMap(ctx *gin.Context) { | ||
|
||
dstConfig := dstConfigUtils.GetDstConfig() | ||
outputImage := filepath.Join(dstUtils.GetClusterBasePath(dstConfig.Cluster), "dst_map.png") | ||
sessionPath := filepath.Join(dstUtils.GetKleiDstPath(), dstConfig.Cluster, "Master", "save", "session") | ||
filePath, err := findLatestMetaFile(sessionPath) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
log.Println("生成地图", filePath, outputImage) | ||
generator := service.NewDSTMapGenerator() | ||
height, width, err := service.ExtractDimensions(filePath) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
err = generator.GenerateMap( | ||
filePath, | ||
outputImage, | ||
height, | ||
width, | ||
) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
ctx.JSON(http.StatusOK, vo.Response{ | ||
Code: 200, | ||
Msg: "success", | ||
Data: nil, | ||
}) | ||
} | ||
|
||
func (d *DstMapApi) GetDstMapImage(ctx *gin.Context) { | ||
|
||
dstConfig := dstConfigUtils.GetDstConfig() | ||
outputImage := filepath.Join(dstUtils.GetClusterBasePath(dstConfig.Cluster), "dst_map.png") | ||
log.Println(outputImage) | ||
// 使用 Gin 提供的文件传输方法返回图片 | ||
ctx.File(outputImage) | ||
ctx.Header("Content-Type", "image/png") | ||
|
||
} | ||
|
||
func (d *DstMapApi) HasWalrusHutPlains(ctx *gin.Context) { | ||
|
||
dstConfig := dstConfigUtils.GetDstConfig() | ||
sessionPath := filepath.Join(dstUtils.GetKleiDstPath(), dstConfig.Cluster, "Master", "save", "session") | ||
filePath, err := findLatestMetaFile(sessionPath) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
file, err := fileUtils.ReadFile(filePath) | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
hasWalrusHutPlains := strings.Contains(file, "WalrusHut_Plains") | ||
ctx.JSON(http.StatusOK, vo.Response{ | ||
Code: 200, | ||
Msg: "success", | ||
Data: hasWalrusHutPlains, | ||
}) | ||
|
||
} | ||
|
||
func findLatestMetaFile(directory string) (string, error) { | ||
// 检查指定目录是否存在 | ||
_, err := os.Stat(directory) | ||
if os.IsNotExist(err) { | ||
return "", fmt.Errorf("目录不存在:%s", directory) | ||
} | ||
|
||
// 获取指定目录下一级的所有子目录 | ||
subdirs, err := ioutil.ReadDir(directory) | ||
if err != nil { | ||
return "", fmt.Errorf("读取目录失败:%s", err) | ||
} | ||
|
||
// 用于存储最新的.meta文件路径和其修改时间 | ||
var latestMetaFile string | ||
var latestMetaFileTime time.Time | ||
|
||
for _, subdir := range subdirs { | ||
// 检查子目录是否是目录 | ||
if subdir.IsDir() { | ||
subdirPath := filepath.Join(directory, subdir.Name()) | ||
|
||
// 获取子目录下的所有文件 | ||
files, err := ioutil.ReadDir(subdirPath) | ||
if err != nil { | ||
return "", fmt.Errorf("读取子目录失败:%s", err) | ||
} | ||
|
||
for _, file := range files { | ||
// 检查文件是否是.meta文件 | ||
if !file.IsDir() && filepath.Ext(file.Name()) != ".meta" { | ||
// 获取文件的修改时间 | ||
modifiedTime := file.ModTime() | ||
|
||
// 如果找到的文件的修改时间比当前最新的.meta文件的修改时间更晚,则更新最新的.meta文件路径和修改时间 | ||
if modifiedTime.After(latestMetaFileTime) { | ||
latestMetaFile = filepath.Join(subdirPath, file.Name()) | ||
latestMetaFileTime = modifiedTime | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if latestMetaFile == "" { | ||
return "", fmt.Errorf("未找到文件") | ||
} | ||
|
||
return latestMetaFile, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GOOS=linux GOARCH=amd64 go build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GOOS=windows GOARCH=amd64 go build |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import os | ||
import re | ||
import json | ||
|
||
# 定义修饰词规则 | ||
SPICE_TRANSLATIONS = { | ||
"_SPICE_GARLIC": "蒜", | ||
"_SPICE_CHILI": "辣", | ||
"_SPICE_SUGAR": "甜", | ||
"_SPICE_SALT": "盐", | ||
} | ||
|
||
def parse_po_file(po_file): | ||
"""解析 .po 文件,从第 9 行开始,返回仅包含 STRINGS.NAMES 的 msgctxt 与 msgstr 的映射""" | ||
translations = {} | ||
with open(po_file, "r", encoding="utf-8") as f: | ||
lines = f.readlines() | ||
|
||
# 从第 9 行开始解析 | ||
lines = lines[8:] | ||
|
||
msgctxt, msgid, msgstr = None, None, None | ||
for line in lines: | ||
line = line.strip() | ||
if line.startswith("msgctxt") and "STRINGS.NAMES." in line: | ||
msgctxt = re.search(r'"STRINGS\.NAMES\.(.+)"', line).group(1) # 提取物品名 | ||
elif line.startswith("msgid"): | ||
msgid = re.search(r'"(.*)"', line).group(1) # 捕获空字符串 | ||
elif line.startswith("msgstr"): | ||
msgstr = re.search(r'"(.*)"', line).group(1) # 捕获空字符串 | ||
|
||
# 只有在 msgctxt、msgid 和 msgstr 都非空时才保存 | ||
if msgctxt and msgid and msgstr: | ||
translations[msgctxt] = msgstr | ||
|
||
# 重置状态 | ||
msgctxt, msgid, msgstr = None, None, None | ||
return translations | ||
|
||
def apply_spice_rule(item, base_translation): | ||
print(item) | ||
"""根据规则生成修饰后的翻译""" | ||
for suffix, spice_translation in SPICE_TRANSLATIONS.items(): | ||
if item.endswith(suffix): | ||
base_item = item.replace(suffix, "") | ||
if base_item in base_translation: | ||
return f"{base_translation[base_item]}-{spice_translation}" | ||
return "未翻译" | ||
|
||
def generate_translations(input_folder, po_file, output_file): | ||
"""生成带翻译的 JSON 数据""" | ||
# 解析 .po 文件 | ||
translations = parse_po_file(po_file) | ||
|
||
# 初始化结果字典 | ||
result = {} | ||
|
||
# 遍历输入文件夹,处理分类数据 | ||
for filename in os.listdir(input_folder): | ||
if filename.endswith(".lua"): | ||
category_name = os.path.splitext(filename)[0] | ||
file_path = os.path.join(input_folder, filename) | ||
with open(file_path, "r", encoding="utf-8") as f: | ||
content = f.read() | ||
if "return" in content: | ||
content = content.split("return", 1)[1].strip().strip("{}") | ||
items = [item.strip().strip('"') for item in content.split(",")] | ||
result[category_name] = { | ||
item: translations.get(item.upper(), apply_spice_rule(item.upper(), translations)) | ||
for item in items | ||
} | ||
|
||
# 保存结果为 JSON 文件 | ||
with open(output_file, "w", encoding="utf-8") as f: | ||
json.dump(result, f, ensure_ascii=False, indent=4) | ||
print(f"翻译结果已保存到 {output_file}") | ||
|
||
|
||
# 设置文件路径 | ||
input_folder = "./scripts/TMIP/list" # 替换为文件夹路径 | ||
po_file = "./chinese_s.po" # 替换为 .po 文件路径 | ||
output_file = "tooManyItemPlus.json" # 输出文件路径 | ||
|
||
# 运行生成函数 | ||
generate_translations(input_folder, po_file, output_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package router | ||
|
||
import ( | ||
"dst-admin-go/api" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func initDstGenMapRouter(router *gin.RouterGroup) { | ||
|
||
dstMapApi := api.DstMapApi{} | ||
dstMap := router.Group("/api/dst/map/") | ||
{ | ||
dstMap.GET("/gen", dstMapApi.GenDstMap) | ||
dstMap.GET("image", dstMapApi.GetDstMapImage) | ||
dstMap.GET("/has/walrusHut/plains", dstMapApi.HasWalrusHutPlains) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.