Explorar o código

Split the kernel data installer and fixed three bugs in it

- The installer is divided by subject: attacks, items, equipment, materia and characters. The record types are in their own headers.
- Added KernelTextList, which reads the names and the compressed descriptions and replaces the copies of that code, and KernelDataParts, with the reading and writing that several kinds of data share.
- Broke up the reading and the writing of the materia.
- Removed a write of a debug file to a fixed path in a home folder.
- The "all rows" targeting bit is read: it was setting "default enemy" instead. Seven attacks change in attacks.lua.
- Reading the summon names and the initial savemap no longer clears the lists of other data.
- Added tests for the text reader and the shared parts.
Iñigo Valentin hai 21 horas
pai
achega
b3bfa9be34

+ 7 - 0
src/installer/extractors/CMakeLists.txt

@@ -3,7 +3,14 @@ vgears_sources(
     BattleDataInstaller.cpp
     FieldDataInstaller.cpp
     FieldTextWriter.cpp
+    KernelAttacks.cpp
+    KernelCharacters.cpp
     KernelDataInstaller.cpp
+    KernelDataParts.cpp
+    KernelEquipment.cpp
+    KernelItems.cpp
+    KernelMateria.cpp
+    KernelTextList.cpp
     MediaDataInstaller.cpp
     ModelsAndAnimationsDb.cpp
     MovieCamera.cpp

+ 402 - 0
src/installer/extractors/KernelAttackData.h

@@ -0,0 +1,402 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "common/TypeDefine.h"
+#include "installer/extractors/KernelDataTypes.h"
+
+/*
+ * Records of the commands and the attacks.
+ */
+namespace KernelData{
+
+    /**
+     * Menus that must be shown when selecting a command.
+     */
+    enum COMMAND_MENU{
+
+        /**
+         * No menu, execute command directly, without target selection.
+         */
+        NONE = 0,
+
+        /**
+         * Show magic menu.
+         */
+        MAGIC = 1,
+
+        /**
+         * Show summon menu.
+         */
+        SUMMON = 2,
+
+        /**
+         * Show item menu.
+         */
+        ITEM = 3,
+
+        /**
+         * Show enemy skill menu.
+         */
+        E_SKILL = 4,
+
+        /**
+         * Show throw menu.
+         */
+        THROW = 5,
+
+        /**
+         * Show limit menu.
+         */
+        LIMIT = 6,
+
+        /**
+         * No menu, skip to target selection.
+         */
+        TARGET = 7,
+
+        /**
+         * Show W-Magic menu
+         */
+        W_MAGIC = 8,
+
+        /**
+         * Show W-Summon menu
+         */
+        W_SUMMON = 9,
+
+        /**
+         * Show W-Item menu
+         */
+        W_ITEM = 10,
+
+        /**
+         * Show Coin menu
+         */
+        COIN = 11,
+
+    };
+
+    /**
+     * Data for each command.
+     *
+     * As found in KERNEL.BIN, file 0, in order, up to camera_multiple.
+     * Members after camera_multiple are not found literally in KERNEL.BIN,
+     * but are derived from other members or found in other files of the
+     * kernel (text files).
+     */
+    struct CommandData{
+
+        /**
+         * Cursor action when the command is selected.
+         */
+        u8 cursor_action;
+
+        /**
+         * Targeting mode. 1 byte.
+         *
+         * See {@see target} and {@see TargetModes} for more information.
+         */
+        u8 target_raw;
+
+        /**
+         * Unknown data (Always 0XFFFF). 2 bytes.
+         */
+        u16 unknown;
+
+        /**
+         * Camera movement ID for single target. 2 bytes.
+         *
+         * Determines the camera movement when the command is executed over
+         * a single target.
+         */
+        u16 camera_single;
+
+        /**
+         * Camera movement ID for multiple target. 2 bytes.
+         *
+         * Determines the camera movement when the command is executed over multiple targets.
+         */
+        u16 camera_multiple;
+
+        /**
+         * Command ID.
+         *
+         * Not actually in the item file data, it's the order at which it
+         * appears.
+         */
+        int id;
+
+        /**
+         * Command name.
+         *
+         * Not in the same file as the rest of the data, but on file 17.
+         */
+        std::string name;
+
+        /**
+         * Command description.
+         *
+         * Not in the same file as the rest of the data, but on file 9.
+         */
+        std::string description;
+
+        /**
+         * Target selection mode.
+         *
+         * Derived from {@see target_raw}.
+         */
+        Target target;
+
+        /**
+         * Menu name
+         *
+         * Derived from {@see cursor_action}.
+         */
+        std::string menu;
+
+    };
+
+    /**
+     * Data for each attack.
+     *
+     * As found in KERNEL.BIN, file 1, in order, up to camera_multiple.
+     * Members after special_raw are not found literally in KERNEL.BIN, but
+     * are derived from other members.
+     */
+    struct AttackData{
+
+        /**
+         * Probability of the attack to land. 1 byte.
+         */
+        u8 accuracy;
+
+        /**
+         * Impact effect. 1 byte.
+         *
+         * The sprite shown when the attack hits.
+         */
+        u8 impact_effect;
+
+        /**
+         * Index of the animation played by the target when hit. 1 byte.
+         */
+        u8 hurt_anim;
+
+        /**
+         * Unknown data. 1 bytes.
+         */
+        u8 unknown;
+
+        /**
+         * MP cost. 2 bytes.
+         */
+        u16 mp;
+
+        /**
+         * Impact sound. 2 bytes.
+         */
+        u16 sound;
+
+        /**
+         * Camera movement ID for single target. 2 bytes.
+         *
+         * Determines the camera movement when the command is executed over
+         * a single target.
+         */
+        u16 camera_single;
+
+        /**
+         * Camera movement ID for multiple target. 2 bytes.
+         *
+         * Determines the camera movement when the command is executed over
+         * multiple targets.
+         */
+        u16 camera_multiple;
+
+        /**
+         * Targeting mode. 1 byte.
+         *
+         * See {@see target} and {@see TargetModes} for more information.
+         */
+        u8 target_raw;
+
+        /**
+         * Atack effect ID. 1 byte.
+         *
+         * Used for animation in battle.
+         */
+        u8 effect;
+
+        /**
+         * Damage formula. 1 byte.
+         *
+         * This byte is divided into two nybbles (four bits). Upper nybble
+         * determines what considerations are made in calculating damage
+         * such as physical/magical, allowing criticals, how to calculate
+         * accuracy, etc. There are also three sets of known formulae that
+         * are paired with the upper nybble values. These are selected in
+         * the Lower Nybble and determine how to calculate pre-defense
+         * damage.
+         */
+        u8 damage_raw;
+
+        /**
+         * The attack power. 1 byte.
+         *
+         * Used for damage/healing calculation.
+         */
+        u8 power;
+
+        /**
+         * The restore type.
+         *
+         * See {@see restore_type} and {@see RESTORE_TYPE}.
+         */
+        u8 condition_raw;
+
+        /**
+         * Information about status change mode and chance. 1 bytes.
+         */
+        u8 status_change_raw;
+
+        /**
+         * Special flags. 1 byte.
+         *
+         * @todo Document and retrieve with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Attack_Special_Effects
+         */
+        u8 additional_effects_raw;
+
+        /**
+         * Special flags. 1 byte.
+         *
+         * @todo Document and retrieve with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Item_data
+         */
+        u8 additional_effects_mod_raw;
+
+        /**
+         * Information about what status can be inflicted or cured. 4 bytes.
+         *
+         * The first six bits are the chance of inflicting/curing (out of
+         * 63). If the seventh bit is set, the status is cured instead of
+         * inflicted. If the eighth bit is set, then the status is toggled
+         * (cured if present, inflicted if not present).
+         */
+        u32 status_raw;
+
+        /**
+         * Information about the item elements. 2 bytes.
+         */
+        u16 element_raw;
+
+        /**
+         * Special flags. 2 bytes.
+         *
+         * @todo document and parse with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
+         */
+        u16 special_raw;
+
+        /**
+         * Attack ID.
+         *
+         * Not in the item file data, it's the order at which it appears.
+         */
+        int id;
+
+        /**
+         * Attack name.
+         *
+         * Not in the same file as the rest of the data, but on file 18.
+         */
+        std::string name;
+
+        /**
+         * Attack description.
+         *
+         * Not in the same file as the rest of the data, but on file 10.
+         */
+        std::string description;
+
+        /**
+         * Target selection mode.
+         *
+         * Derived from {@see target_raw}.
+         */
+        Target target;
+
+        /**
+         * The damage formula.
+         *
+         * It's the upper nybble in {@see damage_raw}.
+         */
+        u8 damage_formula;
+
+        /**
+         * The damage modifier.
+         *
+         * It's the lower nybble in {@see damage_raw}.
+         */
+        u8 damage_modifier;
+
+        /**
+         * What the attack restores when used.
+         *
+         * Same as {@see condition_raw}, except for the wrong cases.
+         */
+        RESTORE_TYPE restore_type;
+
+        /**
+         * Status effects.
+         *
+         * Derived from {@see status_change_raw} and {@see status_raw}.
+         */
+        StatusEffect status;
+
+        /**
+         * Elements of the attack.
+         *
+         * Derived from {@see element_raw}.
+         */
+        std::vector<int> elements;
+
+    };
+
+    /**
+     * List of summon names.
+     *
+     * There is no data file about key items in KERNEL.BIN, there are only
+     * names (file 26).
+     */
+    struct SummonNameData{
+
+        /**
+         * Summon ID.
+         *
+         * Not actually in KERNEL.BIN, it;s the order at which they appear.
+         */
+        int id;
+
+        /**
+         * Summon name.
+         */
+        std::string name;
+    };
+}

+ 285 - 0
src/installer/extractors/KernelAttacks.cpp

@@ -0,0 +1,285 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <fstream>
+#include <iostream>
+#include <memory>
+#include "installer/extractors/KernelDataInstaller.h"
+#include "installer/extractors/KernelDataParts.h"
+#include "installer/extractors/KernelTextList.h"
+
+using namespace KernelData;
+
+int KernelDataInstaller::ReadCommands(){
+
+    // Empty the command list
+    commands_.clear();
+
+    File command_file = kernel_.ExtractGZip(KERNEL_COMMAND_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_COMMAND_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_COMMAND_DESCRIPTIONS));
+    int command_count = 0;
+
+    // There are exactly 32 commands. Some of them are blank, so there is not really a
+    // reliable exit condition.
+    for (int i = 0; i < 32; i ++){
+
+        CommandData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // Read data from the command       m, data section.
+        data.cursor_action = command_file.readU8();
+        data.target_raw = command_file.readU8();
+        data.unknown = command_file.readU16LE();
+        data.camera_single = command_file.readU16LE();
+        data.camera_multiple = command_file.readU16LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
+        else data.target.selection_enabled = false;
+        if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
+        else data.target.default_enemy = false;
+        // Default multiple is inverted
+        if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = false;
+        else data.target.default_multiple = true;
+        if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
+        else data.target.toggle_multiple = false;
+        if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
+        else data.target.fixed_row = false;
+        if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
+        else data.target.short_range = false;
+        if (data.target_raw == (data.target_raw | 0x40)) data.target.all_rows = true;
+        else data.target.all_rows = false;
+        if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
+        else data.target.random = false;
+        switch (data.cursor_action){
+            case NONE: data.menu = "Battle.Menu.NONE"; break;
+            case MAGIC: data.menu = "Battle.Menu.MAGIC"; break;
+            case SUMMON: data.menu = "Battle.Menu.SUMMON"; break;
+            case ITEM: data.menu = "Battle.Menu.ITEM"; break;
+            case E_SKILL: data.menu = "Battle.Menu.E_SKILL"; break;
+            case THROW: data.menu = "Battle.Menu.THROW"; break;
+            case LIMIT: data.menu = "Battle.Menu.LIMIT"; break;
+            case TARGET: data.menu = "Battle.Menu.TARGET"; break;
+            case W_MAGIC: data.menu = "Battle.Menu.W_MAGIC"; break;
+            case W_SUMMON: data.menu = "Battle.Menu.W_SUMMON"; break;
+            case W_ITEM: data.menu = "Battle.Menu.W_ITEM"; break;
+            case COIN: data.menu = "Battle.Menu.COIN"; break;
+            default: data.menu = "Battle.Menu.NONE";
+        }
+
+        // Add to the list of commands.
+        if (data.name != ""){
+            command_names_[i] = data.name;
+            commands_.push_back(data);
+            command_count ++;
+        }
+    }
+
+    return command_count;
+}
+
+void KernelDataInstaller::WriteCommands(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (CommandData command : commands_){
+        file << "Game.Commands[" << command.id << "] = {\n"
+          << "    name = \"" << command.name << "\",\n"
+          << "    description = \"" << command.description << "\",\n"
+          << "    menu = " << command.menu << ",\n"
+          << "    camera = {single = " << command.camera_single << ", "
+          << "multiple = " << command.camera_multiple << "}\n}\n";
+    }
+    file.close();
+}
+
+int KernelDataInstaller::ReadAttacks(){
+
+    // Empty the item list
+    attacks_.clear();
+
+    File attacks_file = kernel_.ExtractGZip(KERNEL_ATTACK_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_ATTACK_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_ATTACK_DESCRIPTIONS));
+    int attack_count = 0;
+
+    limit_attacks_.clear();
+
+    // Limit data, from ff7.exe (file offset of the table at 0x910d94, in the .data section).
+    const int LIMIT_TABLE_OFFSET = 0x910d94 - 0x7b0000 + 0x3ae000;
+    std::unique_ptr<File> exe_file;
+    try{
+        exe_file.reset(new File(exe_path_));
+    }
+    catch (...){
+        std::cerr << "[WARNING] Can't read the data of the limits from ff7.exe." << std::endl;
+    }
+
+    // The kernel has data for 128 attacks, but names and descriptions for 256. The last 128 are the
+    // limit break techniques, which the characters refer to by ID (128 and up).
+    for (int i = 0; i < 256; i ++){
+
+        AttackData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // Limit techniques only have name and description. Both start with control codes, drop
+        // everything before the first letter.
+        // The data of the limits that the characters use (128 to 183) is not in the kernel: it is a table of
+        // attack records, with the same format, in the data of ff7.exe (0x910d94, indexed by the limit ID minus
+        // 128; found from the code that ff7.exe uses to load the limits of a character, 0x5c9524).
+        File* source = &attacks_file;
+        bool limit_from_exe = false;
+        if (i >= 128 && i <= 183 && exe_file != nullptr){
+            exe_file->SetOffset(LIMIT_TABLE_OFFSET + (i - 128) * 0x1c);
+            source = exe_file.get();
+            limit_from_exe = true;
+        }
+        if (i >= 128){
+            for (std::string* text : {&data.name, &data.description}){
+                size_t first = text->find_first_of(
+                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
+                if (first == std::string::npos) text->clear();
+                else text->erase(0, first);
+            }
+            data.id = i;
+            if (!limit_from_exe){
+                limit_attacks_.push_back(data);
+                continue;
+            }
+        }
+
+        // Read data from the item data section.
+        data.accuracy = source->readU8();
+        data.impact_effect = source->readU8();
+        data.hurt_anim = source->readU8();
+        data.unknown = source->readU8();
+        data.mp = source->readU16LE();
+        data.sound = source->readU16LE();
+        data.camera_single = source->readU16LE();
+        data.camera_multiple = source->readU16LE();
+        data.target_raw = source->readU8();
+        data.effect = source->readU8();
+        data.damage_raw = source->readU8();
+        data.power = source->readU8();
+        data.condition_raw = source->readU8();
+        data.status_change_raw = source->readU8();
+        data.additional_effects_raw = source->readU8();
+        data.additional_effects_mod_raw = source->readU8();
+        data.status_raw = source->readU32LE();
+        data.element_raw = source->readU16LE();
+        data.special_raw = source->readU16LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        ReadTarget(data.target_raw, data.target);
+        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
+        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
+        data.restore_type = ReadRestoreType(data.condition_raw);
+        ReadStatusEffect(data.status_change_raw, data.status_raw, data.status);
+        ReadElements(data.element_raw, data.elements);
+
+        // Add to the list of items.
+        attacks_.push_back(data);
+        attack_names_[i] = data.name;
+        attack_count ++;
+    }
+
+    return attack_count;
+}
+
+void KernelDataInstaller::WriteAttacks(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (AttackData attack : attacks_){
+        file << "Game.Attacks[" << attack.id << "] = {\n"
+          << "    name = \"" << attack.name << "\",\n"
+          << "    description = \"" << attack.description << "\",\n"
+          << "    accuracy = " << static_cast<int>(attack.accuracy) << ",\n"
+          << "    impact_effect = " << static_cast<int>(attack.impact_effect) << ",\n"
+          << "    hurt_anim = " << static_cast<int>(attack.hurt_anim) << ",\n"
+          << "    mp = " << attack.mp << ",\n"
+          << "    sound = " << attack.sound << ",\n"
+          << "    camera = {single = " << attack.camera_single << ", "
+          << "multiple = " << attack.camera_multiple << "},\n"
+          << "    effect = " << static_cast<int>(attack.effect) << ",\n"
+          << "    dmg_formula = " << static_cast<int>(attack.damage_formula) << ",\n"
+          << "    dmg_modifier = " << static_cast<int>(attack.damage_modifier) << ",\n"
+          << "    power = " << static_cast<int>(attack.power) << ",\n"
+          << "    additional = {effect = " << static_cast<int>(attack.additional_effects_raw)
+          << ", modifier = " << static_cast<int>(attack.additional_effects_mod_raw) << "},\n"
+          << "    restore = " << attack.restore_type << ",\n";
+        WriteTarget(file, attack.target);
+        file << "    elements = {";
+        for (int element : attack.elements) file << element << ", ";
+        file << "},\n    status = {";
+        // Status effects: ID (bit position), mode (0 inflict, 1 cure, 2 toggle) and chance out of 63.
+        for (int status : attack.status.status){
+            file << "{status = " << status << ", mode = " << attack.status.mode
+              << ", chance = " << attack.status.chance << "}, ";
+        }
+        file << "}\n}\n";
+    }
+    // Limit techniques: only name and description are known.
+    for (AttackData attack : limit_attacks_){
+        file << "Game.Attacks[" << attack.id << "] = {\n"
+          << "    name = \"" << attack.name << "\",\n"
+          << "    description = \"" << attack.description << "\"\n"
+          << "}\n";
+    }
+    file.close();
+}
+
+int KernelDataInstaller::ReadSummonNames(){
+
+    // Empty the summon list
+    summon_names_.clear();
+
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_SUMMON_ATTACK_NAMES));
+    int summon_count = 0;
+
+    for (int i = 0; i < 16; i ++){ // There are exactly 16.
+
+        SummonNameData data;
+
+        data.name = names.NextName();
+
+        // If the key item doesn't have a name, it means all key items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Add to the list of items.
+        data.id = i;
+        summon_names_.push_back(data);
+        summon_count ++;
+    }
+
+    return summon_count;
+}
+
+void KernelDataInstaller::WriteSummonNames(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (SummonNameData summon : summon_names_)
+        file << "Game.Summon_Names[" << summon.id << "] = {\"" << summon.name << "\"}\n";
+    file.close();
+}

+ 321 - 0
src/installer/extractors/KernelCharacterData.h

@@ -0,0 +1,321 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include "common/TypeDefine.h"
+
+/*
+ * Records of the characters and their growth.
+ */
+namespace KernelData{
+
+    /**
+     * Character data.
+     *
+     * Contains information about a character. Contained in the file 3 of
+     * the kernel, one entry for each character. Each section is 56 bytes
+     * long. The character ID is not actually in the data, but it's the
+     * order at which they appear. The character name is also not in the
+     * data and it must be filled manually.
+     */
+    struct CharacterData{
+
+        /**
+         * Strength stat growth curve. 1 byte.
+         */
+        u8 curve_str;
+
+        /**
+         * Vitality stat growth curve. 1 byte.
+         */
+        u8 curve_vit;
+
+        /**
+         * Magic stat growth curve. 1 byte.
+         */
+        u8 curve_mag;
+
+        /**
+         * Spirit stat growth curve. 1 byte.
+         */
+        u8 curve_spr;
+
+        /**
+         * Dexterity stat growth curve. 1 byte.
+         */
+        u8 curve_dex;
+
+        /**
+         * Luck stat growth curve. 1 byte.
+         */
+        u8 curve_lck;
+
+        /**
+         * HP stat growth curve. 1 byte.
+         */
+        u8 curve_hp;
+
+        /**
+         * MP stat growth curve. 1 byte.
+         */
+        u8 curve_mp;
+
+        /**
+         * Experience-for-next-level growth curve. 1 byte.
+         */
+        u8 curve_exp;
+
+        /**
+         * Ignored data. 1 byte.
+         */
+        u8 unused_0;
+
+        /**
+         * Starting level. 1 byte.
+         */
+        u8 initial_level;
+
+        /**
+         * Ignored data. 1 byte.
+         */
+        u8 unused_1;
+
+        /**
+         * 1-1 limit slot command. 1 byte.
+         */
+        u8 limit_1_1;
+
+        /**
+         * 1-2 limit slot command. 1 byte.
+         */
+        u8 limit_1_2;
+
+        /**
+         * 1-3 limit slot command. 1 byte, unused.
+         */
+        u8 limit_1_3;
+
+        /**
+         * 2-1 limit slot command. 1 byte.
+         */
+        u8 limit_2_1;
+
+        /**
+         * 2-2 limit slot command. 1 byte.
+         */
+        u8 limit_2_2;
+
+        /**
+         * 2-3 limit slot command. 1 byte, unused.
+         */
+        u8 limit_2_3;
+
+        /**
+         * 3-1 limit slot command. 1 byte.
+         */
+        u8 limit_3_1;
+
+        /**
+         * 3-2 limit slot command. 1 byte.
+         */
+        u8 limit_3_2;
+
+        /**
+         * 3-3 limit slot command. 1 byte, unused.
+         */
+        u8 limit_3_3;
+
+        /**
+         * 4-1 limit slot command. 1 byte.
+         */
+        u8 limit_4_1;
+
+        /**
+         * 4-2 limit slot command. 1 byte, unused.
+         */
+        u8 limit_4_2;
+
+        /**
+         * 4-3 limit slot command. 1 byte, unused.
+         */
+        u8 limit_4_3;
+
+        /**
+         * Kills required to unlock limit level 2. 2 bytes.
+         */
+        u16 limit_2_kills;
+
+        /**
+         * Kills required to unlock limit level 3. 2 bytes.
+         */
+        u16 limit_3_kills;
+
+        /**
+         * Kills required to unlock limit 1-2. 2 bytes.
+         */
+        u16 limit_1_2_uses;
+
+        /**
+         * Kills required to unlock limit 1-3. 2 bytes, unused.
+         */
+        u16 limit_1_3_uses;
+
+        /**
+         * Kills required to unlock limit 2-2. 2 bytes.
+         */
+        u16 limit_2_2_uses;
+
+        /**
+         * Kills required to unlock limit 2-3. 2 bytes, unused.
+         */
+        u16 limit_2_3_uses;
+
+        /**
+         * Kills required to unlock limit 3-2. 2 bytes.
+         */
+        u16 limit_3_2_uses;
+
+        /**
+         * Kills required to unlock limit 3-3. 2 bytes, unused.
+         */
+        u16 limit_3_3_uses;
+
+        /**
+         * HP divisor for limit level 1.
+         */
+        u32 limit_1_div;
+
+        /**
+         * HP divisor for limit level 2.
+         */
+        u32 limit_2_div;
+
+        /**
+         * HP divisor for limit level 3.
+         */
+        u32 limit_3_div;
+
+        /**
+         * HP divisor for limit level 4.
+         */
+        u32 limit_4_div;
+
+        /**
+         * Character ID.
+         */
+        int id;
+
+        /**
+         * Character default name.
+         */
+        std::string name;
+
+    };
+
+    /**
+     * Types of stat curves.
+     *
+     * Althought any of them can be used for any stat, this is their
+     * intended use.
+     */
+    enum STAT_CURVE_TYPE{
+
+        /**
+         * Normal curve, used for primary stat.
+         */
+        CURVE_PRIMARY = 0,
+
+        /**
+         * HP curve. Base values must be multiplied by 40 when calculating.
+         */
+        CURVE_HP,
+
+        /**
+         * MP curve. Base values must be multiplied by 2 when calculating.
+         */
+        CURVE_MP,
+
+        /**
+         * Experience curve. Base must be ignored when calculating. Gradient
+         * is quadratic.
+         */
+        CURVE_EXP
+
+    };
+
+    /**
+     * Stat curve.
+     *
+     * determines value growth
+     */
+    struct StatCurve{
+
+        /**
+         * Curve ID.
+         */
+        int id;
+
+        /**
+         * Type of curve.
+         */
+        STAT_CURVE_TYPE type;
+
+        /**
+         * Curve gradients.
+         */
+        u8 gradient[8];
+
+        /**
+         * Curve bases.
+         */
+        s8 base[8];
+
+    };
+
+    /**
+     * Battle data.
+     *
+     * Contains several pieces of data related to character growth.
+     * Contained in the file 3 of the kernel, after the character data
+     * (starts at offset 0x01F8). The kernel contains more data, but it's
+     * not needed for V-Gears (random table, scene lookup...)
+     */
+    struct GrowthData{
+
+        /**
+         * Random bonus to primary stats, 12 entries of 1 byte.
+         */
+        u8 bonus_stat[12];
+
+        /**
+         * Random bonus to HP stat, 12 entries of 1 byte.
+         */
+        u8 bonus_hp[12];
+
+        /**
+         * Random bonus to MP stat, 12 entries of 1 byte.
+         */
+        u8 bonus_mp[12];
+
+        /**
+         * Stat curves. 64 curves of 16 bit each.
+         *
+         * 37 for primary stats, 9 for HP, 9 form MP, 9 for experience.
+         */
+        StatCurve curves[64];
+    };
+}

+ 488 - 0
src/installer/extractors/KernelCharacters.cpp

