AbFile.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #pragma once
  16. #include <vector>
  17. #include "common/File.h"
  18. #include "common/TypeDefine.h"
  19. /**
  20. * Represents an animation scripts file.
  21. *
  22. * *ab files are bundled into battle.lgp. They contain animation scripts. More info about them in
  23. * {@see https://forums.qhimm.com/index.php?topic=14204.0}
  24. */
  25. class AbFile{
  26. public:
  27. /**
  28. * Constructor.
  29. *
  30. * Reads the file.
  31. *
  32. * @param[in, out] file The file to read from. The file contents will
  33. * not be altered, but it's offset will be changed while reading it.
  34. * @param[in] enemy Indicates if the animation is for an enemy. Enemies
  35. * have a different number of animations than characters or backgrounds.
  36. */
  37. AbFile(File file, bool enemy);
  38. /**
  39. * Generates script files from the ba file.
  40. *
  41. * A file will be generated with every script in the *ab file. The file
  42. * will be created in the specified path, and will be named like the
  43. * model id, followed by the ".lua" extension. For example, if path is
  44. * "/home/user/.v-gears", the model id is "rt", the file name will be:
  45. * "/home/user/.v-gears/rt.lua". Each script will be named
  46. * "script" + index (script1(), script2() ... script44() ...)
  47. *
  48. * @param[in] model_id ID of the model the animation belongs to, usually
  49. * a two letter code. Used to generate the file name.
  50. * @param[in] path Path to the directory where the files will be saved.
  51. * @return The number of scripts written to the file.
  52. */
  53. unsigned int GenerateScripts(std::string model_id, std::string path) const;
  54. private:
  55. /**
  56. * Reads the Ab file and extracts all the data.
  57. */
  58. void Read();
  59. /**
  60. * Decompiles a script in the *ab file.
  61. *
  62. * It won't nodify the file current offset.
  63. *
  64. * @param[in] offset Offset at which the script starts.
  65. * @return The script text.
  66. */
  67. std::string DecompileScript(u32 offset);
  68. /**
  69. * Reads the next byte of the file.
  70. *
  71. * @return The byte, 0 past the end of the file.
  72. */
  73. u8 ReadByte();
  74. /**
  75. * Reads the next two bytes of the file, little endian.
  76. *
  77. * @return The value, with 0 for the bytes past the end of the file.
  78. */
  79. u16 ReadWord();
  80. /**
  81. * The ab file.
  82. */
  83. File ab_file_;
  84. /**
  85. * Indicates if the model is for an enemy.
  86. */
  87. bool is_enemy_;
  88. /**
  89. * List of animations.
  90. */
  91. std::vector<std::string> scripts_;
  92. };