mix_file_write.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "mix_file_write.h"
  17. #include "mix_file.h"
  18. #include "string_conversion.h"
  19. Cmix_file_write::Cmix_file_write(t_game game)
  20. {
  21. m_game = game;
  22. }
  23. void Cmix_file_write::add_file(int id, const Cvirtual_binary d)
  24. {
  25. m_index[id] = d;
  26. }
  27. void Cmix_file_write::add_file(string name, const Cvirtual_binary d)
  28. {
  29. add_file(Cmix_file::get_id(m_game, name), d);
  30. m_lmd_fw.add_fname(name);
  31. }
  32. void Cmix_file_write::clear()
  33. {
  34. m_index.clear();
  35. }
  36. int Cmix_file_write::write_start()
  37. {
  38. add_file("local mix database.dat", m_lmd_fw.write(m_game));
  39. int r = 4 + sizeof(t_mix_header) + m_index.size() * sizeof(t_mix_index_entry);
  40. for (auto& i : m_index)
  41. r += i.second.size();
  42. return r;
  43. }
  44. int Cmix_file_write::write(byte* d) const
  45. {
  46. byte* w = d;
  47. *reinterpret_cast<__int32*>(w) = 0;
  48. w += 4;
  49. t_mix_header& header = *reinterpret_cast<t_mix_header*>(w);
  50. header.c_files = m_index.size();
  51. w += sizeof(t_mix_header);
  52. t_mix_index_entry* index = reinterpret_cast<t_mix_index_entry*>(w);
  53. w += m_index.size() * sizeof(t_mix_index_entry);
  54. byte* body_start = w;
  55. for (auto& i : m_index)
  56. {
  57. index->id = i.first;
  58. index->offset = w - body_start;
  59. index->size = i.second.size();
  60. index++;
  61. w += i.second.read(w);
  62. }
  63. header.size = w - body_start;
  64. return w - d;
  65. }
  66. Cvirtual_binary Cmix_file_write::write()
  67. {
  68. Cvirtual_binary d;
  69. write(d.write_start(write_start()));
  70. return d;
  71. }