@@ -0,0 +1,488 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <algorithm>
+#include <fstream>
+#include <iostream>
+#include <iterator>
+#include "formats/FF7NameLookup.h"
+#include "installer/extractors/KernelDataInstaller.h"
+
+using namespace KernelData;
+
+int KernelDataInstaller::ReadCharacters(){
+
+    // TODO: Do something about young Cloud and Sephiroth.
+
+    // Empty the item list
+    characters_.clear();
+
+    File characters_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
+    int character_count = 0;
+
+    for (int i = 0; i < 11; i ++){ // 11 characters
+
+        CharacterData data;
+
+        // Read data from the item data section.
+        data.curve_str = characters_file.readU8();
+        data.curve_vit = characters_file.readU8();
+        data.curve_mag = characters_file.readU8();
+        data.curve_spr = characters_file.readU8();
+        data.curve_dex = characters_file.readU8();
+        data.curve_lck = characters_file.readU8();
+        data.curve_hp = characters_file.readU8();
+        data.curve_mp = characters_file.readU8();
+        data.curve_exp = characters_file.readU8();
+        data.unused_0 = characters_file.readU8();
+        data.initial_level = characters_file.readU8();
+        data.unused_1 = characters_file.readU8();
+        data.limit_1_1 = characters_file.readU8();
+        data.limit_1_2 = characters_file.readU8();
+        data.limit_1_3 = characters_file.readU8();
+        data.limit_2_1 = characters_file.readU8();
+        data.limit_2_2 = characters_file.readU8();
+        data.limit_2_3 = characters_file.readU8();
+        data.limit_3_1 = characters_file.readU8();
+        data.limit_3_2 = characters_file.readU8();
+        data.limit_3_3 = characters_file.readU8();
+        data.limit_4_1 = characters_file.readU8();
+        data.limit_4_2 = characters_file.readU8();
+        data.limit_4_3 = characters_file.readU8();
+        data.limit_2_kills = characters_file.readU16LE();
+        data.limit_3_kills = characters_file.readU16LE();
+        data.limit_1_2_uses = characters_file.readU16LE();
+        data.limit_1_3_uses = characters_file.readU16LE();
+        data.limit_2_2_uses = characters_file.readU16LE();
+        data.limit_2_3_uses = characters_file.readU16LE();
+        data.limit_3_2_uses = characters_file.readU16LE();
+        data.limit_3_3_uses = characters_file.readU16LE();
+        data.limit_1_div = characters_file.readU32LE();
+        data.limit_2_div = characters_file.readU32LE();
+        data.limit_3_div = characters_file.readU32LE();
+        data.limit_4_div = characters_file.readU32LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        data.name = VGears::NameLookup::CharName(i);
+
+        // Add to the list of characters.
+        characters_.push_back(data);
+        character_count ++;
+    }
+
+    return character_count;
+}
+
+void KernelDataInstaller::WriteCharacters(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (CharacterData character : characters_){
+        file
+          << "Game.Characters[" << character.id << "] = {\n"
+          << "    id = " << character.id << ",\n"
+          << "    name = \"" << character.name << "\",\n"
+          << "    initial_level = \"" << static_cast<int>(character.initial_level) << "\",\n"
+          << "    curves = {\n"
+          << "        str = " << static_cast<int>(character.curve_str) << ",\n"
+          << "        vit = " << static_cast<int>(character.curve_vit) << ",\n"
+          << "        mag = " << static_cast<int>(character.curve_mag) << ",\n"
+          << "        spr = " << static_cast<int>(character.curve_spr) << ",\n"
+          << "        dex = " << static_cast<int>(character.curve_dex) << ",\n"
+          << "        lck = " << static_cast<int>(character.curve_lck) << ",\n"
+          << "        hp = " << static_cast<int>(character.curve_hp) << ",\n"
+          << "        mp = " << static_cast<int>(character.curve_mp) << ",\n"
+          << "        exp = " << static_cast<int>(character.curve_exp) << ",\n"
+          << "    },\n"
+          << "    limit_divisors = {" << character.limit_1_div << ", " << character.limit_2_div << ", "
+          << character.limit_3_div << ", " << character.limit_4_div << "},\n"
+          << "    limits = {\n"
+          << "        [1] = {\n"
+          << "            kills = 0,\n"
+          << "            techniques = {\n"
+          << "                [1] = {\n"
+          << "                    command = " << static_cast<int>(character.limit_1_1) << ",\n"
+          << "                    uses = 0\n"
+          << "                },\n";
+        if (character.limit_1_2 < 255){ // Some characters only have 1 limit per level.
+            file
+              << "                [2] = {\n"
+              << "                    command = " << static_cast<int>(character.limit_1_2) << ",\n"
+              << "                    uses = " << character.limit_1_2_uses << "\n"
+              << "                },\n";
+        }
+        file
+          << "            }\n"
+          << "        },\n";
+        // Limit level 2.
+        if (character.limit_2_kills < 65535){
+            file
+              << "        [2] = {\n"
+              << "            kills = " << character.limit_2_kills << ",\n"
+              << "            techniques = {\n"
+              << "                [1] = {\n"
+              << "                    command = " << static_cast<int>(character.limit_2_1) << ",\n"
+              << "                    uses = 0\n"
+              << "                },\n";
+            if (character.limit_2_2 < 255){ // Some characters only have 1 limit per level.
+                file
+                  << "                [2] = {\n"
+                  << "                    command = "
+                  << static_cast<int>(character.limit_2_2) << ",\n"
+                  << "                    uses = " << character.limit_2_2_uses << "\n"
+                  << "                },\n";
+            }
+            file
+              << "            }\n"
+              << "        },\n";
+        }
+        // Limit level 3.
+        if (character.limit_3_kills < 65535){
+            file
+              << "        [3] = {\n"
+              << "            kills = " << character.limit_3_kills << ",\n"
+              << "            techniques = {\n"
+              << "                [1] = {\n"
+              << "                    command = " << static_cast<int>(character.limit_3_1) << ",\n"
+              << "                    uses = 0\n"
+              << "                },\n";
+            if (character.limit_3_2 < 255){ // Some characters only have 1 limit per level.
+                file
+                  << "                [2] = {\n"
+                  << "                    command = "
+                  << static_cast<int>(character.limit_3_2) << ",\n"
+                  << "                    uses = " << character.limit_3_2_uses << "\n"
+                  << "                },\n";
+            }
+            file
+              << "            }\n"
+              << "        },\n";
+        }
+        // Limit level 4.
+        if (character.limit_4_1 < 255){
+            file
+              << "        [4] = {\n"
+              << "            kills = 0,\n"
+              << "            techniques = {\n"
+              << "                [1] = {\n"
+              << "                    command = " << static_cast<int>(character.limit_4_1) << ",\n"
+              << "                    uses = 0\n"
+              << "                },\n"
+              << "            }\n"
+              << "        },\n";
+        }
+        file
+          << "    }\n}\n";
+    }
+    file.close();
+}
+
+void KernelDataInstaller::ReadGrowth(){
+
+    File growth_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
+    growth_file.SetOffset(0x01F8); //Skip character data.
+
+    for (int i = 0; i < 12; i ++) growth_.bonus_stat[i] = growth_file.readU8();
+    for (int i = 0; i < 12; i ++) growth_.bonus_hp[i] = growth_file.readU8();
+    for (int i = 0; i < 12; i ++) growth_.bonus_mp[i] = growth_file.readU8();
+    for (int i = 0; i < 64; i ++){
+        growth_.curves[i].id = i;
+        if (i < 37) growth_.curves[i].type = CURVE_PRIMARY;
+        else if (i >=37 && i < 46) growth_.curves[i].type = CURVE_HP;
+        else if (i >=46 && i < 55) growth_.curves[i].type = CURVE_MP;
+        else growth_.curves[i].type = CURVE_EXP;
+        for (int j = 0; j < 8; j ++){
+            growth_.curves[i].gradient[j] = growth_file.readU8();
+            growth_.curves[i].base[j] = static_cast<s8>(growth_file.readU8());
+        }
+    }
+}
+
+void KernelDataInstaller::WriteGrowth(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    file << "Game.Growth.bonus.primary = {";
+    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_stat[i]) << ", ";
+    file << "}\nGame.Growth.bonus.hp = {";
+    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_hp[i]) << ", ";
+    file << "}\nGame.Growth.bonus.mp = {";
+    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_mp[i]) << ", ";
+    file << "}\n";
+    for (int i = 0; i < 64; i ++){
+        file << "Game.Growth.curves[" << i << "] = {\n";
+        for (int j = 0; j < 8; j ++){
+            file << "    [" << j << "] = {gradient = " << static_cast<int>(growth_.curves[i].gradient[j])
+              << ", base = " << static_cast<int>(growth_.curves[i].base[j]) << "},\n";
+        }
+        file << "}\n";
+    }
+
+    file.close();
+}
+
+void KernelDataInstaller::ReadInitialSaveMap(){
+
+    File savemap_file = kernel_.ExtractGZip(KERNEL_INITIALIZATION_DATA);
+    // The initial savemap in KERNEL.BIN startas at 0x0054, directly into Cloud's level.
+    // It skips the header (PC and PSX, checksum, preview data and window colour).
+    for (int i = 0; i < 9; i ++) ReadSaveMapCharacter(savemap_file, savemap_.characters[i]);
+    // The initial savemap ends up after character data, end now.
+}
+
+void KernelDataInstaller::ReadSaveMapCharacter(File& savemap_file, SaveMap::Character& character){
+    character.identifier = savemap_file.readU8();
+    character.level = savemap_file.readU8();
+    character.str = savemap_file.readU8();
+    character.vit = savemap_file.readU8();
+    character.mag = savemap_file.readU8();
+    character.spr = savemap_file.readU8();
+    character.dex = savemap_file.readU8();
+    character.lck = savemap_file.readU8();
+    character.str_bonus = savemap_file.readU8();
+    character.vit_bonus = savemap_file.readU8();
+    character.mag_bonus = savemap_file.readU8();
+    character.spr_bonus = savemap_file.readU8();
+    character.dex_bonus = savemap_file.readU8();
+    character.lck_bonus = savemap_file.readU8();
+    character.limit_level = savemap_file.readU8();
+    character.limit_bar = savemap_file.readU8();
+    bool name_end = false;
+    character.name = "";
+    for (int j = 0; j < 12; j ++){
+        character.name_raw[j] = savemap_file.readU8();
+        if (name_end == false){
+            if (character.name_raw[j] != 0XFF){
+                character.name += ENGLISH_CHARS[character.name_raw[j]];
+            }
+            else name_end = true;
+        }
+    }
+    character.weapon = savemap_file.readU8();
+    character.armor = savemap_file.readU8();
+    character.accessory = savemap_file.readU8();
+    character.status_raw = savemap_file.readU8();
+    if (character.status_raw == (character.status_raw | 0x10)){
+        character.sadness = true;
+        character.fury = false;
+    }
+    else if (character.status_raw == (character.status_raw | 0x20)){
+        character.sadness = false;
+        character.fury = true;
+    }
+    else{
+        character.sadness = false;
+        character.fury = false;
+    }
+    character.row_raw = savemap_file.readU8();
+    if (character.row_raw == 0xFE) character.back_row = true;
+    else character.back_row = false;
+    character.level_progress = savemap_file.readU8();
+    character.learned_limits_raw = savemap_file.readU16LE();
+    for (int j = 0; j < 4; j ++){
+        character.limits_learned[j][0] = false;
+        character.limits_learned[j][1] = false;
+    }
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0001))
+        character.limits_learned[0][0] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0002))
+        character.limits_learned[0][1] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0008))
+        character.limits_learned[1][0] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0010))
+        character.limits_learned[1][1] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0040))
+        character.limits_learned[2][0] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0080))
+        character.limits_learned[2][1] = true;
+    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0200))
+        character.limits_learned[3][0] = true;
+    character.kills = savemap_file.readU16LE();
+    character.limit_uses[0] = savemap_file.readU16LE();
+    character.limit_uses[1] = savemap_file.readU16LE();
+    character.limit_uses[2] = savemap_file.readU16LE();
+    character.hp = savemap_file.readU16LE();
+    character.base_hp = savemap_file.readU16LE();
+    character.mp = savemap_file.readU16LE();
+    character.base_mp = savemap_file.readU16LE();
+    character.unknown = savemap_file.readU32LE();
+    character.max_hp = savemap_file.readU16LE();
+    character.max_mp = savemap_file.readU16LE();
+    character.exp = savemap_file.readU32LE();
+    for (int m = 0; m < 8; m ++){
+        character.weapon_materia[m].id = savemap_file.readU8();
+        // TODO: Identify Enemy Skill, do special treatment
+        character.weapon_materia[m].ap // WARNING: AP has only three bytes.
+          = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
+    }
+    for (int m = 0; m < 8; m ++){
+        character.armor_materia[m].id = savemap_file.readU8();
+        // TODO: Identify Enemy Skill, do special treatment
+        character.armor_materia[m].ap // WARNING: AP has only three bytes.
+          = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
+    }
+    character.exp_to_next = savemap_file.readU32LE();
+}
+
+void KernelDataInstaller::ReadRecruitRecords(const std::string& exe_path){
+    recruit_records_found_ = false;
+    std::ifstream exe(exe_path, std::ios::binary);
+    const std::vector<u8> data(
+      (std::istreambuf_iterator<char>(exe)), std::istreambuf_iterator<char>()
+    );
+    // The data of Cait Sith and Vincent are two character records like the ones of the savemap, one
+    // after the other (at 0x9138d0 in the 1998 build). They are found by their contents: the
+    // character IDs 6 and 7, valid levels, weapons of each one and names that end.
+    const size_t size = 0x84;
+    for (size_t offset = 0; offset + 2 * size <= data.size(); offset ++){
+        bool found = true;
+        for (int r = 0; r < 2 && found; r ++){
+            const u8* record = &data[offset + r * size];
+            const int first_weapon = r == 0 ? 101 : 114;
+            const int last_weapon = r == 0 ? 113 : 127;
+            found = record[0] == 6 + r && record[1] >= 1 && record[1] <= 99
+              && record[0x1C] >= first_weapon && record[0x1C] <= last_weapon
+              && std::find(record + 0x10, record + 0x1C, 0xFF) != record + 0x1C;
+        }
+        if (!found) continue;
+        File records(&data[offset], 0, 2 * size);
+        for (int r = 0; r < 2; r ++) ReadSaveMapCharacter(records, recruit_records_[r]);
+        recruit_records_found_ = true;
+        return;
+    }
+    std::cerr << "[WARNING] Can't find the data of Cait Sith and Vincent in ff7.exe." << std::endl;
+}
+
+void KernelDataInstaller::WriteInitialSaveMap(std::string file_name){
+    // The initial save map only contains character data.
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (int c = 0; c < 9; c ++){
+        file << "Characters[" << c << "] = ";
+        WriteSaveMapCharacter(file, savemap_.characters[c]);
+        file << "Characters.recalculate_stats(" << c << ")\n\n";
+    }
+    if (recruit_records_found_){
+        file << "-- Data that Cait Sith and Vincent get when they join, from ff7.exe.\n"
+          << "Game.RecruitRecords = {}\n";
+        for (int r = 0; r < 2; r ++){
+            file << "Game.RecruitRecords[" << 6 + r << "] = ";
+            WriteSaveMapCharacter(file, recruit_records_[r]);
+        }
+    }
+    file.close();
+}
+
+void KernelDataInstaller::WriteSaveMapCharacter(
+  std::ostream& file, const SaveMap::Character& character
+){
+    int total_count; // Utility to count in loops.
+    file
+      << "{\n"
+      << "    char_id = " << static_cast<int>(character.identifier) << ",\n"
+      << "    name = \"" << character.name << "\",\n"
+      << "    enabled = false,\n"
+      << "    locked = false,\n"
+      << "    level = " << static_cast<int>(character.level) << ",\n"
+      << "    kills = " << character.kills << ",\n"
+      << "    back_row = " << (character.back_row ? "true" : "false") << ",\n"
+      << "    exp = " << character.exp << ",\n"
+      << "    exp_to_next = " << character.exp_to_next << ",\n"
+      << "    stats = {\n"
+      << "        str = {base = " << static_cast<int>(character.str)
+      << ", bonus = " << static_cast<int>(character.str_bonus)
+      << ", total = " << static_cast<int>(character.str + character.str_bonus) << "},\n"
+      << "        vit = {base = " << static_cast<int>(character.vit)
+      << ", bonus = " << static_cast<int>(character.vit_bonus)
+      << ", total = " << static_cast<int>(character.vit + character.vit_bonus) << "},\n"
+      << "        mag = {base = " << static_cast<int>(character.mag)
+      << ", bonus = " << static_cast<int>(character.mag_bonus)
+      << ", total = " << static_cast<int>(character.mag + character.mag_bonus) << "},\n"
+      << "        spr = {base = " << static_cast<int>(character.spr)
+      << ", bonus = " << static_cast<int>(character.spr_bonus)
+      << ", total = " << static_cast<int>(character.spr + character.spr_bonus) << "},\n"
+      << "        dex = {base = " << static_cast<int>(character.dex)
+      << ", bonus = " << static_cast<int>(character.dex_bonus)
+      << ", total = " << static_cast<int>(character.dex + character.dex_bonus) << "},\n"
+      << "        lck = {base = " << static_cast<int>(character.lck)
+      << ", bonus = " << static_cast<int>(character.lck_bonus)
+      << ", total = " << static_cast<int>(character.lck + character.lck_bonus) << "},\n"
+      << "        hp = {current = " << character.hp  << ", base = " << character.base_hp
+      << ", max = " << character.max_hp << "},\n"
+      << "        mp = {current = " << character.mp  << ", base = " << character.base_mp
+      << ", max = " << character.max_mp << "},\n"
+      << "        atk = 0,\n"
+      << "        acc = 0,\n"
+      << "        def = 0,\n"
+      << "        eva = 0,\n"
+      << "        matk = 0,\n"
+      << "        mdef = 0,\n"
+      << "        meva = 0\n"
+      << "    },\n"
+      << "    limit = {\n"
+      << "        current = " << static_cast<int>(character.limit_level) << ",\n"
+      << "        bar = " << static_cast<int>(character.limit_bar) << ",\n"
+      << "        learned = {\n"
+      << "            [1] = {[1] = " << (character.limits_learned[0][0] == 1 ? "true" : "false")
+      << ", [2] = " << (character.limits_learned[0][1] == 1 ? "true" : "false") << "},\n"
+      << "            [2] = {[1] = " << (character.limits_learned[1][0] == 1 ? "true" : "false")
+      << ", [2] = " << (character.limits_learned[1][1] == 1 ? "true" : "false") << "},\n"
+      << "            [3] = {[1] = " << (character.limits_learned[2][0] == 1 ? "true" : "false")
+      << ", [2] = " << (character.limits_learned[2][1] == 1 ? "true" : "false") << "},\n"
+      << "            [4] = {[1] = " << (character.limits_learned[3][0] == 1 ? "true" : "false")
+      << ", [2] = " << (character.limits_learned[3][1] == 1 ? "true" : "false") << "}\n"
+      << "        },\n"
+      << "        uses = {[1] = " << character.limit_uses[0] << ", [2] = "
+      << character.limit_uses[1] << ", [3] = " << character.limit_uses[2] << ", [4] = 0}\n"
+      << "    },\n"
+      << "    weapon = {\n"
+      << "        id = " << static_cast<int>(character.weapon) + 128 << ",\n"
+      << "        materia = {";
+    total_count = 0;
+    for (int m = 0; m < 8; m ++){
+        if (character.weapon_materia[m].id < 255){
+            total_count ++;
+            file << "\n            [" << m << "] = {id = "
+              << static_cast<int>(character.weapon_materia[m].id) << ", ap = "
+              << character.weapon_materia[m].ap << "},";
+        }
+    }
+    if (total_count > 0) file << "\n        }\n    },\n";
+    else file << "}\n    },\n";
+    file
+      << "    armor = {\n"
+      << "        id = " << static_cast<int>(character.armor) + 256 << ",\n"
+      << "        materia = {";
+    total_count = 0;
+    for (int m = 0; m < 8; m ++){
+        if (character.armor_materia[m].id < 255){
+            total_count ++;
+            file << "\n            [" << m << "] = {id = "
+              << static_cast<int>(character.armor_materia[m].id) << ", ap = "
+              << character.armor_materia[m].ap << "},";
+        }
+    }
+    if (total_count > 0) file << "\n        }\n    },\n";
+    else file << "}\n    },\n";
+    file << "    accessory = ";
+    if (character.accessory < 0xFF) file << static_cast<int>(character.accessory) + 288;
+    else file << "nil";
+    file << ",\n    status = {";
+    if (character.fury) file << FURY;
+    if (character.sadness) file << SADNESS;
+    file << "}\n}\n";
+}

+ 1 - 2310
src/installer/extractors/KernelDataInstaller.cpp

@@ -13,17 +13,9 @@
  * GNU General Public License for more details.
  */
 
-
-#include <tinyxml.h>
-#include <algorithm>
-#include <iterator>
 #include <fstream>
-#include <iostream>
-#include <memory>
 #include "installer/formats/DiskImage.h"
-#include "KernelDataInstaller.h"
-#include "formats/FF7NameLookup.h"
-#include <boost/algorithm/string/replace.hpp>
+#include "installer/extractors/KernelDataInstaller.h"
 
 KernelDataInstaller::KernelDataInstaller(DiskImage& disk_image):
   kernel_(*disk_image.filePath("data/kernel/KERNEL.BIN")), exe_path_(*disk_image.filePath("ff7.exe")
@@ -68,2304 +60,3 @@ void KernelDataInstaller::WriteShops(std::string file_name){
         file << "\n    }\n}\n";
     }
 }
