mirror of
https://github.com/GTAIV-Complete-Edition-text/GTA4.CHS
synced 2026-11-26 09:27:55 +08:00
完成python gxt脚本
This commit is contained in:
1 parent
61142336ae
commit
5ab450ba21
8 files changed
+72
-988
No files matched your search
@@ -23,13 +23,10 @@ find_package(RapidJSON CONFIG REQUIRED)
|
||||
find_package(spdlog CONFIG REQUIRED)
|
||||
|
||||
add_subdirectory(plugin)
|
||||
add_subdirectory(gxt_tool)
|
||||
add_subdirectory(whm_io)
|
||||
|
||||
target_precompile_headers(GTA4.CHS PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/stdinc.h")
|
||||
target_precompile_headers(gxt_tool PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/stdinc.h")
|
||||
target_precompile_headers(whm_io PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/stdinc.h")
|
||||
|
||||
target_link_libraries(GTA4.CHS PRIVATE utf8::cpp range-v3 range-v3-meta range-v3::meta range-v3-concepts fmt::fmt-header-only spdlog::spdlog_header_only)
|
||||
target_link_libraries(gxt_tool PRIVATE utf8::cpp range-v3 range-v3-meta range-v3::meta range-v3-concepts fmt::fmt-header-only rapidjson)
|
||||
target_link_libraries(whm_io PRIVATE range-v3 range-v3-meta range-v3::meta range-v3-concepts ZLIB::ZLIB fmt::fmt-header-only rapidjson)
|
||||
@@ -1,5 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.24)
|
||||
|
||||
project(gxt_tool)
|
||||
|
||||
add_executable(gxt_tool IVText.cpp main.cpp ${common_sources})
|
||||
@@ -1,767 +0,0 @@
|
||||
#include "IVText.h"
|
||||
#include "../common/fnv_hash.h"
|
||||
#include "../common/module_path.h"
|
||||
|
||||
void IVText::ProcessT2B(const PathType &inFolder, const PathType &outFolder)
|
||||
{
|
||||
create_directories(outFolder);
|
||||
LoadTexts(inFolder);
|
||||
GenerateBinary(outFolder / "chinese.gxt");
|
||||
}
|
||||
|
||||
void IVText::ProcessJ2B(const PathType &inFolder, const PathType &outFolder)
|
||||
{
|
||||
create_directories(outFolder);
|
||||
LoadJsons(inFolder);
|
||||
GenerateBinary(outFolder / "chinese.gxt");
|
||||
}
|
||||
|
||||
void IVText::ProcessT2J(const PathType &inFolder)
|
||||
{
|
||||
LoadTexts(inFolder);
|
||||
GenerateJsons(inFolder);
|
||||
}
|
||||
|
||||
void IVText::ProcessB2T(const PathType &inFile, const PathType &outFolder)
|
||||
{
|
||||
create_directories(outFolder);
|
||||
LoadBinary(inFile);
|
||||
GenerateTexts(outFolder);
|
||||
}
|
||||
|
||||
void IVText::ProcessB2J(const PathType &inFile, const PathType &outFolder)
|
||||
{
|
||||
create_directories(outFolder);
|
||||
LoadBinary(inFile);
|
||||
GenerateJsons(outFolder);
|
||||
}
|
||||
|
||||
void IVText::ProcessCollect(const PathType &inFolder, const PathType &outFolder)
|
||||
{
|
||||
std::set<char32_t> chars; //文本中出现的非原版字符集合
|
||||
|
||||
create_directories(outFolder);
|
||||
|
||||
ProcessTexts(inFolder, true,
|
||||
[&chars](const std::string &filename, std::ifstream &stream) { CollectCharsFunc(stream, chars); });
|
||||
|
||||
GenerateCollection(outFolder / "characters.txt", chars);
|
||||
GenerateTable(outFolder / "char_table.dat", chars);
|
||||
}
|
||||
|
||||
bool IVText::IsNativeCharacter(char32_t character)
|
||||
{
|
||||
if (character > 0xFFFF)
|
||||
{
|
||||
fmt::printf("Invalid code point: %u larger than 0xFFFF.\n", static_cast<unsigned>(character));
|
||||
return false;
|
||||
}
|
||||
|
||||
return (character < 0x100 || character == L'™');
|
||||
}
|
||||
|
||||
void IVText::ProcessTexts(const PathType &inFolder, bool recursive, const FuncType &func)
|
||||
{
|
||||
std::vector<PathType> filenames;
|
||||
|
||||
if (recursive)
|
||||
{
|
||||
std::filesystem::recursive_directory_iterator dir_it{inFolder};
|
||||
|
||||
while (dir_it != std::filesystem::recursive_directory_iterator{})
|
||||
{
|
||||
PathType path = dir_it->path();
|
||||
|
||||
if (path.extension() == ".txt")
|
||||
{
|
||||
filenames.push_back(path);
|
||||
}
|
||||
|
||||
++dir_it;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::directory_iterator dir_it{inFolder};
|
||||
|
||||
while (dir_it != std::filesystem::directory_iterator{})
|
||||
{
|
||||
PathType path = dir_it->path();
|
||||
|
||||
if (path.extension() == ".txt")
|
||||
{
|
||||
filenames.push_back(path);
|
||||
}
|
||||
|
||||
++dir_it;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &filename : filenames)
|
||||
{
|
||||
std::ifstream ifs(filename);
|
||||
|
||||
if (!ifs)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
func(filename.string(), ifs);
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::ProcessJsons(const PathType &inFolder, bool recursive, const FuncType &func)
|
||||
{
|
||||
std::vector<PathType> filenames;
|
||||
|
||||
if (recursive)
|
||||
{
|
||||
std::filesystem::recursive_directory_iterator dir_it{inFolder};
|
||||
|
||||
while (dir_it != std::filesystem::recursive_directory_iterator{})
|
||||
{
|
||||
PathType path = dir_it->path();
|
||||
|
||||
if (path.extension() == ".json")
|
||||
{
|
||||
filenames.push_back(path);
|
||||
}
|
||||
|
||||
++dir_it;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::filesystem::directory_iterator dir_it{inFolder};
|
||||
|
||||
while (dir_it != std::filesystem::directory_iterator{})
|
||||
{
|
||||
PathType path = dir_it->path();
|
||||
|
||||
if (path.extension() == ".json")
|
||||
{
|
||||
filenames.push_back(path);
|
||||
}
|
||||
|
||||
++dir_it;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &filename : filenames)
|
||||
{
|
||||
std::ifstream ifs(filename);
|
||||
|
||||
if (!ifs)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
func(filename.string(), ifs);
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::LoadTextFunc(const std::string &filename, std::ifstream &stream)
|
||||
{
|
||||
std::regex table_regex(R"(\[([0-9a-zA-Z_]{1,7})\])");
|
||||
std::regex entry_regex(R"((0[xX][0-9a-fA-F]{8})=(.*))");
|
||||
|
||||
std::match_results<const char *> matches;
|
||||
|
||||
std::string line;
|
||||
|
||||
auto table_iter = m_Data.end();
|
||||
|
||||
size_t line_no = 0;
|
||||
|
||||
SkipUTF8Signature(stream);
|
||||
|
||||
while (std::getline(stream, line))
|
||||
{
|
||||
++line_no;
|
||||
|
||||
line.erase(0, line.find_first_not_of(' '));
|
||||
line.erase(line.find_last_not_of(' ') + 1);
|
||||
|
||||
if (line.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.front() == '[' && regex_match(line.c_str(), matches, table_regex))
|
||||
{
|
||||
table_iter = m_Data.emplace(matches.str(1), std::vector<TextEntry>()).first;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool is_original = (line.front() == ';');
|
||||
bool match_result;
|
||||
|
||||
if (is_original)
|
||||
{
|
||||
match_result = std::regex_match(line.c_str() + 1, matches, entry_regex);
|
||||
}
|
||||
else
|
||||
{
|
||||
match_result = std::regex_match(line.c_str(), matches, entry_regex);
|
||||
}
|
||||
|
||||
if (match_result)
|
||||
{
|
||||
if (table_iter != m_Data.end())
|
||||
{
|
||||
auto &table_cont = table_iter->second;
|
||||
auto hash = std::stoul(matches.str(1), nullptr, 16);
|
||||
|
||||
if (table_cont.empty() || table_cont.back().hash != hash)
|
||||
{
|
||||
table_cont.emplace_back();
|
||||
}
|
||||
|
||||
auto p_entry = &table_cont.back();
|
||||
|
||||
p_entry->hash = hash;
|
||||
|
||||
auto b_string = matches.str(2);
|
||||
|
||||
if (is_original)
|
||||
{
|
||||
p_entry->original = b_string;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_entry->translated = b_string;
|
||||
}
|
||||
|
||||
if (!is_original && (ranges::count(b_string, '~') & 1) == 1)
|
||||
{
|
||||
fmt::printf("%s: 第%u行的\'~\'个数不是偶数!\n", filename, line_no);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fmt::printf("%s: 第%u行没有所属的表。\n", filename, line_no);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fmt::printf("%s: 第%u行无法识别。\n", filename, line_no);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::LoadJsonFunc(const std::string &filename, std::ifstream &stream)
|
||||
{
|
||||
rapidjson::Document doc;
|
||||
|
||||
std::string json(std::istreambuf_iterator<char>{stream}, std::istreambuf_iterator<char>{});
|
||||
|
||||
auto table_iter = m_Data.end();
|
||||
|
||||
if (doc.Parse(json.c_str()).HasParseError())
|
||||
{
|
||||
fmt::printf("%s: json解析失败。\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!doc.IsObject())
|
||||
{
|
||||
fmt::printf("%s: json类型错误。\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto it = doc.MemberBegin(); it != doc.MemberEnd(); ++it)
|
||||
{
|
||||
std::string table_name = it->name.GetString();
|
||||
table_iter = m_Data.emplace(table_name, std::vector<TextEntry>()).first;
|
||||
|
||||
auto text_entries_array = doc[table_name.c_str()].GetArray();
|
||||
|
||||
for (auto &entry : text_entries_array)
|
||||
{
|
||||
auto obj = entry.GetObj();
|
||||
|
||||
table_iter->second.emplace_back(
|
||||
TextEntry{obj["hash"].GetUint(), obj["original"].GetString(), obj["translated"].GetString()});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::CollectCharsFunc(std::ifstream &stream, std::set<char32_t> &chars)
|
||||
{
|
||||
std::vector<char> u8_buffer;
|
||||
std::vector<char32_t> u32_buffer;
|
||||
|
||||
u8_buffer.assign(std::istreambuf_iterator(stream), std::istreambuf_iterator<char>());
|
||||
utf8::utf8to32(u8_buffer.begin(), u8_buffer.end(), std::back_inserter(u32_buffer));
|
||||
|
||||
for (auto chr : u32_buffer)
|
||||
{
|
||||
if (!IsNativeCharacter(chr) && chr != 0x3000 && chr != 0xFEFF)
|
||||
{
|
||||
chars.insert(chr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::LoadTexts(const PathType &inFolder)
|
||||
{
|
||||
ProcessTexts(inFolder, false,
|
||||
[this](const std::string &filename, std::ifstream &stream) { LoadTextFunc(filename, stream); });
|
||||
}
|
||||
|
||||
void IVText::LoadJsons(const PathType &inFolder)
|
||||
{
|
||||
ProcessJsons(inFolder, false,
|
||||
[this](const std::string &filename, std::ifstream &stream) { LoadJsonFunc(filename, stream); });
|
||||
}
|
||||
|
||||
void IVText::GenerateBinary(const PathType &output_binary) const
|
||||
{
|
||||
BinaryFile file(output_binary, "wb");
|
||||
|
||||
std::vector<uint8_t> buffer;
|
||||
long writePostion;
|
||||
|
||||
GXTHeader gxtHeader;
|
||||
|
||||
TableBlock tableBlock;
|
||||
KeyBlockOthers keyBlock;
|
||||
|
||||
DataBlock dataBlock;
|
||||
|
||||
TableEntry tableEntry;
|
||||
KeyEntry keyEntry;
|
||||
|
||||
std::vector<TableEntry> tables;
|
||||
std::vector<KeyEntry> keys;
|
||||
std::vector<uint16_t> datas;
|
||||
|
||||
if (!file)
|
||||
{
|
||||
fmt::printf("创建输出文件 %s 失败。\n", output_binary.string());
|
||||
return;
|
||||
}
|
||||
|
||||
gxtHeader.Version = 4;
|
||||
gxtHeader.CharBits = 16;
|
||||
file.Write(gxtHeader);
|
||||
|
||||
tableBlock.TABL[0] = 'T';
|
||||
tableBlock.TABL[1] = 'A';
|
||||
tableBlock.TABL[2] = 'B';
|
||||
tableBlock.TABL[3] = 'L';
|
||||
|
||||
keyBlock.Body.TKEY[0] = 'T';
|
||||
keyBlock.Body.TKEY[1] = 'K';
|
||||
keyBlock.Body.TKEY[2] = 'E';
|
||||
keyBlock.Body.TKEY[3] = 'Y';
|
||||
|
||||
dataBlock.TDAT[0] = 'T';
|
||||
dataBlock.TDAT[1] = 'D';
|
||||
dataBlock.TDAT[2] = 'A';
|
||||
dataBlock.TDAT[3] = 'T';
|
||||
|
||||
tableBlock.Size = m_Data.size() * sizeof(TableEntry);
|
||||
file.Write(tableBlock);
|
||||
|
||||
writePostion = 4 + 8 + tableBlock.Size;
|
||||
|
||||
tables.clear();
|
||||
|
||||
for (auto &table : m_Data)
|
||||
{
|
||||
keys.clear();
|
||||
datas.clear();
|
||||
|
||||
strcpy(tableEntry.Name, table.first.c_str());
|
||||
tableEntry.Offset = static_cast<int>(writePostion);
|
||||
tables.push_back(tableEntry);
|
||||
|
||||
strcpy(keyBlock.Name, table.first.c_str());
|
||||
keyBlock.Body.Size = table.second.size() * sizeof(KeyEntry);
|
||||
|
||||
for (auto &entry : table.second)
|
||||
{
|
||||
if (entry.original.empty() || entry.translated.empty())
|
||||
{
|
||||
fmt::printf("遇到缺失的文本项:\nhash: %u/0x%08X\n\n", entry.hash, entry.hash);
|
||||
}
|
||||
|
||||
if (!CompareTokens(entry.original, entry.translated))
|
||||
{
|
||||
fmt::printf("遇到Token与原文不一致的译文:\nhash: %u/0x%08X\n\n", entry.hash, entry.hash);
|
||||
}
|
||||
|
||||
keyEntry.Hash = entry.hash;
|
||||
keyEntry.Offset = static_cast<std::int32_t>(datas.size() * 2);
|
||||
|
||||
auto w_string_to_write = U8ToWide(entry.translated);
|
||||
ranges::copy(w_string_to_write, std::back_inserter(datas));
|
||||
|
||||
keys.push_back(keyEntry);
|
||||
}
|
||||
|
||||
dataBlock.Size = static_cast<std::int32_t>(datas.size() * 2);
|
||||
|
||||
file.Seek(writePostion, SEEK_SET);
|
||||
|
||||
if (table.first == "MAIN")
|
||||
{
|
||||
file.Write(keyBlock.Body);
|
||||
}
|
||||
else
|
||||
{
|
||||
file.Write(keyBlock);
|
||||
}
|
||||
|
||||
file.WriteArray(keys);
|
||||
file.Write(dataBlock);
|
||||
file.WriteArray(datas);
|
||||
|
||||
writePostion = file.Tell();
|
||||
}
|
||||
|
||||
file.Seek(4 + 8, SEEK_SET);
|
||||
file.WriteArray(tables);
|
||||
}
|
||||
|
||||
void IVText::GenerateCollection(const PathType &outFile, const std::set<char32_t> &chars)
|
||||
{
|
||||
size_t count = 0;
|
||||
u8StringType u8_text;
|
||||
|
||||
for (auto character : chars)
|
||||
{
|
||||
if (count == g_CharsPerLine)
|
||||
{
|
||||
u8_text += L'\n';
|
||||
count = 0;
|
||||
}
|
||||
|
||||
utf8::append(character, std::back_inserter(u8_text));
|
||||
|
||||
++count;
|
||||
}
|
||||
|
||||
std::ofstream stream(outFile, std::ios::trunc);
|
||||
|
||||
std::copy_n(u8_text.c_str(), u8_text.size(), std::ostreambuf_iterator(stream));
|
||||
}
|
||||
|
||||
void IVText::GenerateTable(const PathType &outFile, const std::set<char32_t> &chars)
|
||||
{
|
||||
BinaryFile stream(outFile, "wb");
|
||||
stream.WriteArray2(ranges::to_vector(chars));
|
||||
}
|
||||
|
||||
IVText::wStringType IVText::U8ToWide(const u8StringType &u8_string)
|
||||
{
|
||||
wStringType result;
|
||||
|
||||
utf8::utf8to16(u8_string.begin(), u8_string.end(), std::back_inserter(result));
|
||||
result.push_back(0);
|
||||
LiteralToGame(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
IVText::u8StringType IVText::WideToU8(const wStringType &wide_string)
|
||||
{
|
||||
u8StringType result;
|
||||
|
||||
if (!wide_string.empty())
|
||||
{
|
||||
utf8::utf16to8(wide_string.begin(), wide_string.end() - 1, std::back_inserter(result));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void IVText::FixCharacters(wStringType &wtext)
|
||||
{
|
||||
// bad character in IV stock text: 0x85 0x92 0x94 0x96 0x97 0xA0
|
||||
// bad character in EFLC stock text: 0x93
|
||||
|
||||
for (auto &character : wtext)
|
||||
{
|
||||
switch (character)
|
||||
{
|
||||
case 0x85:
|
||||
character = L' ';
|
||||
break;
|
||||
|
||||
case 0x92:
|
||||
case 0x94:
|
||||
character = L'\'';
|
||||
break;
|
||||
|
||||
case 0x93: // EFLC
|
||||
break;
|
||||
|
||||
case 0x96:
|
||||
character = L'-';
|
||||
break;
|
||||
|
||||
case 0x97:
|
||||
case 0xA0:
|
||||
character = L' ';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::LiteralToGame(wStringType &wtext)
|
||||
{
|
||||
for (auto &character : wtext)
|
||||
{
|
||||
switch (character)
|
||||
{
|
||||
case L'™':
|
||||
character = 0x99;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::GameToLiteral(wStringType &wtext)
|
||||
{
|
||||
for (auto &character : wtext)
|
||||
{
|
||||
switch (character)
|
||||
{
|
||||
case 0x99:
|
||||
character = L'™';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::set<std::string> IVText::CollectTokens(const std::string &str)
|
||||
{
|
||||
std::set<std::string> result;
|
||||
|
||||
std::vector<std::string::size_type> wave_token_indexes, html_token_indexes;
|
||||
|
||||
//收集token的起止点
|
||||
//明显是不支持嵌套尖括号的
|
||||
for (std::string::size_type index = 0; index < str.length(); ++index)
|
||||
{
|
||||
auto chr = str[index];
|
||||
|
||||
if (chr == '~')
|
||||
{
|
||||
wave_token_indexes.push_back(index);
|
||||
}
|
||||
else if (chr == '<')
|
||||
{
|
||||
if (html_token_indexes.empty() || html_token_indexes.back() == '>')
|
||||
{
|
||||
html_token_indexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (chr == '>')
|
||||
{
|
||||
if (!html_token_indexes.empty() && html_token_indexes.back() == '<')
|
||||
{
|
||||
html_token_indexes.push_back(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((wave_token_indexes.size() & 1) == 1 || (html_token_indexes.size() & 1) == 1)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//提取每个token
|
||||
for (std::vector<std::string::size_type>::size_type token_index = 0; token_index < wave_token_indexes.size() / 2;
|
||||
++token_index)
|
||||
{
|
||||
result.emplace(str, wave_token_indexes[token_index * 2],
|
||||
wave_token_indexes[token_index * 2 + 1] - wave_token_indexes[token_index * 2] + 1);
|
||||
}
|
||||
|
||||
for (std::vector<std::string::size_type>::size_type token_index = 0; token_index < html_token_indexes.size() / 2;
|
||||
++token_index)
|
||||
{
|
||||
result.emplace(str, html_token_indexes[token_index * 2],
|
||||
html_token_indexes[token_index * 2 + 1] - html_token_indexes[token_index * 2] + 1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool IVText::CompareTokens(const std::string &s1, const std::string &s2)
|
||||
{
|
||||
auto token1 = CollectTokens(s1);
|
||||
auto token2 = CollectTokens(s2);
|
||||
|
||||
if (token1.size() != token2.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto &token : token1)
|
||||
{
|
||||
if (!token2.contains(token))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void IVText::LoadBinary(const PathType &inFile)
|
||||
{
|
||||
GXTHeader gxtHeader;
|
||||
TableBlock tableBlock;
|
||||
KeyBlockOthers keyBlock;
|
||||
DataBlock tdatHeader;
|
||||
|
||||
std::vector<TableEntry> tables;
|
||||
std::vector<KeyEntry> keys;
|
||||
std::vector<CharType> datas;
|
||||
|
||||
m_Data.clear();
|
||||
|
||||
auto tableIter = m_Data.end();
|
||||
|
||||
BinaryFile file(inFile, "rb");
|
||||
|
||||
if (!file)
|
||||
{
|
||||
fmt::printf("打开输入文件 %s 失败。\n", inFile.string());
|
||||
return;
|
||||
}
|
||||
|
||||
file.Read(gxtHeader);
|
||||
|
||||
file.Read(tableBlock);
|
||||
|
||||
file.ReadArray(tableBlock.Size / sizeof(TableEntry), tables);
|
||||
|
||||
for (TableEntry &table : tables)
|
||||
{
|
||||
tableIter = m_Data.emplace(table.Name, std::vector<TextEntry>()).first;
|
||||
|
||||
file.Seek(table.Offset, SEEK_SET);
|
||||
|
||||
if (strcmp(table.Name, "MAIN") != 0)
|
||||
{
|
||||
file.Read(keyBlock);
|
||||
}
|
||||
else
|
||||
{
|
||||
file.Read(keyBlock.Body);
|
||||
}
|
||||
|
||||
file.ReadArray(keyBlock.Body.Size / sizeof(KeyEntry), keys);
|
||||
|
||||
file.Read(tdatHeader);
|
||||
|
||||
file.ReadArray(tdatHeader.Size / 2, datas);
|
||||
|
||||
for (auto &key : keys)
|
||||
{
|
||||
wStringType w_string;
|
||||
TextEntry entry;
|
||||
|
||||
entry.hash = key.Hash;
|
||||
|
||||
auto offset = key.Offset / 2;
|
||||
|
||||
while (datas[offset] != 0)
|
||||
{
|
||||
w_string.push_back(datas[offset]);
|
||||
++offset;
|
||||
}
|
||||
w_string.push_back(0);
|
||||
|
||||
FixCharacters(w_string);
|
||||
GameToLiteral(w_string);
|
||||
entry.translated = WideToU8(w_string);
|
||||
|
||||
tableIter->second.emplace_back(std::move(entry));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::GenerateTexts(const PathType &output_texts) const
|
||||
{
|
||||
std::ofstream stream;
|
||||
std::string line;
|
||||
|
||||
for (auto &table : m_Data)
|
||||
{
|
||||
stream.open(output_texts / (table.first + ".txt"), std::ios::trunc);
|
||||
|
||||
if (!stream)
|
||||
{
|
||||
fmt::printf("创建输出文件失败\n");
|
||||
}
|
||||
|
||||
stream.write("\xEF\xBB\xBF", 3);
|
||||
|
||||
stream << fmt::sprintf("[%s]\n", table.first);
|
||||
|
||||
for (auto &entry : table.second)
|
||||
{
|
||||
line = fmt::sprintf("0x%08X=%s\n", entry.hash, entry.translated);
|
||||
stream << ';' << line << line << '\n';
|
||||
}
|
||||
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
void IVText::GenerateJsons(const PathType &output_texts) const
|
||||
{
|
||||
std::ofstream stream;
|
||||
rapidjson::StringBuffer buffer(nullptr, 1024 * 1024 * 10); // 10MB
|
||||
rapidjson::PrettyWriter writer(buffer);
|
||||
|
||||
for (auto &table : m_Data)
|
||||
{
|
||||
stream.open(output_texts / (table.first + ".json"), std::ios::trunc);
|
||||
|
||||
if (!stream)
|
||||
{
|
||||
fmt::printf("创建输出文件失败\n");
|
||||
}
|
||||
|
||||
buffer.Clear();
|
||||
writer.StartObject();
|
||||
writer.Key(table.first.c_str());
|
||||
writer.StartArray();
|
||||
|
||||
for (auto &entry : table.second)
|
||||
{
|
||||
writer.StartObject();
|
||||
writer.Key("hash");
|
||||
writer.Uint(entry.hash);
|
||||
writer.Key("original");
|
||||
writer.String(entry.original.c_str());
|
||||
writer.Key("translated");
|
||||
writer.String(entry.translated.c_str());
|
||||
writer.Key("desc");
|
||||
writer.String("");
|
||||
writer.EndObject();
|
||||
}
|
||||
|
||||
writer.EndArray();
|
||||
writer.EndObject();
|
||||
|
||||
stream.write(buffer.GetString(), buffer.GetSize());
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
#pragma once
|
||||
#include "../common/common.h"
|
||||
#include "../common/stdinc.h"
|
||||
|
||||
struct IVTextTableSorting
|
||||
{
|
||||
// 保证'MAIN'是第一个
|
||||
bool operator()(const std::string &lhs, const std::string &rhs) const
|
||||
{
|
||||
// 确保两个'MAIN'比较结果是false
|
||||
if (rhs == "MAIN")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (lhs == "MAIN")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return lhs < rhs;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct GXTHeader
|
||||
{
|
||||
std::uint16_t Version; // 4
|
||||
std::uint16_t CharBits; // 8 or 16
|
||||
};
|
||||
|
||||
struct TableEntry
|
||||
{
|
||||
char Name[8];
|
||||
std::int32_t Offset;
|
||||
};
|
||||
|
||||
struct TableBlock
|
||||
{
|
||||
char TABL[4];
|
||||
std::int32_t Size;
|
||||
// TableEntry Entries[Size / 12];
|
||||
};
|
||||
|
||||
struct KeyEntry
|
||||
{
|
||||
std::int32_t Offset;
|
||||
std::uint32_t Hash;
|
||||
};
|
||||
|
||||
struct KeyBlockMAIN
|
||||
{
|
||||
char TKEY[4];
|
||||
std::int32_t Size;
|
||||
};
|
||||
|
||||
struct KeyBlockOthers
|
||||
{
|
||||
char Name[8];
|
||||
KeyBlockMAIN Body;
|
||||
};
|
||||
|
||||
struct DataBlock
|
||||
{
|
||||
char TDAT[4];
|
||||
std::int32_t Size;
|
||||
// std::uint16_t Entries[Size / 2];
|
||||
};
|
||||
|
||||
class IVText
|
||||
{
|
||||
public:
|
||||
typedef std::filesystem::path PathType; // 路径类型
|
||||
typedef GTAChar CharType; // 宽字符(GXT)类型
|
||||
typedef std::uint32_t HashType; // GXT key类型
|
||||
typedef std::string u8StringType; // 转码用
|
||||
typedef std::vector<GTAChar> wStringType; // GXT字符串类型(手动补0)
|
||||
typedef std::function<void(const std::string &, std::ifstream &)> FuncType; // 文本func类型
|
||||
|
||||
struct TextEntry
|
||||
{
|
||||
uint32 hash;
|
||||
u8StringType original, translated;
|
||||
};
|
||||
|
||||
void ProcessT2B(const PathType &inFolder, const PathType &outFolder);
|
||||
void ProcessB2T(const PathType &inFile, const PathType &outFolder);
|
||||
void ProcessCollect(const PathType &inFolder, const PathType &outFolder);
|
||||
|
||||
void ProcessT2J(const PathType &inFolder);
|
||||
void ProcessJ2B(const PathType &inFolder, const PathType &outFolder);
|
||||
void ProcessB2J(const PathType &inFile, const PathType &outFolder);
|
||||
|
||||
private:
|
||||
static bool IsNativeCharacter(char32_t character);
|
||||
|
||||
// 用func处理文件夹中每个文件
|
||||
static void ProcessTexts(const PathType &inFolder, bool recursive, const FuncType &func);
|
||||
static void ProcessJsons(const PathType &inFolder, bool recursive, const FuncType &func);
|
||||
|
||||
// func: 加载译文
|
||||
void LoadTextFunc(const std::string &filename, std::ifstream &stream);
|
||||
void LoadJsonFunc(const std::string &filename, std::ifstream &stream);
|
||||
|
||||
// func: 收集文本中需要做成字库的字符
|
||||
static void CollectCharsFunc(std::ifstream &stream, std::set<char32_t> &chars);
|
||||
// 生成字库位置数据和字符集
|
||||
static void GenerateCollection(const PathType &outFile, const std::set<char32_t> &chars);
|
||||
static void GenerateTable(const PathType &outFile, const std::set<char32_t> &chars);
|
||||
|
||||
void LoadBinary(const PathType &inFile);
|
||||
void LoadTexts(const PathType &inFolder);
|
||||
void LoadJsons(const PathType &inFolder);
|
||||
|
||||
void GenerateTexts(const PathType &output_texts) const;
|
||||
void GenerateJsons(const PathType &output_texts) const;
|
||||
void GenerateBinary(const PathType &output_binary) const;
|
||||
|
||||
// 文本/GXT形式互转
|
||||
static wStringType U8ToWide(const u8StringType &u8_string);
|
||||
static u8StringType WideToU8(const wStringType &wide_string);
|
||||
|
||||
// 修正原版GXT中的混乱字符
|
||||
static void FixCharacters(wStringType &wtext);
|
||||
|
||||
// 标准编码转到游戏编码
|
||||
static void LiteralToGame(wStringType &wtext);
|
||||
|
||||
// 游戏编码转到标准编码
|
||||
static void GameToLiteral(wStringType &wtext);
|
||||
|
||||
// 收集'~~'和'<>'形式的Token供比较
|
||||
static std::set<std::string> CollectTokens(const std::string &str);
|
||||
static bool CompareTokens(const std::string &s1, const std::string &s2);
|
||||
|
||||
std::map<std::string, std::vector<TextEntry>, IVTextTableSorting> m_Data;
|
||||
};
|
||||
@@ -1,8 +1,10 @@
|
||||
import struct
|
||||
import json
|
||||
import sys
|
||||
import pathlib
|
||||
import time
|
||||
from pathlib import Path
|
||||
from array import array
|
||||
from collections import namedtuple
|
||||
|
||||
def load_gxt(filename):
|
||||
data = dict()
|
||||
@@ -30,7 +32,6 @@ def load_gxt(filename):
|
||||
tdat_data_offset = tdat_block_offset+8
|
||||
for key_index in range(tkey_count):
|
||||
dat_offset,crc = struct.unpack('II', gxt_bin[tkey_data_offset+8*key_index:tkey_data_offset+8*key_index+8])
|
||||
entry_bytes = bytes()
|
||||
entry_bytes_start_offset = tdat_data_offset+dat_offset
|
||||
entry_bytes_cur_offset = 0
|
||||
dat_str = str()
|
||||
@@ -51,9 +52,9 @@ def load_gxt(filename):
|
||||
return data
|
||||
|
||||
def write_jsons(data:dict, folder):
|
||||
pathlib.Path(folder).mkdir(parents=True,exist_ok=True)
|
||||
Path(folder).mkdir(parents=True,exist_ok=True)
|
||||
for table in data.items():
|
||||
with open(pathlib.Path(folder,f"{table[0]}.json"), "w") as f:
|
||||
with open(Path(folder,f"{table[0]}.json"), "w") as f:
|
||||
f.write(json.dumps({table[0]:table[1]},indent=4,sort_keys=False))
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import struct
|
||||
import json
|
||||
import sys
|
||||
import pathlib
|
||||
import time
|
||||
from pathlib import Path
|
||||
from array import array
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
def collect_chars(folder):
|
||||
chars = set()
|
||||
for j in [j for j in pathlib.Path(folder).glob('*.json') if j.is_file()]:
|
||||
with open(pathlib.Path(folder, j), 'r', encoding='utf-8') as f:
|
||||
for j in [j for j in Path(folder).glob('*.json') if j.is_file()]:
|
||||
with open(Path(folder, j), 'r', encoding='utf-8') as f:
|
||||
chars = chars.union(set(f.read()))
|
||||
|
||||
for ascii in range(1, 256):
|
||||
@@ -25,7 +27,7 @@ def write_charset(chars, filename):
|
||||
char_per_line = 64
|
||||
char_index = 0
|
||||
chars.sort()
|
||||
pathlib.Path(filename).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(filename).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(filename, "w", encoding='utf-8') as f:
|
||||
for char in chars:
|
||||
f.write(char)
|
||||
|
||||
+61
-4
@@ -2,11 +2,29 @@
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
from array import array
|
||||
from collections import namedtuple
|
||||
from sys import exception
|
||||
|
||||
#将一个字符串转换成8字节串
|
||||
def str_to_8bytes(string:str):
|
||||
arr = array('B')
|
||||
bstring = string.encode('utf-8')
|
||||
if len(bstring) == 0 or len(bstring) > 7:
|
||||
raise ValueError
|
||||
|
||||
arr.extend(bstring)
|
||||
|
||||
while len(arr) < 8:
|
||||
arr.append(0)
|
||||
|
||||
return bytes(arr)
|
||||
|
||||
|
||||
def load_jsons(folder):
|
||||
data = dict()
|
||||
for j in [j for j in pathlib.Path(folder).glob('*.json') if j.is_file()]:
|
||||
for j in [j for j in Path(folder).glob('*.json') if j.is_file()]:
|
||||
with open(j,'r',encoding='utf-8') as f:
|
||||
table = json.load(f)
|
||||
for table_name in table:
|
||||
@@ -15,7 +33,9 @@ def load_jsons(folder):
|
||||
return data
|
||||
|
||||
def write_gxt(tables,filename):
|
||||
pathlib.Path(filename).parent.mkdir(parents=True,exist_ok=True)
|
||||
TableEntry = namedtuple("TableEntry", "name offset")
|
||||
KeyEntry = namedtuple("KeyEntry", "offset hash")
|
||||
Path(filename).parent.mkdir(parents=True,exist_ok=True)
|
||||
with open(filename,"wb") as f:
|
||||
f.write(b'\x04\x00\x10\x00')
|
||||
f.write(b'TABL')
|
||||
@@ -26,8 +46,45 @@ def write_gxt(tables,filename):
|
||||
f.write(b'TKEY')
|
||||
#排序表名,首先写入'MAIN'table
|
||||
sorted_keys = ['MAIN']+[table_name for table_name in tables.keys() if table_name != 'MAIN']
|
||||
table_entries = list()
|
||||
for table_name in sorted_keys:
|
||||
pass
|
||||
#生成Table Entry
|
||||
table_entries.append(TableEntry(str_to_8bytes(table_name),f.tell()))
|
||||
key_entries = list()
|
||||
#字符串序列化后的字节
|
||||
data_bytes = array('H')
|
||||
for entry in tables[table_name]:
|
||||
key_entries.append(KeyEntry(len(data_bytes)*2,entry['hash']))
|
||||
if len(entry['translated']) == 0:
|
||||
str_to_serialize = entry['original']
|
||||
else:
|
||||
str_to_serialize = entry['translated']
|
||||
|
||||
data_bytes.extend([ord(c) for c in str_to_serialize])
|
||||
data_bytes.append(0)
|
||||
pass
|
||||
|
||||
#写入key block
|
||||
if table_name != 'MAIN':
|
||||
f.write(str_to_8bytes(table_name))
|
||||
|
||||
f.write(b'TKEY')
|
||||
f.write(struct.pack('I', len(key_entries)*12))
|
||||
for key_entry in key_entries:
|
||||
f.write(struct.pack('II', key_entry.offset, key_entry.hash))
|
||||
|
||||
#写入data block
|
||||
f.write(b'TDAT')
|
||||
f.write(struct.pack('I',len(data_bytes)*2))
|
||||
f.write(data_bytes)
|
||||
|
||||
#最后写入TableBlock
|
||||
f.seek(12)
|
||||
table_block_bytes = array('B')
|
||||
for table_entry in table_entries:
|
||||
table_block_bytes.extend(struct.pack("8sI",table_entry.name,table_entry.offset))
|
||||
|
||||
f.write(table_block_bytes)
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_time = time.perf_counter()
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#include "../common/stdinc.h"
|
||||
#include "IVText.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
IVText instance;
|
||||
bool error = false;
|
||||
|
||||
if (argc == 4)
|
||||
{
|
||||
if (std::strcmp(argv[1], "-b2t") == 0)
|
||||
{
|
||||
instance.ProcessB2T(argv[2], argv[3]);
|
||||
}
|
||||
else if (std::strcmp(argv[1], "-b2j") == 0)
|
||||
{
|
||||
instance.ProcessB2J(argv[2], argv[3]);
|
||||
}
|
||||
else if (std::strcmp(argv[1], "-t2b") == 0)
|
||||
{
|
||||
instance.ProcessT2B(argv[2], argv[3]);
|
||||
}
|
||||
else if (std::strcmp(argv[1], "-j2b") == 0)
|
||||
{
|
||||
instance.ProcessJ2B(argv[2], argv[3]);
|
||||
}
|
||||
else if (std::strcmp(argv[1], "-collect") == 0)
|
||||
{
|
||||
instance.ProcessCollect(argv[2], argv[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
else if (argc == 3)
|
||||
{
|
||||
if (std::strcmp(argv[1], "-t2j") == 0)
|
||||
{
|
||||
instance.ProcessT2J(argv[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
|
||||
if (error)
|
||||
{
|
||||
fmt::printf("使用方法:\n"
|
||||
"gxt转txt: gxt_tool.exe -b2t [gxt文件] [txt文件夹]\n"
|
||||
"gxt转json: gxt_tool.exe -b2j [gxt文件] [json文件夹]\n"
|
||||
"txt转gxt: gxt_tool.exe -t2b [txt文件夹] [gxt文件夹]\n"
|
||||
"json转gxt: gxt_tool.exe -j2b [json文件夹] [gxt文件夹]\n"
|
||||
"txt转json: gxt_tool.exe -t2j [txt文件夹]\n"
|
||||
"生成字库: gxt_tool.exe -collect [(任意文本格式)文件夹] [输出文件夹]\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user