init_web.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ///
  2. /// Spine Runtimes License Agreement
  3. /// Last updated April 5, 2025. Replaces all prior versions.
  4. ///
  5. /// Copyright (c) 2013-2025, Esoteric Software LLC
  6. ///
  7. /// Integration of the Spine Runtimes into software or otherwise creating
  8. /// derivative works of the Spine Runtimes is permitted under the terms and
  9. /// conditions of Section 2 of the Spine Editor License Agreement:
  10. /// http://esotericsoftware.com/spine-editor-license
  11. ///
  12. /// Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. /// or otherwise create derivative works of the Spine Runtimes (collectively,
  14. /// "Products"), provided that each user of the Products must obtain their own
  15. /// Spine Editor license and redistribution of the Products in any form must
  16. /// include this license and copyright notice.
  17. ///
  18. /// THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. /// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. /// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. /// DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. /// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. /// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. /// BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. /// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. /// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. /// THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. ///
  29. // ignore_for_file: type_argument_not_matching_bounds
  30. import 'package:flutter/services.dart';
  31. import 'package:inject_js/inject_js.dart' as js;
  32. import 'package:web_ffi_fork/web_ffi.dart';
  33. import 'package:web_ffi_fork/web_ffi_modules.dart';
  34. import 'spine_flutter_bindings_generated.dart';
  35. Module? _module;
  36. class SpineFlutterFFI {
  37. final DynamicLibrary dylib;
  38. final Allocator allocator;
  39. SpineFlutterFFI(this.dylib, this.allocator);
  40. }
  41. Future<SpineFlutterFFI> initSpineFlutterFFI(bool useStaticLinkage) async {
  42. if (_module == null) {
  43. Memory.init();
  44. registerOpaqueType<spine_skeleton_wrapper>();
  45. registerOpaqueType<spine_skeleton_data_wrapper>();
  46. registerOpaqueType<spine_bone_wrapper>();
  47. registerOpaqueType<spine_bone_data_wrapper>();
  48. registerOpaqueType<spine_slot_wrapper>();
  49. registerOpaqueType<spine_slot_data_wrapper>();
  50. registerOpaqueType<spine_skin_wrapper>();
  51. registerOpaqueType<spine_attachment_wrapper>();
  52. registerOpaqueType<spine_region_attachment_wrapper>();
  53. registerOpaqueType<spine_vertex_attachment_wrapper>();
  54. registerOpaqueType<spine_mesh_attachment_wrapper>();
  55. registerOpaqueType<spine_clipping_attachment_wrapper>();
  56. registerOpaqueType<spine_bounding_box_attachment_wrapper>();
  57. registerOpaqueType<spine_path_attachment_wrapper>();
  58. registerOpaqueType<spine_point_attachment_wrapper>();
  59. registerOpaqueType<spine_texture_region_wrapper>();
  60. registerOpaqueType<spine_sequence_wrapper>();
  61. registerOpaqueType<spine_constraint_wrapper>();
  62. registerOpaqueType<spine_constraint_data_wrapper>();
  63. registerOpaqueType<spine_ik_constraint_wrapper>();
  64. registerOpaqueType<spine_ik_constraint_data_wrapper>();
  65. registerOpaqueType<spine_transform_constraint_wrapper>();
  66. registerOpaqueType<spine_transform_constraint_data_wrapper>();
  67. registerOpaqueType<spine_path_constraint_wrapper>();
  68. registerOpaqueType<spine_path_constraint_data_wrapper>();
  69. registerOpaqueType<spine_animation_state_wrapper>();
  70. registerOpaqueType<spine_animation_state_data_wrapper>();
  71. registerOpaqueType<spine_animation_state_events_wrapper>();
  72. registerOpaqueType<spine_event_wrapper>();
  73. registerOpaqueType<spine_event_data_wrapper>();
  74. registerOpaqueType<spine_track_entry_wrapper>();
  75. registerOpaqueType<spine_animation_wrapper>();
  76. registerOpaqueType<spine_atlas_wrapper>();
  77. registerOpaqueType<spine_skeleton_data_result_wrapper>();
  78. registerOpaqueType<spine_render_command_wrapper>();
  79. registerOpaqueType<spine_bounds_wrapper>();
  80. registerOpaqueType<spine_color_wrapper>();
  81. registerOpaqueType<spine_vector_wrapper>();
  82. registerOpaqueType<spine_skeleton_drawable_wrapper>();
  83. registerOpaqueType<spine_skin_entry_wrapper>();
  84. registerOpaqueType<spine_skin_entries_wrapper>();
  85. await js.importLibrary('assets/packages/spine_flutter/lib/assets/libspine_flutter.js');
  86. Uint8List wasmBinaries =
  87. (await rootBundle.load('packages/spine_flutter/lib/assets/libspine_flutter.wasm')).buffer.asUint8List();
  88. _module = await EmscriptenModule.compile(wasmBinaries, 'libspine_flutter');
  89. }
  90. Module? m = _module;
  91. if (m != null) {
  92. final dylib = DynamicLibrary.fromModule(m);
  93. return SpineFlutterFFI(dylib, dylib.boundMemory);
  94. } else {
  95. throw Exception("Couldn't load libspine-flutter.js/.wasm");
  96. }
  97. }