-
-int KernelDataInstaller::ReadCommands(){
-
-    // Empty the command list
-    commands_.clear();
-
-    File command_file = kernel_.ExtractGZip(KERNEL_COMMAND_DATA);
-    File command_name_file = kernel_.ExtractGZip(KERNEL_COMMAND_NAMES);
-    File command_desc_file = kernel_.ExtractGZip(KERNEL_COMMAND_DESCRIPTIONS);
-    int command_count = 0;
-
-    // There are exactly 32 commands. Some of them are blank, so there is not really a
-    // reliable exit condition.
-    for (int i = 0; i < 32; i ++){
-
-        CommandData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = command_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = command_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = command_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = command_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (command_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = command_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[command_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // Read data from the command       m, data section.
-        data.cursor_action = command_file.readU8();
-        data.target_raw = command_file.readU8();
-        data.unknown = command_file.readU16LE();
-        data.camera_single = command_file.readU16LE();
-        data.camera_multiple = command_file.readU16LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
-        else data.target.selection_enabled = false;
-        if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
-        else data.target.default_enemy = false;
-        // Default multiple is inverted
-        if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = false;
-        else data.target.default_multiple = true;
-        if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
-        else data.target.toggle_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
-        else data.target.fixed_row = false;
-        if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
-        else data.target.short_range = false;
-        if (data.target_raw == (data.target_raw | 0x40)) data.target.all_rows = true;
-        else data.target.all_rows = false;
-        if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
-        else data.target.random = false;
-        switch (data.cursor_action){
-            case NONE: data.menu = "Battle.Menu.NONE"; break;
-            case MAGIC: data.menu = "Battle.Menu.MAGIC"; break;
-            case SUMMON: data.menu = "Battle.Menu.SUMMON"; break;
-            case ITEM: data.menu = "Battle.Menu.ITEM"; break;
-            case E_SKILL: data.menu = "Battle.Menu.E_SKILL"; break;
-            case THROW: data.menu = "Battle.Menu.THROW"; break;
-            case LIMIT: data.menu = "Battle.Menu.LIMIT"; break;
-            case TARGET: data.menu = "Battle.Menu.TARGET"; break;
-            case W_MAGIC: data.menu = "Battle.Menu.W_MAGIC"; break;
-            case W_SUMMON: data.menu = "Battle.Menu.W_SUMMON"; break;
-            case W_ITEM: data.menu = "Battle.Menu.W_ITEM"; break;
-            case COIN: data.menu = "Battle.Menu.COIN"; break;
-            default: data.menu = "Battle.Menu.NONE";
-        }
-
-        // Add to the list of commands.
-        if (data.name != ""){
-            command_names_[i] = data.name;
-            commands_.push_back(data);
-            command_count ++;
-        }
-    }
-
-    return command_count;
-}
-
-void KernelDataInstaller::WriteCommands(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (CommandData command : commands_){
-        file << "Game.Commands[" << command.id << "] = {\n"
-          << "    name = \"" << command.name << "\",\n"
-          << "    description = \"" << command.description << "\",\n"
-          << "    menu = " << command.menu << ",\n"
-          << "    camera = {single = " << command.camera_single << ", "
-          << "multiple = " << command.camera_multiple << "}\n}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadAttacks(){
-
-    // Empty the item list
-    attacks_.clear();
-
-    File attacks_file = kernel_.ExtractGZip(KERNEL_ATTACK_DATA);
-    File attacks_name_file = kernel_.ExtractGZip(KERNEL_ATTACK_NAMES);
-    File attacks_desc_file = kernel_.ExtractGZip(KERNEL_ATTACK_DESCRIPTIONS);
-    int attack_count = 0;
-
-    limit_attacks_.clear();
-
-    // Limit data, from ff7.exe (file offset of the table at 0x910d94, in the .data section).
-    const int LIMIT_TABLE_OFFSET = 0x910d94 - 0x7b0000 + 0x3ae000;
-    std::unique_ptr<File> exe_file;
-    try{
-        exe_file.reset(new File(exe_path_));
-    }
-    catch (...){
-        std::cerr << "[WARNING] Can't read the data of the limits from ff7.exe." << std::endl;
-    }
-
-    // The kernel has data for 128 attacks, but names and descriptions for 256. The last 128 are the
-    // limit break techniques, which the characters refer to by ID (128 and up).
-    for (int i = 0; i < 256; i ++){
-
-        AttackData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = attacks_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = attacks_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = attacks_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = attacks_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (attacks_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = attacks_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[
-                          attacks_desc_file.GetU8(desc_offset - 1 - comp_offset + c)
-                        ];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // Limit techniques only have name and description. Both start with control codes, drop
-        // everything before the first letter.
-        // The data of the limits that the characters use (128 to 183) is not in the kernel: it is a table of
-        // attack records, with the same format, in the data of ff7.exe (0x910d94, indexed by the limit ID minus
-        // 128; found from the code that ff7.exe uses to load the limits of a character, 0x5c9524).
-        File* source = &attacks_file;
-        bool limit_from_exe = false;
-        if (i >= 128 && i <= 183 && exe_file != nullptr){
-            exe_file->SetOffset(LIMIT_TABLE_OFFSET + (i - 128) * 0x1c);
-            source = exe_file.get();
-            limit_from_exe = true;
-        }
-        if (i >= 128){
-            for (std::string* text : {&data.name, &data.description}){
-                size_t first = text->find_first_of(
-                  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
-                if (first == std::string::npos) text->clear();
-                else text->erase(0, first);
-            }
-            data.id = i;
-            if (!limit_from_exe){
-                limit_attacks_.push_back(data);
-                continue;
-            }
-        }
-
-        // Read data from the item data section.
-        data.accuracy = source->readU8();
-        data.impact_effect = source->readU8();
-        data.hurt_anim = source->readU8();
-        data.unknown = source->readU8();
-        data.mp = source->readU16LE();
-        data.sound = source->readU16LE();
-        data.camera_single = source->readU16LE();
-        data.camera_multiple = source->readU16LE();
-        data.target_raw = source->readU8();
-        data.effect = source->readU8();
-        data.damage_raw = source->readU8();
-        data.power = source->readU8();
-        data.condition_raw = source->readU8();
-        data.status_change_raw = source->readU8();
-        data.additional_effects_raw = source->readU8();
-        data.additional_effects_mod_raw = source->readU8();
-        data.status_raw = source->readU32LE();
-        data.element_raw = source->readU16LE();
-        data.special_raw = source->readU16LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
-        else data.target.selection_enabled = false;
-        if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
-        else data.target.default_enemy = false;
-        if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
-        else data.target.default_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
-        else data.target.toggle_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
-        else data.target.fixed_row = false;
-        if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
-        else data.target.short_range = false;
-        if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
-        else data.target.all_rows = false;
-        if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
-        else data.target.random = false;
-        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
-        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
-        // Status attack byte: the lower 6 bits are the chance out of 63, and the upper bits the
-        // mode (0x80 toggles the statuses, 0x40 cures them, otherwise they are inflicted).
-        data.status.chance = data.status_change_raw & 0x3F;
-        if ((data.status_change_raw & 0x80) != 0) data.status.mode = TOGGLE;
-        else if ((data.status_change_raw & 0x40) != 0) data.status.mode = CURE;
-        else data.status.mode = INFLICT;
-        switch (data.condition_raw){
-            case 0: data.restore_type = RESTORE_HP; break;
-            case 1: data.restore_type = RESTORE_MP; break;
-            case 2: data.restore_type = RESTORE_STATUS; break;
-            default: data.restore_type = RESTORE_NONE;
-        }
-        // In KERNEL.BIN, items that don't induce statuses actually have all statuses marked, and
-        // a chance of 255. If it's the case, skip statuses altogether. The status IDs are the bit
-        // positions of the original game: 0 death, 1 near death, 2 sleep, 3 poison, 4 fury,
-        // 5 sadness, 6 confusion, 7 silence, 8 haste, 9 slow, 10 stop...
-        if (data.status_change_raw != 0xFF && data.status_raw != 0xFFFFFFFF){
-            for (int bit = 0; bit < 32; bit ++){
-                if ((data.status_raw & (1u << bit)) != 0) data.status.status.push_back(bit);
-            }
-        }
-        // Get elements
-        if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
-        if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
-        if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
-        if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
-        if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
-        if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
-        if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
-        if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
-        if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
-        if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
-        if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
-        if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
-        if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
-        if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
-        if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
-        if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
-        // In KERNEL.BIN, Non elemental item actually have all elements marked.
-        // If that happens, clear them all.
-        if (data.elements.size() == 16) data.elements.clear();
-
-        // Add to the list of items.
-        attacks_.push_back(data);
-        attack_names_[i] = data.name;
-        attack_count ++;
-    }
-
-    return attack_count;
-}
-
-void KernelDataInstaller::WriteAttacks(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (AttackData attack : attacks_){
-        file << "Game.Attacks[" << attack.id << "] = {\n"
-          << "    name = \"" << attack.name << "\",\n"
-          << "    description = \"" << attack.description << "\",\n"
-          << "    accuracy = " << static_cast<int>(attack.accuracy) << ",\n"
-          << "    impact_effect = " << static_cast<int>(attack.impact_effect) << ",\n"
-          << "    hurt_anim = " << static_cast<int>(attack.hurt_anim) << ",\n"
-          << "    mp = " << attack.mp << ",\n"
-          << "    sound = " << attack.sound << ",\n"
-          << "    camera = {single = " << attack.camera_single << ", "
-          << "multiple = " << attack.camera_multiple << "},\n"
-          << "    effect = " << static_cast<int>(attack.effect) << ",\n"
-          << "    dmg_formula = " << static_cast<int>(attack.damage_formula) << ",\n"
-          << "    dmg_modifier = " << static_cast<int>(attack.damage_modifier) << ",\n"
-          << "    power = " << static_cast<int>(attack.power) << ",\n"
-          << "    additional = {effect = " << static_cast<int>(attack.additional_effects_raw)
-          << ", modifier = " << static_cast<int>(attack.additional_effects_mod_raw) << "},\n"
-          << "    restore = " << attack.restore_type << ",\n"
-          << "    target = {selectable = " << attack.target.selection_enabled
-          << ", default_enemy = " << attack.target.default_enemy
-          << ", default_multiple = " << attack.target.default_multiple
-          << ", toggle_multiple = " << attack.target.toggle_multiple
-          << ", fixed_row = " << attack.target.fixed_row
-          << ", short_range = " << attack.target.short_range
-          << ", all_rows = " << attack.target.all_rows
-          << ", random = " << attack.target.random << "},\n"
-          << "    elements = {";
-        for (int element : attack.elements) file << element << ", ";
-        file << "},\n    status = {";
-        // Status effects: ID (bit position), mode (0 inflict, 1 cure, 2 toggle) and chance out of 63.
-        for (int status : attack.status.status){
-            file << "{status = " << status << ", mode = " << attack.status.mode
-              << ", chance = " << attack.status.chance << "}, ";
-        }
-        file << "}\n}\n";
-    }
-    // Limit techniques: only name and description are known.
-    for (AttackData attack : limit_attacks_){
-        file << "Game.Attacks[" << attack.id << "] = {\n"
-          << "    name = \"" << attack.name << "\",\n"
-          << "    description = \"" << attack.description << "\"\n"
-          << "}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadCharacters(){
-
-    // TODO: Do something about young Cloud and Sephiroth.
-
-    // Empty the item list
-    characters_.clear();
-
-    File characters_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
-    int character_count = 0;
-
-    for (int i = 0; i < 11; i ++){ // 11 characters
-
-        CharacterData data;
-
-        // Read data from the item data section.
-        data.curve_str = characters_file.readU8();
-        data.curve_vit = characters_file.readU8();
-        data.curve_mag = characters_file.readU8();
-        data.curve_spr = characters_file.readU8();
-        data.curve_dex = characters_file.readU8();
-        data.curve_lck = characters_file.readU8();
-        data.curve_hp = characters_file.readU8();
-        data.curve_mp = characters_file.readU8();
-        data.curve_exp = characters_file.readU8();
-        data.unused_0 = characters_file.readU8();
-        data.initial_level = characters_file.readU8();
-        data.unused_1 = characters_file.readU8();
-        data.limit_1_1 = characters_file.readU8();
-        data.limit_1_2 = characters_file.readU8();
-        data.limit_1_3 = characters_file.readU8();
-        data.limit_2_1 = characters_file.readU8();
-        data.limit_2_2 = characters_file.readU8();
-        data.limit_2_3 = characters_file.readU8();
-        data.limit_3_1 = characters_file.readU8();
-        data.limit_3_2 = characters_file.readU8();
-        data.limit_3_3 = characters_file.readU8();
-        data.limit_4_1 = characters_file.readU8();
-        data.limit_4_2 = characters_file.readU8();
-        data.limit_4_3 = characters_file.readU8();
-        data.limit_2_kills = characters_file.readU16LE();
-        data.limit_3_kills = characters_file.readU16LE();
-        data.limit_1_2_uses = characters_file.readU16LE();
-        data.limit_1_3_uses = characters_file.readU16LE();
-        data.limit_2_2_uses = characters_file.readU16LE();
-        data.limit_2_3_uses = characters_file.readU16LE();
-        data.limit_3_2_uses = characters_file.readU16LE();
-        data.limit_3_3_uses = characters_file.readU16LE();
-        data.limit_1_div = characters_file.readU32LE();
-        data.limit_2_div = characters_file.readU32LE();
-        data.limit_3_div = characters_file.readU32LE();
-        data.limit_4_div = characters_file.readU32LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        data.name = VGears::NameLookup::CharName(i);
-
-        // Add to the list of characters.
-        characters_.push_back(data);
-        character_count ++;
-    }
-
-    return character_count;
-}
-
-void KernelDataInstaller::WriteCharacters(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (CharacterData character : characters_){
-        file
-          << "Game.Characters[" << character.id << "] = {\n"
-          << "    id = " << character.id << ",\n"
-          << "    name = \"" << character.name << "\",\n"
-          << "    initial_level = \"" << static_cast<int>(character.initial_level) << "\",\n"
-          << "    curves = {\n"
-          << "        str = " << static_cast<int>(character.curve_str) << ",\n"
-          << "        vit = " << static_cast<int>(character.curve_vit) << ",\n"
-          << "        mag = " << static_cast<int>(character.curve_mag) << ",\n"
-          << "        spr = " << static_cast<int>(character.curve_spr) << ",\n"
-          << "        dex = " << static_cast<int>(character.curve_dex) << ",\n"
-          << "        lck = " << static_cast<int>(character.curve_lck) << ",\n"
-          << "        hp = " << static_cast<int>(character.curve_hp) << ",\n"
-          << "        mp = " << static_cast<int>(character.curve_mp) << ",\n"
-          << "        exp = " << static_cast<int>(character.curve_exp) << ",\n"
-          << "    },\n"
-          << "    limit_divisors = {" << character.limit_1_div << ", " << character.limit_2_div << ", "
-          << character.limit_3_div << ", " << character.limit_4_div << "},\n"
-          << "    limits = {\n"
-          << "        [1] = {\n"
-          << "            kills = 0,\n"
-          << "            techniques = {\n"
-          << "                [1] = {\n"
-          << "                    command = " << static_cast<int>(character.limit_1_1) << ",\n"
-          << "                    uses = 0\n"
-          << "                },\n";
-        if (character.limit_1_2 < 255){ // Some characters only have 1 limit per level.
-            file
-              << "                [2] = {\n"
-              << "                    command = " << static_cast<int>(character.limit_1_2) << ",\n"
-              << "                    uses = " << character.limit_1_2_uses << "\n"
-              << "                },\n";
-        }
-        file
-          << "            }\n"
-          << "        },\n";
-        // Limit level 2.
-        if (character.limit_2_kills < 65535){
-            file
-              << "        [2] = {\n"
-              << "            kills = " << character.limit_2_kills << ",\n"
-              << "            techniques = {\n"
-              << "                [1] = {\n"
-              << "                    command = " << static_cast<int>(character.limit_2_1) << ",\n"
-              << "                    uses = 0\n"
-              << "                },\n";
-            if (character.limit_2_2 < 255){ // Some characters only have 1 limit per level.
-                file
-                  << "                [2] = {\n"
-                  << "                    command = "
-                  << static_cast<int>(character.limit_2_2) << ",\n"
-                  << "                    uses = " << character.limit_2_2_uses << "\n"
-                  << "                },\n";
-            }
-            file
-              << "            }\n"
-              << "        },\n";
-        }
-        // Limit level 3.
-        if (character.limit_3_kills < 65535){
-            file
-              << "        [3] = {\n"
-              << "            kills = " << character.limit_3_kills << ",\n"
-              << "            techniques = {\n"
-              << "                [1] = {\n"
-              << "                    command = " << static_cast<int>(character.limit_3_1) << ",\n"
-              << "                    uses = 0\n"
-              << "                },\n";
-            if (character.limit_3_2 < 255){ // Some characters only have 1 limit per level.
-                file
-                  << "                [2] = {\n"
-                  << "                    command = "
-                  << static_cast<int>(character.limit_3_2) << ",\n"
-                  << "                    uses = " << character.limit_3_2_uses << "\n"
-                  << "                },\n";
-            }
-            file
-              << "            }\n"
-              << "        },\n";
-        }
-        // Limit level 4.
-        if (character.limit_4_1 < 255){
-            file
-              << "        [4] = {\n"
-              << "            kills = 0,\n"
-              << "            techniques = {\n"
-              << "                [1] = {\n"
-              << "                    command = " << static_cast<int>(character.limit_4_1) << ",\n"
-              << "                    uses = 0\n"
-              << "                },\n"
-              << "            }\n"
-              << "        },\n";
-        }
-        file
-          << "    }\n}\n";
-    }
-    file.close();
-}
-
-void KernelDataInstaller::ReadGrowth(){
-
-    File growth_file = kernel_.ExtractGZip(KERNEL_BATTLE_AND_GROWTH_DATA);
-    growth_file.SetOffset(0x01F8); //Skip character data.
-
-    for (int i = 0; i < 12; i ++) growth_.bonus_stat[i] = growth_file.readU8();
-    for (int i = 0; i < 12; i ++) growth_.bonus_hp[i] = growth_file.readU8();
-    for (int i = 0; i < 12; i ++) growth_.bonus_mp[i] = growth_file.readU8();
-    for (int i = 0; i < 64; i ++){
-        growth_.curves[i].id = i;
-        if (i < 37) growth_.curves[i].type = CURVE_PRIMARY;
-        else if (i >=37 && i < 46) growth_.curves[i].type = CURVE_HP;
-        else if (i >=46 && i < 55) growth_.curves[i].type = CURVE_MP;
-        else growth_.curves[i].type = CURVE_EXP;
-        for (int j = 0; j < 8; j ++){
-            growth_.curves[i].gradient[j] = growth_file.readU8();
-            growth_.curves[i].base[j] = static_cast<s8>(growth_file.readU8());
-        }
-    }
-}
-
-void KernelDataInstaller::WriteGrowth(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    file << "Game.Growth.bonus.primary = {";
-    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_stat[i]) << ", ";
-    file << "}\nGame.Growth.bonus.hp = {";
-    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_hp[i]) << ", ";
-    file << "}\nGame.Growth.bonus.mp = {";
-    for (int i = 0; i < 12; i ++) file << "[" << i << "] = " << static_cast<int>(growth_.bonus_mp[i]) << ", ";
-    file << "}\n";
-    for (int i = 0; i < 64; i ++){
-        file << "Game.Growth.curves[" << i << "] = {\n";
-        for (int j = 0; j < 8; j ++){
-            file << "    [" << j << "] = {gradient = " << static_cast<int>(growth_.curves[i].gradient[j])
-              << ", base = " << static_cast<int>(growth_.curves[i].base[j]) << "},\n";
-        }
-        file << "}\n";
-    }
-
-    file.close();
-}
-
-int KernelDataInstaller::ReadItems(){
-
-    // Empty the item list
-    items_.clear();
-
-    File items_file = kernel_.ExtractGZip(KERNEL_ITEM_DATA);
-    File items_name_file = kernel_.ExtractGZip(KERNEL_ITEM_NAMES);
-    File items_desc_file = kernel_.ExtractGZip(KERNEL_ITEM_DESCRIPTIONS);
-    int item_count = 0;
-
-    for (int i = 0; i < 128; i ++){
-
-        ItemData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = items_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = items_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = items_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = items_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (items_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = items_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[items_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // If the item doesn't have a name, it means all items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Read data from the item data section.
-        data.unknown_0 = items_file.readU32LE();
-        data.unknown_1 = items_file.readU32LE();
-        data.camera = items_file.readU16LE();
-        data.restrict_raw = items_file.readU16LE();
-        data.target_raw = items_file.readU8();
-        data.effect = items_file.readU8();
-        data.damage_raw = items_file.readU8();
-        data.power = items_file.readU8();
-        data.condition_raw = items_file.readU8();
-        data.status_change_raw = items_file.readU8();
-        data.additional_effects_raw = items_file.readU8();
-        data.additional_effects_mod_raw = items_file.readU8();
-        data.status_raw = items_file.readU32LE();
-        data.element_raw = items_file.readU16LE();
-        data.special_raw = items_file.readU16LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        data.sellable = !(data.restrict_raw & (1 << 0));
-        data.useable_battle = !(data.restrict_raw & (2 << 0));
-        data.useable_menu = !(data.restrict_raw & (4 << 0));
-        data.throwable = !(data.restrict_raw & (8 << 0));
-        if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
-        else data.target.selection_enabled = false;
-        if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
-        else data.target.default_enemy = false;
-        if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
-        else data.target.default_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
-        else data.target.toggle_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
-        else data.target.fixed_row = false;
-        if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
-        else data.target.short_range = false;
-        if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
-        else data.target.all_rows = false;
-        if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
-        else data.target.random = false;
-        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
-        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
-        // Status byte: the lower 6 bits are the chance out of 63, and the upper bits the mode (0x80
-        // toggles the statuses, 0x40 cures them, otherwise they are inflicted). The old formula
-        // wrapped around for cure and toggle items, so Antidote, Hyper... lost their statuses.
-        data.status.chance = data.status_change_raw & 0x3F;
-        if ((data.status_change_raw & 0x80) != 0) data.status.mode = TOGGLE;
-        else if ((data.status_change_raw & 0x40) != 0) data.status.mode = CURE;
-        else data.status.mode = INFLICT;
-        switch (data.condition_raw){
-            case 0: data.restore_type = RESTORE_HP; break;
-            case 1: data.restore_type = RESTORE_MP; break;
-            case 2: data.restore_type = RESTORE_STATUS; break;
-            default: data.restore_type = RESTORE_NONE;
-        }
-        // In KERNEL.BIN, items that don't induce statuses actually have all statuses marked, and
-        // a chance of 255. If it's the case, skip statuses altogether. The status IDs are the bit
-        // positions of the original game (4 sadness, 5 fury...).
-        if (data.status_change_raw != 0xFF && data.status_raw != 0xFFFFFFFF){
-            for (int bit = 0; bit < 32; bit ++){
-                if ((data.status_raw & (1u << bit)) != 0) data.status.status.push_back(bit);
-            }
-        }
-
-        // Get elements
-        if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
-        if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
-        if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
-        if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
-        if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
-        if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
-        if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
-        if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
-        if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
-        if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
-        if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
-        if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
-        if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
-        if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
-        if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
-        if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
-        // In KERNEL.BIN, Non elemental item actually have all elements marked.
-        // If that happens, clear them all.
-        if (data.elements.size() == 16) data.elements.clear();
-
-        // Add to the list of items.
-        items_.push_back(data);
-        item_count ++;
-    }
-
-    return item_count;
-}
-
-void KernelDataInstaller::WriteItems(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (ItemData item : items_){
-        file << "Game.Items[" << item.id << "] = {\n"
-          << "    type = Inventory.ITEM_TYPE.ITEM,\n"
-          << "    id = " << item.id << ",\n"
-          << "    price = " << prices_[item.id] << ",\n"
-          << "    name = \"" << item.name << "\",\n"
-          << "    description = \"" << item.description << "\",\n"
-          << "    camera = " << item.camera << ",\n"
-          << "    sell = " << item.sellable << ",\n"
-          << "    battle = " << item.useable_battle << ",\n"
-          << "    menu = " << item.useable_menu << ",\n"
-          << "    throw = " << item.throwable << ",\n"
-          << "    effect = " << static_cast<int>(item.effect) << ",\n"
-          << "    dmg_formula = " << static_cast<int>(item.damage_formula) << ",\n"
-          << "    dmg_modifier = " << static_cast<int>(item.damage_modifier) << ",\n"
-          << "    power = " << static_cast<int>(item.power) << ",\n"
-          << "    restore = " << item.restore_type << ",\n"
-          << "    target = {selectable = " << item.target.selection_enabled
-          << ", default_enemy = " << item.target.default_enemy
-          << ", default_multiple = " << item.target.default_multiple
-          << ", toggle_multiple = " << item.target.toggle_multiple
-          << ", fixed_row = " << item.target.fixed_row
-          << ", short_range = " << item.target.short_range
-          << ", all_rows = " << item.target.all_rows
-          << ", random = " << item.target.random << "},\n"
-          << "    status = {";
-        for (int s : item.status.status){
-            file << "\n        {status = " << s << ", mode = " << item.status.mode
-              << ", chance = " << item.status.chance << "},";
-        }
-        if (item.status.status.size() > 0) file << "\n    },\n";
-        else file << "},\n";
-        file << "    elements = {";
-        for (int e : item.elements) file << e << ", ";
-        file << "},\n}\n";
-
-
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadWeapons(){
-
-    // Empty the item list
-    weapons_.clear();
-
-    File weapons_file = kernel_.ExtractGZip(KERNEL_WEAPON_DATA);
-    File weapons_name_file = kernel_.ExtractGZip(KERNEL_WEAPON_NAMES);
-    File weapons_desc_file = kernel_.ExtractGZip(KERNEL_WEAPON_DESCRIPTIONS);
-    int weapon_count = 0;
-
-    for (int i = 0; i < 128; i ++){
-
-        WeaponData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = weapons_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = weapons_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = weapons_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = weapons_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (weapons_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = weapons_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[weapons_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // If the item doesn't have a name, it means all items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Read data from the weapon data section.
-        data.target_raw = weapons_file.readU8();
-        data.unused_0 = weapons_file.readU8();
-        data.damage_raw = weapons_file.readU8();
-        data.unused_1 = weapons_file.readU8();
-        data.power = weapons_file.readU8();
-        data.status = weapons_file.readU8();
-        data.growth = weapons_file.readU8();
-        data.critical = weapons_file.readU8();
-        data.accuracy = weapons_file.readU8();
-        data.model_raw = weapons_file.readU8();
-        data.unused_2 = weapons_file.readU8();
-        data.high_sound = weapons_file.readU8();
-        data.camera = weapons_file.readU16LE();
-        data.equip_raw = weapons_file.readU16LE();
-        data.element_raw = weapons_file.readU16LE();
-        data.unused_3 = weapons_file.readU16LE();
-        for (int s = 0; s < 4; s ++) data.stat_raw[s] = weapons_file.readU8();
-        for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = weapons_file.readU8();
-        for (int s = 0; s < 8; s ++) data.slots_raw[s] = weapons_file.readU8();
-        data.sound = weapons_file.readU8();
-        data.sound_critical = weapons_file.readU8();
-        data.sound_miss = weapons_file.readU8();
-        data.effect = weapons_file.readU8();
-        data.special_raw = weapons_file.readU16LE();
-        data.restrict_raw = weapons_file.readU16LE();
-
-
-        // Derive data from the read data.
-        data.id = i;
-        if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
-        data.sellable = !(data.restrict_raw & (1 << 0));
-        data.useable_battle = !(data.restrict_raw & (2 << 0));
-        data.useable_menu = !(data.restrict_raw & (4 << 0));
-        data.throwable = !(data.restrict_raw & (8 << 0));
-        if (data.target_raw == (data.target_raw | 0x01)) data.target.selection_enabled = true;
-        else data.target.selection_enabled = false;
-        if (data.target_raw == (data.target_raw | 0x02)) data.target.default_enemy = true;
-        else data.target.default_enemy = false;
-        if (data.target_raw == (data.target_raw | 0x04)) data.target.default_multiple = true;
-        else data.target.default_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x08)) data.target.toggle_multiple = true;
-        else data.target.toggle_multiple = false;
-        if (data.target_raw == (data.target_raw | 0x10)) data.target.fixed_row = true;
-        else data.target.fixed_row = false;
-        if (data.target_raw == (data.target_raw | 0x20)) data.target.short_range = true;
-        else data.target.short_range = false;
-        if (data.target_raw == (data.target_raw | 0x40)) data.target.default_enemy = true;
-        else data.target.all_rows = false;
-        if (data.target_raw == (data.target_raw | 0x80)) data.target.random = true;
-        else data.target.random = false;
-        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
-        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
-        data.animation_index = data.model_raw >> 4; // Upper nybble.
-        data.model = data.model_raw & 15; // Lower nybble.
-        if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
-        if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
-        if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
-        if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
-        if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
-        if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
-        if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
-        if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
-        if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
-        if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
-        if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
-        for (int s = 0; s < 4; s ++){
-            if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
-                StatBonus stat_bonus;
-                stat_bonus.stat = data.stat_raw[s];
-                stat_bonus.bonus = data.stat_bonus_raw[s];
-                data.stat_bonus.push_back(stat_bonus);
-            }
-        }
-        for (int s = 0; s < 8; s ++){
-            if (data.slots_raw[s] == 1 || data.slots_raw[s] == 5) data.materia_slots.push_back(SLOT_UNLINKED);
-            else if (
-              data.slots_raw[s] == 2 || data.slots_raw[s] == 3 || data.slots_raw[s] == 6 || data.slots_raw[s] == 7
-            ) data.materia_slots.push_back(SLOT_LINKED);
-        }
-        if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
-        if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
-        if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
-        if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
-        if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
-        if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
-        if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
-        if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
-        if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
-        if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
-        if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
-        if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
-        if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
-        if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
-        if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
-        if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
-        // In KERNEL.BIN, Non elemental item actually have all elements marked.
-        // If that happens, clear them all.
-        if (data.elements.size() == 16) data.elements.clear();
-
-        // Add to the list of weapons.
-        weapons_.push_back(data);
-        weapon_count ++;
-    }
-
-    return weapon_count;
-}
-
-void KernelDataInstaller::WriteWeapons(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (WeaponData weapon : weapons_){
-        file << "Game.Items[" << weapon.id + 128 << "] = {\n"
-          << "    type = Inventory.ITEM_TYPE.WEAPON,\n"
-          << "    id = " << weapon.id + 128 << ",\n"
-          << "    price = " << prices_[weapon.id + 128] << ",\n"
-          << "    name = \"" << weapon.name << "\",\n"
-          << "    description = \"" << weapon.description << "\",\n"
-          << "    camera = " << weapon.camera << ",\n"
-          << "    sell = " << weapon.sellable << ",\n"
-          << "    battle = " << weapon.useable_battle << ",\n"
-          << "    menu = " << weapon.useable_menu << ",\n"
-          << "    throw = " << weapon.throwable << ",\n"
-          << "    effect = " << static_cast<int>(weapon.effect) << ",\n"
-          << "    dmg_formula = " << static_cast<int>(weapon.damage_formula) << ",\n"
-          << "    dmg_modifier = " << static_cast<int>(weapon.damage_modifier) << ",\n"
-          << "    power = " << static_cast<int>(weapon.power) << ",\n"
-          << "    growth = " << static_cast<int>(weapon.growth) << ",\n"
-          << "    accuracy = " << static_cast<int>(weapon.accuracy) << ",\n"
-          << "    critical = " << static_cast<int>(weapon.critical) << ",\n"
-          << "    model = " << static_cast<int>(weapon.model) << ",\n"
-          << "    animation = " << static_cast<int>(weapon.animation_index) << ",\n"
-          << "    target = {selectable = " << weapon.target.selection_enabled
-          << ", default_enemy = " << weapon.target.default_enemy
-          << ", default_multiple = " << weapon.target.default_multiple
-          << ", toggle_multiple = " << weapon.target.toggle_multiple
-          << ", fixed_row = " << weapon.target.fixed_row
-          << ", short_range = " << weapon.target.short_range
-          << ", all_rows = " << weapon.target.all_rows
-          << ", random = " << weapon.target.random << "},\n"
-          << "    elements = {";
-        for (int s : weapon.elements) file << s << ", ";
-        file << "},\n";
-        file << "    slots = {";
-        for (u8 slot : weapon.materia_slots) file << static_cast<int>(slot) << ", ";
-        file << "},\n    stats = {";
-        for (StatBonus s : weapon.stat_bonus){
-            file << "\n        " << GetBasicStatName(s.stat) << " = "
-              << static_cast<int>(s.bonus) << ",";
-        }
-        if (weapon.stat_bonus.size() > 0) file << "\n    },\n";
-        else file << "},\n";
-        file << "    users = {";
-        for (int s : weapon.equip) file << s << ", ";
-        file << "}\n}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadArmors(){
-
-    // Empty the item list
-    armors_.clear();
-
-    File armor_file = kernel_.ExtractGZip(KERNEL_ARMOR_DATA);
-    File armor_name_file = kernel_.ExtractGZip(KERNEL_ARMOR_NAMES);
-    File armor_desc_file = kernel_.ExtractGZip(KERNEL_ARMOR_DESCRIPTIONS);
-    int armor_count = 0;
-
-    for (int i = 0; i < 32; i ++){ // There are exactly 32.
-
-        ArmorData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = armor_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = armor_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = armor_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = armor_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (armor_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = armor_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[armor_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // If the armor doesn't have a name, it means all items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Read data from the armor data section.
-        data.unknown_0 = armor_file.readU8();
-        data.element_defense_mode = armor_file.readU8();
-        data.defense = armor_file.readU8();
-        data.m_defense = armor_file.readU8();
-        data.evasion = armor_file.readU8();
-        data.m_evasion = armor_file.readU8();
-        data.status = armor_file.readU8();
-        data.unknown_1 = armor_file.readU16LE();
-        for (int s = 0; s < 8; s ++) data.slots_raw[s] = armor_file.readU8();
-        data.growth = armor_file.readU8();
-        data.equip_raw = armor_file.readU16LE();
-        data.element_raw = armor_file.readU16LE();
-        data.unknown_2 = armor_file.readU16LE();
-        for (int s = 0; s < 4; s ++) data.stat_raw[s] = armor_file.readU8();
-        for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = armor_file.readU8();
-        data.restrict_raw = armor_file.readU16LE();
-        data.unknown_3 = armor_file.readU16LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
-        data.sellable = !(data.restrict_raw & (1 << 0));
-        data.useable_battle = !(data.restrict_raw & (2 << 0));
-        data.useable_menu = !(data.restrict_raw & (4 << 0));
-        data.throwable = !(data.restrict_raw & (8 << 0));
-        if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
-        if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
-        if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
-        if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
-        if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
-        if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
-        if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
-        if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
-        if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
-        if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
-        if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
-        for (int s = 0; s < 4; s ++){
-            if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
-                StatBonus stat_bonus;
-                stat_bonus.stat = data.stat_raw[s];
-                stat_bonus.bonus = data.stat_bonus_raw[s];
-                data.stat_bonus.push_back(stat_bonus);
-            }
-        }
-        for (int i = 0; i < 8; i ++){
-            if (data.slots_raw[i] == 1 || data.slots_raw[i] == 5){
-                data.materia_slots.push_back(SLOT_UNLINKED);
-            }
-            else if (
-              data.slots_raw[i] == 2 || data.slots_raw[i] == 3 || data.slots_raw[i] == 6 || data.slots_raw[i] == 7
-            ) data.materia_slots.push_back(SLOT_LINKED);
-        }
-        // In KERNEL.BIN, items with no elemental defense actually do have all elements marked,
-        // with mode 255. If, that's the case, consider that there are no elements.
-        if (data.element_defense_mode != 0xFF){
-            if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
-            if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
-            if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
-            if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
-            if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
-            if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
-            if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
-            if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
-            if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
-            if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
-            if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
-            if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
-            if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
-            if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
-            if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
-            if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
-        }
-        // In KERNEL.BIN, Non elemental item actually have all elements marked.
-        // If that happens, clear them all.
-        if (data.elements.size() == 16) data.elements.clear();
-
-        // Add to the list of weapons.
-        armors_.push_back(data);
-        armor_count ++;
-    }
-
-    return armor_count;
-}
-
-void KernelDataInstaller::WriteArmors(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (ArmorData armor : armors_){
-        file << "Game.Items[" << armor.id + 256 << "] = {\n"
-          << "    type = Inventory.ITEM_TYPE.ARMOR,\n"
-          << "    id = " << armor.id + 256 << ",\n"
-          << "    price = " << prices_[armor.id + 256] << ",\n"
-          << "    name = \"" << armor.name << "\",\n"
-          << "    description = \"" << armor.description << "\",\n"
-          << "    sell = " << armor.sellable << ",\n"
-          << "    battle = " << armor.useable_battle << ",\n"
-          << "    menu = " << armor.useable_menu << ",\n"
-          << "    throw = " << armor.throwable << ",\n"
-          << "    defense = {physical = " << static_cast<int>(armor.defense) << ", magical = "
-          << static_cast<int>(armor.m_defense) << "},\n"
-          << "    evasion = {physical = " << static_cast<int>(armor.evasion) << ", magical = "
-          << static_cast<int>(armor.m_evasion) << "},\n"
-          << "    growth = " << static_cast<int>(armor.growth) << ",\n"
-          << "    status = {";
-        if (armor.status != 0XFF) file << armor.status;
-        file
-          << "},\n"
-          << "    elements = {";
-        for (int s : armor.elements){
-            file << "\n        {id = " << s << ", mode = "
-              << static_cast<int>(armor.element_defense_mode) << "},";
-        }
-        if (armor.elements.size() > 0) file << "\n    },\n";
-        else file << "},\n";
-        file << "    slots = {";
-        for (u8 slot : armor.materia_slots) file << static_cast<int>(slot) << ", ";
-        file << "},\n    stats = {";
-        for (StatBonus s : armor.stat_bonus){
-            file << "\n        " << GetBasicStatName(s.stat) << " = "
-              << static_cast<int>(s.bonus) << ",";
-        }
-        if (armor.stat_bonus.size() > 0) file << "\n    },\n";
-        else file << "},\n";
-        file << "    users = {";
-        for (int s : armor.equip) file << s << ", ";
-        file << "}\n}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadAccessories(){
-
-    // Empty the item list
-    accessories_.clear();
-
-    File accessory_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_DATA);
-    File accessory_name_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_NAMES);
-    File accessory_desc_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_DESCRIPTIONS);
-    int accessory_count = 0;
-
-    for (int i = 0; i < 32; i ++){ // There are exactly 32.
-
-        AccessoryData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = accessory_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = accessory_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = accessory_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = accessory_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (accessory_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = accessory_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[accessory_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // If the item doesn't have a name, it means all items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Read data from the accessory data section.
-        for (int s = 0; s < 2; s ++) data.stat_raw[s] = accessory_file.readU8();
-        for (int s = 0; s < 2; s ++) data.stat_bonus_raw[s] = accessory_file.readU8();
-        data.element_defense_mode = accessory_file.readU8();
-        data.effect = accessory_file.readU8();
-        data.element_raw = accessory_file.readU16LE();
-        data.status_raw = accessory_file.readU32LE();
-        data.equip_raw = accessory_file.readU16LE();
-        data.restrict_raw = accessory_file.readU16LE();
-
-        // Derive data from the read data.
-        data.id = i;
-        data.sellable = !(data.restrict_raw & (1 << 0));
-        data.useable_battle = !(data.restrict_raw & (2 << 0));
-        data.useable_menu = !(data.restrict_raw & (4 << 0));
-        data.throwable = !(data.restrict_raw & (8 << 0));
-        if (data.status_raw == (data.status_raw | 0x00000001)) data.status.push_back(DEATH);
-        if (data.status_raw == (data.status_raw | 0x00000002)) data.status.push_back(NEAR_DEATH);
-        if (data.status_raw == (data.status_raw | 0x00000004)) data.status.push_back(SLEEP);
-        if (data.status_raw == (data.status_raw | 0x00000008)) data.status.push_back(POISONED);
-        if (data.status_raw == (data.status_raw | 0x00000010)) data.status.push_back(SADNESS);
-        if (data.status_raw == (data.status_raw | 0x00000020)) data.status.push_back(FURY);
-        if (data.status_raw == (data.status_raw | 0x00000040)) data.status.push_back(CONFU);
-        if (data.status_raw == (data.status_raw | 0x00000080)) data.status.push_back(SILENCE);
-        if (data.status_raw == (data.status_raw | 0x00000100)) data.status.push_back(HASTE);
-        if (data.status_raw == (data.status_raw | 0x00000200)) data.status.push_back(SLOW);
-        if (data.status_raw == (data.status_raw | 0x00000400)) data.status.push_back(STOP);
-        if (data.status_raw == (data.status_raw | 0x00000800)) data.status.push_back(FROG);
-        if (data.status_raw == (data.status_raw | 0x00001000)) data.status.push_back(SMALL);
-        if (data.status_raw == (data.status_raw | 0x00002000)) data.status.push_back(SLOW_NUMB);
-        if (data.status_raw == (data.status_raw | 0x00004000)) data.status.push_back(PETRIFY);
-        if (data.status_raw == (data.status_raw | 0x00008000)) data.status.push_back(REGEN);
-        if (data.status_raw == (data.status_raw | 0x00010000)) data.status.push_back(BARRIER);
-        if (data.status_raw == (data.status_raw | 0x00020000)) data.status.push_back(M_BARRIER);
-        if (data.status_raw == (data.status_raw | 0x00040000)) data.status.push_back(REFLECT);
-        if (data.status_raw == (data.status_raw | 0x00080000)) data.status.push_back(DUAL);
-        if (data.status_raw == (data.status_raw | 0x00100000)) data.status.push_back(SHIELD);
-        if (data.status_raw == (data.status_raw | 0x00200000)) data.status.push_back(D_SENTENCE);
-        if (data.status_raw == (data.status_raw | 0x00400000)) data.status.push_back(MANIPULATE);
-        if (data.status_raw == (data.status_raw | 0x00800000)) data.status.push_back(BERSERK);
-        if (data.status_raw == (data.status_raw | 0x01000000)) data.status.push_back(PEERLESS);
-        if (data.status_raw == (data.status_raw | 0x02000000)) data.status.push_back(PARALYSIS);
-        if (data.status_raw == (data.status_raw | 0x04000000)) data.status.push_back(DARKNESS);
-        if (data.status_raw == (data.status_raw | 0x08000000)) data.status.push_back(DUAL_DRAIN);
-        if (data.status_raw == (data.status_raw | 0x10000000)) data.status.push_back(DEATH_FORCE);
-        if (data.status_raw == (data.status_raw | 0x20000000)) data.status.push_back(RESIST);
-        if (data.status_raw == (data.status_raw | 0x40000000)) data.status.push_back(LUCKY_GIRL);
-        if (data.status_raw == (data.status_raw | 0x80000000)) data.status.push_back(IMPRISONED);
-        if (data.equip_raw == (data.equip_raw | 0x0001)) data.equip.push_back(0);
-        if (data.equip_raw == (data.equip_raw | 0x0002)) data.equip.push_back(1);
-        if (data.equip_raw == (data.equip_raw | 0x0004)) data.equip.push_back(2);
-        if (data.equip_raw == (data.equip_raw | 0x0008)) data.equip.push_back(3);
-        if (data.equip_raw == (data.equip_raw | 0x0010)) data.equip.push_back(4);
-        if (data.equip_raw == (data.equip_raw | 0x0020)) data.equip.push_back(5);
-        if (data.equip_raw == (data.equip_raw | 0x0040)) data.equip.push_back(6);
-        if (data.equip_raw == (data.equip_raw | 0x0080)) data.equip.push_back(7);
-        if (data.equip_raw == (data.equip_raw | 0x0100)) data.equip.push_back(8);
-        if (data.equip_raw == (data.equip_raw | 0x0200)) data.equip.push_back(9);
-        if (data.equip_raw == (data.equip_raw | 0x0400)) data.equip.push_back(10);
-        for (int s = 0; s < 2; s ++){
-            if (data.stat_raw[s] < 0XFF && data.stat_bonus_raw[s] > 0){
-                StatBonus stat_bonus;
-                stat_bonus.stat = data.stat_raw[s];
-                stat_bonus.bonus = data.stat_bonus_raw[s];
-                data.stat_bonus.push_back(stat_bonus);
-            }
-        }
-        // In KERNEL.BIN, items with no elemental defense actually do have all elements marked,
-        // with mode 255. If that's the case, consider that there are no elements.
-        if (data.element_defense_mode != 0xFF){
-            if (data.element_raw == (data.element_raw | 0x0001)) data.elements.push_back(FIRE);
-            if (data.element_raw == (data.element_raw | 0x0002)) data.elements.push_back(ICE);
-            if (data.element_raw == (data.element_raw | 0x0004)) data.elements.push_back(BOLT);
-            if (data.element_raw == (data.element_raw | 0x0008)) data.elements.push_back(EARTH);
-            if (data.element_raw == (data.element_raw | 0x0010)) data.elements.push_back(POISON);
-            if (data.element_raw == (data.element_raw | 0x0020)) data.elements.push_back(GRAVITY);
-            if (data.element_raw == (data.element_raw | 0x0040)) data.elements.push_back(WATER);
-            if (data.element_raw == (data.element_raw | 0x0080)) data.elements.push_back(WIND);
-            if (data.element_raw == (data.element_raw | 0x0100)) data.elements.push_back(HOLY);
-            if (data.element_raw == (data.element_raw | 0x0200)) data.elements.push_back(RESTORATIVE);
-            if (data.element_raw == (data.element_raw | 0x0400)) data.elements.push_back(CUT);
-            if (data.element_raw == (data.element_raw | 0x0800)) data.elements.push_back(HIT);
-            if (data.element_raw == (data.element_raw | 0x1000)) data.elements.push_back(PUNCH);
-            if (data.element_raw == (data.element_raw | 0x2000)) data.elements.push_back(SHOOT);
-            if (data.element_raw == (data.element_raw | 0x4000)) data.elements.push_back(SHOUT);
-            if (data.element_raw == (data.element_raw | 0x8000)) data.elements.push_back(HIDDEN);
-        }
-        // In KERNEL.BIN, Non elemental item actually have all elements marked.
-        // If that happens, clear them all.
-        if (data.elements.size() == 16) data.elements.clear();
-
-        // Add to the list of weapons.
-        accessories_.push_back(data);
-        accessory_count ++;
-    }
-
-    return accessory_count;
-}
-
-void KernelDataInstaller::WriteAccessories(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (AccessoryData accessory : accessories_){
-        file << "Game.Items[" << accessory.id + 288 << "] = {\n"
-          << "    type = Inventory.ITEM_TYPE.ACCESSORY,\n"
-          << "    id = " << accessory.id + 288 << ",\n"
-          << "    price = " << prices_[accessory.id + 288] << ",\n"
-          << "    name = \"" << accessory.name << "\",\n"
-          << "    description = \"" << accessory.description << "\",\n"
-          << "    sell = " << accessory.sellable << ",\n"
-          << "    battle = " << accessory.useable_battle << ",\n"
-          << "    menu = " << accessory.useable_menu << ",\n"
-          << "    throw = " << accessory.throwable << ",\n"
-          << "    special_effect = {";
-        if (accessory.effect != 0XFF) file << static_cast<int>(accessory.effect);
-        file
-          << "},\n"
-          << "    status = {";
-        for (int s : accessory.status) file << s << ", ";
-        file
-          << "},\n"
-          << "    elements = {";
-        // Elements and their resistance mode (0 absorbs, 1 nullifies, 2 halves), as in armors.
-        for (int s : accessory.elements){
-            file << "\n        {id = " << s << ", mode = "
-              << static_cast<int>(accessory.element_defense_mode) << "},";
-        }
-        if (accessory.elements.size() > 0) file << "\n    ";
-        file << "},\n    stats = {";
-        for (StatBonus s : accessory.stat_bonus){
-            file << "\n        " << GetBasicStatName(s.stat) << " = "
-              << static_cast<int>(s.bonus) << ",";
-        }
-        if (accessory.stat_bonus.size() > 0) file << "\n    },\n";
-        else file << "},\n";
-        file << "    users = {";
-        for (int s : accessory.equip) file << s << ", ";
-        file << "}\n}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadMateria(){
-
-    // Empty the item list
-    materia_.clear();
-
-    File materia_file = kernel_.ExtractGZip(KERNEL_MATERIA_DATA);
-    File materia_name_file = kernel_.ExtractGZip(KERNEL_MATERIA_NAMES);
-    File materia_desc_file = kernel_.ExtractGZip(KERNEL_MATERIA_DESCRIPTIONS);
-    int materia_count = 0;
-
-    for (int i = 0; i < 96; i ++){ // There are exactly 96.
-
-        MateriaData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = materia_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = materia_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = materia_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = materia_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (materia_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = materia_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int c = 0; c < comp_size; c ++){
-                        data.description += ENGLISH_CHARS[materia_desc_file.GetU8(desc_offset - 1 - comp_offset + c)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // Read data from the materia data section.
-        for (int i = 0; i < 4; i ++) data.level_up_ap[i] = materia_file.readU16LE();
-        data.stat_raw = materia_file.readU8();
-        data.status_raw // WARNING: Status has only three bytes.
-          = materia_file.readU8() + (materia_file.readU8() << 8) + (materia_file.readU8() << 16);
-        data.element = materia_file.readU8();
-        data.type_raw = materia_file.readU8();
-        for (int i = 0; i < 6; i ++) data.attribute[i] = materia_file.readU8();
-
-        // If the materia doesn't have a name, don't bother processing the rest of the data,
-        // it's not going to be added to the list.
-        if ("" == data.name) continue;
-
-        // Derive data from the read data.
-        data.id = i;
-        // Status. 3 bytes, so only the first 24 can be present.
-        if (data.status_raw == (data.status_raw | 0x000001)) data.status.push_back(DEATH);
-        if (data.status_raw == (data.status_raw | 0x000002)) data.status.push_back(NEAR_DEATH);
-        if (data.status_raw == (data.status_raw | 0x000004)) data.status.push_back(SLEEP);
-        if (data.status_raw == (data.status_raw | 0x000008)) data.status.push_back(POISONED);
-        if (data.status_raw == (data.status_raw | 0x000010)) data.status.push_back(SADNESS);
-        if (data.status_raw == (data.status_raw | 0x000020)) data.status.push_back(FURY);
-        if (data.status_raw == (data.status_raw | 0x000040)) data.status.push_back(CONFU);
-        if (data.status_raw == (data.status_raw | 0x000080)) data.status.push_back(SILENCE);
-        if (data.status_raw == (data.status_raw | 0x000100)) data.status.push_back(HASTE);
-        if (data.status_raw == (data.status_raw | 0x000200)) data.status.push_back(SLOW);
-        if (data.status_raw == (data.status_raw | 0x000400)) data.status.push_back(STOP);
-        if (data.status_raw == (data.status_raw | 0x000800)) data.status.push_back(FROG);
-        if (data.status_raw == (data.status_raw | 0x001000)) data.status.push_back(SMALL);
-        if (data.status_raw == (data.status_raw | 0x002000)) data.status.push_back(SLOW_NUMB);
-        if (data.status_raw == (data.status_raw | 0x004000)) data.status.push_back(PETRIFY);
-        if (data.status_raw == (data.status_raw | 0x008000)) data.status.push_back(REGEN);
-        if (data.status_raw == (data.status_raw | 0x010000)) data.status.push_back(BARRIER);
-        if (data.status_raw == (data.status_raw | 0x020000)) data.status.push_back(M_BARRIER);
-        if (data.status_raw == (data.status_raw | 0x040000)) data.status.push_back(REFLECT);
-        if (data.status_raw == (data.status_raw | 0x080000)) data.status.push_back(DUAL);
-        if (data.status_raw == (data.status_raw | 0x100000)) data.status.push_back(SHIELD);
-        if (data.status_raw == (data.status_raw | 0x200000)) data.status.push_back(D_SENTENCE);
-        if (data.status_raw == (data.status_raw | 0x400000)) data.status.push_back(MANIPULATE);
-        if (data.status_raw == (data.status_raw | 0x800000)) data.status.push_back(BERSERK);
-        // Limit type to materia color.
-        data.type = data.type_raw & 15; // Lower nybble.
-        data.sub_type = data.type_raw & 15; // Upper nyble.
-        switch (data.type){
-            case 0x09:
-            case 0x0A:
-                data.type = static_cast<int>(MATERIA_TYPE::MAGIC);
-                break;
-            case 0x05:
-                data.type = static_cast<int>(MATERIA_TYPE::SUPPORT);
-                break;
-            case 0x02:
-            case 0x03:
-            case 0x06:
-            case 0x07:
-            case 0x08:
-                data.type = static_cast<int>(MATERIA_TYPE::COMMAND);
-                break;
-            case 0x0B:
-            case 0x0C:
-                data.type = static_cast<int>(MATERIA_TYPE::SUMMON);
-                break;
-            default:
-                data.type = static_cast<int>(MATERIA_TYPE::INDEPENDENT);
-        }
-        // Stat bonuses. Manually done.
-        data.stats.str = 0;
-        data.stats.vit = 0;
-        data.stats.mag = 0;
-        data.stats.spr = 0;
-        data.stats.dex = 0;
-        data.stats.lck = 0;
-        data.stats.hp = 0;
-        data.stats.mp = 0;
-        data.stats.change = false;
-        switch (data.stat_raw){
-            case 0x01:
-                data.stats.change = true;
-                data.stats.str = -2;
-                data.stats.vit = -1;
-                data.stats.mag = 2;
-                data.stats.spr = 1;
-                data.stats.dex = 0;
-                data.stats.lck = 0;
-                data.stats.hp = -5;
-                data.stats.mp = 5;
-                break;
-            case 0x02:
-                data.stats.change = true;
-                data.stats.str = -4;
-                data.stats.vit = -2;
-                data.stats.mag = 4;
-                data.stats.spr = 2;
-                data.stats.hp = -10;
-                data.stats.mp = 10;
-                break;
-            case 0x03:
-                data.stats.change = true;
-                data.stats.dex = 2;
-                data.stats.lck = -2;
-                break;
-            case 0x04:
-                data.stats.change = true;
-                data.stats.str = -1;
-                data.stats.vit = -1;
-                data.stats.mag = 1;
-                data.stats.spr = 1;
-                break;
-            case 0x05:
-                data.stats.change = true;
-                data.stats.str = 1;
-                data.stats.vit = 1;
-                data.stats.mag = -1;
-                data.stats.spr = -1;
-                break;
-            case 0x06:
-                data.stats.change = true;
-                data.stats.vit = 1;
-                break;
-            case 0x07:
-                data.stats.change = true;
-                data.stats.lck = 1;
-                break;
-            case 0x08:
-                data.stats.change = true;
-                data.stats.lck = -1;
-                break;
-            case 0x09:
-                data.stats.change = true;
-                data.stats.dex = -2;
-                break;
-            case 0x0A:
-                data.stats.change = true;
-                data.stats.dex = 2;
-                break;
-            case 0x0B:
-                data.stats.change = true;
-                data.stats.str = -1;
-                data.stats.mag = 1;
-                data.stats.hp = -2;
-                data.stats.mp = 2;
-                break;
-            case 0x0C:
-                data.stats.change = true;
-                data.stats.mag = 1;
-                data.stats.hp = -2;
-                data.stats.mp = 2;
-                break;
-            case 0x0D:
-                data.stats.change = true;
-                data.stats.mag = 1;
-                data.stats.spr = 1;
-                data.stats.hp = -5;
-                data.stats.mp = 5;
-                break;
-            case 0x0E:
-                data.stats.change = true;
-                data.stats.mag = 2;
-                data.stats.spr = 2;
-                data.stats.hp = -10;
-                data.stats.mp = 10;
-                break;
-            case 0x0F:
-                data.stats.change = true;
-                data.stats.mag = 4;
-                data.stats.spr = 4;
-                data.stats.hp = -10;
-                data.stats.mp = 15;
-                break;
-            case 0x10:
-                data.stats.change = true;
-                data.stats.mag = 8;
-                data.stats.spr = 8;
-                data.stats.hp = -10;
-                data.stats.mp = 20;
-                break;
-        }
-
-        // Attributtes. Very hardcoded!
-        /*u8 sub_type = data.type_raw >> 4;
-        u8 type = data.type_raw & 15;
-        switch (type){
-            case 0x09: // Magic, normal.
-            case 0x0A: // Magic, high tier.
-                // Always enable magic command.
-                // Each attribute is an attack unlocked each level.
-                data.command.push_back(2);
-                for (int a = 0; a < 6; a ++){
-                    if (data.attribute[i] == 0xFF) data.attack[i] = -1;
-                    else data.attack[i] = data.attribute[i];
-                    data.attack_times[i] = -1;
-                }
-                break;
-            case 0x05: //Support materia.
-                    switch (data.attribute[i]){
-                        case 0x54: data.ability = "Materia.ABILITY.COMMAND_COUNTER"; break;
-                        case 0x55: data.ability = "Materia.ABILITY.MAGIC_COUNTER"; break;
-                        case 0x56: data.ability = "Materia.ABILITY.SNEAK_ATTACK"; break;
-                        case 0x58: data.ability = "Materia.ABILITY.MP_TURBO"; break;
-                        case 0x59: data.ability = "Materia.ABILITY.HP_ABSORB"; break;
-                        case 0x5A: data.ability = "Materia.ABILITY.MP_ABSORB"; break;
-                        case 0x5C: data.ability = "Materia.ABILITY.ADDED_CUT"; break;
-                        case 0x5D: data.ability = "Materia.ABILITY.STEAL_AS_WELL"; break;
-                        case 0x5E: data.ability = "Materia.ABILITY.ELEMENTAL"; break;
-                        case 0x5F: data.ability = "Materia.ABILITY.ADDED_EFFECT"; break;
-                    }
-                    for (int a = 1; a < 6; a ++){
-                        if (data.attribute[i] != 0xFF)
-                            data.ability_parameters[a - 1] = data.attribute[i];
-                    }
-                break;
-            case 0x02: // Command, non-existent.
-                for (int a = 0; a < 6; a ++)
-                    if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
-                break;
-            case 0x03: // Command, W commands.
-                for (int a = 0; a < 6; a ++)
-                    if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
-                break;
-            case 0x06: // Command, replace attack, different by level.
-                for (int a = 0; a < 6; a ++)
-                    if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
-                break;
-            case 0x07: // E. Skill
-                data.command.push_back(13); break; // E.Skill.
-                break;
-            case 0x08: // Adds commands. Can be different by level.
-                for (int a = 0; a < 6; a ++)
-                    if (data.attribute[i] != 0xFF) data.command.push_back(data.attribute[i]);
-                break;
-            case 0x0B: // Summon, normal.
-            case 0x0C: // Summon, high tier.
-                // Always enable Summon command (3), always enable attack in attribute 0.
-                // Attributes 1-5 are the times the summon can be used per battle. They are always
-                // 1-5.
-                data.command.push_back(3);
-                //data.attack.push_back(data.attribute[i]);
-                for (int a = 1; a <= 6; a ++){
-                    data.attack_times[a - 1] = a;
-                }
-                break;
-            //default:
-                //data.type = static_cast<int>(MATERIA_TYPE::INDEPENDENT);
-        }*/
-
-        // Add to the list of materia.
-        materia_.push_back(data);
-        materia_count ++;
-    }
-
-    return materia_count;
-}
-
-void KernelDataInstaller::WriteMateria(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (MateriaData materia : materia_){
-        file << "Game.Materia[" << materia.id << "] = {\n"
-          << "    price = " << prices_[materia.id + 320] << ",\n"
-          << "    name = \"" << materia.name << "\",\n"
-          << "    description = \"" << materia.description << "\",\n"
-          << "    type = " << materia.type << ",\n"
-
-
-        // TODO Remove from here...
-          << "    sub_type = " << (int)materia.sub_type << ",\n"
-          << "    type_raw = " << (int)materia.type_raw << ",\n"
-          << "    type_raw_upp = " << (int)(materia.type_raw >> 4) << ",\n"
-          << "    type_raw_low = " << (int)(materia.type_raw & 15) << ",\n"
-          << "    attributtes = {";
-        for (int i = 0; i < 6; i ++)
-            file << (int) materia.attribute[i] << ", ";
-        file << "},\n"
-        // TODO ... until here
-
-
-          << "    levels_ap = {[1] = 0, ";
-        for (int i = 0; i < 4; i ++){
-            if (materia.level_up_ap[i] < 65535)
-                file << "[" << (i + 2) << "] = " << (materia.level_up_ap[i] * 100) << ", ";
-        }
-        file
-          << "},\n"
-          << "    status = {";
-        for (int s : materia.status) file << s << ", ";
-        file
-          << "},\n"
-          << "    elements = {";
-        if (materia.element != 0xFF) file << static_cast<int>(materia.element);
-        file << "},\n    stats = {";
-        if (materia.stats.str != 0) file << "\n        str = " << materia.stats.str << ",";
-        if (materia.stats.vit != 0) file << "\n        vit = " << materia.stats.vit << ",";
-        if (materia.stats.mag != 0) file << "\n        mag = " << materia.stats.mag << ",";
-        if (materia.stats.spr != 0) file << "\n        spr = " << materia.stats.spr << ",";
-        if (materia.stats.dex != 0) file << "\n        dex = " << materia.stats.dex << ",";
-        if (materia.stats.lck != 0) file << "\n        lck = " << materia.stats.lck << ",";
-        if (materia.stats.hp != 0) file << "\n        hp = " << materia.stats.hp << ",";
-        if (materia.stats.mp != 0) file << "\n        mp = " << materia.stats.mp << ",";
-        if (materia.stats.change) file << "\n    },\n";
-        else file << "},\n";
-
-        // Type-dependant
-        if (materia.type == static_cast<int>(MATERIA_TYPE::MAGIC)){
-            file << "    magic = {\n";
-            for (int a = 0; a < 6; a ++){
-                if (materia.attribute[a] != 0xFF){
-                    file << "        [" << a + 1 << "] = " << static_cast<int>(materia.attribute[a])
-                      << ", -- " << attack_names_[static_cast<int>(materia.attribute[a])] << "\n";
-                }
-            }
-            file << "    }\n";
-        }
-        else if (materia.type == static_cast<int>(MATERIA_TYPE::SUMMON)){
-            file << "    summon = " << static_cast<int>(materia.attribute[0]) << ", -- "
-              << attack_names_[static_cast<int>(materia.attribute[0])] << "\n";
-            file << "    times = {";
-            for (int a = 1; a < 6; a ++)
-                file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
-            file << "}\n";
-        }
-        else if (materia.type == static_cast<int>(MATERIA_TYPE::COMMAND)){
-            switch (materia.sub_type){
-                case 0x02: // Command, replace attack.
-                    file << "    command = {\n";
-                    for (int a = 0; a < 6; a ++){
-                        if (materia.attribute[a] != 0xFF){
-                            file << "        [" << a + 1 << "] = "
-                              << static_cast<int>(materia.attribute[a]) << ", -- "
-                              << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
-                        }
-                    }
-                    file << "    },\n    command_replace = 1 -- Attack\n";
-                    break;
-                case 0x03: // Command, W commands.
-                    file << "    command = {\n";
-                    for (int a = 0; a < 6; a ++){
-                        if (materia.attribute[a] != 0xFF){
-                            file << "        [" << a + 1 << "] = "
-                              << static_cast<int>(materia.attribute[a]) << ", -- "
-                              << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
-                        }
-                    }
-                    file << "    },\n    command_replace = "
-                      << static_cast<int>(materia.attribute[0] - 19) << " -- "
-                      << command_names_[static_cast<int>(materia.attribute[0] - 19)] << "\n";
-                    break;
-                case 0x06: // Normal command.
-                case 0x07: // E.Skill.
-                    file << "    command = {\n";
-                        for (int a = 0; a < 6; a ++){
-                            if (materia.attribute[a] != 0xFF){
-                                file << "        [" << a + 1 << "] = "
-                                  << static_cast<int>(materia.attribute[a]) << ", -- "
-                                  << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
-                            }
-                        }
-                        file << "    }\n";
-                    break;
-                case 0x08: // Master command.
-                    // TODO
-                    break;
-            }
-        }
-        else if (materia.type == static_cast<int>(MATERIA_TYPE::SUPPORT)){
-            file << "    link = {\n        id = ";
-            switch (materia.attribute[0]){
-                case 0x51: file << "Materia.LINK.ALL,\n"; break;
-                case 0x54: file << "Materia.LINK.COMMAND_COUNTER,\n"; break;
-                case 0x55: file << "Materia.LINK.MAGIC_COUNTER,\n"; break;
-                case 0x56: file << "Materia.LINK.SNEAK_ATTACK,\n"; break;
-                case 0x57: file << "Materia.LINK.FINAL_ATTACK,\n"; break;
-                case 0x58: file << "Materia.LINK.MP_TURBO,\n"; break;
-                case 0x59: file << "Materia.LINK.MP_ABSORB,\n"; break;
-                case 0x5A: file << "Materia.LINK.HP_ABSORB,\n"; break;
-                case 0x5C: file << "Materia.LINK.ADDED_CUT,\n"; break;
-                case 0x5D: file << "Materia.LINK.STEAL_AS_WELL,\n"; break;
-                case 0x5E: file << "Materia.LINK.ELEMENTAL,\n"; break;
-                case 0x5F: file << "Materia.LINK.ADDED_EFFECT,\n"; break;
-                case 0x63: file << "Materia.LINK.QUADRA_MAGIC,\n"; break;
-                default: file << "nil,\n"; break;
-            }
-            file << "        param = {";
-            for (int a = 1; a < 6; a ++){
-                if (materia.attribute[a] != 0xFF){
-                    file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
-                }
-            }
-            file << "}\n    }\n";
-        }
-        else if (materia.type == static_cast<int>(MATERIA_TYPE::INDEPENDENT)){
-            bool invert_attrs = false;
-            file << "    ability = {\n        id = ";
-            switch (materia.sub_type){
-                case 0x00: // Character effects
-                    switch (materia.attribute[0]){
-                        case 0x02: file << "Materia.ABILITY.MAG_PLUS,\n"; break;
-                        case 0x04: file << "Materia.ABILITY.DEX_PLUS,\n"; break;
-                        case 0x05: file << "Materia.ABILITY.LCK_PLUS,\n"; break;
-                        case 0x08: file << "Materia.ABILITY.HP_PLUS,\n"; break;
-                        case 0x09: file << "Materia.ABILITY.MP_PLUS,\n"; break;
-                        case 0x0A: file << "Materia.ABILITY.EXP_PLUS,\n"; break;
-                        case 0x0B: file << "Materia.ABILITY.COVER,\n"; break;
-                        case 0x0C: file << "Materia.ABILITY.UNDERWATER,\n"; break;
-                        case 0x50: file << "Materia.ABILITY.LONG_RANGE,\n"; break;
-                        case 0x53: file << "Materia.ABILITY.COUNTER_ATTACK,\n"; break;
-                        case 0x62: file << "Materia.ABILITY.HP_MP,\n"; break;
-                        default: file << "nil,\n"; break;
-                    }
-                    break;
-                case 0x01: // Party effects
-                    switch (materia.attribute[0]){
-                        case 0x00: file << "Materia.ABILITY.MONEY_PLUS,\n"; break;
-                        case 0x01: // Enemy Away / Enemy lure / Chocobo lure
-                            if (materia.id == 7) file << "Materia.ABILITY.ENCOUNTER_DOWN,\n";
-                            else if (materia.id == 8) file << "Materia.ABILITY.ENCOUNTER_UP,\n";
-                            else if (materia.id == 9) file << "Materia.ABILITY.ENCOUNTER_CHOCOBO,\n";
-                            else file << "nil,\n";
-                            break;
-                        case 0x03: file << "Materia.ABILITY.PREEMPTIVE,\n"; break;
-                        default: file << "nil,\n"; break;
-                    }
-                    break;
-                case 0x04:
-                    file << "Materia.ABILITY.MEGA_ALL,\n";
-                    break;
-                default: file << "nil,\n"; break;
-            }
-            file << "        param = {";
-                for (int a = 1; a < 6; a ++){
-                    if (materia.attribute[a] != 0xFF){
-                        file << "[" << a << "] = "
-                          << static_cast<int>(materia.attribute[a]) << ", ";
-                    }
-                }
-                file << "}\n    }\n";
-        }
-        file << "}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadKeyItems(){
-
-    // Empty the item list
-    key_items_.clear();
-
-    File key_items_name_file = kernel_.ExtractGZip(KERNEL_KEY_ITEM_NAMES);
-    File key_items_desc_file = kernel_.ExtractGZip(KERNEL_KEY_ITEM_DESCRIPTIONS);
-    int key_item_count = 0;
-
-    for (int i = 0; i < 128; i ++){
-
-        KeyItemData data;
-        int name_offset;
-        int desc_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = key_items_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = key_items_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // For descriptions, do the same as for names.
-        data.description = "";
-        ch = 1;
-        desc_offset = key_items_desc_file.readU16LE();
-        while (ch != 0xFF){
-            ch = key_items_desc_file.GetU8(desc_offset);
-            if (ch != 0xFF){
-                // Warning: If the char is 0xF9, special function!
-                // the next byte (in bits, aaoooooo) means to read aa data at offset oooooo.
-                // This is to compress information in the kernel.
-                if (ch == 0xF9){
-                    // Get how much to read, two upper bits of the next byte.
-                    // Multiply by 2 and add 4. That's just how it is.
-                    u8 comp_size = (key_items_desc_file.GetU8(desc_offset + 1) >> 6) * 2 + 4;
-                    // Get new offset, lower six bits of the next byte.
-                    // The offset goes backwards from the position of the 0xF9 byte.
-                    u8 comp_offset = key_items_desc_file.GetU8(desc_offset + 1) & 63;
-
-                    for (int i = 0; i < comp_size; i ++){
-                        data.description += ENGLISH_CHARS[key_items_desc_file.GetU8(desc_offset - 1 - comp_offset + i)];
-                    }
-                    desc_offset ++;
-                }
-                else data.description += ENGLISH_CHARS[ch];
-            }
-            desc_offset ++;
-        }
-        boost::replace_all(data.description, "\"", "'");
-
-        // If the key item doesn't have a name, it means all key items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Add to the list of items.
-        data.id = i;
-        key_items_.push_back(data);
-        key_item_count ++;
-    }
-
-    return key_item_count;
-}
-
-void KernelDataInstaller::WriteKeyItems(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (KeyItemData key_item : key_items_){
-        file << "Game.Key_Items[" << key_item.id << "] = {\n"
-          << "    name = \"" << key_item.name << "\",\n description = \"" << key_item.description << "\"\n}\n";
-    }
-    file.close();
-}
-
-int KernelDataInstaller::ReadSummonNames(){
-
-    // Empty the summon list
-    key_items_.clear();
-
-    File summon_name_file = kernel_.ExtractGZip(KERNEL_SUMMON_ATTACK_NAMES);
-    int summon_count = 0;
-
-    for (int i = 0; i < 16; i ++){ // There are exactly 16.
-
-        SummonNameData data;
-        int name_offset;
-
-        // From the current name file offset, read 16 bytes.
-        // This is the pointer to the offset for the item name.
-        // Start reading there until 0xFF is found.
-        data.name = "";
-        u8 ch = 1;
-        name_offset = summon_name_file.readU16LE();
-        while (ch != 0xFF){
-            ch = summon_name_file.GetU8(name_offset);
-            if (ch != 0xFF) data.name += ENGLISH_CHARS[ch];
-            name_offset ++;
-        }
-        boost::replace_all(data.name, "\"", "'");
-
-        // If the key item doesn't have a name, it means all key items have been read.
-        // break the loop.
-        if ("" == data.name) break;
-
-        // Add to the list of items.
-        data.id = i;
-        summon_names_.push_back(data);
-        summon_count ++;
-    }
-
-    return summon_count;
-}
-
-void KernelDataInstaller::WriteSummonNames(std::string file_name){
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (SummonNameData summon : summon_names_)
-        file << "Game.Summon_Names[" << summon.id << "] = {\"" << summon.name << "\"}\n";
-    file.close();
-}
-
-void KernelDataInstaller::ReadInitialSaveMap(){
-
-    // Empty the item list
-    attacks_.clear();
-
-    File savemap_file = kernel_.ExtractGZip(KERNEL_INITIALIZATION_DATA);
-    savemap_file.WriteFile("/home/ivalentin/initialsavemap");
-    // The initial savemap in KERNEL.BIN startas at 0x0054, directly into Cloud's level.
-    // It skips the header (PC and PSX, checksum, preview data and window colour).
-    for (int i = 0; i < 9; i ++) ReadSaveMapCharacter(savemap_file, savemap_.characters[i]);
-    // The initial savemap ends up after character data, end now.
-}
-
-void KernelDataInstaller::ReadSaveMapCharacter(File& savemap_file, SaveMap::Character& character){
-    character.identifier = savemap_file.readU8();
-    character.level = savemap_file.readU8();
-    character.str = savemap_file.readU8();
-    character.vit = savemap_file.readU8();
-    character.mag = savemap_file.readU8();
-    character.spr = savemap_file.readU8();
-    character.dex = savemap_file.readU8();
-    character.lck = savemap_file.readU8();
-    character.str_bonus = savemap_file.readU8();
-    character.vit_bonus = savemap_file.readU8();
-    character.mag_bonus = savemap_file.readU8();
-    character.spr_bonus = savemap_file.readU8();
-    character.dex_bonus = savemap_file.readU8();
-    character.lck_bonus = savemap_file.readU8();
-    character.limit_level = savemap_file.readU8();
-    character.limit_bar = savemap_file.readU8();
-    bool name_end = false;
-    character.name = "";
-    for (int j = 0; j < 12; j ++){
-        character.name_raw[j] = savemap_file.readU8();
-        if (name_end == false){
-            if (character.name_raw[j] != 0XFF){
-                character.name += ENGLISH_CHARS[character.name_raw[j]];
-            }
-            else name_end = true;
-        }
-    }
-    character.weapon = savemap_file.readU8();
-    character.armor = savemap_file.readU8();
-    character.accessory = savemap_file.readU8();
-    character.status_raw = savemap_file.readU8();
-    if (character.status_raw == (character.status_raw | 0x10)){
-        character.sadness = true;
-        character.fury = false;
-    }
-    else if (character.status_raw == (character.status_raw | 0x20)){
-        character.sadness = false;
-        character.fury = true;
-    }
-    else{
-        character.sadness = false;
-        character.fury = false;
-    }
-    character.row_raw = savemap_file.readU8();
-    if (character.row_raw == 0xFE) character.back_row = true;
-    else character.back_row = false;
-    character.level_progress = savemap_file.readU8();
-    character.learned_limits_raw = savemap_file.readU16LE();
-    for (int j = 0; j < 4; j ++){
-        character.limits_learned[j][0] = false;
-        character.limits_learned[j][1] = false;
-    }
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0001))
-        character.limits_learned[0][0] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0002))
-        character.limits_learned[0][1] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0008))
-        character.limits_learned[1][0] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0010))
-        character.limits_learned[1][1] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0040))
-        character.limits_learned[2][0] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0080))
-        character.limits_learned[2][1] = true;
-    if (character.learned_limits_raw == (character.learned_limits_raw | 0x0200))
-        character.limits_learned[3][0] = true;
-    character.kills = savemap_file.readU16LE();
-    character.limit_uses[0] = savemap_file.readU16LE();
-    character.limit_uses[1] = savemap_file.readU16LE();
-    character.limit_uses[2] = savemap_file.readU16LE();
-    character.hp = savemap_file.readU16LE();
-    character.base_hp = savemap_file.readU16LE();
-    character.mp = savemap_file.readU16LE();
-    character.base_mp = savemap_file.readU16LE();
-    character.unknown = savemap_file.readU32LE();
-    character.max_hp = savemap_file.readU16LE();
-    character.max_mp = savemap_file.readU16LE();
-    character.exp = savemap_file.readU32LE();
-    for (int m = 0; m < 8; m ++){
-        character.weapon_materia[m].id = savemap_file.readU8();
-        // TODO: Identify Enemy Skill, do special treatment
-        character.weapon_materia[m].ap // WARNING: AP has only three bytes.
-          = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
-    }
-    for (int m = 0; m < 8; m ++){
-        character.armor_materia[m].id = savemap_file.readU8();
-        // TODO: Identify Enemy Skill, do special treatment
-        character.armor_materia[m].ap // WARNING: AP has only three bytes.
-          = savemap_file.readU8() + (savemap_file.readU8() << 8) + (savemap_file.readU8() << 16);
-    }
-    character.exp_to_next = savemap_file.readU32LE();
-}
-
-void KernelDataInstaller::ReadRecruitRecords(const std::string& exe_path){
-    recruit_records_found_ = false;
-    std::ifstream exe(exe_path, std::ios::binary);
-    const std::vector<u8> data(
-      (std::istreambuf_iterator<char>(exe)), std::istreambuf_iterator<char>()
-    );
-    // The data of Cait Sith and Vincent are two character records like the ones of the savemap, one
-    // after the other (at 0x9138d0 in the 1998 build). They are found by their contents: the
-    // character IDs 6 and 7, valid levels, weapons of each one and names that end.
-    const size_t size = 0x84;
-    for (size_t offset = 0; offset + 2 * size <= data.size(); offset ++){
-        bool found = true;
-        for (int r = 0; r < 2 && found; r ++){
-            const u8* record = &data[offset + r * size];
-            const int first_weapon = r == 0 ? 101 : 114;
-            const int last_weapon = r == 0 ? 113 : 127;
-            found = record[0] == 6 + r && record[1] >= 1 && record[1] <= 99
-              && record[0x1C] >= first_weapon && record[0x1C] <= last_weapon
-              && std::find(record + 0x10, record + 0x1C, 0xFF) != record + 0x1C;
-        }
-        if (!found) continue;
-        File records(&data[offset], 0, 2 * size);
-        for (int r = 0; r < 2; r ++) ReadSaveMapCharacter(records, recruit_records_[r]);
-        recruit_records_found_ = true;
-        return;
-    }
-    std::cerr << "[WARNING] Can't find the data of Cait Sith and Vincent in ff7.exe." << std::endl;
-}
-
-void KernelDataInstaller::WriteInitialSaveMap(std::string file_name){
-    // The initial save map only contains character data.
-    std::fstream file;
-    file.open(file_name, std::ios::out);
-    if (!file) return;
-    for (int c = 0; c < 9; c ++){
-        file << "Characters[" << c << "] = ";
-        WriteSaveMapCharacter(file, savemap_.characters[c]);
-        file << "Characters.recalculate_stats(" << c << ")\n\n";
-    }
-    if (recruit_records_found_){
-        file << "-- Data that Cait Sith and Vincent get when they join, from ff7.exe.\n"
-          << "Game.RecruitRecords = {}\n";
-        for (int r = 0; r < 2; r ++){
-            file << "Game.RecruitRecords[" << 6 + r << "] = ";
-            WriteSaveMapCharacter(file, recruit_records_[r]);
-        }
-    }
-    file.close();
-}
-
-void KernelDataInstaller::WriteSaveMapCharacter(
-  std::ostream& file, const SaveMap::Character& character
-){
-    int total_count; // Utility to count in loops.
-    file
-      << "{\n"
-      << "    char_id = " << static_cast<int>(character.identifier) << ",\n"
-      << "    name = \"" << character.name << "\",\n"
-      << "    enabled = false,\n"
-      << "    locked = false,\n"
-      << "    level = " << static_cast<int>(character.level) << ",\n"
-      << "    kills = " << character.kills << ",\n"
-      << "    back_row = " << (character.back_row ? "true" : "false") << ",\n"
-      << "    exp = " << character.exp << ",\n"
-      << "    exp_to_next = " << character.exp_to_next << ",\n"
-      << "    stats = {\n"
-      << "        str = {base = " << static_cast<int>(character.str)
-      << ", bonus = " << static_cast<int>(character.str_bonus)
-      << ", total = " << static_cast<int>(character.str + character.str_bonus) << "},\n"
-      << "        vit = {base = " << static_cast<int>(character.vit)
-      << ", bonus = " << static_cast<int>(character.vit_bonus)
-      << ", total = " << static_cast<int>(character.vit + character.vit_bonus) << "},\n"
-      << "        mag = {base = " << static_cast<int>(character.mag)
-      << ", bonus = " << static_cast<int>(character.mag_bonus)
-      << ", total = " << static_cast<int>(character.mag + character.mag_bonus) << "},\n"
-      << "        spr = {base = " << static_cast<int>(character.spr)
-      << ", bonus = " << static_cast<int>(character.spr_bonus)
-      << ", total = " << static_cast<int>(character.spr + character.spr_bonus) << "},\n"
-      << "        dex = {base = " << static_cast<int>(character.dex)
-      << ", bonus = " << static_cast<int>(character.dex_bonus)
-      << ", total = " << static_cast<int>(character.dex + character.dex_bonus) << "},\n"
-      << "        lck = {base = " << static_cast<int>(character.lck)
-      << ", bonus = " << static_cast<int>(character.lck_bonus)
-      << ", total = " << static_cast<int>(character.lck + character.lck_bonus) << "},\n"
-      << "        hp = {current = " << character.hp  << ", base = " << character.base_hp
-      << ", max = " << character.max_hp << "},\n"
-      << "        mp = {current = " << character.mp  << ", base = " << character.base_mp
-      << ", max = " << character.max_mp << "},\n"
-      << "        atk = 0,\n"
-      << "        acc = 0,\n"
-      << "        def = 0,\n"
-      << "        eva = 0,\n"
-      << "        matk = 0,\n"
-      << "        mdef = 0,\n"
-      << "        meva = 0\n"
-      << "    },\n"
-      << "    limit = {\n"
-      << "        current = " << static_cast<int>(character.limit_level) << ",\n"
-      << "        bar = " << static_cast<int>(character.limit_bar) << ",\n"
-      << "        learned = {\n"
-      << "            [1] = {[1] = " << (character.limits_learned[0][0] == 1 ? "true" : "false")
-      << ", [2] = " << (character.limits_learned[0][1] == 1 ? "true" : "false") << "},\n"
-      << "            [2] = {[1] = " << (character.limits_learned[1][0] == 1 ? "true" : "false")
-      << ", [2] = " << (character.limits_learned[1][1] == 1 ? "true" : "false") << "},\n"
-      << "            [3] = {[1] = " << (character.limits_learned[2][0] == 1 ? "true" : "false")
-      << ", [2] = " << (character.limits_learned[2][1] == 1 ? "true" : "false") << "},\n"
-      << "            [4] = {[1] = " << (character.limits_learned[3][0] == 1 ? "true" : "false")
-      << ", [2] = " << (character.limits_learned[3][1] == 1 ? "true" : "false") << "}\n"
-      << "        },\n"
-      << "        uses = {[1] = " << character.limit_uses[0] << ", [2] = "
-      << character.limit_uses[1] << ", [3] = " << character.limit_uses[2] << ", [4] = 0}\n"
-      << "    },\n"
-      << "    weapon = {\n"
-      << "        id = " << static_cast<int>(character.weapon) + 128 << ",\n"
-      << "        materia = {";
-    total_count = 0;
-    for (int m = 0; m < 8; m ++){
-        if (character.weapon_materia[m].id < 255){
-            total_count ++;
-            file << "\n            [" << m << "] = {id = "
-              << static_cast<int>(character.weapon_materia[m].id) << ", ap = "
-              << character.weapon_materia[m].ap << "},";
-        }
-    }
-    if (total_count > 0) file << "\n        }\n    },\n";
-    else file << "}\n    },\n";
-    file
-      << "    armor = {\n"
-      << "        id = " << static_cast<int>(character.armor) + 256 << ",\n"
-      << "        materia = {";
-    total_count = 0;
-    for (int m = 0; m < 8; m ++){
-        if (character.armor_materia[m].id < 255){
-            total_count ++;
-            file << "\n            [" << m << "] = {id = "
-              << static_cast<int>(character.armor_materia[m].id) << ", ap = "
-              << character.armor_materia[m].ap << "},";
-        }
-    }
-    if (total_count > 0) file << "\n        }\n    },\n";
-    else file << "}\n    },\n";
-    file << "    accessory = ";
-    if (character.accessory < 0xFF) file << static_cast<int>(character.accessory) + 288;
-    else file << "nil";
-    file << ",\n    status = {";
-    if (character.fury) file << FURY;
-    if (character.sadness) file << SADNESS;
-    file << "}\n}\n";
-}
-
-std::string KernelDataInstaller::GetBasicStatName(int id){
-    std::string name = "stat";
-    switch (id){
-        case 0: name = "str"; break;
-        case 1: name = "vit"; break;
-        case 2: name = "mag"; break;
-        case 3: name = "spr"; break;
-        case 4: name = "dex"; break;
-        case 5: name = "lck"; break;
-    }
-    return name;
-}

+ 70 - 2218
src/installer/extractors/KernelDataInstaller.h

@@ -20,6 +20,11 @@
 #include "installer/formats/DiskImage.h"
 #include "installer/tables/Characters.h"
 #include "installer/formats/SaveMap.h"
+#include "installer/extractors/KernelAttackData.h"
+#include "installer/extractors/KernelItemData.h"
+#include "installer/extractors/KernelEquipmentData.h"
+#include "installer/extractors/KernelMateriaData.h"
+#include "installer/extractors/KernelCharacterData.h"
 
 class KernelDataInstaller{
 
@@ -361,2281 +366,128 @@ class KernelDataInstaller{
              */
             KERNEL_SUMMON_ATTACK_NAMES
 
-
-        };
-
-        enum ELEMENT{
-
-            /**
-             * Fire element.
-             */
-            FIRE = 0,
-
-
-            /**
-             * Ice element.
-             */
-            ICE,
-
-
-            /**
-             * Bolt element.
-             */
-            BOLT,
-
-
-            /**
-             * Earth element.
-             */
-            EARTH,
-
-
-            /**
-             * Poison element.
-             */
-            POISON,
-
-
-            /**
-             * Gravity element.
-             */
-            GRAVITY,
-
-
-            /**
-             * Water element.
-             */
-            WATER,
-
-
-            /**
-             * Wind element.
-             */
-            WIND,
-
-
-            /**
-             * Holy element.
-             */
-            HOLY,
-
-
-            /**
-             * Restorative element.
-             */
-            RESTORATIVE,
-
-
-            /**
-             * Cut element.
-             */
-            CUT,
-
-
-            /**
-             * Hit element.
-             */
-            HIT,
-
-
-            /**
-             * Punch element.
-             */
-            PUNCH,
-
-
-            /**
-             * Shoot element.
-             */
-            SHOOT,
-
-
-            /**
-             * Shout element.
-             */
-            SHOUT,
-
-
-            /**
-             * Hidden element.
-             */
-            HIDDEN,
-        };
-
-        /**
-         * List of target selection options.
-         */
-        struct Target{
-
-            /**
-             * The target can be selected.
-             *
-             * If the target of the attack is selectable, at least between some
-             * subset of possible targets, this must be true.
-             */
-            bool selection_enabled;
-
-            /**
-             * The default target is an enemy.
-             *
-             * When true, the cursor starts in an enemy row. When false, it
-             * starts in the ally row. It may or may not be possible to change
-             * it.
-             */
-            bool default_enemy;
-
-            /**
-             * The default target is a group.
-             *
-             * When true, the cursor starts will all the members of a row
-             * selected. When false, it starts with only one target selected. It
-             * may or may not be possible to change it.
-             */
-            bool default_multiple;
-
-            /**
-             * The selection can change between groups and individuals.
-             *
-             * When true, the target can be changed between groups and
-             * individuals. When false, only {@see default_multiple} determines
-             * if the target is an individual or a group.
-             */
-            bool toggle_multiple;
-
-            /**
-             * The target group can be changed.
-             *
-             * When true, the target cannot be changed between enemies and
-             * allies. In this case {@see default_enemy} will determine the
-             * group that can be targeted.
-             */
-            bool fixed_row;
-
-            /**
-             * The command is short ranged.
-             *
-             * When true, if the target or the caster is not in the front of
-             * their row, the target will take half damage. Also, when true, an
-             * enemy covered by another enemy can't be targeted.
-             */
-            bool short_range;
-
-            /**
-             * Targets all, enemies and allies.
-             *
-             * When true, all allies and enemies are targets, and the selection
-             * can't be changed.
-             */
-            bool all_rows;
-
-            /**
-             * The target is random.
-             *
-             * When multiple targets are selected, one will be selected at
-             * random to be the receiving target. Cursor will cycle among all
-             * viable targets.
-             */
-            bool random;
-
-        };
-
-        /**
-         * Determines what an item affects on when used from the menu.
-         */
-        enum RESTORE_TYPE{
-
-            /**
-             * The item affects a party member or group HP.
-             */
-            RESTORE_HP = 0,
-
-            /**
-             * The item affects a party member or group MP.
-             */
-            RESTORE_MP = 1,
-
-            /**
-             * The item affects a party member or group status.
-             */
-            RESTORE_STATUS = 2,
-
-            /**
-             * The item is not restorative.
-             */
-            RESTORE_NONE = 3
-        };
-
-        /**
-         * List of altered status.
-         */
-        enum STATUS{
-
-            /**
-             * The character is dead.
-             */
-            DEATH = 0,
-
-            /**
-             * The character has critical HP.
-             */
-            NEAR_DEATH,
-
-            /**
-             * The character is asleep.
-             */
-            SLEEP,
-
-            /**
-             * The character is poisoned.
-             */
-            POISONED,
-
-            /**
-             * The character is sad.
-             */
-            SADNESS,
-
-            /**
-             * The character is furious.
-             */
-            FURY,
-
-            /**
-             * The character is confused.
-             */
-            CONFU,
-
-            /**
-             * The character is silenced.
-             */
-            SILENCE,
-
-            /**
-             * The character is slow.
-             */
-            HASTE,
-
-            /**
-             * The character is
-             */
-            SLOW,
-
-            /**
-             * The character is stopped.
-             */
-            STOP,
-
-            /**
-             * The character is a frog.
-             */
-            FROG,
-
-            /**
-             * The character is small.
-             */
-            SMALL,
-
-            /**
-             * The character is going to be petrified.
-             */
-            SLOW_NUMB,
-
-            /**
-             * The character is petrified.
-             */
-            PETRIFY,
-
-            /**
-             * The character is regenerating health.
-             */
-            REGEN,
-
-            /**
-             * The character has a physical barrier.
-             */
-            BARRIER,
-
-            /**
-             * The character has a magical barrier
-             */
-            M_BARRIER,
-
-            /**
-             * The character reflects magical attacks.
-             */
-            REFLECT,
-
-            /**
-             * The character has dual status.
-             *
-             * @todo What does it do.
-             */
-            DUAL,
-
-            /**
-             * The character has a shield.
-             */
-            SHIELD,
-
-            /**
-             * The character has a death sentence.
-             */
-            D_SENTENCE,
-
-            /**
-             * The character is being manipulated.
-             */
-            MANIPULATE,
-
-            /**
-             * The character is berserk.
-             */
-            BERSERK,
-
-            /**
-             * The character is invincible.
-             */
-            PEERLESS,
-
-            /**
-             * The character is paralyzed.
-             */
-            PARALYSIS,
-
-            /**
-             * The character is blinded.
-             */
-            DARKNESS,
-
-            /**
-             * The character has dual drain.
-             */
-            DUAL_DRAIN,
-
-            /**
-             * The character has deathforce.
-             */
-            DEATH_FORCE,
-
-            /**
-             * The character has resist.
-             */
-            RESIST,
-
-            /**
-             * Cait' Sith lucky girl mode.
-             */
-            LUCKY_GIRL,
-
-            /**
-             * The character is imprisioned.
-             */
-            IMPRISONED
-        };
-
-        /**
-         * How a status effect modifier attack or item can act.
-         */
-        enum STATUS_EFFECT{
-
-            /**
-             * Inflicts status changes.
-             */
-            INFLICT = 0,
-
-            /**
-             * Cures status changes.
-             */
-            CURE = 1,
-
-            /**
-             * Toggles status changes.
-             *
-             * If the target doesn't have the status change, inflict it. If the
-             * target already has the status change, cure it.
-             */
-            TOGGLE = 2
-        };
-
-        /**
-         * Menus that must be shown when selecting a command.
-         */
-        enum COMMAND_MENU{
-
-            /**
-             * No menu, execute command directly, without target selection.
-             */
-            NONE = 0,
-
-            /**
-             * Show magic menu.
-             */
-            MAGIC = 1,
-
-            /**
-             * Show summon menu.
-             */
-            SUMMON = 2,
-
-            /**
-             * Show item menu.
-             */
-            ITEM = 3,
-
-            /**
-             * Show enemy skill menu.
-             */
-            E_SKILL = 4,
-
-            /**
-             * Show throw menu.
-             */
-            THROW = 5,
-
-            /**
-             * Show limit menu.
-             */
-            LIMIT = 6,
-
-            /**
-             * No menu, skip to target selection.
-             */
-            TARGET = 7,
-
-            /**
-             * Show W-Magic menu
-             */
-            W_MAGIC = 8,
-
-            /**
-             * Show W-Summon menu
-             */
-            W_SUMMON = 9,
-
-            /**
-             * Show W-Item menu
-             */
-            W_ITEM = 10,
-
-            /**
-             * Show Coin menu
-             */
-            COIN = 11,
-
-
-        };
-
-        struct StatusEffect{
-
-            /**
-             * The list of status to inflict or cure.
-             */
-            std::vector<int> status;
-
-            /**
-             * The chance of landing a status effect (out of 63).
-             */
-            u16 chance;
-
-            /**
-             * The status change mode.
-             */
-            STATUS_EFFECT mode;
-
-        };
-
-        /**
-         * Data for each command.
-         *
-         * As found in KERNEL.BIN, file 0, in order, up to camera_multiple.
-         * Members after camera_multiple are not found literally in KERNEL.BIN,
-         * but are derived from other members or found in other files of the
-         * kernel (text files).
-         */
-        struct CommandData{
-
-            /**
-             * Cursor action when the command is selected.
-             */
-            u8 cursor_action;
-
-            /**
-             * Targeting mode. 1 byte.
-             *
-             * See {@see target} and {@see TargetModes} for more information.
-             */
-            u8 target_raw;
-
-            /**
-             * Unknown data (Always 0XFFFF). 2 bytes.
-             */
-            u16 unknown;
-
-            /**
-             * Camera movement ID for single target. 2 bytes.
-             *
-             * Determines the camera movement when the command is executed over
-             * a single target.
-             */
-            u16 camera_single;
-
-            /**
-             * Camera movement ID for multiple target. 2 bytes.
-             *
-             * Determines the camera movement when the command is executed over multiple targets.
-             */
-            u16 camera_multiple;
-
-            /**
-             * Command ID.
-             *
-             * Not actually in the item file data, it's the order at which it
-             * appears.
-             */
-            int id;
-
-            /**
-             * Command name.
-             *
-             * Not in the same file as the rest of the data, but on file 17.
-             */
-            std::string name;
-
-            /**
-             * Command description.
-             *
-             * Not in the same file as the rest of the data, but on file 9.
-             */
-            std::string description;
-
-            /**
-             * Target selection mode.
-             *
-             * Derived from {@see target_raw}.
-             */
-            Target target;
-
-            /**
-             * Menu name
-             *
-             * Derived from {@see cursor_action}.
-             */
-            std::string menu;
-
-        };
-
-        /**
-         * Data for each attack.
-         *
-         * As found in KERNEL.BIN, file 1, in order, up to camera_multiple.
-         * Members after special_raw are not found literally in KERNEL.BIN, but
-         * are derived from other members.
-         */
-        struct AttackData{
-
-            /**
-             * Probability of the attack to land. 1 byte.
-             */
-            u8 accuracy;
-
-            /**
-             * Impact effect. 1 byte.
-             *
-             * The sprite shown when the attack hits.
-             */
-            u8 impact_effect;
-
-            /**
-             * Index of the animation played by the target when hit. 1 byte.
-             */
-            u8 hurt_anim;
-
-            /**
-             * Unknown data. 1 bytes.
-             */
-            u8 unknown;
-
-            /**
-             * MP cost. 2 bytes.
-             */
-            u16 mp;
-
-            /**
-             * Impact sound. 2 bytes.
-             */
-            u16 sound;
-
-            /**
-             * Camera movement ID for single target. 2 bytes.
-             *
-             * Determines the camera movement when the command is executed over
-             * a single target.
-             */
-            u16 camera_single;
-
-            /**
-             * Camera movement ID for multiple target. 2 bytes.
-             *
-             * Determines the camera movement when the command is executed over
-             * multiple targets.
-             */
-            u16 camera_multiple;
-
-            /**
-             * Targeting mode. 1 byte.
-             *
-             * See {@see target} and {@see TargetModes} for more information.
-             */
-            u8 target_raw;
-
-            /**
-             * Atack effect ID. 1 byte.
-             *
-             * Used for animation in battle.
-             */
-            u8 effect;
-
-            /**
-             * Damage formula. 1 byte.
-             *
-             * This byte is divided into two nybbles (four bits). Upper nybble
-             * determines what considerations are made in calculating damage
-             * such as physical/magical, allowing criticals, how to calculate
-             * accuracy, etc. There are also three sets of known formulae that
-             * are paired with the upper nybble values. These are selected in
-             * the Lower Nybble and determine how to calculate pre-defense
-             * damage.
-             */
-            u8 damage_raw;
-
-            /**
-             * The attack power. 1 byte.
-             *
-             * Used for damage/healing calculation.
-             */
-            u8 power;
-
-            /**
-             * The restore type.
-             *
-             * See {@see restore_type} and {@see RESTORE_TYPE}.
-             */
-            u8 condition_raw;
-
-            /**
-             * Information about status change mode and chance. 1 bytes.
-             */
-            u8 status_change_raw;
-
-            /**
-             * Special flags. 1 byte.
-             *
-             * @todo Document and retrieve with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Attack_Special_Effects
-             */
-            u8 additional_effects_raw;
-
-            /**
-             * Special flags. 1 byte.
-             *
-             * @todo Document and retrieve with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Item_data
-             */
-            u8 additional_effects_mod_raw;
-
-            /**
-             * Information about what status can be inflicted or cured. 4 bytes.
-             *
-             * The first six bits are the chance of inflicting/curing (out of
-             * 63). If the seventh bit is set, the status is cured instead of
-             * inflicted. If the eighth bit is set, then the status is toggled
-             * (cured if present, inflicted if not present).
-             */
-            u32 status_raw;
-
-            /**
-             * Information about the item elements. 2 bytes.
-             */
-            u16 element_raw;
-
-            /**
-             * Special flags. 2 bytes.
-             *
-             * @todo document and parse with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
-             */
-            u16 special_raw;
-
-            /**
-             * Attack ID.
-             *
-             * Not in the item file data, it's the order at which it appears.
-             */
-            int id;
-
-            /**
-             * Attack name.
-             *
-             * Not in the same file as the rest of the data, but on file 18.
-             */
-            std::string name;
-
-            /**
-             * Attack description.
-             *
-             * Not in the same file as the rest of the data, but on file 10.
-             */
-            std::string description;
-
-            /**
-             * Target selection mode.
-             *
-             * Derived from {@see target_raw}.
-             */
-            Target target;
-
-            /**
-             * The damage formula.
-             *
-             * It's the upper nybble in {@see damage_raw}.
-             */
-            u8 damage_formula;
-
-            /**
-             * The damage modifier.
-             *
-             * It's the lower nybble in {@see damage_raw}.
-             */
-            u8 damage_modifier;
-
-            /**
-             * What the attack restores when used.
-             *
-             * Same as {@see condition_raw}, except for the wrong cases.
-             */
-            RESTORE_TYPE restore_type;
-
-            /**
-             * Status effects.
-             *
-             * Derived from {@see status_change_raw} and {@see status_raw}.
-             */
-            StatusEffect status;
-
-            /**
-             * Elements of the attack.
-             *
-             * Derived from {@see element_raw}.
-             */
-            std::vector<int> elements;
-
-        };
-
-        /**
-         * Data for each items
-         *
-         * As found in KERNEL.BIN, file 4, in order, up to special. Members
-         * after special are not found literally in KERNEL.BIN, but are derived
-         * from other members or found in other files (text files).
-         */
-        struct ItemData{
-
-            /**
-             * Unknown data (Always 0XFFFF). 4 bytes.
-             */
-            u32 unknown_0;
-
-            /**
-             * Unknown data (Always 0XFFFF). 4 bytes.
-             */
-            u32 unknown_1;
-
-            /**
-             * Camera movement ID. 2 bytes.
-             *
-             * Used for items useable in battle, determines the camera movement.
-             */
-            u16 camera;
-
-            /**
-             * Item restrictions. 2 bytes.
-             *
-             * Contains bits indicating if the item is {@see selleable},
-             * {@see useable_battle} and {@see useable_menu}.
-             */
-            u16 restrict_raw;
-
-            /**
-             * Targeting mode. 1 byte.
-             *
-             * See {@see target} and {@see TargetModes} for more information.
-             */
-            u8 target_raw;
-
-            /**
-             * Item effect ID. 1 byte.
-             *
-             * Used for animation in battle.
-             */
-            u8 effect;
-
-            /**
-             * Damage formula. 1 byte.
-             *
-             * This byte is divided into two nybbles (four bits). Upper nybble
-             * determines what considerations are made in calculating damage
-             * such as physical/magical, allowing criticals, how to calculate
-             * accuracy, etc. There are also three sets of known formulae that
-             * are paired with the upper nybble values. These are selected in
-             * the Lower Nybble and determine how to calculate pre-defense
-             * damage.
-             */
-            u8 damage_raw;
-
-            /**
-             * The item power. 1 byte.
-             *
-             * Used for damage/healing calculation.
-             */
-            u8 power;
-
-            /**
-             * The restore type.
-             *
-             * See {@see restore_type} and {@see RESTORE_TYPE}.
-             */
-            u8 condition_raw;
-
-            /**
-             * Information about status change mode and chance. 1 bytes.
-             */
-            u8 status_change_raw;
-
-            /**
-             * Special flags. 1 byte.
-             *
-             * @todo Document and retrieve with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Attack_Special_Effects
-             */
-            u8 additional_effects_raw;
-
-            /**
-             * Special flags. 1 byte.
-             *
-             * @todo Document and retrieve with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Item_data
-             */
-            u8 additional_effects_mod_raw;
-
-            /**
-             * Information about what status can be inflicted or cured. 4 bytes.
-             *
-             * The first six bits are the chance of inflicting/curing (out of
-             * 63). If the seventh bit is set, the status is cured instead of
-             * inflicted. If the eighth bit is set, then the status is toggled
-             * (cured if present, inflicted if not present).
-             */
-            u32 status_raw;
-
-            /**
-             * Information about the item elements. 2 bytes.
-             */
-            u16 element_raw;
-
-            /**
-             * Special flags. 2 bytes.
-             *
-             * @todo document and parse with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
-             */
-            u16 special_raw;
-
-            /**
-             * Item ID.
-             *
-             * Not in the item file data, it's the order at which it appears.
-             */
-            int id;
-
-            /**
-             * Item name.
-             *
-             * Not in the same file as the rest of the data, but on file 19.
-             */
-            std::string name;
-
-            /**
-             * Item description.
-             *
-             * Not in the same file as the rest of the data, but on file 11.
-             */
-            std::string description;
-
-            /**
-             * Indicates if the item can be sold.
-             *
-             * True if the bit 0 in {@see restrict_raw} is 0.
-             */
-            bool sellable;
-
-            /**
-             * Indicates if the item can be used in battle.
-             *
-             * True if the bit 2 in {@see restrict_raw} is 0.
-             */
-            bool useable_battle;
-
-            /**
-             * Indicates if the item can be used in the menu.
-             *
-             * True if the bit 4 in {@see restrict_raw} is 0.
-             */
-            bool useable_menu;
-
-            /** Whether the item can be thrown (Throw command). */
-            bool throwable;
-
-            /**
-             * Target selection mode.
-             *
-             * Derived from {@see target_raw}.
-             */
-            Target target;
-
-            /**
-             * The damage formula.
-             *
-             * It's the upper nybble in {@see damage_raw}.
-             */
-            u8 damage_formula;
-
-            /**
-             * The damage modifier.
-             *
-             * It's the lower nybble in {@see damage_raw}.
-             */
-            u8 damage_modifier;
-
-            /**
-             * What the item restores when used.
-             *
-             * Same as {@see condition_raw}, except for the wrong cases.
-             */
-            RESTORE_TYPE restore_type;
-
-            /**
-             * Status effects.
-             *
-             * Derived from {@see status_change_raw} and {@see status_raw}.
-             */
-            StatusEffect status;
-
-            /**
-             * Elements of the item.
-             *
-             * Derived from {@see element_raw}.
-             */
-            std::vector<int> elements;
-
-        };
-
-        /**
-         * Materia slot types.
-         */
-        enum SLOT_TYPE{
-
-            /**
-             * A non linked slot.
-             */
-            SLOT_UNLINKED = 0,
-
-            /**
-             * A linked slot.
-             *
-             * If the slot is in an even position, it's linked to the one in the
-             * right. If it's in an odd position, it's linked to the one in the
-             * left. Remember that slot positions are 0-index.
-             */
-            SLOT_LINKED = 1
-        };
-
-        /**
-         * Bonus in stat givven by weapons or armor.
-         */
-        struct StatBonus{
-
-            /**
-             * ID of the raised stat.
-             */
-            u8 stat;
-
-            /**
-             * Stat value raise.
-             */
-            u8 bonus;
-        };
-
-        /**
-         * Data for each weapon
-         *
-         * As found in KERNEL.BIN, file 5, in order, up to special. Members
-         * after special are not found literally in KERNEL.BIN, but are derived
-         * from other members or found in other kernel files (text files).
-         */
-        struct WeaponData{
-
-            /**
-             * Targeting mode. 1 byte.
-             *
-             * See {@see target} and {@see TargetModes} for more information.
-             */
-            u8 target_raw;
-
-            /**
-             * Effect ID, unused (Always 0XFF). 1 byte.
-             */
-            u8 unused_0;
-
-            /**
-             * Damage formula. 1 byte.
-             *
-             * This byte is divided into two nybbles (four bits). Upper nybble
-             * determines what considerations are made in calculating damage
-             * such as physical/magical, allowing criticals, how to calculate
-             * accuracy, etc. There are also three sets of known formulae that
-             * are paired with the upper nybble values. These are selected in
-             * the Lower Nybble and determine how to calculate pre-defense
-             * damage.
-             */
-            u8 damage_raw;
-
-            /**
-             * Unused data (Always 0XFF). 1 byte.
-             */
-            u8 unused_1;
-
-            /**
-             * The weapon power. 1 byte.
-             *
-             * Used for damage calculation.
-             */
-            u8 power;
-
-            /**
-             * The status effect the weapon induces. 1 byte.
-             */
-            u8 status;
-
-            /**
-             * Weapon materia growth multiplier. 1 byte.
-             */
-            u8 growth;
-
-            /**
-             * Weapon critical chance. 1 byte.
-             */
-            u8 critical;
-
-            /**
-             * Weapon accuracy. 1 byte.
-             */
-            u8 accuracy;
-
-            /**
-             * Weapon battle model. 1 byte.
-             *
-             * This byte is divided into two nybbles (four bits). Upper nybble
-             * determines the attack animation modifier, and it's only used for
-             * Barret and Vincent. The lower nybble is actually the index of the
-             * weapon model.
-             */
-            u8 model_raw;
-
-            /**
-             * Unused data (Always 0XFF). 1 byte.
-             */
-            u8 unused_2;
-
-            /**
-             * Consider it unused.
-             *
-             * @todo Verify.
-             */
-            u8 high_sound;
-
-            /**
-             * Camera movement ID. 2 bytes.
-             *
-             * Used for both single and multiple targeted attacks with this
-             * weapon. Always 0xFFFF.
-             */
-            u16 camera;
-
-            /**
-             * Characters that can equip the weapon. 2 bytes.
-             *
-             * Contains bits indicating the characters that can equip the weapon
-             * {@see character} for more info.
-             */
-            u16 equip_raw;
-
-            /**
-             * Weapon element ids.
-             */
-            u16 element_raw;
-
-            /**
-             * Unused data (Always 0XFFFF). 1 byte.
-             */
-            u8 unused_3;
-
-            /**
-             * Stat the weapon raises.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_raw[4];
-
-            /**
-             * The amount the raised stats are raised by.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_bonus_raw[4];
-
-            /**
-             * Materia slots.
-             *
-             * {@see slots} for more info.
-             */
-            u8 slots_raw[8];
-
-            /**
-             * Sound effect id for normal hit.
-             */
-            u8 sound;
-
-            /**
-             * Sound effect id for critical hit.
-             */
-            u8 sound_critical;
-
-            /**
-             * Sound effect id for miss hit.
-             */
-            u8 sound_miss;
-
-            /**
-             * Impcat effect ID. 1 byte.
-             *
-             * Used for animation in battle.
-             */
-            u8 effect;
-
-            /**
-             * Special flags. 2 bytes.
-             *
-             * @todo document and parse with info from
-             * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
-             */
-            u16 special_raw;
-
-            /**
-             * Weapon restrictions. 2 bytes.
-             *
-             * Contains bits indicating if the item is {@see selleable},
-             * {@see useable_battle} and {@see useable_menu} and
-             * {@see throwable}.
-             */
-            u16 restrict_raw;
-
-            /**
-             * Weapon ID.
-             *
-             * Not actually in the item file data, it's the order at which it
-             * appears.
-             */
-            int id;
-
-            /**
-             * Weapon name.
-             *
-             * Not in the same file as the rest of the data, but on file 20.
-             */
-            std::string name;
-
-            /**
-             * Weapon description.
-             *
-             * Not in the same file as the rest of the data, but on file 12.
-             */
-            std::string description;
-
-            /**
-             * Indicates if the weapon can be sold.
-             *
-             * True if the bit 0 in {@see restrict_raw} is 0.
-             */
-            bool sellable;
-
-            /**
-             * Indicates if the weapon can be used in battle.
-             *
-             * True if the bit 2 in {@see restrict_raw} is 0.
-             */
-            bool useable_battle;
-
-            /**
-             * Indicates if the weapon can be used in the menu.
-             *
-             * True if the bit 4 in {@see restrict_raw} is 0.
-             */
-            bool useable_menu;
-
-            /**
-             * Indicates if the weapon can be sold.
-             *
-             * True if the bit 8 in {@see restrict_raw} is 0.
-             */
-            bool throwable;
-
-            /**
-             * Target selection mode.
-             *
-             * Derived from {@see target_raw}.
-             */
-            Target target;
-
-            /**
-             * The damage formula.
-             *
-             * It's the upper nybble in {@see damage_raw}.
-             */
-            u8 damage_formula;
-
-            /**
-             * The damage modifier.
-             *
-             * It's the lower nybble in {@see damage_raw}.
-             */
-            u8 damage_modifier;
-
-            /**
-             * Attack animation ID.
-             *
-             * It's the upper nybble in {@see model_raw}. It's only used for
-             * Barret and Vincent.
-             */
-            u8 animation_index;
-
-            /**
-             * Weapon model ID.
-             *
-             * It's the lower nybble in {@see model_raw}.
-             */
-            u8 model;
-
-            /**
-             * The weapon battle model index.
-             *
-             * It's the lower nybble in {@see model_raw}.
-             */
-            std::vector<u8> equip;
-
-            /**
-             * Bonus in stats.
-             *
-             * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
-             */
-            std::vector<StatBonus> stat_bonus;
-
-            /**
-             * Materia slots.
-             *
-             * Derived and simplified from {@see slots_raw}.
-             */
-            std::vector<u8> materia_slots;
-
-            /**
-             * Elements of the weapon.
-             *
-             * Derived from {@see element_raw}.
-             */
-            std::vector<int> elements;
-
-        };
-
-        /**
-         * Data for each armor
-         *
-         * As found in KERNEL.BIN, file 6, in order, up to unknown_3. Members
-         * after unknown_3 are not found literally in KERNEL.BIN, but are
-         * derived from other members or found in other kernel files (text
-         * files).
-         */
-        struct ArmorData{
-
-            /**
-             * Unknown (Always 0XFF). 1 byte.
-             */
-            u8 unknown_0;
-
-            /**
-             * Elemental resistance mode. 1 byte.
-             *
-             * For all elements in {@see element_raw}:
-             * If 0: Absorbs.
-             * If 1: Nullifies.
-             * If 2: Halves.
-             * If any other value: normal.
-             */
-            u8 element_defense_mode;
-
-            /**
-             * Defense stat. 1 byte.
-             */
-            u8 defense;
-
-            /**
-             * Magic defense stat. 1 byte.
-             */
-            u8 m_defense;
-
-            /**
-             * Evasion stat. 1 byte.
-             */
-            u8 evasion;
-
-            /**
-             * Magic evasion stat. 1 byte.
-             */
-            u8 m_evasion;
-
-            /**
-             * The status effect the armor protects against. 1 byte.
-             */
-            u8 status;
-
-            /**
-             * Unknown (Always 0XFFFF). 1 byte.
-             */
-            u16 unknown_1;
-
-            /**
-             * Materia slots.
-             *
-             * {@see slots} for more info.
-             */
-            u8 slots_raw[8];
-
-            /**
-             * Armor materia growth multiplier. 1 byte.
-             */
-            u8 growth;
-
-            /**
-             * Characters that can equip the armor. 2 bytes.
-             *
-             * Contains bits indicating the characters that can equip the armor
-             * {@see character} for more info.
-             */
-            u16 equip_raw;
-
-            /**
-             * Elements the armor protects agains.
-             */
-            u16 element_raw;
-
-            /**
-             * Unknown (Always 0XFF). 1 byte.
-             */
-            u16 unknown_2;
-
-            /**
-             * Stat the weapon raises.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_raw[4];
-
-            /**
-             * The amount the raised stats are raised by.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_bonus_raw[4];
-
-            /**
-             * Weapon restrictions. 2 bytes.
-             *
-             * Contains bits indicating if the item is {@see selleable},
-             * {@see useable_battle} and {@see useable_menu} and
-             * {@see throwable}.
-             */
-            u16 restrict_raw;
-
-            /**
-             * Unknown (Always 0XFFFF). 1 byte.
-             */
-            u16 unknown_3;
-
-            /**
-             * Armor ID.
-             *
-             * Not in the item file data, it's the order at which it appears.
-             */
-            int id;
-
-            /**
-             * Armor name.
-             *
-             * Not in the same file as the rest of the data, but on file 20.
-             */
-            std::string name;
-
-            /**
-             * Armor description.
-             *
-             * Not in the same file as the rest of the data, but on file 12.
-             */
-            std::string description;
-
-            /**
-             * Indicates if the armor can be sold.
-             *
-             * True if the bit 0 in {@see restrict_raw} is 0.
-             */
-            bool sellable;
-
-            /**
-             * Indicates if the armor can be used in battle.
-             *
-             * True if the bit 2 in {@see restrict_raw} is 0.
-             */
-            bool useable_battle;
-
-            /**
-             * Indicates if the armor can be used in the menu.
-             *
-             * True if the bit 4 in {@see restrict_raw} is 0.
-             */
-            bool useable_menu;
-
-            /** Whether the item can be thrown (Throw command). */
-            bool throwable;
-
-            /**
-             * The weapon battle model index.
-             *
-             * It's the lower nybble in {@see model_raw}.
-             */
-            std::vector<u8> equip;
-
-            /**
-             * Bonus in stats.
-             *
-             * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
-             */
-            std::vector<StatBonus> stat_bonus;
-
-            /**
-             * Materia slots.
-             *
-             * Derived and simplified from {@see slots_raw}.
-             */
-            std::vector<u8> materia_slots;
-
-            /**
-             * Elements of the weapon.
-             *
-             * Derived from {@see element_raw}.
-             */
-            std::vector<int> elements;
-
-        };
-
-        /**
-         * Data for each accessory
-         *
-         * As found in KERNEL.BIN, file 7, in order, up to unknown_3. Members
-         * after unknown_3 are not found literally in KERNEL.BIN, but are not
-         * found literally in KERNEL.BIN, but are derived from other members or
-         * found in other files of the kernel (text files).
-         */
-        struct AccessoryData{
-
-            /**
-             * Stat the weapon raises.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_raw[2];
-
-            /**
-             * The amount the raised stats are raised by.
-             *
-             * {@see stat_bonus} for more info.
-             */
-            u8 stat_bonus_raw[2];
-
-            /**
-             * Elemental resistance mode. 1 byte.
-             *
-             * For all elements in {@see element_raw}:
-             * If 0: Absorbs.
-             * If 1: Nullifies.
-             * If 2: Halves.
-             * If any other value: normal.
-             */
-            u8 element_defense_mode;
-
-            /**
-             * The special status effect the accesory induces.
-             *
-             * 0: Auto haste.
-             * 1: Auto berserk.
-             * 2: Curse.
-             * 3: Auto reflect.
-             * 4: Steal rate +.
-             * 5: Manipulate rate +.
-             * 6: Barrier + MBarrier.
-             * Other: Nothing.
-             */
-            u8 effect;
-
-            /**
-             * Elements the armor protects against. 2 bytes.
-             *
-             * The defense type is defined in {@see element_defense_mode}.
-             */
-            u16 element_raw;
-
-            /**
-             * The status effect the armor protects against. 4 bytes.
-             */
-            u32 status_raw;
-
-            /**
-             * Characters that can equip the armor. 2 bytes.
-             *
-             * Contains bits indicating the characters that can equip the armor
-             * {@see character} for more info. 
-             * for more info.
-             */
-            u16 equip_raw;
-
-            /**
-             * Weapon restrictions. 2 bytes.
-             *
-             * Contains bits indicating if the item is {@see selleable},
-             * {@see useable_battle} and {@see useable_menu} and
-             * {@see throwable}.
-             */
-            u16 restrict_raw;
-
-            /**
-             * Accessory ID.
-             *
-             * Not in the item file data, it's the order at which itappears.
-             */
-            int id;
-
-            /**
-             * Accessory name.
-             *
-             * Not in the same file as the rest of the data, but on file 20.
-             */
-            std::string name;
-
-            /**
-             * Accessory description.
-             *
-             * Not in the same file as the rest of the data, but on file 12.
-             */
-            std::string description;
-
-            /**
-             * Indicates if the accessory can be sold.
-             *
-             * True if the bit 0 in {@see restrict_raw} is 0.
-             */
-            bool sellable;
-
-            /**
-             * Indicates if the accessory can be used in battle.
-             *
-             * True if the bit 2 in {@see restrict_raw} is 0.
-             */
-            bool useable_battle;
-
-            /**
-             * Indicates if the accessory can be used in the menu.
-             *
-             * True if the bit 4 in {@see restrict_raw} is 0.
-             */
-            bool useable_menu;
-
-            /** Whether the item can be thrown (Throw command). */
-            bool throwable;
-
-            /**
-             * The weapon battle model index.
-             *
-             * It's the lower nybble in {@see model_raw}.
-             */
-            std::vector<u8> equip;
-
-            /**
-             * Bonus in stats.
-             *
-             * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
-             */
-            std::vector<StatBonus> stat_bonus;
-
-            /**
-             * Elements of the accesory.
-             *
-             * Derived from {@see element_raw}.
-             */
-            std::vector<int> elements;
-
-            /**
-             * Statuses the accessory protects against.
-             *
-             * Derived from {@see status_raw}.
-             */
-            std::vector<int> status;
-
-        };
-
-        /**
-         * Types of materia.
-         */
-        enum class MATERIA_TYPE{
-
-            /**
-             * Magic materia.
-             */
-            MAGIC = 1,
-
-            /**
-             * Support materia.
-             */
-            SUPPORT,
-
-            /**
-             * Command materia.
-             */
-            COMMAND,
-
-            /**
-             * Independent materia.
-             */
-            INDEPENDENT,
-
-            /**
-             * Summon materia.
-             */
-            SUMMON
-        };
-
-        /**
-         * Stat bonuses given by equipping materia.
-         *
-         * They can be negative.
-         */
-        struct MateriaStatBonus{
-
-            /**
-             * Strength bonus. Absolute.
-             */
-            int str;
-
-            /**
-             * Vitality bonus. Absolute.
-             */
-            int vit;
-
-            /**
-             * Magic bonus. Absolute.
-             */
-            int mag;
-
-            /**
-             * Spirit bonus. Absolute.
-             */
-            int spr;
-
-            /**
-             * Dexterity bonus. Absolute.
-             */
-            int dex;
-
-            /**
-             * Luck bonus. Absolute.
-             */
-            int lck;
-
-            /**
-             * Max HP bonus. Percentile.
-             */
-            int hp;
-
-            /**
-             * Max MP bonus. Percentile.
-             */
-            int mp;
-
-            /**
-             * Indicates if at least one of the stats is changed by the materia.
-             */
-            bool change;
-        };
-
-        /**
-         * Data for each materia.
-         *
-         * As found in KERNEL.BIN, file 8, in order, up to attributes. Members
-         * after attributes are not found literally in KERNEL.BIN, but are
-         * derived from other members or found in other files of the kernel
-         * (text files).
-         */
-        struct MateriaData{
-
-            /**
-             * AP amounts at which the materia levels up. 4 x 1 byte.
-             *
-             * It can be used to determine how many levels the materia has.
-             */
-            u16 level_up_ap[4];
-
-            /**
-             * Index of stat modifier. 1 byte.
-             */
-            u8 stat_raw;
-
-            /**
-             * Status effects. 3 bytes, not 4!
-             */
-            u32 status_raw;
-
-            /**
-             * Materia element. 1 byte.
-             */
-            u8 element;
-
-            /**
-             * Materia type. 1 byte.
-             *
-             * 2 nybbles. The upper one is the subtype, and it's ignored in
-             * V-Gears. The lower one the is actual type.
-             */
-            u8 type_raw;
-
-            /**
-             * Materia attributes. 6 x 1 byte.
-             *
-             * They are mostly ignored in V-Gears.
-             */
-            u8 attribute[6];
-
-            /**
-             * Accessory ID.
-             *
-             * Not in the item file data, it's the order at which it appears.
-             */
-            int id;
-
-            /**
-             * Accessory name.
-             *
-             * Not in the same file as the rest of the data, but on file 20.
-             */
-            std::string name;
-
-            /**
-             * Accessory description.
-             *
-             * Not in the same file as the rest of the data, but on file 12.
-             */
-            std::string description;
-
-            /**
-             * Materia type.
-             *
-             * Derived from {@see type_raw}.
-             */
-            int type;
-
-            /**
-             * Materia type.
-             *
-             * Derived from {@see type_raw}.
-             */
-            int sub_type;
-
-            /**
-             * Stat bonuses for equipping the materia.
-             *
-             * Derived from {@see stat_raw}.
-             */
-            MateriaStatBonus stats;
-
-            /**
-             * Statuses the materia inflicts.
-             *
-             * Derived from {@see status_raw}.
-             */
-            std::vector<int> status;
-
-            /**
-             * Commands the materia enables in battle.
-             *
-             * Derived from {@see type_raw}.
-             */
-            std::vector<int> command;
-
-            /**
-             * Attacks the materia enables in battle, at each level.
-             *
-             * Derived from {@see type_raw} and {@see attribute}.
-             */
-            int attack[6];
-
-            /**
-             * Number of times per battle an attack can be used, per level.
-             *
-             * -1 means infinite times. Derived from {@see type_raw} and
-             * {@see attribute}.
-             */
-            int attack_times[6];
-
-            /**
-             * Ability confered by the materia.
-             *
-             * Can be provided by independent and independent materia.
-             */
-            std::string ability;
-
-            /**
-             * Parameter for the ability, varies per level.
-             *
-             * It can be the number of times the ability can be executed, or the
-             * chance to activate...
-             */
-            int ability_parameters[5];
         };
 
         /**
-         * Data for each key item.
-         *
-         * There is no data file about key items in KERNEL.BIN, there are only
-         * names (file 24) and descriptions (file 16)
+         * Items read from the kernel.
          */
-        struct KeyItemData{
-
-            /**
-             * Key item ID.
-             *
-             * Not in KERNEL.BIN, it's the order at which they appear.
-             */
-            int id;
-
-            /**
-             * Key item name.
-             */
-            std::string name;
-
-            /**
-             * Key item description.
-             */
-            std::string description;
-        };
+        std::vector<KernelData::CommandData> commands_;
 
         /**
-         * List of summon names.
-         *
-         * There is no data file about key items in KERNEL.BIN, there are only
-         * names (file 26).
+         * Attacks read from the kernel.
          */
-        struct SummonNameData{
-
-            /**
-             * Summon ID.
-             *
-             * Not actually in KERNEL.BIN, it;s the order at which they appear.
-             */
-            int id;
-
-            /**
-             * Summon name.
-             */
-            std::string name;
-        };
+        std::vector<KernelData::AttackData> attacks_;
+        /** Limit techniques (attack IDs 128 and up). Only name and description are filled. */
+        std::vector<KernelData::AttackData> limit_attacks_;
 
         /**
-         * Character data.
-         *
-         * Contains information about a character. Contained in the file 3 of
-         * the kernel, one entry for each character. Each section is 56 bytes
-         * long. The character ID is not actually in the data, but it's the
-         * order at which they appear. The character name is also not in the
-         * data and it must be filled manually.
+         * Character info read from the kernel.
          */
-        struct CharacterData{
-
-            /**
-             * Strength stat growth curve. 1 byte.
-             */
-            u8 curve_str;
-
-            /**
-             * Vitality stat growth curve. 1 byte.
-             */
-            u8 curve_vit;
-
-            /**
-             * Magic stat growth curve. 1 byte.
-             */
-            u8 curve_mag;
-
-            /**
-             * Spirit stat growth curve. 1 byte.
-             */
-            u8 curve_spr;
-
-            /**
-             * Dexterity stat growth curve. 1 byte.
-             */
-            u8 curve_dex;
-
-            /**
-             * Luck stat growth curve. 1 byte.
-             */
-            u8 curve_lck;
-
-            /**
-             * HP stat growth curve. 1 byte.
-             */
-            u8 curve_hp;
-
-            /**
-             * MP stat growth curve. 1 byte.
-             */
-            u8 curve_mp;
-
-            /**
-             * Experience-for-next-level growth curve. 1 byte.
-             */
-            u8 curve_exp;
-
-            /**
-             * Ignored data. 1 byte.
-             */
-            u8 unused_0;
-
-            /**
-             * Starting level. 1 byte.
-             */
-            u8 initial_level;
-
-            /**
-             * Ignored data. 1 byte.
-             */
-            u8 unused_1;
-
-            /**
-             * 1-1 limit slot command. 1 byte.
-             */
-            u8 limit_1_1;
-
-            /**
-             * 1-2 limit slot command. 1 byte.
-             */
-            u8 limit_1_2;
-
-            /**
-             * 1-3 limit slot command. 1 byte, unused.
-             */
-            u8 limit_1_3;
-
-            /**
-             * 2-1 limit slot command. 1 byte.
-             */
-            u8 limit_2_1;
-
-            /**
-             * 2-2 limit slot command. 1 byte.
-             */
-            u8 limit_2_2;
-
-            /**
-             * 2-3 limit slot command. 1 byte, unused.
-             */
-            u8 limit_2_3;
-
-            /**
-             * 3-1 limit slot command. 1 byte.
-             */
-            u8 limit_3_1;
-
-            /**
-             * 3-2 limit slot command. 1 byte.
-             */
-            u8 limit_3_2;
-
-            /**
-             * 3-3 limit slot command. 1 byte, unused.
-             */
-            u8 limit_3_3;
-
-            /**
-             * 4-1 limit slot command. 1 byte.
-             */
-            u8 limit_4_1;
-
-            /**
-             * 4-2 limit slot command. 1 byte, unused.
-             */
-            u8 limit_4_2;
-
-            /**
-             * 4-3 limit slot command. 1 byte, unused.
-             */
-            u8 limit_4_3;
-
-            /**
-             * Kills required to unlock limit level 2. 2 bytes.
-             */
-            u16 limit_2_kills;
-
-            /**
-             * Kills required to unlock limit level 3. 2 bytes.
-             */
-            u16 limit_3_kills;
-
-            /**
-             * Kills required to unlock limit 1-2. 2 bytes.
-             */
-            u16 limit_1_2_uses;
-
-            /**
-             * Kills required to unlock limit 1-3. 2 bytes, unused.
-             */
-            u16 limit_1_3_uses;
-
-            /**
-             * Kills required to unlock limit 2-2. 2 bytes.
-             */
-            u16 limit_2_2_uses;
-
-            /**
-             * Kills required to unlock limit 2-3. 2 bytes, unused.
-             */
-            u16 limit_2_3_uses;
-
-            /**
-             * Kills required to unlock limit 3-2. 2 bytes.
-             */
-            u16 limit_3_2_uses;
-
-            /**
-             * Kills required to unlock limit 3-3. 2 bytes, unused.
-             */
-            u16 limit_3_3_uses;
+        std::vector<KernelData::CharacterData> characters_;
 
-            /**
-             * HP divisor for limit level 1.
-             */
-            u32 limit_1_div;
-
-            /**
-             * HP divisor for limit level 2.
-             */
-            u32 limit_2_div;
-
-            /**
-             * HP divisor for limit level 3.
-             */
-            u32 limit_3_div;
-
-            /**
-             * HP divisor for limit level 4.
-             */
-            u32 limit_4_div;
-
-            /**
-             * Character ID.
-             */
-            int id;
-
-            /**
-             * Character default name.
-             */
-            std::string name;
-
-        };
+        KernelData::GrowthData growth_;
 
         /**
-         * Types of stat curves.
-         *
-         * Althought any of them can be used for any stat, this is their
-         * intended use.
+         * Items read from the kernel.
          */
-        enum STAT_CURVE_TYPE{
-
-            /**
-             * Normal curve, used for primary stat.
-             */
-            CURVE_PRIMARY = 0,
-
-            /**
-             * HP curve. Base values must be multiplied by 40 when calculating.
-             */
-            CURVE_HP,
-
-            /**
-             * MP curve. Base values must be multiplied by 2 when calculating.
-             */
-            CURVE_MP,
-
-            /**
-             * Experience curve. Base must be ignored when calculating. Gradient
-             * is quadratic.
-             */
-            CURVE_EXP
-
-        };
+        std::vector<KernelData::ItemData> items_;
 
         /**
-         * Stat curve.
-         *
-         * determines value growth
+         * Weapons read from the kernel.
          */
-        struct StatCurve{
-
-            /**
-             * Curve ID.
-             */
-            int id;
-
-            /**
-             * Type of curve.
-             */
-            STAT_CURVE_TYPE type;
-
-            /**
-             * Curve gradients.
-             */
-            u8 gradient[8];
-
-            /**
-             * Curve bases.
-             */
-            s8 base[8];
-
-        };
+        std::vector<KernelData::WeaponData> weapons_;
 
         /**
-         * Battle data.
-         *
-         * Contains several pieces of data related to character growth.
-         * Contained in the file 3 of the kernel, after the character data
-         * (starts at offset 0x01F8). The kernel contains more data, but it's
-         * not needed for V-Gears (random table, scene lookup...)
+         * Armors read from the kernel.
          */
-        struct GrowthData{
-
-            /**
-             * Random bonus to primary stats, 12 entries of 1 byte.
-             */
-            u8 bonus_stat[12];
-
-            /**
-             * Random bonus to HP stat, 12 entries of 1 byte.
-             */
-            u8 bonus_hp[12];
-
-            /**
-             * Random bonus to MP stat, 12 entries of 1 byte.
-             */
-            u8 bonus_mp[12];
-
-            /**
-             * Stat curves. 64 curves of 16 bit each.
-             *
-             * 37 for primary stats, 9 for HP, 9 form MP, 9 for experience.
-             */
-            StatCurve curves[64];
-        };
+        std::vector<KernelData::ArmorData> armors_;
 
         /**
-         * Retrieves the 3-letter name of a basic stat from it's id.
-         *
-         * @param[in] id The stat ID.
-         * @return Stat name abbreviation. If the stat ID is unknown, "stat"
-         * will be returned.
+         * Accessories read from the kernel.
          */
-        std::string GetBasicStatName(int id);
+        std::vector<KernelData::AccessoryData> accessories_;
 
         /**
-         * Items read from the kernel.
+         * Materia read from the kernel.
          */
-        std::vector<CommandData> commands_;
+        std::vector<KernelData::MateriaData> materia_;
 
         /**
-         * Attacks read from the kernel.
+         * Key items read from the kernel.
          */
-        std::vector<AttackData> attacks_;
-        /** Limit techniques (attack IDs 128 and up). Only name and description are filled. */
-        std::vector<AttackData> limit_attacks_;
+        std::vector<KernelData::KeyItemData> key_items_;
 
         /**
-         * Character info read from the kernel.
+         * Summon names read from the kernel.
          */
-        std::vector<CharacterData> characters_;
+        std::vector<KernelData::SummonNameData> summon_names_;
 
-        GrowthData growth_;
+        /**
+         * The initial savemap structure.
+         */
+        SaveMap savemap_;
 
         /**
-         * Items read from the kernel.
+         * Sets the colour of a materia and the type it belongs to.
+         *
+         * @param[in,out] data The materia. Its raw type has to be set.
          */
-        std::vector<ItemData> items_;
+        static void SetMateriaType(KernelData::MateriaData& data);
 
         /**
-         * Weapons read from the kernel.
+         * Sets the stats that a materia changes.
+         *
+         * @param[in,out] data The materia. Its raw stat byte has to be set.
          */
-        std::vector<WeaponData> weapons_;
+        static void SetMateriaStats(KernelData::MateriaData& data);
 
         /**
-         * Armors read from the kernel.
+         * Writes the attacks that a magic materia teaches.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        std::vector<ArmorData> armors_;
+        void WriteMagicMateria(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
-         * Accessories read from the kernel.
+         * Writes the summon of a summon materia.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        std::vector<AccessoryData> accessories_;
+        void WriteSummonMateria(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
-         * Materia read from the kernel.
+         * Writes the commands of a command materia.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        std::vector<MateriaData> materia_;
+        void WriteCommandMateria(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
-         * Key items read from the kernel.
+         * Writes the list of the commands that a materia gives.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        std::vector<KeyItemData> key_items_;
+        void WriteMateriaCommandList(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
-         * Summon names read from the kernel.
+         * Writes the effect of a support materia on the materia linked to it.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        std::vector<SummonNameData> summon_names_;
+        static void WriteSupportMateria(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
-         * The initial savemap structure.
+         * Writes the ability of an independent materia.
+         *
+         * @param[in] file The file to write to.
+         * @param[in] materia The materia.
          */
-        SaveMap savemap_;
+        static void WriteIndependentMateria(std::ostream& file, const KernelData::MateriaData& materia);
 
         /**
          * Reads one character record of the savemap.

+ 123 - 0
src/installer/extractors/KernelDataParts.cpp

@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "installer/extractors/KernelDataParts.h"
+
+namespace KernelData{
+
+    void ReadTarget(const u8 raw, Target& target){
+        target.selection_enabled = raw & 0x01;
+        target.default_enemy = raw & 0x02;
+        target.default_multiple = raw & 0x04;
+        target.toggle_multiple = raw & 0x08;
+        target.fixed_row = raw & 0x10;
+        target.short_range = raw & 0x20;
+        target.all_rows = raw & 0x40;
+        target.random = raw & 0x80;
+    }
+
+    void ReadElements(const u16 raw, std::vector<int>& elements){
+        for (int element = FIRE; element <= HIDDEN; element ++)
+            if (raw & (1 << element)) elements.push_back(element);
+        // In KERNEL.BIN, non elemental records actually have all the elements marked.
+        if (elements.size() == 16) elements.clear();
+    }
+
+    void ReadStatuses(const u32 raw, std::vector<int>& statuses){
+        for (int status = DEATH; status <= IMPRISONED; status ++)
+            if (raw & (1u << status)) statuses.push_back(status);
+    }
+
+    void ReadStatusEffect(const u8 change, const u32 raw, StatusEffect& status){
+        // The lower 6 bits are the chance out of 63, and the upper bits the mode (0x80 toggles the
+        // statuses, 0x40 cures them, otherwise they are inflicted).
+        status.chance = change & 0x3F;
+        if ((change & 0x80) != 0) status.mode = TOGGLE;
+        else if ((change & 0x40) != 0) status.mode = CURE;
+        else status.mode = INFLICT;
+        // In KERNEL.BIN, records that don't change statuses actually have all of them marked, and
+        // a chance of 255. The status IDs are the bit positions of the original game.
+        if (change != 0xFF && raw != 0xFFFFFFFF) ReadStatuses(raw, status.status);
+    }
+
+    RESTORE_TYPE ReadRestoreType(const u8 condition){
+        switch (condition){
+            case 0: return RESTORE_HP;
+            case 1: return RESTORE_MP;
+            case 2: return RESTORE_STATUS;
+            default: return RESTORE_NONE;
+        }
+    }
+
+    void ReadUsers(const u16 raw, std::vector<u8>& users){
+        for (int character = 0; character <= 10; character ++)
+            if (raw & (1 << character)) users.push_back(character);
+    }
+
+    void ReadStatBonuses(
+      const u8* stats, const u8* amounts, const int count, std::vector<StatBonus>& bonuses
+    ){
+        for (int s = 0; s < count; s ++){
+            if (stats[s] < 0XFF && amounts[s] > 0){
+                StatBonus stat_bonus;
+                stat_bonus.stat = stats[s];
+                stat_bonus.bonus = amounts[s];
+                bonuses.push_back(stat_bonus);
+            }
+        }
+    }
+
+    void ReadMateriaSlots(const u8 (&raw)[8], std::vector<u8>& slots){
+        for (const u8 slot : raw){
+            if (slot == 1 || slot == 5) slots.push_back(SLOT_UNLINKED);
+            else if (slot == 2 || slot == 3 || slot == 6 || slot == 7) slots.push_back(SLOT_LINKED);
+        }
+    }
+
+    void WriteTarget(std::ostream& file, const Target& target){
+        file
+          << "    target = {selectable = " << target.selection_enabled
+          << ", default_enemy = " << target.default_enemy
+          << ", default_multiple = " << target.default_multiple
+          << ", toggle_multiple = " << target.toggle_multiple
+          << ", fixed_row = " << target.fixed_row
+          << ", short_range = " << target.short_range
+          << ", all_rows = " << target.all_rows
+          << ", random = " << target.random << "},\n";
+    }
+
+    void WriteStatBonuses(std::ostream& file, const std::vector<StatBonus>& bonuses){
+        file << "    stats = {";
+        for (const StatBonus& s : bonuses){
+            file << "\n        " << GetBasicStatName(s.stat) << " = "
+              << static_cast<int>(s.bonus) << ",";
+        }
+        if (bonuses.size() > 0) file << "\n    },\n";
+        else file << "},\n";
+    }
+
+    std::string GetBasicStatName(const int id){
+        std::string name = "stat";
+        switch (id){
+            case 0: name = "str"; break;
+            case 1: name = "vit"; break;
+            case 2: name = "mag"; break;
+            case 3: name = "spr"; break;
+            case 4: name = "dex"; break;
+            case 5: name = "lck"; break;
+        }
+        return name;
+    }
+}

+ 132 - 0
src/installer/extractors/KernelDataParts.h

@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <ostream>
+#include <string>
+#include <vector>
+#include "installer/extractors/KernelDataTypes.h"
+
+/*
+ * Reading and writing of the data that several kinds of kernel records have.
+ */
+namespace KernelData{
+
+    /**
+     * Reads who can be targeted, as in the records of attacks, items and weapons.
+     *
+     * @param[in] raw The target byte.
+     * @param[in,out] target The target.
+     */
+    void ReadTarget(const u8 raw, Target& target);
+
+    /**
+     * Reads the elements of a record. A record with all of them has none.
+     *
+     * @param[in] raw A bit for each element.
+     * @param[in,out] elements The elements are added here.
+     */
+    void ReadElements(const u16 raw, std::vector<int>& elements);
+
+    /**
+     * Reads the statuses of a record.
+     *
+     * @param[in] raw A bit for each status.
+     * @param[in,out] statuses The statuses are added here.
+     */
+    void ReadStatuses(const u32 raw, std::vector<int>& statuses);
+
+    /**
+     * Reads the statuses that an attack or an item changes, how, and with what chance.
+     *
+     * @param[in] change The status change byte.
+     * @param[in] raw A bit for each status.
+     * @param[in,out] status The status effect.
+     */
+    void ReadStatusEffect(const u8 change, const u32 raw, StatusEffect& status);
+
+    /**
+     * Reads what an attack or an item restores.
+     *
+     * @param[in] condition The condition byte.
+     * @return What it restores.
+     */
+    RESTORE_TYPE ReadRestoreType(const u8 condition);
+
+    /**
+     * Reads the characters that can equip a record.
+     *
+     * @param[in] raw A bit for each character.
+     * @param[in,out] users The IDs of the characters are added here.
+     */
+    void ReadUsers(const u16 raw, std::vector<u8>& users);
+
+    /**
+     * Reads the stat bonuses of a piece of equipment.
+     *
+     * @param[in] stats The stats, 0xFF for none.
+     * @param[in] amounts The bonus of each stat.
+     * @param[in] count Number of bonuses.
+     * @param[in,out] bonuses The bonuses are added here.
+     */
+    void ReadStatBonuses(
+      const u8* stats, const u8* amounts, const int count, std::vector<StatBonus>& bonuses
+    );
+
+    /**
+     * Reads the materia slots of a weapon or an armor.
+     *
+     * @param[in] raw The eight slot bytes.
+     * @param[in,out] slots The slots are added here.
+     */
+    void ReadMateriaSlots(const u8 (&raw)[8], std::vector<u8>& slots);
+
+    /**
+     * Reads where an item can be used and if it can be sold or thrown.
+     *
+     * @param[in,out] data The record. Its restriction bits have to be set.
+     */
+    template<typename T> void ReadRestrictions(T& data){
+        data.sellable = !(data.restrict_raw & 1);
+        data.useable_battle = !(data.restrict_raw & 2);
+        data.useable_menu = !(data.restrict_raw & 4);
+        data.throwable = !(data.restrict_raw & 8);
+    }
+
+    /**
+     * Writes a target as the field of a Lua table.
+     *
+     * @param[in] file The file to write to.
+     * @param[in] target The target.
+     */
+    void WriteTarget(std::ostream& file, const Target& target);
+
+    /**
+     * Writes the stat bonuses of a piece of equipment as the field of a Lua table.
+     *
+     * @param[in] file The file to write to.
+     * @param[in] bonuses The bonuses.
+     */
+    void WriteStatBonuses(std::ostream& file, const std::vector<StatBonus>& bonuses);
+
+    /**
+     * Retrieves the name of a stat as used in the Lua scripts.
+     *
+     * @param[in] id The stat.
+     * @return The name of the stat.
+     */
+    std::string GetBasicStatName(const int id);
+}

+ 473 - 0
src/installer/extractors/KernelDataTypes.h

@@ -0,0 +1,473 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <vector>
+#include "common/TypeDefine.h"
+
+/*
+ * Types of the kernel data that several kinds of data use.
+ */
+namespace KernelData{
+
+    enum ELEMENT{
+
+        /**
+         * Fire element.
+         */
+        FIRE = 0,
+
+
+        /**
+         * Ice element.
+         */
+        ICE,
+
+
+        /**
+         * Bolt element.
+         */
+        BOLT,
+
+
+        /**
+         * Earth element.
+         */
+        EARTH,
+
+
+        /**
+         * Poison element.
+         */
+        POISON,
+
+
+        /**
+         * Gravity element.
+         */
+        GRAVITY,
+
+
+        /**
+         * Water element.
+         */
+        WATER,
+
+
+        /**
+         * Wind element.
+         */
+        WIND,
+
+
+        /**
+         * Holy element.
+         */
+        HOLY,
+
+
+        /**
+         * Restorative element.
+         */
+        RESTORATIVE,
+
+
+        /**
+         * Cut element.
+         */
+        CUT,
+
+
+        /**
+         * Hit element.
+         */
+        HIT,
+
+
+        /**
+         * Punch element.
+         */
+        PUNCH,
+
+
+        /**
+         * Shoot element.
+         */
+        SHOOT,
+
+
+        /**
+         * Shout element.
+         */
+        SHOUT,
+
+
+        /**
+         * Hidden element.
+         */
+        HIDDEN,
+    };
+
+    /**
+     * List of target selection options.
+     */
+    struct Target{
+
+        /**
+         * The target can be selected.
+         *
+         * If the target of the attack is selectable, at least between some
+         * subset of possible targets, this must be true.
+         */
+        bool selection_enabled;
+
+        /**
+         * The default target is an enemy.
+         *
+         * When true, the cursor starts in an enemy row. When false, it
+         * starts in the ally row. It may or may not be possible to change
+         * it.
+         */
+        bool default_enemy;
+
+        /**
+         * The default target is a group.
+         *
+         * When true, the cursor starts will all the members of a row
+         * selected. When false, it starts with only one target selected. It
+         * may or may not be possible to change it.
+         */
+        bool default_multiple;
+
+        /**
+         * The selection can change between groups and individuals.
+         *
+         * When true, the target can be changed between groups and
+         * individuals. When false, only {@see default_multiple} determines
+         * if the target is an individual or a group.
+         */
+        bool toggle_multiple;
+
+        /**
+         * The target group can be changed.
+         *
+         * When true, the target cannot be changed between enemies and
+         * allies. In this case {@see default_enemy} will determine the
+         * group that can be targeted.
+         */
+        bool fixed_row;
+
+        /**
+         * The command is short ranged.
+         *
+         * When true, if the target or the caster is not in the front of
+         * their row, the target will take half damage. Also, when true, an
+         * enemy covered by another enemy can't be targeted.
+         */
+        bool short_range;
+
+        /**
+         * Targets all, enemies and allies.
+         *
+         * When true, all allies and enemies are targets, and the selection
+         * can't be changed.
+         */
+        bool all_rows;
+
+        /**
+         * The target is random.
+         *
+         * When multiple targets are selected, one will be selected at
+         * random to be the receiving target. Cursor will cycle among all
+         * viable targets.
+         */
+        bool random;
+
+    };
+
+    /**
+     * Determines what an item affects on when used from the menu.
+     */
+    enum RESTORE_TYPE{
+
+        /**
+         * The item affects a party member or group HP.
+         */
+        RESTORE_HP = 0,
+
+        /**
+         * The item affects a party member or group MP.
+         */
+        RESTORE_MP = 1,
+
+        /**
+         * The item affects a party member or group status.
+         */
+        RESTORE_STATUS = 2,
+
+        /**
+         * The item is not restorative.
+         */
+        RESTORE_NONE = 3
+    };
+
+    /**
+     * List of altered status.
+     */
+    enum STATUS{
+
+        /**
+         * The character is dead.
+         */
+        DEATH = 0,
+
+        /**
+         * The character has critical HP.
+         */
+        NEAR_DEATH,
+
+        /**
+         * The character is asleep.
+         */
+        SLEEP,
+
+        /**
+         * The character is poisoned.
+         */
+        POISONED,
+
+        /**
+         * The character is sad.
+         */
+        SADNESS,
+
+        /**
+         * The character is furious.
+         */
+        FURY,
+
+        /**
+         * The character is confused.
+         */
+        CONFU,
+
+        /**
+         * The character is silenced.
+         */
+        SILENCE,
+
+        /**
+         * The character is slow.
+         */
+        HASTE,
+
+        /**
+         * The character is
+         */
+        SLOW,
+
+        /**
+         * The character is stopped.
+         */
+        STOP,
+
+        /**
+         * The character is a frog.
+         */
+        FROG,
+
+        /**
+         * The character is small.
+         */
+        SMALL,
+
+        /**
+         * The character is going to be petrified.
+         */
+        SLOW_NUMB,
+
+        /**
+         * The character is petrified.
+         */
+        PETRIFY,
+
+        /**
+         * The character is regenerating health.
+         */
+        REGEN,
+
+        /**
+         * The character has a physical barrier.
+         */
+        BARRIER,
+
+        /**
+         * The character has a magical barrier
+         */
+        M_BARRIER,
+
+        /**
+         * The character reflects magical attacks.
+         */
+        REFLECT,
+
+        /**
+         * The character has dual status.
+         *
+         * @todo What does it do.
+         */
+        DUAL,
+
+        /**
+         * The character has a shield.
+         */
+        SHIELD,
+
+        /**
+         * The character has a death sentence.
+         */
+        D_SENTENCE,
+
+        /**
+         * The character is being manipulated.
+         */
+        MANIPULATE,
+
+        /**
+         * The character is berserk.
+         */
+        BERSERK,
+
+        /**
+         * The character is invincible.
+         */
+        PEERLESS,
+
+        /**
+         * The character is paralyzed.
+         */
+        PARALYSIS,
+
+        /**
+         * The character is blinded.
+         */
+        DARKNESS,
+
+        /**
+         * The character has dual drain.
+         */
+        DUAL_DRAIN,
+
+        /**
+         * The character has deathforce.
+         */
+        DEATH_FORCE,
+
+        /**
+         * The character has resist.
+         */
+        RESIST,
+
+        /**
+         * Cait' Sith lucky girl mode.
+         */
+        LUCKY_GIRL,
+
+        /**
+         * The character is imprisioned.
+         */
+        IMPRISONED
+    };
+
+    /**
+     * How a status effect modifier attack or item can act.
+     */
+    enum STATUS_EFFECT{
+
+        /**
+         * Inflicts status changes.
+         */
+        INFLICT = 0,
+
+        /**
+         * Cures status changes.
+         */
+        CURE = 1,
+
+        /**
+         * Toggles status changes.
+         *
+         * If the target doesn't have the status change, inflict it. If the
+         * target already has the status change, cure it.
+         */
+        TOGGLE = 2
+    };
+
+    struct StatusEffect{
+
+        /**
+         * The list of status to inflict or cure.
+         */
+        std::vector<int> status;
+
+        /**
+         * The chance of landing a status effect (out of 63).
+         */
+        u16 chance;
+
+        /**
+         * The status change mode.
+         */
+        STATUS_EFFECT mode;
+
+    };
+
+    /**
+     * Materia slot types.
+     */
+    enum SLOT_TYPE{
+
+        /**
+         * A non linked slot.
+         */
+        SLOT_UNLINKED = 0,
+
+        /**
+         * A linked slot.
+         *
+         * If the slot is in an even position, it's linked to the one in the
+         * right. If it's in an odd position, it's linked to the one in the
+         * left. Remember that slot positions are 0-index.
+         */
+        SLOT_LINKED = 1
+    };
+
+    /**
+     * Bonus in stat givven by weapons or armor.
+     */
+    struct StatBonus{
+
+        /**
+         * ID of the raised stat.
+         */
+        u8 stat;
+
+        /**
+         * Stat value raise.
+         */
+        u8 bonus;
+    };
+}

+ 321 - 0
src/installer/extractors/KernelEquipment.cpp

@@ -0,0 +1,321 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <fstream>
+#include "installer/extractors/KernelDataInstaller.h"
+#include "installer/extractors/KernelDataParts.h"
+#include "installer/extractors/KernelTextList.h"
+
+using namespace KernelData;
+
+int KernelDataInstaller::ReadWeapons(){
+
+    // Empty the item list
+    weapons_.clear();
+
+    File weapons_file = kernel_.ExtractGZip(KERNEL_WEAPON_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_WEAPON_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_WEAPON_DESCRIPTIONS));
+    int weapon_count = 0;
+
+    for (int i = 0; i < 128; i ++){
+
+        WeaponData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // If the item doesn't have a name, it means all items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Read data from the weapon data section.
+        data.target_raw = weapons_file.readU8();
+        data.unused_0 = weapons_file.readU8();
+        data.damage_raw = weapons_file.readU8();
+        data.unused_1 = weapons_file.readU8();
+        data.power = weapons_file.readU8();
+        data.status = weapons_file.readU8();
+        data.growth = weapons_file.readU8();
+        data.critical = weapons_file.readU8();
+        data.accuracy = weapons_file.readU8();
+        data.model_raw = weapons_file.readU8();
+        data.unused_2 = weapons_file.readU8();
+        data.high_sound = weapons_file.readU8();
+        data.camera = weapons_file.readU16LE();
+        data.equip_raw = weapons_file.readU16LE();
+        data.element_raw = weapons_file.readU16LE();
+        data.unused_3 = weapons_file.readU16LE();
+        for (int s = 0; s < 4; s ++) data.stat_raw[s] = weapons_file.readU8();
+        for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = weapons_file.readU8();
+        for (int s = 0; s < 8; s ++) data.slots_raw[s] = weapons_file.readU8();
+        data.sound = weapons_file.readU8();
+        data.sound_critical = weapons_file.readU8();
+        data.sound_miss = weapons_file.readU8();
+        data.effect = weapons_file.readU8();
+        data.special_raw = weapons_file.readU16LE();
+        data.restrict_raw = weapons_file.readU16LE();
+
+
+        // Derive data from the read data.
+        data.id = i;
+        if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
+        ReadRestrictions(data);
+        ReadTarget(data.target_raw, data.target);
+        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
+        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
+        data.animation_index = data.model_raw >> 4; // Upper nybble.
+        data.model = data.model_raw & 15; // Lower nybble.
+        ReadUsers(data.equip_raw, data.equip);
+        ReadStatBonuses(data.stat_raw, data.stat_bonus_raw, 4, data.stat_bonus);
+        ReadMateriaSlots(data.slots_raw, data.materia_slots);
+        ReadElements(data.element_raw, data.elements);
+
+        // Add to the list of weapons.
+        weapons_.push_back(data);
+        weapon_count ++;
+    }
+
+    return weapon_count;
+}
+
+void KernelDataInstaller::WriteWeapons(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (WeaponData weapon : weapons_){
+        file << "Game.Items[" << weapon.id + 128 << "] = {\n"
+          << "    type = Inventory.ITEM_TYPE.WEAPON,\n"
+          << "    id = " << weapon.id + 128 << ",\n"
+          << "    price = " << prices_[weapon.id + 128] << ",\n"
+          << "    name = \"" << weapon.name << "\",\n"
+          << "    description = \"" << weapon.description << "\",\n"
+          << "    camera = " << weapon.camera << ",\n"
+          << "    sell = " << weapon.sellable << ",\n"
+          << "    battle = " << weapon.useable_battle << ",\n"
+          << "    menu = " << weapon.useable_menu << ",\n"
+          << "    throw = " << weapon.throwable << ",\n"
+          << "    effect = " << static_cast<int>(weapon.effect) << ",\n"
+          << "    dmg_formula = " << static_cast<int>(weapon.damage_formula) << ",\n"
+          << "    dmg_modifier = " << static_cast<int>(weapon.damage_modifier) << ",\n"
+          << "    power = " << static_cast<int>(weapon.power) << ",\n"
+          << "    growth = " << static_cast<int>(weapon.growth) << ",\n"
+          << "    accuracy = " << static_cast<int>(weapon.accuracy) << ",\n"
+          << "    critical = " << static_cast<int>(weapon.critical) << ",\n"
+          << "    model = " << static_cast<int>(weapon.model) << ",\n"
+          << "    animation = " << static_cast<int>(weapon.animation_index) << ",\n";
+        WriteTarget(file, weapon.target);
+        file << "    elements = {";
+        for (int s : weapon.elements) file << s << ", ";
+        file << "},\n";
+        file << "    slots = {";
+        for (u8 slot : weapon.materia_slots) file << static_cast<int>(slot) << ", ";
+        file << "},\n";
+        WriteStatBonuses(file, weapon.stat_bonus);
+        file << "    users = {";
+        for (int s : weapon.equip) file << s << ", ";
+        file << "}\n}\n";
+    }
+    file.close();
+}
+
+int KernelDataInstaller::ReadArmors(){
+
+    // Empty the item list
+    armors_.clear();
+
+    File armor_file = kernel_.ExtractGZip(KERNEL_ARMOR_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_ARMOR_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_ARMOR_DESCRIPTIONS));
+    int armor_count = 0;
+
+    for (int i = 0; i < 32; i ++){ // There are exactly 32.
+
+        ArmorData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // If the armor doesn't have a name, it means all items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Read data from the armor data section.
+        data.unknown_0 = armor_file.readU8();
+        data.element_defense_mode = armor_file.readU8();
+        data.defense = armor_file.readU8();
+        data.m_defense = armor_file.readU8();
+        data.evasion = armor_file.readU8();
+        data.m_evasion = armor_file.readU8();
+        data.status = armor_file.readU8();
+        data.unknown_1 = armor_file.readU16LE();
+        for (int s = 0; s < 8; s ++) data.slots_raw[s] = armor_file.readU8();
+        data.growth = armor_file.readU8();
+        data.equip_raw = armor_file.readU16LE();
+        data.element_raw = armor_file.readU16LE();
+        data.unknown_2 = armor_file.readU16LE();
+        for (int s = 0; s < 4; s ++) data.stat_raw[s] = armor_file.readU8();
+        for (int s = 0; s < 4; s ++) data.stat_bonus_raw[s] = armor_file.readU8();
+        data.restrict_raw = armor_file.readU16LE();
+        data.unknown_3 = armor_file.readU16LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        if (data.growth != 0 && data.growth != 2 && data.growth != 3) data.growth = 1;
+        ReadRestrictions(data);
+        ReadUsers(data.equip_raw, data.equip);
+        ReadStatBonuses(data.stat_raw, data.stat_bonus_raw, 4, data.stat_bonus);
+        ReadMateriaSlots(data.slots_raw, data.materia_slots);
+        // In KERNEL.BIN, records with no elemental defense actually have all elements marked,
+        // with mode 255.
+        if (data.element_defense_mode != 0xFF) ReadElements(data.element_raw, data.elements);
+
+        // Add to the list of weapons.
+        armors_.push_back(data);
+        armor_count ++;
+    }
+
+    return armor_count;
+}
+
+void KernelDataInstaller::WriteArmors(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (ArmorData armor : armors_){
+        file << "Game.Items[" << armor.id + 256 << "] = {\n"
+          << "    type = Inventory.ITEM_TYPE.ARMOR,\n"
+          << "    id = " << armor.id + 256 << ",\n"
+          << "    price = " << prices_[armor.id + 256] << ",\n"
+          << "    name = \"" << armor.name << "\",\n"
+          << "    description = \"" << armor.description << "\",\n"
+          << "    sell = " << armor.sellable << ",\n"
+          << "    battle = " << armor.useable_battle << ",\n"
+          << "    menu = " << armor.useable_menu << ",\n"
+          << "    throw = " << armor.throwable << ",\n"
+          << "    defense = {physical = " << static_cast<int>(armor.defense) << ", magical = "
+          << static_cast<int>(armor.m_defense) << "},\n"
+          << "    evasion = {physical = " << static_cast<int>(armor.evasion) << ", magical = "
+          << static_cast<int>(armor.m_evasion) << "},\n"
+          << "    growth = " << static_cast<int>(armor.growth) << ",\n"
+          << "    status = {";
+        if (armor.status != 0XFF) file << armor.status;
+        file
+          << "},\n"
+          << "    elements = {";
+        for (int s : armor.elements){
+            file << "\n        {id = " << s << ", mode = "
+              << static_cast<int>(armor.element_defense_mode) << "},";
+        }
+        if (armor.elements.size() > 0) file << "\n    },\n";
+        else file << "},\n";
+        file << "    slots = {";
+        for (u8 slot : armor.materia_slots) file << static_cast<int>(slot) << ", ";
+        file << "},\n";
+        WriteStatBonuses(file, armor.stat_bonus);
+        file << "    users = {";
+        for (int s : armor.equip) file << s << ", ";
+        file << "}\n}\n";
+    }
+    file.close();
+}
+
+int KernelDataInstaller::ReadAccessories(){
+
+    // Empty the item list
+    accessories_.clear();
+
+    File accessory_file = kernel_.ExtractGZip(KERNEL_ACCESSORY_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_ACCESSORY_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_ACCESSORY_DESCRIPTIONS));
+    int accessory_count = 0;
+
+    for (int i = 0; i < 32; i ++){ // There are exactly 32.
+
+        AccessoryData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // If the item doesn't have a name, it means all items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Read data from the accessory data section.
+        for (int s = 0; s < 2; s ++) data.stat_raw[s] = accessory_file.readU8();
+        for (int s = 0; s < 2; s ++) data.stat_bonus_raw[s] = accessory_file.readU8();
+        data.element_defense_mode = accessory_file.readU8();
+        data.effect = accessory_file.readU8();
+        data.element_raw = accessory_file.readU16LE();
+        data.status_raw = accessory_file.readU32LE();
+        data.equip_raw = accessory_file.readU16LE();
+        data.restrict_raw = accessory_file.readU16LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        ReadRestrictions(data);
+        ReadStatuses(data.status_raw, data.status);
+        ReadUsers(data.equip_raw, data.equip);
+        ReadStatBonuses(data.stat_raw, data.stat_bonus_raw, 2, data.stat_bonus);
+        // In KERNEL.BIN, records with no elemental defense actually have all elements marked,
+        // with mode 255.
+        if (data.element_defense_mode != 0xFF) ReadElements(data.element_raw, data.elements);
+
+        // Add to the list of weapons.
+        accessories_.push_back(data);
+        accessory_count ++;
+    }
+
+    return accessory_count;
+}
+
+void KernelDataInstaller::WriteAccessories(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (AccessoryData accessory : accessories_){
+        file << "Game.Items[" << accessory.id + 288 << "] = {\n"
+          << "    type = Inventory.ITEM_TYPE.ACCESSORY,\n"
+          << "    id = " << accessory.id + 288 << ",\n"
+          << "    price = " << prices_[accessory.id + 288] << ",\n"
+          << "    name = \"" << accessory.name << "\",\n"
+          << "    description = \"" << accessory.description << "\",\n"
+          << "    sell = " << accessory.sellable << ",\n"
+          << "    battle = " << accessory.useable_battle << ",\n"
+          << "    menu = " << accessory.useable_menu << ",\n"
+          << "    throw = " << accessory.throwable << ",\n"
+          << "    special_effect = {";
+        if (accessory.effect != 0XFF) file << static_cast<int>(accessory.effect);
+        file
+          << "},\n"
+          << "    status = {";
+        for (int s : accessory.status) file << s << ", ";
+        file
+          << "},\n"
+          << "    elements = {";
+        // Elements and their resistance mode (0 absorbs, 1 nullifies, 2 halves), as in armors.
+        for (int s : accessory.elements){
+            file << "\n        {id = " << s << ", mode = "
+              << static_cast<int>(accessory.element_defense_mode) << "},";
+        }
+        if (accessory.elements.size() > 0) file << "\n    ";
+        file << "},\n";
+        WriteStatBonuses(file, accessory.stat_bonus);
+        file << "    users = {";
+        for (int s : accessory.equip) file << s << ", ";
+        file << "}\n}\n";
+    }
+    file.close();
+}

