💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.1 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/control-profile.hpp"
  20. #include <engine/resources/serializer.hpp>
  21. #include <engine/resources/serialize-error.hpp>
  22. #include <engine/resources/deserializer.hpp>
  23. #include <engine/resources/deserialize-error.hpp>
  24. #include <engine/resources/resource-loader.hpp>
  25. #include <engine/debug/log.hpp>
  26. /**
  27. * Serializes a control profile.
  28. *
  29. * @param[in] profile Control profile to serialize.
  30. * @param[in,out] ctx Serialize context.
  31. *
  32. * @throw serialize_error Write error.
  33. * @throw serialize_error Unsupported mapping type.
  34. */
  35. template <>
  36. void serializer<::control_profile>::serialize(const ::control_profile& profile, serialize_context& ctx)
  37. {
  38. // Write number of mappings
  39. std::uint64_t size = static_cast<std::uint64_t>(profile.mappings.size());
  40. ctx.write64<std::endian::big>(reinterpret_cast<const std::byte*>(&size), 1);
  41. // Write mappings
  42. for (const auto& [key, value]: profile.mappings)
  43. {
  44. // Write key
  45. ctx.write32<std::endian::big>(reinterpret_cast<const std::byte*>(&key), 1);
  46. // Write mapping type
  47. const input::mapping_type mapping_type = value->get_mapping_type();
  48. ctx.write8(reinterpret_cast<const std::byte*>(&mapping_type), 1);
  49. // Write mapping
  50. switch (mapping_type)
  51. {
  52. case input::mapping_type::gamepad_axis:
  53. serializer<input::gamepad_axis_mapping>().serialize(*static_cast<const input::gamepad_axis_mapping*>(value.get()), ctx);
  54. break;
  55. case input::mapping_type::gamepad_button:
  56. serializer<input::gamepad_button_mapping>().serialize(*static_cast<const input::gamepad_button_mapping*>(value.get()), ctx);
  57. break;
  58. case input::mapping_type::key:
  59. serializer<input::key_mapping>().serialize(*static_cast<const input::key_mapping*>(value.get()), ctx);
  60. break;
  61. case input::mapping_type::mouse_button:
  62. serializer<input::mouse_button_mapping>().serialize(*static_cast<const input::mouse_button_mapping*>(value.get()), ctx);
  63. break;
  64. case input::mapping_type::mouse_motion:
  65. serializer<input::mouse_motion_mapping>().serialize(*static_cast<const input::mouse_motion_mapping*>(value.get()), ctx);
  66. break;
  67. case input::mapping_type::mouse_scroll:
  68. serializer<input::mouse_scroll_mapping>().serialize(*static_cast<const input::mouse_scroll_mapping*>(value.get()), ctx);
  69. break;
  70. default:
  71. throw serialize_error("Unsupported mapping type");
  72. break;
  73. }
  74. }
  75. // Write settings
  76. serializer<dict<hash::fnv1a32_t>>().serialize(profile.settings, ctx);
  77. }
  78. /**
  79. * Deserializes a control profile.
  80. *
  81. * @param[out] profile Control profile to deserialize.
  82. * @param[in,out] ctx Deserialize context.
  83. *
  84. * @throw deserialize_error Read error.
  85. * @throw deserialize_error Unsupported mapping type.
  86. */
  87. template <>
  88. void deserializer<::control_profile>::deserialize(::control_profile& profile, deserialize_context& ctx)
  89. {
  90. profile.mappings.clear();
  91. // Read number of mappings
  92. std::uint64_t size = 0;
  93. ctx.read64<std::endian::big>(reinterpret_cast<std::byte*>(&size), 1);
  94. // Read mappings
  95. for (std::uint64_t i = 0; i < size; ++i)
  96. {
  97. // Read key
  98. hash::fnv1a32_t key;
  99. ctx.read32<std::endian::big>(reinterpret_cast<std::byte*>(&key), 1);
  100. // Read mapping type
  101. input::mapping_type mapping_type;
  102. ctx.read8(reinterpret_cast<std::byte*>(&mapping_type), 1);
  103. // Read mapping
  104. switch (mapping_type)
  105. {
  106. case input::mapping_type::gamepad_axis:
  107. {
  108. input::gamepad_axis_mapping mapping;
  109. deserializer<input::gamepad_axis_mapping>().deserialize(mapping, ctx);
  110. profile.mappings.emplace(key, std::make_unique<input::gamepad_axis_mapping>(std::move(mapping)));
  111. break;
  112. }
  113. case input::mapping_type::gamepad_button:
  114. {
  115. input::gamepad_button_mapping mapping;
  116. deserializer<input::gamepad_button_mapping>().deserialize(mapping, ctx);
  117. profile.mappings.emplace(key, std::make_unique<input::gamepad_button_mapping>(std::move(mapping)));
  118. break;
  119. }
  120. case input::mapping_type::key:
  121. {
  122. input::key_mapping mapping;
  123. deserializer<input::key_mapping>().deserialize(mapping, ctx);
  124. profile.mappings.emplace(key, std::make_unique<input::key_mapping>(std::move(mapping)));
  125. break;
  126. }
  127. case input::mapping_type::mouse_button:
  128. {
  129. input::mouse_button_mapping mapping;
  130. deserializer<input::mouse_button_mapping>().deserialize(mapping, ctx);
  131. profile.mappings.emplace(key, std::make_unique<input::mouse_button_mapping>(std::move(mapping)));
  132. break;
  133. }
  134. case input::mapping_type::mouse_motion:
  135. {
  136. input::mouse_motion_mapping mapping;
  137. deserializer<input::mouse_motion_mapping>().deserialize(mapping, ctx);
  138. profile.mappings.emplace(key, std::make_unique<input::mouse_motion_mapping>(std::move(mapping)));
  139. break;
  140. }
  141. case input::mapping_type::mouse_scroll:
  142. {
  143. input::mouse_scroll_mapping mapping;
  144. deserializer<input::mouse_scroll_mapping>().deserialize(mapping, ctx);
  145. profile.mappings.emplace(key, std::make_unique<input::mouse_scroll_mapping>(std::move(mapping)));
  146. break;
  147. }
  148. default:
  149. throw deserialize_error("Unsupported mapping type");
  150. break;
  151. }
  152. }
  153. // Read settings
  154. deserializer<dict<hash::fnv1a32_t>>().deserialize(profile.settings, ctx);
  155. }
  156. template <>
  157. std::unique_ptr<control_profile> resource_loader<control_profile>::load(::resource_manager& resource_manager, deserialize_context& ctx)
  158. {
  159. std::unique_ptr<control_profile> profile = std::make_unique<control_profile>();
  160. deserializer<control_profile>().deserialize(*profile, ctx);
  161. return profile;
  162. }