@@ -13,9 +13,12 @@ using namespace Live2D::Cubism::Framework::Rendering;
1313
1414class JniUserModel : public CubismUserModel {
1515public:
16- JniUserModel (JNIEnv* env, jobject javaObj ) {
16+ JniUserModel (JNIEnv* env) {
1717 env->GetJavaVM (&_jvm);
18- _javaObj = env->NewGlobalRef (javaObj);
18+ }
19+
20+ void link (JNIEnv* env, jobject obj) {
21+ _javaObj = env->NewGlobalRef (obj);
1922 }
2023
2124 ~JniUserModel () {
@@ -43,15 +46,12 @@ class JniUserModel : public CubismUserModel {
4346 std::vector<csmByte> motionBuf (buffer, buffer + size);
4447 auto * motion = CubismMotion::Create (motionBuf.data (), (csmSizeInt)motionBuf.size ());
4548 if (!motion) return ;
46-
4749 _motionBuffers[motion] = std::move (motionBuf);
48-
4950 motion->SetFinishedMotionHandlerAndMotionCustomData ([](ACubismMotion* self) {
5051 auto * m = static_cast <CubismMotion*>(self);
5152 auto * model = static_cast <JniUserModel*>(m->GetFinishedMotionCustomData ());
5253 model->queueFinishedMotion (m);
5354 }, this );
54-
5555 _motionManager->StartMotionPriority (motion, true , priority);
5656 }
5757
@@ -62,7 +62,6 @@ class JniUserModel : public CubismUserModel {
6262
6363 void update (float dt) {
6464 if (!_model) return ;
65-
6665 {
6766 std::lock_guard<std::mutex> lock (_pendingMutex);
6867 for (auto * m : _pendingDeletion) {
@@ -72,12 +71,10 @@ class JniUserModel : public CubismUserModel {
7271 }
7372 _pendingDeletion.clear ();
7473 }
75-
7674 _model->LoadParameters ();
7775 _motionManager->UpdateMotion (_model, dt);
7876 _model->SaveParameters ();
7977 if (_pose) _pose->UpdateParameters (_model, dt);
80-
8178 if (_dragManager) {
8279 _dragManager->Update (dt);
8380 auto * idm = CubismFramework::GetIdManager ();
@@ -86,7 +83,6 @@ class JniUserModel : public CubismUserModel {
8683 _model->AddParameterValue (idm->GetId (" ParamEyeBallX" ), _dragManager->GetX ());
8784 _model->AddParameterValue (idm->GetId (" ParamEyeBallY" ), _dragManager->GetY ());
8885 }
89-
9086 if (_physics) _physics->Evaluate (_model, dt);
9187 _model->Update ();
9288 }
@@ -112,9 +108,8 @@ class JniUserModel : public CubismUserModel {
112108 if (_jvm->GetEnv ((void **)&env, JNI_VERSION_1_6) == JNI_EDETACHED) _jvm->AttachCurrentThread ((void **)&env, nullptr );
113109 return env;
114110 }
115-
116111 JavaVM* _jvm;
117- jobject _javaObj;
112+ jobject _javaObj = nullptr ;
118113 std::vector<csmByte> _mocBuffer, _physicsBuffer, _poseBuffer;
119114 std::map<CubismMotion*, std::vector<csmByte>> _motionBuffers;
120115 std::vector<CubismMotion*> _pendingDeletion;
@@ -123,8 +118,15 @@ class JniUserModel : public CubismUserModel {
123118
124119extern " C" {
125120
126- JNIEXPORT jlong JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_createNative (JNIEnv* env, jobject thiz) {
127- return (jlong) new JniUserModel (env, thiz);
121+ JNIEXPORT jlong JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_createNative (JNIEnv* env, jclass) {
122+ return (jlong) new JniUserModel (env);
123+ }
124+
125+ JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_linkNative (JNIEnv* env, jobject thiz) {
126+ jclass cls = env->GetObjectClass (thiz);
127+ jfieldID fid = env->GetFieldID (cls, " _ptr" , " J" );
128+ jlong ptr = env->GetLongField (thiz, fid);
129+ ((JniUserModel*)ptr)->link (env, thiz);
128130}
129131
130132JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_deleteNative (JNIEnv*, jclass, jlong ptr) {
@@ -226,4 +228,4 @@ JNIEXPORT void JNICALL Java_dev_eatgrapes_live2d_CubismUserModel_drawNative(JNIE
226228 env->ReleaseFloatArrayElements (matrix, m_ptr, JNI_ABORT);
227229}
228230
229- }
231+ }
0 commit comments