+ 660 - 0
src/installer/extractors/KernelEquipmentData.h

@@ -0,0 +1,660 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "common/TypeDefine.h"
+#include "installer/extractors/KernelDataTypes.h"
+
+/*
+ * Records of the weapons, armors and accessories.
+ */
+namespace KernelData{
+
+    /**
+     * Data for each weapon
+     *
+     * As found in KERNEL.BIN, file 5, in order, up to special. Members
+     * after special are not found literally in KERNEL.BIN, but are derived
+     * from other members or found in other kernel files (text files).
+     */
+    struct WeaponData{
+
+        /**
+         * Targeting mode. 1 byte.
+         *
+         * See {@see target} and {@see TargetModes} for more information.
+         */
+        u8 target_raw;
+
+        /**
+         * Effect ID, unused (Always 0XFF). 1 byte.
+         */
+        u8 unused_0;
+
+        /**
+         * Damage formula. 1 byte.
+         *
+         * This byte is divided into two nybbles (four bits). Upper nybble
+         * determines what considerations are made in calculating damage
+         * such as physical/magical, allowing criticals, how to calculate
+         * accuracy, etc. There are also three sets of known formulae that
+         * are paired with the upper nybble values. These are selected in
+         * the Lower Nybble and determine how to calculate pre-defense
+         * damage.
+         */
+        u8 damage_raw;
+
+        /**
+         * Unused data (Always 0XFF). 1 byte.
+         */
+        u8 unused_1;
+
+        /**
+         * The weapon power. 1 byte.
+         *
+         * Used for damage calculation.
+         */
+        u8 power;
+
+        /**
+         * The status effect the weapon induces. 1 byte.
+         */
+        u8 status;
+
+        /**
+         * Weapon materia growth multiplier. 1 byte.
+         */
+        u8 growth;
+
+        /**
+         * Weapon critical chance. 1 byte.
+         */
+        u8 critical;
+
+        /**
+         * Weapon accuracy. 1 byte.
+         */
+        u8 accuracy;
+
+        /**
+         * Weapon battle model. 1 byte.
+         *
+         * This byte is divided into two nybbles (four bits). Upper nybble
+         * determines the attack animation modifier, and it's only used for
+         * Barret and Vincent. The lower nybble is actually the index of the
+         * weapon model.
+         */
+        u8 model_raw;
+
+        /**
+         * Unused data (Always 0XFF). 1 byte.
+         */
+        u8 unused_2;
+
+        /**
+         * Consider it unused.
+         *
+         * @todo Verify.
+         */
+        u8 high_sound;
+
+        /**
+         * Camera movement ID. 2 bytes.
+         *
+         * Used for both single and multiple targeted attacks with this
+         * weapon. Always 0xFFFF.
+         */
+        u16 camera;
+
+        /**
+         * Characters that can equip the weapon. 2 bytes.
+         *
+         * Contains bits indicating the characters that can equip the weapon
+         * {@see character} for more info.
+         */
+        u16 equip_raw;
+
+        /**
+         * Weapon element ids.
+         */
+        u16 element_raw;
+
+        /**
+         * Unused data (Always 0XFFFF). 1 byte.
+         */
+        u8 unused_3;
+
+        /**
+         * Stat the weapon raises.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_raw[4];
+
+        /**
+         * The amount the raised stats are raised by.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_bonus_raw[4];
+
+        /**
+         * Materia slots.
+         *
+         * {@see slots} for more info.
+         */
+        u8 slots_raw[8];
+
+        /**
+         * Sound effect id for normal hit.
+         */
+        u8 sound;
+
+        /**
+         * Sound effect id for critical hit.
+         */
+        u8 sound_critical;
+
+        /**
+         * Sound effect id for miss hit.
+         */
+        u8 sound_miss;
+
+        /**
+         * Impcat effect ID. 1 byte.
+         *
+         * Used for animation in battle.
+         */
+        u8 effect;
+
+        /**
+         * Special flags. 2 bytes.
+         *
+         * @todo document and parse with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
+         */
+        u16 special_raw;
+
+        /**
+         * Weapon restrictions. 2 bytes.
+         *
+         * Contains bits indicating if the item is {@see selleable},
+         * {@see useable_battle} and {@see useable_menu} and
+         * {@see throwable}.
+         */
+        u16 restrict_raw;
+
+        /**
+         * Weapon ID.
+         *
+         * Not actually in the item file data, it's the order at which it
+         * appears.
+         */
+        int id;
+
+        /**
+         * Weapon name.
+         *
+         * Not in the same file as the rest of the data, but on file 20.
+         */
+        std::string name;
+
+        /**
+         * Weapon description.
+         *
+         * Not in the same file as the rest of the data, but on file 12.
+         */
+        std::string description;
+
+        /**
+         * Indicates if the weapon can be sold.
+         *
+         * True if the bit 0 in {@see restrict_raw} is 0.
+         */
+        bool sellable;
+
+        /**
+         * Indicates if the weapon can be used in battle.
+         *
+         * True if the bit 2 in {@see restrict_raw} is 0.
+         */
+        bool useable_battle;
+
+        /**
+         * Indicates if the weapon can be used in the menu.
+         *
+         * True if the bit 4 in {@see restrict_raw} is 0.
+         */
+        bool useable_menu;
+
+        /**
+         * Indicates if the weapon can be sold.
+         *
+         * True if the bit 8 in {@see restrict_raw} is 0.
+         */
+        bool throwable;
+
+        /**
+         * Target selection mode.
+         *
+         * Derived from {@see target_raw}.
+         */
+        Target target;
+
+        /**
+         * The damage formula.
+         *
+         * It's the upper nybble in {@see damage_raw}.
+         */
+        u8 damage_formula;
+
+        /**
+         * The damage modifier.
+         *
+         * It's the lower nybble in {@see damage_raw}.
+         */
+        u8 damage_modifier;
+
+        /**
+         * Attack animation ID.
+         *
+         * It's the upper nybble in {@see model_raw}. It's only used for
+         * Barret and Vincent.
+         */
+        u8 animation_index;
+
+        /**
+         * Weapon model ID.
+         *
+         * It's the lower nybble in {@see model_raw}.
+         */
+        u8 model;
+
+        /**
+         * The weapon battle model index.
+         *
+         * It's the lower nybble in {@see model_raw}.
+         */
+        std::vector<u8> equip;
+
+        /**
+         * Bonus in stats.
+         *
+         * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
+         */
+        std::vector<StatBonus> stat_bonus;
+
+        /**
+         * Materia slots.
+         *
+         * Derived and simplified from {@see slots_raw}.
+         */
+        std::vector<u8> materia_slots;
+
+        /**
+         * Elements of the weapon.
+         *
+         * Derived from {@see element_raw}.
+         */
+        std::vector<int> elements;
+
+    };
+
+    /**
+     * Data for each armor
+     *
+     * As found in KERNEL.BIN, file 6, in order, up to unknown_3. Members
+     * after unknown_3 are not found literally in KERNEL.BIN, but are
+     * derived from other members or found in other kernel files (text
+     * files).
+     */
+    struct ArmorData{
+
+        /**
+         * Unknown (Always 0XFF). 1 byte.
+         */
+        u8 unknown_0;
+
+        /**
+         * Elemental resistance mode. 1 byte.
+         *
+         * For all elements in {@see element_raw}:
+         * If 0: Absorbs.
+         * If 1: Nullifies.
+         * If 2: Halves.
+         * If any other value: normal.
+         */
+        u8 element_defense_mode;
+
+        /**
+         * Defense stat. 1 byte.
+         */
+        u8 defense;
+
+        /**
+         * Magic defense stat. 1 byte.
+         */
+        u8 m_defense;
+
+        /**
+         * Evasion stat. 1 byte.
+         */
+        u8 evasion;
+
+        /**
+         * Magic evasion stat. 1 byte.
+         */
+        u8 m_evasion;
+
+        /**
+         * The status effect the armor protects against. 1 byte.
+         */
+        u8 status;
+
+        /**
+         * Unknown (Always 0XFFFF). 1 byte.
+         */
+        u16 unknown_1;
+
+        /**
+         * Materia slots.
+         *
+         * {@see slots} for more info.
+         */
+        u8 slots_raw[8];
+
+        /**
+         * Armor materia growth multiplier. 1 byte.
+         */
+        u8 growth;
+
+        /**
+         * Characters that can equip the armor. 2 bytes.
+         *
+         * Contains bits indicating the characters that can equip the armor
+         * {@see character} for more info.
+         */
+        u16 equip_raw;
+
+        /**
+         * Elements the armor protects agains.
+         */
+        u16 element_raw;
+
+        /**
+         * Unknown (Always 0XFF). 1 byte.
+         */
+        u16 unknown_2;
+
+        /**
+         * Stat the weapon raises.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_raw[4];
+
+        /**
+         * The amount the raised stats are raised by.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_bonus_raw[4];
+
+        /**
+         * Weapon restrictions. 2 bytes.
+         *
+         * Contains bits indicating if the item is {@see selleable},
+         * {@see useable_battle} and {@see useable_menu} and
+         * {@see throwable}.
+         */
+        u16 restrict_raw;
+
+        /**
+         * Unknown (Always 0XFFFF). 1 byte.
+         */
+        u16 unknown_3;
+
+        /**
+         * Armor ID.
+         *
+         * Not in the item file data, it's the order at which it appears.
+         */
+        int id;
+
+        /**
+         * Armor name.
+         *
+         * Not in the same file as the rest of the data, but on file 20.
+         */
+        std::string name;
+
+        /**
+         * Armor description.
+         *
+         * Not in the same file as the rest of the data, but on file 12.
+         */
+        std::string description;
+
+        /**
+         * Indicates if the armor can be sold.
+         *
+         * True if the bit 0 in {@see restrict_raw} is 0.
+         */
+        bool sellable;
+
+        /**
+         * Indicates if the armor can be used in battle.
+         *
+         * True if the bit 2 in {@see restrict_raw} is 0.
+         */
+        bool useable_battle;
+
+        /**
+         * Indicates if the armor can be used in the menu.
+         *
+         * True if the bit 4 in {@see restrict_raw} is 0.
+         */
+        bool useable_menu;
+
+        /** Whether the item can be thrown (Throw command). */
+        bool throwable;
+
+        /**
+         * The weapon battle model index.
+         *
+         * It's the lower nybble in {@see model_raw}.
+         */
+        std::vector<u8> equip;
+
+        /**
+         * Bonus in stats.
+         *
+         * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
+         */
+        std::vector<StatBonus> stat_bonus;
+
+        /**
+         * Materia slots.
+         *
+         * Derived and simplified from {@see slots_raw}.
+         */
+        std::vector<u8> materia_slots;
+
+        /**
+         * Elements of the weapon.
+         *
+         * Derived from {@see element_raw}.
+         */
+        std::vector<int> elements;
+
+    };
+
+    /**
+     * Data for each accessory
+     *
+     * As found in KERNEL.BIN, file 7, in order, up to unknown_3. Members
+     * after unknown_3 are not found literally in KERNEL.BIN, but are not
+     * found literally in KERNEL.BIN, but are derived from other members or
+     * found in other files of the kernel (text files).
+     */
+    struct AccessoryData{
+
+        /**
+         * Stat the weapon raises.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_raw[2];
+
+        /**
+         * The amount the raised stats are raised by.
+         *
+         * {@see stat_bonus} for more info.
+         */
+        u8 stat_bonus_raw[2];
+
+        /**
+         * Elemental resistance mode. 1 byte.
+         *
+         * For all elements in {@see element_raw}:
+         * If 0: Absorbs.
+         * If 1: Nullifies.
+         * If 2: Halves.
+         * If any other value: normal.
+         */
+        u8 element_defense_mode;
+
+        /**
+         * The special status effect the accesory induces.
+         *
+         * 0: Auto haste.
+         * 1: Auto berserk.
+         * 2: Curse.
+         * 3: Auto reflect.
+         * 4: Steal rate +.
+         * 5: Manipulate rate +.
+         * 6: Barrier + MBarrier.
+         * Other: Nothing.
+         */
+        u8 effect;
+
+        /**
+         * Elements the armor protects against. 2 bytes.
+         *
+         * The defense type is defined in {@see element_defense_mode}.
+         */
+        u16 element_raw;
+
+        /**
+         * The status effect the armor protects against. 4 bytes.
+         */
+        u32 status_raw;
+
+        /**
+         * Characters that can equip the armor. 2 bytes.
+         *
+         * Contains bits indicating the characters that can equip the armor
+         * {@see character} for more info. 
+         * for more info.
+         */
+        u16 equip_raw;
+
+        /**
+         * Weapon restrictions. 2 bytes.
+         *
+         * Contains bits indicating if the item is {@see selleable},
+         * {@see useable_battle} and {@see useable_menu} and
+         * {@see throwable}.
+         */
+        u16 restrict_raw;
+
+        /**
+         * Accessory ID.
+         *
+         * Not in the item file data, it's the order at which itappears.
+         */
+        int id;
+
+        /**
+         * Accessory name.
+         *
+         * Not in the same file as the rest of the data, but on file 20.
+         */
+        std::string name;
+
+        /**
+         * Accessory description.
+         *
+         * Not in the same file as the rest of the data, but on file 12.
+         */
+        std::string description;
+
+        /**
+         * Indicates if the accessory can be sold.
+         *
+         * True if the bit 0 in {@see restrict_raw} is 0.
+         */
+        bool sellable;
+
+        /**
+         * Indicates if the accessory can be used in battle.
+         *
+         * True if the bit 2 in {@see restrict_raw} is 0.
+         */
+        bool useable_battle;
+
+        /**
+         * Indicates if the accessory can be used in the menu.
+         *
+         * True if the bit 4 in {@see restrict_raw} is 0.
+         */
+        bool useable_menu;
+
+        /** Whether the item can be thrown (Throw command). */
+        bool throwable;
+
+        /**
+         * The weapon battle model index.
+         *
+         * It's the lower nybble in {@see model_raw}.
+         */
+        std::vector<u8> equip;
+
+        /**
+         * Bonus in stats.
+         *
+         * Derived from {@see stat_raw} and {@see stat_bonus_raw}.
+         */
+        std::vector<StatBonus> stat_bonus;
+
+        /**
+         * Elements of the accesory.
+         *
+         * Derived from {@see element_raw}.
+         */
+        std::vector<int> elements;
+
+        /**
+         * Statuses the accessory protects against.
+         *
+         * Derived from {@see status_raw}.
+         */
+        std::vector<int> status;
+
+    };
+}

