Skip to content

Commit

Permalink
Support BMG file IDs, apply changes in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed May 10, 2024
1 parent 39cfe3c commit cc5ba52
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 47 deletions.
4 changes: 3 additions & 1 deletion NitroEdit_pc/modules/Bmg/include/base_Include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ constexpr ntr::Result ResultLoadBMGXmlInvalidChildTag = 0xe009;
constexpr ntr::Result ResultLoadBMGXmlMessageIdMismatch = 0xe00a;
constexpr ntr::Result ResultLoadBMGXmlAttributesMismatch = 0xe00b;
constexpr ntr::Result ResultLoadBMGXmlInvalidMessageToken = 0xe00c;
constexpr ntr::Result ResultBMGInvalidFileId = 0xe00d;

constexpr ntr::ResultDescriptionEntry ResultDescriptionTable[] = {
{ ResultEditBMGInvalidEscapeByte, "Invalid escape byte found in BMG text" },
Expand All @@ -29,7 +30,8 @@ constexpr ntr::ResultDescriptionEntry ResultDescriptionTable[] = {
{ ResultLoadBMGXmlInvalidChildTag, "Invalid XML file to parse as BMG: expected child 'message' element" },
{ ResultLoadBMGXmlMessageIdMismatch, "Invalid XML file to parse as BMG: some messages have ID and others do not" },
{ ResultLoadBMGXmlAttributesMismatch, "Invalid XML file to parse as BMG: messages have different attributes size" },
{ ResultLoadBMGXmlInvalidMessageToken, "Invalid XML file to parse as BMG: invalid message token (expected plain text or escape token)" }
{ ResultLoadBMGXmlInvalidMessageToken, "Invalid XML file to parse as BMG: invalid message token (expected plain text or escape token)" },
{ ResultBMGInvalidFileId, "Invalid BMG file ID integer" }
};

inline constexpr ntr::Result GetResultDescription(const ntr::Result rc, std::string &out_desc) {
Expand Down
2 changes: 1 addition & 1 deletion NitroEdit_pc/modules/Bmg/include/ui/ui_BmgSubWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace ui {

private:
ntr::fmt::BMG::Encoding GetCurrentEncoding();
void ReloadEncoding();
void ReloadFields();
void OnMessageListViewDoubleClick(const QModelIndex &idx);

Ui::BmgSubWindow *win_ui;
Expand Down
13 changes: 10 additions & 3 deletions NitroEdit_pc/modules/Bmg/source/base_Include.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ namespace {
ntr::Result ParseFromXml(const QDomDocument &doc, std::shared_ptr<ntr::fmt::BMG> &out_bmg) {
const auto root = doc.documentElement();

ntr::u32 file_id = 0;
ntr::fmt::BMG::Encoding enc;
std::vector<ntr::fmt::BMG::Message> msgs;

Expand All @@ -286,6 +287,13 @@ namespace {
const auto encoding_raw = root.attribute("encoding");
NTR_R_TRY(ParseEncoding(encoding_raw, enc));

if(root.hasAttribute("id")) {
const auto file_id_raw = root.attribute("encoding");
if(!ParseStringInteger(file_id_raw, file_id)) {
NTR_R_FAIL(ResultBMGInvalidFileId);
}
}

std::optional<bool> has_message_ids;
std::optional<size_t> attribute_size;

Expand Down Expand Up @@ -393,8 +401,7 @@ namespace {
attribute_size.emplace(0);
}

// TODO: file id?
NTR_R_TRY(out_bmg->CreateFrom(enc, has_message_ids.value(), attribute_size.value(), msgs, 0));
NTR_R_TRY(out_bmg->CreateFrom(enc, has_message_ids.value(), attribute_size.value(), msgs, file_id));

NTR_R_SUCCEED();
}
Expand All @@ -410,7 +417,7 @@ ntr::Result SaveBmgXml(std::shared_ptr<ntr::fmt::BMG> &bmg, const QString &path)
}

QTextStream out(&file);
bmg_xml.save(out, 4);
bmg_xml.save(out, 0);
file.close();

NTR_R_SUCCEED();
Expand Down
76 changes: 38 additions & 38 deletions NitroEdit_pc/modules/Bmg/source/mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,42 +200,34 @@ namespace {
}
}

void CreateBmg(const std::string &enc_str, const std::string &txt_path, const std::string &out_bmg_path) {
ntr::fmt::BMG::Encoding enc;
if(!ReadEncoding(enc_str, enc)) {
std::cerr << "'" << enc_str << "' is not a supported encoding!" << std::endl;
return;
void ConvertToXml(const std::string &bmg_path, const std::string &out_xml_path) {
auto bmg = std::make_shared<ntr::fmt::BMG>();
auto rc = bmg->ReadFrom(bmg_path, std::make_shared<ntr::fs::StdioFileHandle>());
if(rc.IsSuccess()) {
rc = SaveBmgXml(bmg, QString::fromStdString(out_xml_path));
if(rc.IsFailure()) {
std::cerr << "Unable to convert BMG and save XML file to '" << out_xml_path << "': " << rc.GetDescription() << std::endl;
return;
}
}
std::ifstream ifs(txt_path);
if(!ifs) {
std::cerr << "'" << txt_path << "' is not a valid text file!" << std::endl;
else {
std::cerr << "Unable to read BMG file '" << bmg_path << "': " << rc.GetDescription() << std::endl;
return;
}
}

std::vector<ntr::fmt::BMG::Message> msgs;
std::string txt_str;
while(std::getline(ifs, txt_str)) {
ntr::fmt::BMG::Message msg;

if(!BuildMessage(txt_str, msg)) {
void CreateFromXml(const std::string &xml_path, const std::string &out_bmg_path) {
auto bmg = std::make_shared<ntr::fmt::BMG>();
auto rc = LoadBmgXml(QString::fromStdString(xml_path), bmg);
if(rc.IsSuccess()) {
rc = bmg->WriteTo(out_bmg_path, std::make_shared<ntr::fs::StdioFileHandle>());
if(rc.IsFailure()) {
std::cerr << "Unable to save BMG file to '" << out_bmg_path << "': " << rc.GetDescription() << std::endl;
return;
}

msgs.push_back(msg);
}

ntr::fmt::BMG bmg;
auto rc = bmg.CreateFrom(enc, false, 0, msgs, 0);
if(rc.IsFailure()) {
std::cerr << "Unable to create BMG file: " << rc.GetDescription() << std::endl;
return;
}

std::cout << "saving..." << std::endl;

rc = bmg.WriteTo(out_bmg_path, std::make_shared<ntr::fs::StdioFileHandle>());
if(rc.IsFailure()) {
std::cerr << "Unable to save BMG file: " << rc.GetDescription() << std::endl;
else {
std::cerr << "Unable to load XML file '" << xml_path << "' as BMG: " << rc.GetDescription() << std::endl;
return;
}
}
Expand All @@ -246,21 +238,25 @@ namespace {

args::Group commands(parser, "Commands:", args::Group::Validators::Xor);

args::Command list(commands, "list", "List BMG message strings");
args::Command list(commands, "list", "List BMG messages");
args::Group list_required(list, "", args::Group::Validators::All);
args::ValueFlag<std::string> list_bmg_file(list_required, "bmg_file", "Input BMG file", {'i', "in"});
args::Flag list_verbose(list, "verbose", "Print more information, not just the message strings", {'v', "verbose"});
args::Flag list_verbose(list, "verbose", "Print more information, not just the messages", {'v', "verbose"});

args::Command get(commands, "get", "Get BMG message by index");
args::Command get(commands, "get", "Get specific BMG message by index");
args::Group get_required(get, "", args::Group::Validators::All);
args::ValueFlag<std::string> get_bmg_file(get_required, "bmg_file", "Input BMG file", {'i', "in"});
args::ValueFlag<std::string> get_idx(get_required, "idx", "BMG message index", {"idx"});

args::Command create(commands, "create", "Create BMG file(s) from text file");
args::Command create(commands, "create", "Create BMG file from XML file");
args::Group create_required(create, "", args::Group::Validators::All);
args::ValueFlag<std::string> create_txt_file(create_required, "txt_file", "Input text file", {'i', "in"});
args::ValueFlag<std::string> create_txt_file(create_required, "xml_file", "Input XML file", {'i', "in"});
args::ValueFlag<std::string> create_out_bmg_file(create_required, "out_bmg_file", "Output BMG file", {'o', "out"});
args::ValueFlag<std::string> create_encoding(create_required, "encoding", "Message encoding ('utf8' or 'utf16' are supported)", {"enc"});

args::Command convert(commands, "convert", "Convert BMG file to XML file");
args::Group convert_required(convert, "", args::Group::Validators::All);
args::ValueFlag<std::string> convert_bmg_file(convert_required, "bmg_file", "Input BMG file", {'i', "in"});
args::ValueFlag<std::string> convert_out_xml_file(convert_required, "out_xml_file", "Output XML file", {'o', "out"});

try {
parser.ParseArgs(args);
Expand All @@ -282,10 +278,14 @@ namespace {
GetBmgString(bmg_path, idx_str);
}
else if(create) {
const auto enc_str = create_encoding.Get();
const auto txt_path = create_txt_file.Get();
const auto xml_path = create_txt_file.Get();
const auto out_bmg_path = create_out_bmg_file.Get();
CreateBmg(enc_str, txt_path, out_bmg_path);
CreateFromXml(xml_path, out_bmg_path);
}
else if(convert) {
const auto bmg_path = convert_bmg_file.Get();
const auto out_xml_path = convert_out_xml_file.Get();
ConvertToXml(bmg_path, out_xml_path);
}
}

Expand Down
24 changes: 20 additions & 4 deletions NitroEdit_pc/modules/Bmg/source/ui/ui_BmgSubWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ namespace ui {
this->win_ui->comboBoxEncoding->addItem(FormatEncoding(ntr::fmt::BMG::Encoding::UTF16));
this->win_ui->comboBoxEncoding->addItem(FormatEncoding(ntr::fmt::BMG::Encoding::ShiftJIS));
this->win_ui->comboBoxEncoding->addItem(FormatEncoding(ntr::fmt::BMG::Encoding::UTF8));
this->ReloadEncoding();
this->ReloadFields();

this->win_ui->comboBoxEncoding->setToolTip("Change the encoding used with all messages");

this->win_ui->lineEditFileId->setToolTip("Change the file ID");

this->win_ui->listViewMessages->setToolTip("Double-click on a message to edit it");

this->ReloadPreviews();
Expand All @@ -36,6 +38,14 @@ namespace ui {
return true;
}

ntr::u32 cur_file_id;
if(!ParseStringInteger(this->win_ui->lineEditFileId->text(), cur_file_id)) {
return true;
}
if(cur_file_id != this->bmg_file->info.file_id) {
return true;
}

if(this->msg_edited) {
return true;
}
Expand All @@ -45,6 +55,11 @@ namespace ui {

ntr::Result BmgSubWindow::Save() {
this->bmg_file->header.encoding = this->GetCurrentEncoding();

if(!ParseStringInteger(this->win_ui->lineEditFileId->text(), this->bmg_file->info.file_id)) {
NTR_R_FAIL(ResultBMGInvalidFileId);
}

NTR_R_TRY(this->bmg_file->WriteTo(this->file_handle));

this->msg_edited = false;
Expand Down Expand Up @@ -73,7 +88,7 @@ namespace ui {
NTR_R_TRY(LoadBmgXml(file, this->bmg_file));
}

this->ReloadEncoding();
this->ReloadFields();
this->ReloadPreviews();
this->msg_edited = true;
}
Expand Down Expand Up @@ -128,8 +143,9 @@ namespace ui {
return static_cast<ntr::fmt::BMG::Encoding>(this->win_ui->comboBoxEncoding->currentIndex() + 1);
}

void BmgSubWindow::ReloadEncoding() {
void BmgSubWindow::ReloadFields() {
this->win_ui->comboBoxEncoding->setCurrentIndex(static_cast<size_t>(this->bmg_file->header.encoding) - 1);
this->win_ui->lineEditFileId->setText(QString::number(this->bmg_file->info.file_id));
}

void BmgSubWindow::OnMessageListViewDoubleClick(const QModelIndex &idx) {
Expand Down
13 changes: 13 additions & 0 deletions NitroEdit_pc/modules/Bmg/source/ui/ui_BmgSubWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEditFileId">
<property name="geometry">
<rect>
<x>280</x>
<y>40</y>
<width>113</width>
<height>32</height>
</rect>
</property>
<property name="placeholderText">
<string>File ID</string>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit cc5ba52

Please sign in to comment.