RedSpineBakeManage.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // RedSpineBakeModel.hpp
  3. // empty2dx-desktop
  4. //
  5. // Created by Liang zhong on 2022/11/5.
  6. //
  7. #ifndef RedSpineBakeModel_h
  8. #define RedSpineBakeModel_h
  9. #include <stdio.h>
  10. #include <queue>
  11. #include <thread>
  12. #include <mutex>
  13. #include <condition_variable>
  14. #include "cocos2d.h"
  15. #include "RedSlotBakeModel.h"
  16. #include "RedAnimationBakeModel.h"
  17. #include "spine_bake_data.pb.h"
  18. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  19. # include <jni.h>
  20. # include "platform/android/jni/JniHelper.h"
  21. # include "platform/android/CCFileUtils-android.h"
  22. # include <android/asset_manager.h>
  23. # define BinaryFileClassName "org/cocos2dx/cpp/BinaryFileUtils"
  24. typedef unsigned char BYTE;
  25. struct ByteData {
  26. std::vector<jbyte> data;
  27. int length;
  28. };
  29. #endif
  30. typedef struct _AsyncLoadAnimationFileRequest {
  31. std::string key = "";
  32. std::string fileFullPath = "";
  33. bool isCharacter = false;
  34. } AsyncLoadAnimationFileRequest;
  35. class RedSpineBakeManage
  36. {
  37. public:
  38. static RedSpineBakeManage* getInstance();
  39. static void destoryInstance();
  40. void setCurrentAnimationBakeModel(std::string aName,RedAnimationBakeModel *aAni);
  41. RedAnimationBakeModel * getAnimateByName(std::string aFileName,std::string aName);
  42. void endBakeSaveToFile(std::string aPath);
  43. void startBake(std::string aFileName);
  44. void loadAnimationFileAsync(const std::string& aFileName, const std::string& aPath, bool isCharacter = false);
  45. void loadAnimationFile(const std::string& aFileName, const std::string& aPath, bool isCharacter = false);
  46. void releaseAimationFile(std::string aFileName);
  47. void fetchAnimations(const std::string& aFileName, std::vector<std::string>& anims);
  48. private:
  49. RedSpineBakeManage() = default;
  50. ~RedSpineBakeManage() = default;
  51. /**
  52. * 功能:释放烘培管理器
  53. */
  54. void _release();
  55. /**
  56. * 功能:子线程读取烘培动画文件
  57. */
  58. void _readAnimationFileInThread();
  59. /**
  60. * 功能:解析指定路径的动画proto文件
  61. * 参数 fileFullPath:动画proto文件的全路径
  62. * 参数 dataIndex_pb:proto文件解析后得到的数据
  63. * 返回值:解析得到的数据,如果解析失败数据为空
  64. */
  65. cocos2d::Data _parseProtoData(const std::string& fileFullPath, const std::string& fileName, google::protobuf::Map<std::string, ::RedSpineBakeProto::DataInfo>& dataIndexPb, bool isCharacter = false);
  66. private:
  67. std::unordered_map<std::string, std::unordered_map<std::string, RedAnimationBakeModel *>>animateList;//记录所有animate
  68. std::string fileName;
  69. std::string filePath;
  70. std::map<std::string, std::list<std::function<void(void)>>> cbsLoading;
  71. //记录所有文件
  72. std::unordered_map<std::string, cocos2d::Data> byteArrayList;
  73. std::unordered_map<std::string, ::google::protobuf::Map<std::string, ::RedSpineBakeProto::DataInfo>> dataIndexPbList;
  74. ///异步加载文件线程
  75. std::thread* _loadThread = nullptr;
  76. ///异步加载文件请求队列
  77. std::queue<AsyncLoadAnimationFileRequest*> _requests;
  78. ////异步加载文件请求队列锁
  79. std::mutex _requestsMtx;
  80. ///线程休眠条件变量
  81. condition_variable _sleepCondition;
  82. };
  83. #endif /* RedSpineBakeModel_h */