+ 261 - 0
src/installer/extractors/KernelItemData.h

@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "common/TypeDefine.h"
+#include "installer/extractors/KernelDataTypes.h"
+
+/*
+ * Records of the items.
+ */
+namespace KernelData{
+
+    /**
+     * Data for each items
+     *
+     * As found in KERNEL.BIN, file 4, in order, up to special. Members
+     * after special are not found literally in KERNEL.BIN, but are derived
+     * from other members or found in other files (text files).
+     */
+    struct ItemData{
+
+        /**
+         * Unknown data (Always 0XFFFF). 4 bytes.
+         */
+        u32 unknown_0;
+
+        /**
+         * Unknown data (Always 0XFFFF). 4 bytes.
+         */
+        u32 unknown_1;
+
+        /**
+         * Camera movement ID. 2 bytes.
+         *
+         * Used for items useable in battle, determines the camera movement.
+         */
+        u16 camera;
+
+        /**
+         * Item restrictions. 2 bytes.
+         *
+         * Contains bits indicating if the item is {@see selleable},
+         * {@see useable_battle} and {@see useable_menu}.
+         */
+        u16 restrict_raw;
+
+        /**
+         * Targeting mode. 1 byte.
+         *
+         * See {@see target} and {@see TargetModes} for more information.
+         */
+        u8 target_raw;
+
+        /**
+         * Item effect ID. 1 byte.
+         *
+         * Used for animation in battle.
+         */
+        u8 effect;
+
+        /**
+         * Damage formula. 1 byte.
+         *
+         * This byte is divided into two nybbles (four bits). Upper nybble
+         * determines what considerations are made in calculating damage
+         * such as physical/magical, allowing criticals, how to calculate
+         * accuracy, etc. There are also three sets of known formulae that
+         * are paired with the upper nybble values. These are selected in
+         * the Lower Nybble and determine how to calculate pre-defense
+         * damage.
+         */
+        u8 damage_raw;
+
+        /**
+         * The item power. 1 byte.
+         *
+         * Used for damage/healing calculation.
+         */
+        u8 power;
+
+        /**
+         * The restore type.
+         *
+         * See {@see restore_type} and {@see RESTORE_TYPE}.
+         */
+        u8 condition_raw;
+
+        /**
+         * Information about status change mode and chance. 1 bytes.
+         */
+        u8 status_change_raw;
+
+        /**
+         * Special flags. 1 byte.
+         *
+         * @todo Document and retrieve with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Attack_Special_Effects
+         */
+        u8 additional_effects_raw;
+
+        /**
+         * Special flags. 1 byte.
+         *
+         * @todo Document and retrieve with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Item_data
+         */
+        u8 additional_effects_mod_raw;
+
+        /**
+         * Information about what status can be inflicted or cured. 4 bytes.
+         *
+         * The first six bits are the chance of inflicting/curing (out of
+         * 63). If the seventh bit is set, the status is cured instead of
+         * inflicted. If the eighth bit is set, then the status is toggled
+         * (cured if present, inflicted if not present).
+         */
+        u32 status_raw;
+
+        /**
+         * Information about the item elements. 2 bytes.
+         */
+        u16 element_raw;
+
+        /**
+         * Special flags. 2 bytes.
+         *
+         * @todo document and parse with info from
+         * https://wiki.ffrtt.ru/index.php?title=FF7/Battle/Special_Attack_Flags
+         */
+        u16 special_raw;
+
+        /**
+         * Item ID.
+         *
+         * Not in the item file data, it's the order at which it appears.
+         */
+        int id;
+
+        /**
+         * Item name.
+         *
+         * Not in the same file as the rest of the data, but on file 19.
+         */
+        std::string name;
+
+        /**
+         * Item description.
+         *
+         * Not in the same file as the rest of the data, but on file 11.
+         */
+        std::string description;
+
+        /**
+         * Indicates if the item can be sold.
+         *
+         * True if the bit 0 in {@see restrict_raw} is 0.
+         */
+        bool sellable;
+
+        /**
+         * Indicates if the item can be used in battle.
+         *
+         * True if the bit 2 in {@see restrict_raw} is 0.
+         */
+        bool useable_battle;
+
+        /**
+         * Indicates if the item can be used in the menu.
+         *
+         * True if the bit 4 in {@see restrict_raw} is 0.
+         */
+        bool useable_menu;
+
+        /** Whether the item can be thrown (Throw command). */
+        bool throwable;
+
+        /**
+         * Target selection mode.
+         *
+         * Derived from {@see target_raw}.
+         */
+        Target target;
+
+        /**
+         * The damage formula.
+         *
+         * It's the upper nybble in {@see damage_raw}.
+         */
+        u8 damage_formula;
+
+        /**
+         * The damage modifier.
+         *
+         * It's the lower nybble in {@see damage_raw}.
+         */
+        u8 damage_modifier;
+
+        /**
+         * What the item restores when used.
+         *
+         * Same as {@see condition_raw}, except for the wrong cases.
+         */
+        RESTORE_TYPE restore_type;
+
+        /**
+         * Status effects.
+         *
+         * Derived from {@see status_change_raw} and {@see status_raw}.
+         */
+        StatusEffect status;
+
+        /**
+         * Elements of the item.
+         *
+         * Derived from {@see element_raw}.
+         */
+        std::vector<int> elements;
+
+    };
+
+    /**
+     * Data for each key item.
+     *
+     * There is no data file about key items in KERNEL.BIN, there are only
+     * names (file 24) and descriptions (file 16)
+     */
+    struct KeyItemData{
+
+        /**
+         * Key item ID.
+         *
+         * Not in KERNEL.BIN, it's the order at which they appear.
+         */
+        int id;
+
+        /**
+         * Key item name.
+         */
+        std::string name;
+
+        /**
+         * Key item description.
+         */
+        std::string description;
+    };
+}

