handling_compatibility_breakages.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. .. _doc_handling_compatibility_breakages:
  2. Handling compatibility breakages
  3. ================================
  4. .. TODO: Elaborate on types of compatibility and procedure.
  5. So you've added a new parameter to a method, changed the return type,
  6. changed the type of a parameter, or changed its default value,
  7. and now the automated testing is complaining about compatibility breakages?
  8. Breaking compatibility should be avoided, but when necessary there are systems in place
  9. to handle this in a way that makes the transition as smooth as possible.
  10. A practical example
  11. -------------------
  12. .. TODO: Add example that showcases more details like original default arguments etc.
  13. These changes are taken from `pull request #88047 <https://github.com/godotengine/godot/pull/88047>`_, which added
  14. new pathing options to ``AStarGrid2D`` and other AStar classes.
  15. Among other changes, these methods were modified in ``core/math/a_star_grid_2d.h``:
  16. .. code-block:: cpp
  17. Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to);
  18. TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to);
  19. To:
  20. .. code-block:: cpp
  21. Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  22. TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  23. This meant adding new compatibility method bindings to the file, which should be in the ``protected`` section of
  24. the code, usually placed next to ``_bind_methods()``:
  25. .. code-block:: cpp
  26. #ifndef DISABLE_DEPRECATED
  27. TypedArray<Vector2i> _get_id_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  28. Vector<Vector2> _get_point_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  29. static void _bind_compatibility_methods();
  30. #endif
  31. They should start with a ``_`` to indicate that they are internal, and end with ``_bind_compat_`` followed by the PR number
  32. that introduced the change (``88047`` in this example). These compatibility methods need to be implemented in a dedicated file,
  33. like ``core/math/a_star_grid_2d.compat.inc`` in this case:
  34. .. code-block:: cpp
  35. :caption: core/math/a_star_grid_2d.compat.inc
  36. /**************************************************************************/
  37. /* a_star_grid_2d.compat.inc */
  38. /**************************************************************************/
  39. /* This file is part of: */
  40. /* GODOT ENGINE */
  41. /* https://godotengine.org */
  42. /**************************************************************************/
  43. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  44. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  45. /* */
  46. /* Permission is hereby granted, free of charge, to any person obtaining */
  47. /* a copy of this software and associated documentation files (the */
  48. /* "Software"), to deal in the Software without restriction, including */
  49. /* without limitation the rights to use, copy, modify, merge, publish, */
  50. /* distribute, sublicense, and/or sell copies of the Software, and to */
  51. /* permit persons to whom the Software is furnished to do so, subject to */
  52. /* the following conditions: */
  53. /* */
  54. /* The above copyright notice and this permission notice shall be */
  55. /* included in all copies or substantial portions of the Software. */
  56. /* */
  57. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  58. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  59. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  60. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  61. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  62. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  63. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  64. /**************************************************************************/
  65. #ifndef DISABLE_DEPRECATED
  66. #include "core/variant/typed_array.h"
  67. TypedArray<Vector2i> AStarGrid2D::_get_id_path_bind_compat_88047(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  68. return get_id_path(p_from_id, p_to_id, false);
  69. }
  70. Vector<Vector2> AStarGrid2D::_get_point_path_bind_compat_88047(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  71. return get_point_path(p_from_id, p_to_id, false);
  72. }
  73. void AStarGrid2D::_bind_compatibility_methods() {
  74. ClassDB::bind_compatibility_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStarGrid2D::_get_id_path_bind_compat_88047);
  75. ClassDB::bind_compatibility_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::_get_point_path_bind_compat_88047);
  76. }
  77. #endif // DISABLE_DEPRECATED
  78. Unless the change in compatibility is complex, the compatibility method should simply call the modified method directly,
  79. instead of duplicating that method. Make sure to match the default arguments for that method (in the example above this would be ``false``).
  80. This file should always be placed next to the original file, and have ``.compat.inc`` at the end instead of ``.cpp`` or ``.h``.
  81. Next, this should be included in the ``.cpp`` file we're adding compatibility methods to, so ``core/math/a_star_grid_2d.cpp``:
  82. .. code-block:: cpp
  83. :caption: core/math/a_star_grid_2d.cpp
  84. #include "a_star_grid_2d.h"
  85. #include "a_star_grid_2d.compat.inc"
  86. #include "core/variant/typed_array.h"
  87. And finally, the changes reported by the API validation step should be added to the relevant validation file. Because this was
  88. done during the development of 4.3, this would be ``misc/extension_api_validation/4.2-stable.expected`` (including changes not shown in
  89. this example):
  90. .. code-block:: text
  91. :caption: misc/extension_api_validation/4.2-stable.expected
  92. GH-88047
  93. --------
  94. Validate extension JSON: Error: Field 'classes/AStar2D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  95. Validate extension JSON: Error: Field 'classes/AStar2D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  96. Validate extension JSON: Error: Field 'classes/AStar3D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  97. Validate extension JSON: Error: Field 'classes/AStar3D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  98. Validate extension JSON: Error: Field 'classes/AStarGrid2D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  99. Validate extension JSON: Error: Field 'classes/AStarGrid2D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  100. Added optional "allow_partial_path" argument to get_id_path and get_point_path methods in AStar classes.
  101. Compatibility methods registered.
  102. The instructions for how to add to that file are at the top of the file itself.
  103. If you get a "Hash changed" error for a method, it means that the compatibility binding is missing or incorrect.
  104. Such lines shouldn't be added to the ``.expected`` file, but fixed by binding the proper compatibility method.
  105. And that's it! You might run into a bit more complicated cases, like rearranging arguments,
  106. changing return types, etc., but this covers the basic on how to use this system.
  107. For more information, see `pull request #76446 <https://github.com/godotengine/godot/pull/76446>`_.