V-Gears 0
Free Final Fantasy VII engine.
FieldDataInstaller.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18#include <vector>
19#include <string>
20#include <OgreMesh.h>
21#include <tinyxml.h>
23#include "ScopedLgp.h"
24#include "SpawnPointDb.h"
26#include "FieldTextWriter.h"
30
31typedef std::vector<std::string> MapList;
32
33typedef std::map<u16, SpawnPointDb> FieldSpawnPointsMap;
34
35typedef std::map<u16, float> FieldScaleFactorMap;
36
38
39 public:
40
49 const std::string& field_name, const std::vector<std::string>& field_id_to_name_lookup,
50 FieldSpawnPointsMap& spawn_points
51 ) :
52 field_name_(field_name), field_id_to_name_lookup_(field_id_to_name_lookup),
53 spawn_points_(spawn_points)
54 {}
55
68 virtual std::string GetSpawnPointName(
69 unsigned int map_id, const std::string& entity,
70 const std::string& function_name, unsigned int address
71 ) override{
72 return
73 field_name_ + "_" + entity + "_" + function_name + "_addr_" + std::to_string(address);
74 }
75
82 virtual std::string GetMapName(unsigned int map_id) override{
83 return field_id_to_name_lookup_[map_id];
84 }
85
94 virtual std::string GetFriendlyVarName(
95 unsigned int bank, unsigned int addr
96 ) override{
98 }
99
107 virtual std::string GetFriendlyEntityName(const std::string& entity) override{
109 }
110
118 virtual std::string GetFriendlyCharName(int char_id) override{
119 return VGears::NameLookup::CharName(char_id);
120 }
121
130 virtual std::string GetFriendlyFunctionName(
131 const std::string& entity, const std::string& function_name
132 ) override{
134 field_name_, entity, function_name
135 );
136 }
137
146 virtual std::string GetFunctionComment(
147 const std::string& entity, const std::string& function_name
148 ) override{
150 field_name_, entity, function_name
151 );
152 }
153
154 protected:
155
159 std::string field_name_;
160
165
169 const std::vector<std::string>& field_id_to_name_lookup_;
170};
171
176
177 // TODO: Move declaration to a .h file.
178
179 public:
180
185 const std::string& field_name, const std::vector<std::string>& field_id_to_name_lookup,
186 FieldSpawnPointsMap& spawn_points, size_t this_field_id
187 ) :
188 BaseFF7FieldScriptFormatter(field_name,field_id_to_name_lookup, spawn_points),
189 field_id_(this_field_id)
190 {}
191
208 virtual void AddSpawnPoint(
209 unsigned int map_id, const std::string& entity, const std::string& function_name,
210 unsigned int address, int x, int y, int z, int angle
211 ) override {
212 auto it = spawn_points_.find(map_id);
214 gw.destination_field_id = map_id;
215 gw.destination.x = x;
216 gw.destination.y = y;
217 gw.destination.z = z;
218 gw.dir = angle;
219 if (it != std::end(spawn_points_)){
220 // Add to the list of gateways that link to destination_field_id.
222 rec.from_script = true;
223 rec.field_id = field_id_;
224 rec.gateway = gw;
225 rec.entity_name = entity;
226 rec.script_function_name = function_name;
228 it->second.gateways_to_this_field.push_back(rec);
229 }
230 else{
231 // Create a new record for destination_field_id
232 SpawnPointDb db;
233 db.target_field_id = map_id;
235 rec.from_script = true;
236 rec.field_id = field_id_;
237 rec.gateway = gw;
238 rec.entity_name = entity;
239 rec.script_function_name = function_name;
241 db.gateways_to_this_field.push_back(rec);
242 spawn_points_.insert(std::make_pair(db.target_field_id, db));
243 }
244 }
245
246 private:
247
251 size_t field_id_;
252
253};
254
259
260 public:
261
271 const std::string& field_name, const VGears::ModelListFilePtr& models,
272 const std::vector<std::string>& field_id_to_name_lookup,
273 FieldSpawnPointsMap& spawn_points
274 ) :
275 BaseFF7FieldScriptFormatter(field_name, field_id_to_name_lookup, spawn_points),
276 model_loader_(models)
277 {}
278
279 // Names an animation, can't return empty
288 virtual std::string GetFriendlyAnimationName(int char_id, int id) override{
289 // Get the animation file name, then look up the friendly name of
290 // the "raw" animation.
291 if (static_cast<unsigned int>(char_id) >= model_loader_->GetModels().size()){
292 std::cerr << "FF7FieldScriptFormatter::AnimationName ERROR:"
293 << "Char ID " << char_id << " out of bounds" << std::endl;
294 return std::to_string(id);
295 }
296 const auto& model_info = model_loader_->GetModels().at(char_id);
297 if (static_cast<unsigned int>(id) >= model_info.animations.size()){
298 std::cerr << "FF7FieldScriptFormatter::AnimationName ERROR:"
299 << "In field " << field_name_ << " the model " << model_info.name
300 << " animation with ID " << id << " is out of bounds ("
301 << model_info.animations.size() << ")" << std::endl;
302 return std::to_string(id);
303 }
304 const auto raw_name = model_info.animations.at(id).name;
305 // Trim off ".yos" or whatever extension the model loader adds in.
306 Ogre::String base_name;
307 VGears::StringUtil::splitBase(raw_name, base_name);
308 VGears::StringUtil::toLowerCase(base_name);
309 return VGears::NameLookup::Animation(base_name);
310 }
311
312 private:
313
318};
319
321
322 public:
323
332 FieldDataInstaller(const std::string input_dir, const std::string output_dir);
333
338
347 int CollectSpawnAndScaleFactorsInit(Ogre::ResourceGroupManager* res_mgr);
348
357 void CollectSpawnAndScaleFactors(int field_index);
358
365 void Convert(int field_index);
366
374 int WriteInit();
375
384 void Write(int field_index);
385
391 void WriteEnd();
392
400 std::vector<std::string> ConvertModelsInit();
401
409 void ConvertModels(std::string model_name);
410
411 private:
412
421 static std::string CreateGateWayScript(
422 const std::string& gateway_entity_name, const std::string& target_map_name,
423 const std::string& source_spawn_point_name
424 );
425
434 static size_t GetFieldId(
435 const std::string& name, const std::vector<std::string>& field_id_to_name_lookup
436 );
437
447 static bool IsAFieldFile(const Ogre::String& resource_name);
448
461 static bool WillCrash(const Ogre::String& resource_name);
462
472 static bool IsTestField(const Ogre::String& resource_name);
473
483 static void CollectFieldScaleFactors(
484 VGears::FLevelFilePtr& field, FieldScaleFactorMap& scale_factors,
485 const std::vector<std::string>& field_id_to_name_lookup
486 );
487
500 static void CollectSpawnPoints(
501 VGears::FLevelFilePtr& field, const std::vector<std::string>& field_id_to_name_lookup,
502 FieldSpawnPointsMap& spawn_points
503 );
504
511 static float LINE_SCALE_FACTOR;
512
516 static std::string FIELD_MODELS_DIR;
517
521 static std::string FIELD_MAPS_DIR;
522
527
536 void ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh);
537
544 std::vector<int> ExtractMusicTrackIds(VGears::FLevelFilePtr& field);
545
552
560 float GetFieldScaleFactor(size_t field_id);
561
569 void CreateDir(const std::string& dir);
570
574 std::string input_dir_;
575
579 std::string output_dir_;
580
585
590
595
599 std::unique_ptr<ScopedLgp> fields_lgp_;
600
604 std::unique_ptr<ScopedLgp> textures_lgp_;
605
609 std::unique_ptr<ScopedLgp> field_models_lgp_;
610
611
615 Ogre::StringVectorPtr flevel_file_list_;
616
620 std::vector<std::string> map_list_;
621
626
631
636
641
646
651
655 Ogre::StringVectorPtr field_model_file_list_;
656
662 std::unique_ptr<TiXmlDocument> doc_;
663
669 std::unique_ptr<TiXmlElement> element_;
670
674 ModelAnimationMap::iterator model_animation_map_iterator_;
675
679 std::function<void(std::string)> write_output_line_;
680
684 std::function<void(std::string)> set_progress_label_;
685
690
694 std::vector<std::string> materials_;
695};
std::vector< std::string > MapList
Definition: FieldDataInstaller.h:31
std::map< u16, float > FieldScaleFactorMap
Definition: FieldDataInstaller.h:35
std::map< u16, SpawnPointDb > FieldSpawnPointsMap
Definition: FieldDataInstaller.h:33
Definition: FieldDataInstaller.h:37
virtual std::string GetMapName(unsigned int map_id) override
Retrieves a map name from it's ID.
Definition: FieldDataInstaller.h:82
BaseFF7FieldScriptFormatter(const std::string &field_name, const std::vector< std::string > &field_id_to_name_lookup, FieldSpawnPointsMap &spawn_points)
Constructor.
Definition: FieldDataInstaller.h:48
FieldSpawnPointsMap & spawn_points_
The list of spawn points.
Definition: FieldDataInstaller.h:164
virtual std::string GetSpawnPointName(unsigned int map_id, const std::string &entity, const std::string &function_name, unsigned int address) override
Composes a spawn point name.
Definition: FieldDataInstaller.h:68
virtual std::string GetFriendlyEntityName(const std::string &entity) override
Retrieves a user friendly entity name.
Definition: FieldDataInstaller.h:107
virtual std::string GetFriendlyFunctionName(const std::string &entity, const std::string &function_name) override
Retrieves a user friendly function name.
Definition: FieldDataInstaller.h:130
const std::vector< std::string > & field_id_to_name_lookup_
Look up table relatingfield IDs and names.
Definition: FieldDataInstaller.h:169
std::string field_name_
The field name.
Definition: FieldDataInstaller.h:159
virtual std::string GetFunctionComment(const std::string &entity, const std::string &function_name) override
Retrieves the header comment for a function in an entity.
Definition: FieldDataInstaller.h:146
virtual std::string GetFriendlyCharName(int char_id) override
Retrieves a user friendly character name.
Definition: FieldDataInstaller.h:118
virtual std::string GetFriendlyVarName(unsigned int bank, unsigned int addr) override
Retrieves a user friendly variable name.
Definition: FieldDataInstaller.h:94
Handles the formatting of field scripts.
Definition: FieldDataInstaller.h:258
FF7FieldScriptFormatter(const std::string &field_name, const VGears::ModelListFilePtr &models, const std::vector< std::string > &field_id_to_name_lookup, FieldSpawnPointsMap &spawn_points)
Constructor.
Definition: FieldDataInstaller.h:270
virtual std::string GetFriendlyAnimationName(int char_id, int id) override
Retreieves an animation name from iths ID.
Definition: FieldDataInstaller.h:288
const VGears::ModelListFilePtr & model_loader_
The field model loader.
Definition: FieldDataInstaller.h:317
Handles the script gateway colection.
Definition: FieldDataInstaller.h:175
size_t field_id_
The field map ID.
Definition: FieldDataInstaller.h:251
FF7FieldScriptGatewayCollector(const std::string &field_name, const std::vector< std::string > &field_id_to_name_lookup, FieldSpawnPointsMap &spawn_points, size_t this_field_id)
Cosntructor.
Definition: FieldDataInstaller.h:184
virtual void AddSpawnPoint(unsigned int map_id, const std::string &entity, const std::string &function_name, unsigned int address, int x, int y, int z, int angle) override
Adds an spawn point.
Definition: FieldDataInstaller.h:208
Definition: FieldDataInstaller.h:320
VGears::FLevelFilePtr field_
Field currently being processed.
Definition: FieldDataInstaller.h:650
FieldTextWriter field_text_writer_
Field text writer.
Definition: FieldDataInstaller.h:689
MapList::iterator converted_map_list_iterator_
Iterator for {.
Definition: FieldDataInstaller.h:645
std::string output_dir_
The path to the directory where to save the V-Gears data.
Definition: FieldDataInstaller.h:579
ModelAnimationMap::iterator model_animation_map_iterator_
Iterator for {.
Definition: FieldDataInstaller.h:674
size_t progress_step_num_elements_
Helper variable to indicate internal progress of installation steps.
Definition: FieldDataInstaller.h:589
static void CollectFieldScaleFactors(VGears::FLevelFilePtr &field, FieldScaleFactorMap &scale_factors, const std::vector< std::string > &field_id_to_name_lookup)
Collects the scale factor from a map.
Definition: FieldDataInstaller.cpp:150
void WriteEnd()
Cleans up after {.
Definition: FieldDataInstaller.cpp:297
static bool IsAFieldFile(const Ogre::String &resource_name)
Checks if a file is a field file.
Definition: FieldDataInstaller.cpp:74
static size_t GetFieldId(const std::string &name, const std::vector< std::string > &field_id_to_name_lookup)
Retrieves a field ID from a name.
Definition: FieldDataInstaller.cpp:66
std::vector< std::string > map_list_
The list of maps.
Definition: FieldDataInstaller.h:620
FieldDataInstaller(const std::string input_dir, const std::string output_dir)
Installer constructor.
Definition: FieldDataInstaller.cpp:210
std::vector< int > ExtractMusicTrackIds(VGears::FLevelFilePtr &field)
Reads the music track IDs from a specific field.
Definition: FieldDataInstaller.cpp:469
static bool IsTestField(const Ogre::String &resource_name)
Checks if a map is a test field.
Definition: FieldDataInstaller.cpp:101
FieldScaleFactorMap scale_factors_
Map of th collected scale factors.
Definition: FieldDataInstaller.h:630
int CollectSpawnAndScaleFactorsInit(Ogre::ResourceGroupManager *res_mgr)
Initializer for {.
Definition: FieldDataInstaller.cpp:216
std::string input_dir_
The path to the directory from which to read the PC game data.
Definition: FieldDataInstaller.h:574
void PcFieldToVGearsField(VGears::FLevelFilePtr &field)
Converts a FFVII PC field to a V-Gears field.
Definition: FieldDataInstaller.cpp:488
void ConvertModels(std::string model_name)
Converts the field models to V-Gears format.
Definition: FieldDataInstaller.cpp:333
static int INACTIVE_GATEWAY_ID
Gateways to this map ID are considered to be inactive.
Definition: FieldDataInstaller.h:526
void CreateDir(const std::string &dir)
Creates a directory in the outputh path.
Definition: FieldDataInstaller.cpp:144
FieldSpawnPointsMap spawn_points_
Map of the collected spawn points.
Definition: FieldDataInstaller.h:625
Ogre::StringVectorPtr field_model_file_list_
List of model files.
Definition: FieldDataInstaller.h:655
static bool WillCrash(const Ogre::String &resource_name)
Checks if a map cause fatal crash bugs.
Definition: FieldDataInstaller.cpp:83
std::unique_ptr< ScopedLgp > fields_lgp_
LGP archive with field data.
Definition: FieldDataInstaller.h:599
void Convert(int field_index)
Converts FFVII PC fields to V-Gears format.
Definition: FieldDataInstaller.cpp:248
std::unique_ptr< ScopedLgp > textures_lgp_
LGP archive with texture data.
Definition: FieldDataInstaller.h:604
std::function< void(std::string)> set_progress_label_
Function used to print set the current installation progress text.
Definition: FieldDataInstaller.h:684
Ogre::StringVectorPtr flevel_file_list_
List of flevel files.
Definition: FieldDataInstaller.h:615
static std::string FIELD_MAPS_DIR
Path to the field maps directory.
Definition: FieldDataInstaller.h:521
void Write(int field_index)
Saves the game maps to XML files.
Definition: FieldDataInstaller.cpp:283
MapList converted_map_list_
List of converted maps.
Definition: FieldDataInstaller.h:640
size_t iterator_counter_
Iterator counter.
Definition: FieldDataInstaller.h:594
static std::string FIELD_MODELS_DIR
Path to the field models directory.
Definition: FieldDataInstaller.h:516
static std::string CreateGateWayScript(const std::string &gateway_entity_name, const std::string &target_map_name, const std::string &source_spawn_point_name)
Creates a gateway script.
Definition: FieldDataInstaller.cpp:36
std::unique_ptr< ScopedLgp > field_models_lgp_
LGP archive with field model data.
Definition: FieldDataInstaller.h:609
float GetFieldScaleFactor(size_t field_id)
Retrieves a field scale factor.
Definition: FieldDataInstaller.cpp:462
size_t conversion_step_
Helper variable to indicate internal progress of installation steps.
Definition: FieldDataInstaller.h:584
std::vector< std::string > ConvertModelsInit()
Initializer for {.
Definition: FieldDataInstaller.cpp:302
std::unique_ptr< TiXmlDocument > doc_
An XML document.
Definition: FieldDataInstaller.h:662
ModelsAndAnimationsDb used_models_and_anims_
ModelsAndAnimationsUsedByConvertedFields.
Definition: FieldDataInstaller.h:635
std::unique_ptr< TiXmlElement > element_
An XML element.
Definition: FieldDataInstaller.h:669
std::function< void(std::string)> write_output_line_
Function used to print text to the log output, line by line.
Definition: FieldDataInstaller.h:679
std::vector< std::string > materials_
Written materials.
Definition: FieldDataInstaller.h:694
void ExportMesh(const std::string outdir, const Ogre::MeshPtr &mesh)
Exports a mesh to a file.
Definition: FieldDataInstaller.cpp:367
int WriteInit()
Initializer for {.
Definition: FieldDataInstaller.cpp:277
void CollectSpawnAndScaleFactors(int field_index)
Reads spawn points and scale factors from flevel files.
Definition: FieldDataInstaller.cpp:228
static float LINE_SCALE_FACTOR
The scale factor for line point coordinates.
Definition: FieldDataInstaller.h:511
static void CollectSpawnPoints(VGears::FLevelFilePtr &field, const std::vector< std::string > &field_id_to_name_lookup, FieldSpawnPointsMap &spawn_points)
Collects the spawn point from a field.
Definition: FieldDataInstaller.cpp:158
~FieldDataInstaller()
Installer destructor.
Definition: FieldDataInstaller.cpp:214
Formatter for field scripts.
Definition: FieldScriptFormatter.h:26
Handles the.
Definition: FieldTextWriter.h:26
Database of model animations.
Definition: ModelsAndAnimationsDb.h:25
static String FieldScriptEntityName(const String &old_entity_name)
Matches entity names.
Definition: FF7NameLookup.h:422
static String FieldScriptVarName(int bank, int addr)
Retrieves a variable name.
Definition: FF7NameLookup.h:399
static String FieldScriptFunctionName(const String &field_name, const String &entity_name, const String &old_function_name)
Matches function names.
Definition: FF7NameLookup.h:463
static const String & Animation(const String &key)
Matches animation names.
Definition: FF7NameLookup.h:373
static String CharName(int char_id)
Retrieves a character name from an ID.
Definition: FF7NameLookup.h:354
static String FieldScriptFunctionComment(const String &field_name, const String &entity_name, const String &old_function_name)
Matches function comments.
Definition: FF7NameLookup.h:440
static void splitBase(const String &name, String &basename)
Extract a file name without extension.
Definition: VGearsStringUtil.cpp:25
Ogre::SharedPtr< ModelListFile > ModelListFilePtr
Definition: FF7ModelListFile.h:197
Ogre::String String
Definition: TypeDefine.h:37
Ogre::SharedPtr< FLevelFile > FLevelFilePtr
Definition: VGearsFLevelFile.h:303
A spawn point database record.
Definition: SpawnPointDb.h:34
u16 field_id
Field that links to {.
Definition: SpawnPointDb.h:39
std::string script_function_name
Definition: SpawnPointDb.h:68
std::string entity_name
Definition: SpawnPointDb.h:61
u32 gateway_index_or_map_jump_address
Index of the gateway in {.
Definition: SpawnPointDb.h:44
bool from_script
Indicates if the gateway is a map jump from a script.
Definition: SpawnPointDb.h:54
VGears::TriggersFile::Gateway gateway
Gateway data.
Definition: SpawnPointDb.h:49
A database of spawn points.
Definition: SpawnPointDb.h:21
std::vector< Record > gateways_to_this_field
List of gateways to this field.
Definition: SpawnPointDb.h:74
u16 target_field_id
Field IDs.
Definition: SpawnPointDb.h:29
A gateway.
Definition: VGearsTriggersFile.h:179
u16 destination_field_id
ID of the destination field.
Definition: VGearsTriggersFile.h:194
TriggerVertex destination
The destination point in the target map.
Definition: VGearsTriggersFile.h:189
u8 dir
Definition: VGearsTriggersFile.h:199
s16 y
Y coordinate.
Definition: VGearsTriggersFile.h:166
s16 z
Z coordinate.
Definition: VGearsTriggersFile.h:171
s16 x
X coordinate.
Definition: VGearsTriggersFile.h:161