+ 156 - 0
src/installer/extractors/KernelItems.cpp

@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <fstream>
+#include "installer/extractors/KernelDataInstaller.h"
+#include "installer/extractors/KernelDataParts.h"
+#include "installer/extractors/KernelTextList.h"
+
+using namespace KernelData;
+
+int KernelDataInstaller::ReadItems(){
+
+    // Empty the item list
+    items_.clear();
+
+    File items_file = kernel_.ExtractGZip(KERNEL_ITEM_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_ITEM_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_ITEM_DESCRIPTIONS));
+    int item_count = 0;
+
+    for (int i = 0; i < 128; i ++){
+
+        ItemData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // If the item doesn't have a name, it means all items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Read data from the item data section.
+        data.unknown_0 = items_file.readU32LE();
+        data.unknown_1 = items_file.readU32LE();
+        data.camera = items_file.readU16LE();
+        data.restrict_raw = items_file.readU16LE();
+        data.target_raw = items_file.readU8();
+        data.effect = items_file.readU8();
+        data.damage_raw = items_file.readU8();
+        data.power = items_file.readU8();
+        data.condition_raw = items_file.readU8();
+        data.status_change_raw = items_file.readU8();
+        data.additional_effects_raw = items_file.readU8();
+        data.additional_effects_mod_raw = items_file.readU8();
+        data.status_raw = items_file.readU32LE();
+        data.element_raw = items_file.readU16LE();
+        data.special_raw = items_file.readU16LE();
+
+        // Derive data from the read data.
+        data.id = i;
+        ReadRestrictions(data);
+        ReadTarget(data.target_raw, data.target);
+        data.damage_formula = data.damage_raw >> 4; // Upper nybble.
+        data.damage_modifier = data.damage_raw & 15; // Lower nybble.
+        data.restore_type = ReadRestoreType(data.condition_raw);
+        ReadStatusEffect(data.status_change_raw, data.status_raw, data.status);
+
+        ReadElements(data.element_raw, data.elements);
+
+        // Add to the list of items.
+        items_.push_back(data);
+        item_count ++;
+    }
+
+    return item_count;
+}
+
+void KernelDataInstaller::WriteItems(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (ItemData item : items_){
+        file << "Game.Items[" << item.id << "] = {\n"
+          << "    type = Inventory.ITEM_TYPE.ITEM,\n"
+          << "    id = " << item.id << ",\n"
+          << "    price = " << prices_[item.id] << ",\n"
+          << "    name = \"" << item.name << "\",\n"
+          << "    description = \"" << item.description << "\",\n"
+          << "    camera = " << item.camera << ",\n"
+          << "    sell = " << item.sellable << ",\n"
+          << "    battle = " << item.useable_battle << ",\n"
+          << "    menu = " << item.useable_menu << ",\n"
+          << "    throw = " << item.throwable << ",\n"
+          << "    effect = " << static_cast<int>(item.effect) << ",\n"
+          << "    dmg_formula = " << static_cast<int>(item.damage_formula) << ",\n"
+          << "    dmg_modifier = " << static_cast<int>(item.damage_modifier) << ",\n"
+          << "    power = " << static_cast<int>(item.power) << ",\n"
+          << "    restore = " << item.restore_type << ",\n";
+        WriteTarget(file, item.target);
+        file << "    status = {";
+        for (int s : item.status.status){
+            file << "\n        {status = " << s << ", mode = " << item.status.mode
+              << ", chance = " << item.status.chance << "},";
+        }
+        if (item.status.status.size() > 0) file << "\n    },\n";
+        else file << "},\n";
+        file << "    elements = {";
+        for (int e : item.elements) file << e << ", ";
+        file << "},\n}\n";
+
+
+    }
+    file.close();
+}
+
+int KernelDataInstaller::ReadKeyItems(){
+
+    // Empty the item list
+    key_items_.clear();
+
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_KEY_ITEM_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_KEY_ITEM_DESCRIPTIONS));
+    int key_item_count = 0;
+
+    for (int i = 0; i < 128; i ++){
+
+        KeyItemData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // If the key item doesn't have a name, it means all key items have been read.
+        // break the loop.
+        if ("" == data.name) break;
+
+        // Add to the list of items.
+        data.id = i;
+        key_items_.push_back(data);
+        key_item_count ++;
+    }
+
+    return key_item_count;
+}
+
+void KernelDataInstaller::WriteKeyItems(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (KeyItemData key_item : key_items_){
+        file << "Game.Key_Items[" << key_item.id << "] = {\n"
+          << "    name = \"" << key_item.name << "\",\n description = \"" << key_item.description << "\"\n}\n";
+    }
+    file.close();
+}

