Created
July 19, 2020 14:53
-
-
Save richard1122/33bc1db367e56f4fdd82afbe7c86ad94 to your computer and use it in GitHub Desktop.
origin engine parse/merge translation file script
This file contains hidden or 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
import java.io.BufferedWriter | |
import java.io.File | |
import java.io.FileInputStream | |
import java.util.* | |
import kotlin.test.assertTrue | |
const val FOLDER_PATH = "c:/data" | |
const val CHINESE_FILE_PATH = "$FOLDER_PATH/chinese.txt" | |
const val CHINESE_FILE_TEST_PATH = "$FOLDER_PATH/chinese_temp.txt" | |
const val ENGLISH_FILE_PATH = "$FOLDER_PATH/english.txt" | |
const val ENGLISH_FILE_TEST_PATH = "$FOLDER_PATH/english_temp.txt" | |
const val RESULT_FILE_PATH = "$FOLDER_PATH/result/result.txt" | |
const val LOG_KEYS_NOT_EXIST_PATH = "$FOLDER_PATH/result/log_not_exists_keys.txt" | |
const val LOG_KEYS_REPLACED_PATH = "$FOLDER_PATH/result/log_replaced_keys.txt" | |
class TUFile( | |
val header: List<String>, | |
val items: List<AbstractItem>, | |
val footer: List<String> | |
) { | |
fun print(path: String) { | |
val file = File(path) | |
val out = file.bufferedWriter(Charsets.UTF_16LE) | |
header.forEach { | |
out.write(it) | |
out.newLine() | |
} | |
items.forEach { it.print(out) } | |
footer.forEach { | |
out.write(it) | |
out.newLine() | |
} | |
out.flush() | |
} | |
fun prepareIndex() { | |
items.forEach { | |
if (it !is Item) return@forEach | |
indexRecord.putIfAbsent(it.key, it.value) | |
} | |
} | |
val indexRecord = mutableMapOf<String, String>() | |
} | |
sealed class AbstractItem { | |
abstract fun print(out: BufferedWriter) | |
} | |
class Item( | |
val key: String, | |
val midText: String, | |
var value: String, | |
val comment: String | |
) : AbstractItem() { | |
override fun print(out: BufferedWriter) { | |
out.write(key) | |
out.write(midText) | |
out.write(value) | |
out.write(comment) | |
out.newLine() | |
} | |
} | |
class PureComment(val comment: String) : AbstractItem() { | |
override fun print(out: BufferedWriter) { | |
out.write(comment) | |
out.newLine() | |
} | |
} | |
class EmptyLine : AbstractItem() { | |
override fun print(out: BufferedWriter) { | |
out.newLine() | |
} | |
} | |
fun readEncoded(startIndex: Int, string: String): Int? { | |
var index = startIndex | |
assertTrue { string[index] == '"' } | |
++index | |
var isEscape = false | |
while (index < string.length && (string[index] != '"' || isEscape)) { | |
isEscape = string[index] == '\\' | |
++index | |
} | |
if (index == string.length) return null | |
return index + 1 | |
} | |
fun readItems(path: String): TUFile { | |
val scanner = Scanner(FileInputStream(File(path)), Charsets.UTF_16LE) | |
val header = (1..5).map { scanner.nextLine() } | |
var lineNumber = 5 | |
val items = mutableListOf<AbstractItem>() | |
var line = scanner.nextLine() | |
while (line != "}" && scanner.hasNextLine()) { | |
println(line) | |
++lineNumber | |
if (line.startsWith("/")) { | |
items.add(PureComment(line)) | |
} else if (line.isBlank()) { | |
items.add(EmptyLine()) | |
} else { | |
val keyEnd = readEncoded(0, line)!! | |
val key = line.substring(0, keyEnd) | |
var valueStart = readEncoded(keyEnd - 1, line)!! | |
--valueStart | |
val midText = line.substring(keyEnd, valueStart) | |
var valueEnd = readEncoded(valueStart, line) | |
while (valueEnd == null) { | |
line = "$line\n${scanner.nextLine()}" | |
valueEnd = readEncoded(valueStart, line) | |
} | |
val value = line.substring(valueStart, valueEnd) | |
var comment = "" | |
if (valueEnd != line.length) { | |
comment = line.substring(valueEnd) | |
} | |
println("key=$key,value=$value,comment=$comment") | |
items.add(Item(key, midText, value, comment)) | |
} | |
println(lineNumber) | |
line = scanner.nextLine() | |
} | |
val footer = mutableListOf<String>() | |
footer.add(line) | |
while (scanner.hasNextLine()) { | |
footer.add(scanner.nextLine()) | |
} | |
return TUFile(header, items, footer) | |
} | |
fun main() { | |
val chineseFile = readItems(CHINESE_FILE_PATH) | |
chineseFile.print(CHINESE_FILE_TEST_PATH) | |
val englishFile = readItems(ENGLISH_FILE_PATH) | |
englishFile.print(ENGLISH_FILE_TEST_PATH) | |
chineseFile.prepareIndex() | |
englishFile.prepareIndex() | |
val keysNotExist = mutableListOf<String>() | |
val keysReplaced = mutableListOf<String>() | |
englishFile.items.forEach { | |
if (it !is Item) return@forEach | |
val valueInChn = chineseFile.indexRecord[it.key] | |
if (valueInChn == null) keysNotExist.add(it.key) | |
else { | |
it.value = valueInChn | |
keysReplaced.add(it.value) | |
} | |
} | |
englishFile.print(RESULT_FILE_PATH) | |
File(LOG_KEYS_NOT_EXIST_PATH).also { | |
it.writeText(keysNotExist.joinToString(separator = "\n")) | |
} | |
File(LOG_KEYS_REPLACED_PATH).also { | |
it.writeText(keysReplaced.joinToString(separator = "\n")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment