add .include directives in quest assembler

This commit is contained in:
Martin Michelsen
2024-06-22 15:22:32 -07:00
parent ce8277b96a
commit 9ca1b79409
5 changed files with 45 additions and 4 deletions
+7 -1
View File
@@ -1237,7 +1237,13 @@ Action a_assemble_quest_script(
uncompressed .bind file instead.\n",
+[](Arguments& args) {
string text = read_input_data(args);
string result = assemble_quest_script(text);
const string& input_filename = args.get<string>(1, false);
string include_dir = (!input_filename.empty() && (input_filename != "-"))
? dirname(input_filename)
: ".";
string result = assemble_quest_script(text, include_dir);
bool compress = !args.get<bool>("decompressed");
if (compress) {
result = prs_compress_optimal(result);
+2 -1
View File
@@ -543,7 +543,8 @@ QuestIndex::QuestIndex(
file_data = decode_dlq_data(load_file(file_path));
filename.resize(filename.size() - 4);
} else if (ends_with(filename, ".txt")) {
file_data = assemble_quest_script(load_file(file_path));
string include_dir = dirname(file_path);
file_data = assemble_quest_script(load_file(file_path), include_dir);
filename.resize(filename.size() - 4);
if (ends_with(filename, ".bin")) {
filename.push_back('d');
+30 -1
View File
@@ -12,6 +12,12 @@
#include <unordered_map>
#include <vector>
#ifdef HAVE_RESOURCE_FILE
#include <resource_file/Emulators/PPC32Emulator.hh>
#include <resource_file/Emulators/SH4Emulator.hh>
#include <resource_file/Emulators/X86Emulator.hh>
#endif
#include "BattleParamsIndex.hh"
#include "CommandFormats.hh"
#include "Compression.hh"
@@ -1964,7 +1970,7 @@ struct RegisterAssigner {
array<shared_ptr<Register>, 0x100> numbered_regs;
};
std::string assemble_quest_script(const std::string& text) {
std::string assemble_quest_script(const std::string& text, const std::string& include_directory) {
auto lines = split(text, '\n');
// Strip comments and whitespace
@@ -2196,6 +2202,29 @@ std::string assemble_quest_script(const std::string& text) {
} else if (starts_with(line, ".zero ")) {
size_t size = stoull(line.substr(6), nullptr, 0);
code_w.extend_by(size, 0x00);
} else if (starts_with(line, ".include_bin ")) {
string filename = line.substr(13);
strip_whitespace(filename);
code_w.write(load_file(include_directory + "/" + filename));
} else if (starts_with(line, ".include_native ")) {
#ifdef HAVE_RESOURCE_FILE
string filename = line.substr(16);
strip_whitespace(filename);
string native_text = load_file(include_directory + "/" + filename);
string code;
if (is_ppc(quest_version)) {
code = std::move(PPC32Emulator::assemble(native_text).code);
} else if (is_x86(quest_version)) {
code = std::move(X86Emulator::assemble(native_text).code);
} else if (is_sh4(quest_version)) {
code = std::move(SH4Emulator::assemble(native_text).code);
} else {
throw runtime_error("unknown architecture");
}
code_w.write(code);
#else
throw runtime_error("native code cannot be compiled; rebuild newserv with libresource_file");
#endif
}
continue;
}
+1 -1
View File
@@ -83,6 +83,6 @@ struct PSOQuestHeaderBB {
Episode episode_for_quest_episode_number(uint8_t episode_number);
std::string disassemble_quest_script(const void* data, size_t size, Version version, uint8_t override_language = 0xFF, bool reassembly_mode = false);
std::string assemble_quest_script(const std::string& text);
std::string assemble_quest_script(const std::string& text, const std::string& include_directory);
Episode find_quest_episode_from_script(const void* data, size_t size, Version version);