+ 319 - 0
src/installer/extractors/KernelMateria.cpp

@@ -0,0 +1,319 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <fstream>
+#include "installer/extractors/KernelDataInstaller.h"
+#include "installer/extractors/KernelDataParts.h"
+#include "installer/extractors/KernelTextList.h"
+
+using namespace KernelData;
+
+int KernelDataInstaller::ReadMateria(){
+
+    // Empty the item list
+    materia_.clear();
+
+    File materia_file = kernel_.ExtractGZip(KERNEL_MATERIA_DATA);
+    KernelTextList names(kernel_.ExtractGZip(KERNEL_MATERIA_NAMES));
+    KernelTextList descriptions(kernel_.ExtractGZip(KERNEL_MATERIA_DESCRIPTIONS));
+    int materia_count = 0;
+
+    for (int i = 0; i < 96; i ++){ // There are exactly 96.
+
+        MateriaData data;
+
+        data.name = names.NextName();
+        data.description = descriptions.NextDescription();
+
+        // Read data from the materia data section.
+        for (int i = 0; i < 4; i ++) data.level_up_ap[i] = materia_file.readU16LE();
+        data.stat_raw = materia_file.readU8();
+        data.status_raw // WARNING: Status has only three bytes.
+          = materia_file.readU8() + (materia_file.readU8() << 8) + (materia_file.readU8() << 16);
+        data.element = materia_file.readU8();
+        data.type_raw = materia_file.readU8();
+        for (int i = 0; i < 6; i ++) data.attribute[i] = materia_file.readU8();
+
+        // If the materia doesn't have a name, don't bother processing the rest of the data,
+        // it's not going to be added to the list.
+        if ("" == data.name) continue;
+
+        // Derive data from the read data.
+        data.id = i;
+        // Status. 3 bytes, so only the first 24 can be present.
+        ReadStatuses(data.status_raw, data.status);
+        SetMateriaType(data);
+        SetMateriaStats(data);
+
+        // Add to the list of materia.
+        materia_.push_back(data);
+        materia_count ++;
+    }
+
+    return materia_count;
+}
+
+void KernelDataInstaller::SetMateriaType(MateriaData& data){
+    // Limit type to materia color.
+    data.type = data.type_raw & 15; // Lower nybble.
+    data.sub_type = data.type_raw & 15; // Upper nyble.
+    switch (data.type){
+        case 0x09:
+        case 0x0A:
+            data.type = static_cast<int>(MATERIA_TYPE::MAGIC);
+            break;
+        case 0x05:
+            data.type = static_cast<int>(MATERIA_TYPE::SUPPORT);
+            break;
+        case 0x02:
+        case 0x03:
+        case 0x06:
+        case 0x07:
+        case 0x08:
+            data.type = static_cast<int>(MATERIA_TYPE::COMMAND);
+            break;
+        case 0x0B:
+        case 0x0C:
+            data.type = static_cast<int>(MATERIA_TYPE::SUMMON);
+            break;
+        default:
+            data.type = static_cast<int>(MATERIA_TYPE::INDEPENDENT);
+    }
+}
+
+void KernelDataInstaller::SetMateriaStats(MateriaData& data){
+    // The stats of each value of the raw byte, from 0x01 on: str, vit, mag, spr, dex, lck, hp and
+    // mp. Other values change nothing.
+    static const int STATS[16][8] = {
+      { -2,  -1,   2,   1,   0,   0,  -5,   5}, // 0x01
+      { -4,  -2,   4,   2,   0,   0, -10,  10}, // 0x02
+      {  0,   0,   0,   0,   2,  -2,   0,   0}, // 0x03
+      { -1,  -1,   1,   1,   0,   0,   0,   0}, // 0x04
+      {  1,   1,  -1,  -1,   0,   0,   0,   0}, // 0x05
+      {  0,   1,   0,   0,   0,   0,   0,   0}, // 0x06
+      {  0,   0,   0,   0,   0,   1,   0,   0}, // 0x07
+      {  0,   0,   0,   0,   0,  -1,   0,   0}, // 0x08
+      {  0,   0,   0,   0,  -2,   0,   0,   0}, // 0x09
+      {  0,   0,   0,   0,   2,   0,   0,   0}, // 0x0A
+      { -1,   0,   1,   0,   0,   0,  -2,   2}, // 0x0B
+      {  0,   0,   1,   0,   0,   0,  -2,   2}, // 0x0C
+      {  0,   0,   1,   1,   0,   0,  -5,   5}, // 0x0D
+      {  0,   0,   2,   2,   0,   0, -10,  10}, // 0x0E
+      {  0,   0,   4,   4,   0,   0, -10,  15}, // 0x0F
+      {  0,   0,   8,   8,   0,   0, -10,  20}  // 0x10
+    };
+    const bool changes = data.stat_raw >= 1 && data.stat_raw <= 16;
+    static const int NONE_CHANGED[8] = {};
+    const int* values = changes ? STATS[data.stat_raw - 1] : NONE_CHANGED;
+    data.stats.change = changes;
+    data.stats.str = values[0];
+    data.stats.vit = values[1];
+    data.stats.mag = values[2];
+    data.stats.spr = values[3];
+    data.stats.dex = values[4];
+    data.stats.lck = values[5];
+    data.stats.hp = values[6];
+    data.stats.mp = values[7];
+}
+
+void KernelDataInstaller::WriteMateria(std::string file_name){
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    for (MateriaData materia : materia_){
+        file << "Game.Materia[" << materia.id << "] = {\n"
+          << "    price = " << prices_[materia.id + 320] << ",\n"
+          << "    name = \"" << materia.name << "\",\n"
+          << "    description = \"" << materia.description << "\",\n"
+          << "    type = " << materia.type << ",\n"
+
+
+        // TODO Remove from here...
+          << "    sub_type = " << (int)materia.sub_type << ",\n"
+          << "    type_raw = " << (int)materia.type_raw << ",\n"
+          << "    type_raw_upp = " << (int)(materia.type_raw >> 4) << ",\n"
+          << "    type_raw_low = " << (int)(materia.type_raw & 15) << ",\n"
+          << "    attributtes = {";
+        for (int i = 0; i < 6; i ++)
+            file << (int) materia.attribute[i] << ", ";
+        file << "},\n"
+        // TODO ... until here
+
+
+          << "    levels_ap = {[1] = 0, ";
+        for (int i = 0; i < 4; i ++){
+            if (materia.level_up_ap[i] < 65535)
+                file << "[" << (i + 2) << "] = " << (materia.level_up_ap[i] * 100) << ", ";
+        }
+        file
+          << "},\n"
+          << "    status = {";
+        for (int s : materia.status) file << s << ", ";
+        file
+          << "},\n"
+          << "    elements = {";
+        if (materia.element != 0xFF) file << static_cast<int>(materia.element);
+        file << "},\n    stats = {";
+        if (materia.stats.str != 0) file << "\n        str = " << materia.stats.str << ",";
+        if (materia.stats.vit != 0) file << "\n        vit = " << materia.stats.vit << ",";
+        if (materia.stats.mag != 0) file << "\n        mag = " << materia.stats.mag << ",";
+        if (materia.stats.spr != 0) file << "\n        spr = " << materia.stats.spr << ",";
+        if (materia.stats.dex != 0) file << "\n        dex = " << materia.stats.dex << ",";
+        if (materia.stats.lck != 0) file << "\n        lck = " << materia.stats.lck << ",";
+        if (materia.stats.hp != 0) file << "\n        hp = " << materia.stats.hp << ",";
+        if (materia.stats.mp != 0) file << "\n        mp = " << materia.stats.mp << ",";
+        if (materia.stats.change) file << "\n    },\n";
+        else file << "},\n";
+
+        // Type-dependant
+        switch (static_cast<MATERIA_TYPE>(materia.type)){
+            case MATERIA_TYPE::MAGIC: WriteMagicMateria(file, materia); break;
+            case MATERIA_TYPE::SUMMON: WriteSummonMateria(file, materia); break;
+            case MATERIA_TYPE::COMMAND: WriteCommandMateria(file, materia); break;
+            case MATERIA_TYPE::SUPPORT: WriteSupportMateria(file, materia); break;
+            case MATERIA_TYPE::INDEPENDENT: WriteIndependentMateria(file, materia); break;
+        }
+        file << "}\n";
+    }
+    file.close();
+}
+
+void KernelDataInstaller::WriteMagicMateria(std::ostream& file, const MateriaData& materia){
+    file << "    magic = {\n";
+    for (int a = 0; a < 6; a ++){
+        if (materia.attribute[a] != 0xFF){
+            file << "        [" << a + 1 << "] = " << static_cast<int>(materia.attribute[a])
+              << ", -- " << attack_names_[static_cast<int>(materia.attribute[a])] << "\n";
+        }
+    }
+    file << "    }\n";
+}
+
+void KernelDataInstaller::WriteSummonMateria(std::ostream& file, const MateriaData& materia){
+    file << "    summon = " << static_cast<int>(materia.attribute[0]) << ", -- "
+      << attack_names_[static_cast<int>(materia.attribute[0])] << "\n";
+    file << "    times = {";
+    for (int a = 1; a < 6; a ++)
+        file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
+    file << "}\n";
+}
+
+void KernelDataInstaller::WriteCommandMateria(std::ostream& file, const MateriaData& materia){
+    switch (materia.sub_type){
+        case 0x02: // Command, replace attack.
+            WriteMateriaCommandList(file, materia);
+            file << "    },\n    command_replace = 1 -- Attack\n";
+            break;
+        case 0x03: // Command, W commands.
+            WriteMateriaCommandList(file, materia);
+            file << "    },\n    command_replace = "
+              << static_cast<int>(materia.attribute[0] - 19) << " -- "
+              << command_names_[static_cast<int>(materia.attribute[0] - 19)] << "\n";
+            break;
+        case 0x06: // Normal command.
+        case 0x07: // E.Skill.
+            WriteMateriaCommandList(file, materia);
+            file << "    }\n";
+            break;
+        case 0x08: // Master command.
+            // TODO
+            break;
+    }
+}
+
+void KernelDataInstaller::WriteMateriaCommandList(
+  std::ostream& file, const MateriaData& materia
+){
+    file << "    command = {\n";
+    for (int a = 0; a < 6; a ++){
+        if (materia.attribute[a] != 0xFF){
+            file << "        [" << a + 1 << "] = " << static_cast<int>(materia.attribute[a])
+              << ", -- " << command_names_[static_cast<int>(materia.attribute[a])] << "\n";
+        }
+    }
+}
+
+void KernelDataInstaller::WriteSupportMateria(std::ostream& file, const MateriaData& materia){
+    file << "    link = {\n        id = ";
+    switch (materia.attribute[0]){
+        case 0x51: file << "Materia.LINK.ALL,\n"; break;
+        case 0x54: file << "Materia.LINK.COMMAND_COUNTER,\n"; break;
+        case 0x55: file << "Materia.LINK.MAGIC_COUNTER,\n"; break;
+        case 0x56: file << "Materia.LINK.SNEAK_ATTACK,\n"; break;
+        case 0x57: file << "Materia.LINK.FINAL_ATTACK,\n"; break;
+        case 0x58: file << "Materia.LINK.MP_TURBO,\n"; break;
+        case 0x59: file << "Materia.LINK.MP_ABSORB,\n"; break;
+        case 0x5A: file << "Materia.LINK.HP_ABSORB,\n"; break;
+        case 0x5C: file << "Materia.LINK.ADDED_CUT,\n"; break;
+        case 0x5D: file << "Materia.LINK.STEAL_AS_WELL,\n"; break;
+        case 0x5E: file << "Materia.LINK.ELEMENTAL,\n"; break;
+        case 0x5F: file << "Materia.LINK.ADDED_EFFECT,\n"; break;
+        case 0x63: file << "Materia.LINK.QUADRA_MAGIC,\n"; break;
+        default: file << "nil,\n"; break;
+    }
+    file << "        param = {";
+    for (int a = 1; a < 6; a ++){
+        if (materia.attribute[a] != 0xFF){
+            file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
+        }
+    }
+    file << "}\n    }\n";
+}
+
+void KernelDataInstaller::WriteIndependentMateria(std::ostream& file, const MateriaData& materia){
+    file << "    ability = {\n        id = ";
+    switch (materia.sub_type){
+        case 0x00: // Character effects
+            switch (materia.attribute[0]){
+                case 0x02: file << "Materia.ABILITY.MAG_PLUS,\n"; break;
+                case 0x04: file << "Materia.ABILITY.DEX_PLUS,\n"; break;
+                case 0x05: file << "Materia.ABILITY.LCK_PLUS,\n"; break;
+                case 0x08: file << "Materia.ABILITY.HP_PLUS,\n"; break;
+                case 0x09: file << "Materia.ABILITY.MP_PLUS,\n"; break;
+                case 0x0A: file << "Materia.ABILITY.EXP_PLUS,\n"; break;
+                case 0x0B: file << "Materia.ABILITY.COVER,\n"; break;
+                case 0x0C: file << "Materia.ABILITY.UNDERWATER,\n"; break;
+                case 0x50: file << "Materia.ABILITY.LONG_RANGE,\n"; break;
+                case 0x53: file << "Materia.ABILITY.COUNTER_ATTACK,\n"; break;
+                case 0x62: file << "Materia.ABILITY.HP_MP,\n"; break;
+                default: file << "nil,\n"; break;
+            }
+            break;
+        case 0x01: // Party effects
+            switch (materia.attribute[0]){
+                case 0x00: file << "Materia.ABILITY.MONEY_PLUS,\n"; break;
+                case 0x01: // Enemy Away / Enemy lure / Chocobo lure
+                    if (materia.id == 7) file << "Materia.ABILITY.ENCOUNTER_DOWN,\n";
+                    else if (materia.id == 8) file << "Materia.ABILITY.ENCOUNTER_UP,\n";
+                    else if (materia.id == 9) file << "Materia.ABILITY.ENCOUNTER_CHOCOBO,\n";
+                    else file << "nil,\n";
+                    break;
+                case 0x03: file << "Materia.ABILITY.PREEMPTIVE,\n"; break;
+                default: file << "nil,\n"; break;
+            }
+            break;
+        case 0x04:
+            file << "Materia.ABILITY.MEGA_ALL,\n";
+            break;
+        default: file << "nil,\n"; break;
+    }
+    file << "        param = {";
+    for (int a = 1; a < 6; a ++){
+        if (materia.attribute[a] != 0xFF){
+            file << "[" << a << "] = " << static_cast<int>(materia.attribute[a]) << ", ";
+        }
+    }
+    file << "}\n    }\n";
+}

+ 245 - 0
src/installer/extractors/KernelMateriaData.h

@@ -0,0 +1,245 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "common/TypeDefine.h"
+#include "installer/extractors/KernelDataTypes.h"
+
+/*
+ * Records of the materia.
+ */
+namespace KernelData{
+
+    /**
+     * Types of materia.
+     */
+    enum class MATERIA_TYPE{
+
+        /**
+         * Magic materia.
+         */
+        MAGIC = 1,
+
+        /**
+         * Support materia.
+         */
+        SUPPORT,
+
+        /**
+         * Command materia.
+         */
+        COMMAND,
+
+        /**
+         * Independent materia.
+         */
+        INDEPENDENT,
+
+        /**
+         * Summon materia.
+         */
+        SUMMON
+    };
+
+    /**
+     * Stat bonuses given by equipping materia.
+     *
+     * They can be negative.
+     */
+    struct MateriaStatBonus{
+
+        /**
+         * Strength bonus. Absolute.
+         */
+        int str;
+
+        /**
+         * Vitality bonus. Absolute.
+         */
+        int vit;
+
+        /**
+         * Magic bonus. Absolute.
+         */
+        int mag;
+
+        /**
+         * Spirit bonus. Absolute.
+         */
+        int spr;
+
+        /**
+         * Dexterity bonus. Absolute.
+         */
+        int dex;
+
+        /**
+         * Luck bonus. Absolute.
+         */
+        int lck;
+
+        /**
+         * Max HP bonus. Percentile.
+         */
+        int hp;
+
+        /**
+         * Max MP bonus. Percentile.
+         */
+        int mp;
+
+        /**
+         * Indicates if at least one of the stats is changed by the materia.
+         */
+        bool change;
+    };
+
+    /**
+     * Data for each materia.
+     *
+     * As found in KERNEL.BIN, file 8, in order, up to attributes. Members
+     * after attributes are not found literally in KERNEL.BIN, but are
+     * derived from other members or found in other files of the kernel
+     * (text files).
+     */
+    struct MateriaData{
+
+        /**
+         * AP amounts at which the materia levels up. 4 x 1 byte.
+         *
+         * It can be used to determine how many levels the materia has.
+         */
+        u16 level_up_ap[4];
+
+        /**
+         * Index of stat modifier. 1 byte.
+         */
+        u8 stat_raw;
+
+        /**
+         * Status effects. 3 bytes, not 4!
+         */
+        u32 status_raw;
+
+        /**
+         * Materia element. 1 byte.
+         */
+        u8 element;
+
+        /**
+         * Materia type. 1 byte.
+         *
+         * 2 nybbles. The upper one is the subtype, and it's ignored in
+         * V-Gears. The lower one the is actual type.
+         */
+        u8 type_raw;
+
+        /**
+         * Materia attributes. 6 x 1 byte.
+         *
+         * They are mostly ignored in V-Gears.
+         */
+        u8 attribute[6];
+
+        /**
+         * Accessory ID.
+         *
+         * Not in the item file data, it's the order at which it appears.
+         */
+        int id;
+
+        /**
+         * Accessory name.
+         *
+         * Not in the same file as the rest of the data, but on file 20.
+         */
+        std::string name;
+
+        /**
+         * Accessory description.
+         *
+         * Not in the same file as the rest of the data, but on file 12.
+         */
+        std::string description;
+
+        /**
+         * Materia type.
+         *
+         * Derived from {@see type_raw}.
+         */
+        int type;
+
+        /**
+         * Materia type.
+         *
+         * Derived from {@see type_raw}.
+         */
+        int sub_type;
+
+        /**
+         * Stat bonuses for equipping the materia.
+         *
+         * Derived from {@see stat_raw}.
+         */
+        MateriaStatBonus stats;
+
+        /**
+         * Statuses the materia inflicts.
+         *
+         * Derived from {@see status_raw}.
+         */
+        std::vector<int> status;
+
+        /**
+         * Commands the materia enables in battle.
+         *
+         * Derived from {@see type_raw}.
+         */
+        std::vector<int> command;
+
+        /**
+         * Attacks the materia enables in battle, at each level.
+         *
+         * Derived from {@see type_raw} and {@see attribute}.
+         */
+        int attack[6];
+
+        /**
+         * Number of times per battle an attack can be used, per level.
+         *
+         * -1 means infinite times. Derived from {@see type_raw} and
+         * {@see attribute}.
+         */
+        int attack_times[6];
+
+        /**
+         * Ability confered by the materia.
+         *
+         * Can be provided by independent and independent materia.
+         */
+        std::string ability;
+
+        /**
+         * Parameter for the ability, varies per level.
+         *
+         * It can be the number of times the ability can be executed, or the
+         * chance to activate...
+         */
+        int ability_parameters[5];
+    };
+}

+ 49 - 0
src/installer/extractors/KernelTextList.cpp

@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <boost/algorithm/string/replace.hpp>
+#include "installer/extractors/KernelTextList.h"
+#include "installer/tables/Characters.h"
+
+KernelTextList::KernelTextList(const File& section): section_(section){}
+
+std::string KernelTextList::NextName(){
+    std::string text;
+    int offset = section_.readU16LE();
+    for (u8 ch = section_.GetU8(offset); ch != 0xFF; ch = section_.GetU8(++ offset))
+        text += ENGLISH_CHARS[ch];
+    boost::replace_all(text, "\"", "'");
+    return text;
+}
+
+std::string KernelTextList::NextDescription(){
+    std::string text;
+    int offset = section_.readU16LE();
+    for (u8 ch = section_.GetU8(offset); ch != 0xFF; ch = section_.GetU8(++ offset)){
+        if (ch != 0xF9){
+            text += ENGLISH_CHARS[ch];
+            continue;
+        }
+        // The next byte (in bits, aaoooooo) tells to copy some bytes from before this one: their
+        // number is aa * 2 + 4, and they start oooooo + 1 bytes before the 0xF9 byte.
+        const u8 reference = section_.GetU8(offset + 1);
+        const int size = (reference >> 6) * 2 + 4;
+        const int start = offset - 1 - (reference & 63);
+        for (int c = 0; c < size; c ++) text += ENGLISH_CHARS[section_.GetU8(start + c)];
+        offset ++;
+    }
+    boost::replace_all(text, "\"", "'");
+    return text;
+}

+ 61 - 0
src/installer/extractors/KernelTextList.h

@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <string>
+#include "common/File.h"
+
+/**
+ * A text section of KERNEL.BIN: a list of names or descriptions.
+ *
+ * It starts with the offset of each text, which ends with the byte 0xFF. The texts are read one
+ * after the other. Double quotes are changed to single ones, so a text can be written in a Lua
+ * string.
+ */
+class KernelTextList{
+
+    public:
+
+        /**
+         * Constructor.
+         *
+         * @param[in] section The decompressed section.
+         */
+        explicit KernelTextList(const File& section);
+
+        /**
+         * Reads the next name.
+         *
+         * @return The name.
+         */
+        std::string NextName();
+
+        /**
+         * Reads the next description.
+         *
+         * The descriptions are compressed: the byte 0xF9 copies a piece of the text before it.
+         *
+         * @return The description.
+         */
+        std::string NextDescription();
+
+    private:
+
+        /**
+         * The section.
+         */
+        File section_;
+};

+ 1 - 0
test/installer/extractors/CMakeLists.txt

@@ -1,6 +1,7 @@
 # Tests of this folder (see vgears_tests and vgears_installer_tests in test/CMakeLists.txt).
 vgears_tests(
     FieldDataInstaller.cpp
+    KernelData.cpp
     SfxDump.cpp
     WindowFonts.cpp
 )

+ 89 - 0
test/installer/extractors/KernelData.cpp

@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <boost/test/unit_test.hpp>
+#include <vector>
+#include "installer/extractors/KernelDataParts.h"
+#include "installer/extractors/KernelTextList.h"
+#include "installer/tables/Characters.h"
+
+/**
+ * Retrieves the byte that stands for a character in the texts of the kernel.
+ */
+static u8 Encode(const char character){
+    for (int code = 0; code < 256; code ++)
+        if (ENGLISH_CHARS[code] == static_cast<unsigned char>(character)) return code;
+    BOOST_FAIL("Character without a code.");
+    return 0;
+}
+
+BOOST_AUTO_TEST_CASE(TestKernelTextListReadsNamesInOrderAndChangesQuotes){
+    // Two offsets, and the texts "AB" and a double quote.
+    const std::vector<u8> section = {
+      4, 0, 7, 0, Encode('A'), Encode('B'), 0xFF, Encode('"'), 0xFF
+    };
+    KernelTextList names(File(section.data(), 0, section.size()));
+    BOOST_CHECK_EQUAL(names.NextName(), "AB");
+    BOOST_CHECK_EQUAL(names.NextName(), "'");
+}
+
+BOOST_AUTO_TEST_CASE(TestKernelTextListExpandsCompressedDescriptions){
+    // "ABCD", and a reference to the four bytes that start four bytes before the 0xF9 byte.
+    const std::vector<u8> section = {
+      2, 0, Encode('A'), Encode('B'), Encode('C'), Encode('D'), 0xF9, 3, 0xFF
+    };
+    KernelTextList descriptions(File(section.data(), 0, section.size()));
+    BOOST_CHECK_EQUAL(descriptions.NextDescription(), "ABCDABCD");
+}
+
+BOOST_AUTO_TEST_CASE(TestKernelDataBitsOfElementsAndUsers){
+    std::vector<int> elements;
+    KernelData::ReadElements(0x8001, elements);
+    BOOST_REQUIRE_EQUAL(elements.size(), 2u);
+    BOOST_CHECK_EQUAL(elements[0], static_cast<int>(KernelData::FIRE));
+    BOOST_CHECK_EQUAL(elements[1], static_cast<int>(KernelData::HIDDEN));
+    // All of them means none.
+    elements.clear();
+    KernelData::ReadElements(0xFFFF, elements);
+    BOOST_CHECK(elements.empty());
+    // Only eleven characters.
+    std::vector<u8> users;
+    KernelData::ReadUsers(0xFFFF, users);
+    BOOST_CHECK_EQUAL(users.size(), 11u);
+}
+
+BOOST_AUTO_TEST_CASE(TestKernelDataStatusEffects){
+    KernelData::StatusEffect cure;
+    KernelData::ReadStatusEffect(0x40 | 63, 0x8, cure);
+    BOOST_CHECK_EQUAL(cure.mode, KernelData::CURE);
+    BOOST_CHECK_EQUAL(cure.chance, 63);
+    BOOST_REQUIRE_EQUAL(cure.status.size(), 1u);
+    BOOST_CHECK_EQUAL(cure.status[0], static_cast<int>(KernelData::POISONED));
+    // Records that don't change statuses have all of them marked.
+    KernelData::StatusEffect none;
+    KernelData::ReadStatusEffect(0xFF, 0xFFFFFFFF, none);
+    BOOST_CHECK(none.status.empty());
+}
+
+BOOST_AUTO_TEST_CASE(TestKernelDataTargets){
+    KernelData::Target target;
+    // All rows, and not the enemies by default.
+    KernelData::ReadTarget(0x40, target);
+    BOOST_CHECK(target.all_rows);
+    BOOST_CHECK(!target.default_enemy);
+    KernelData::ReadTarget(0x02, target);
+    BOOST_CHECK(!target.all_rows);
+    BOOST_CHECK(target.default_enemy);
+}