mirror of https://github.com/axmolengine/axmol.git
Integrate CocosPlayClient
This commit is contained in:
parent
4f4d49ef79
commit
58c45ca144
|
@ -37,6 +37,7 @@
|
|||
#include "base/CCDirector.h"
|
||||
#include "base/CCScheduler.h"
|
||||
#include "platform/android/CCFileUtils-android.h"
|
||||
#include "platform/android/jni/CocosPlayClient.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
using namespace cocos2d::experimental;
|
||||
|
@ -237,12 +238,15 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume
|
|||
break;
|
||||
|
||||
auto& player = _audioPlayers[currentAudioID];
|
||||
auto initPlayer = player.init( _engineEngine, _outputMixObject, FileUtils::getInstance()->fullPathForFilename(filePath), volume, loop);
|
||||
auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
|
||||
cocosplay::updateAssets(fullPath);
|
||||
auto initPlayer = player.init(_engineEngine, _outputMixObject, fullPath, volume, loop);
|
||||
if (!initPlayer){
|
||||
_audioPlayers.erase(currentAudioID);
|
||||
log("%s,%d message:create player for %s fail", __func__, __LINE__, filePath.c_str());
|
||||
break;
|
||||
}
|
||||
cocosplay::notifyFileLoaded(fullPath);
|
||||
|
||||
audioId = currentAudioID++;
|
||||
player._audioID = audioId;
|
||||
|
|
|
@ -20,7 +20,8 @@ jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp \
|
|||
jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp \
|
||||
jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp \
|
||||
jni/JniHelper.cpp \
|
||||
jni/TouchesJni.cpp
|
||||
jni/TouchesJni.cpp \
|
||||
jni/CocosPlayClient.cpp
|
||||
|
||||
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ THE SOFTWARE.
|
|||
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
|
||||
#include "jni/CocosPlayClient.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LOG_TAG "CCFileUtils-android.cpp"
|
||||
|
@ -77,10 +77,25 @@ FileUtilsAndroid::~FileUtilsAndroid()
|
|||
|
||||
bool FileUtilsAndroid::init()
|
||||
{
|
||||
_defaultResRootPath = "assets/";
|
||||
cocosplay::lazyInit();
|
||||
if (cocosplay::isEnabled() && !cocosplay::isDemo())
|
||||
{
|
||||
_defaultResRootPath = cocosplay::getGameRoot();
|
||||
}
|
||||
else
|
||||
{
|
||||
_defaultResRootPath = "assets/";
|
||||
}
|
||||
|
||||
return FileUtils::init();
|
||||
}
|
||||
|
||||
void FileUtilsAndroid::purgeCachedEntries()
|
||||
{
|
||||
FileUtils::purgeCachedEntries();
|
||||
cocosplay::purgeCachedEntries();
|
||||
}
|
||||
|
||||
std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const
|
||||
{
|
||||
std::string newFileName = FileUtils::getNewFilename(filename);
|
||||
|
@ -140,6 +155,11 @@ bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const
|
|||
return false;
|
||||
}
|
||||
|
||||
if (cocosplay::isEnabled() && !cocosplay::isDemo())
|
||||
{
|
||||
return cocosplay::fileExists(strFilePath);
|
||||
}
|
||||
|
||||
bool bFound = false;
|
||||
|
||||
// Check whether file exists in apk.
|
||||
|
@ -196,7 +216,8 @@ Data FileUtilsAndroid::getData(const std::string& filename, bool forString)
|
|||
unsigned char* data = nullptr;
|
||||
ssize_t size = 0;
|
||||
string fullPath = fullPathForFilename(filename);
|
||||
|
||||
cocosplay::updateAssets(fullPath);
|
||||
|
||||
if (fullPath[0] != '/')
|
||||
{
|
||||
string relativePath = string();
|
||||
|
@ -287,6 +308,7 @@ Data FileUtilsAndroid::getData(const std::string& filename, bool forString)
|
|||
else
|
||||
{
|
||||
ret.fastSet(data, size);
|
||||
cocosplay::notifyFileLoaded(fullPath);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -317,7 +339,8 @@ unsigned char* FileUtilsAndroid::getFileData(const std::string& filename, const
|
|||
}
|
||||
|
||||
string fullPath = fullPathForFilename(filename);
|
||||
|
||||
cocosplay::updateAssets(fullPath);
|
||||
|
||||
if (fullPath[0] != '/')
|
||||
{
|
||||
string relativePath = string();
|
||||
|
@ -388,7 +411,10 @@ unsigned char* FileUtilsAndroid::getFileData(const std::string& filename, const
|
|||
msg.append(filename).append(") failed!");
|
||||
CCLOG("%s", msg.c_str());
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
cocosplay::notifyFileLoaded(fullPath);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,6 +61,8 @@ public:
|
|||
/* override funtions */
|
||||
bool init();
|
||||
|
||||
virtual void purgeCachedEntries() override;
|
||||
|
||||
virtual std::string getNewFilename(const std::string &filename) const override;
|
||||
|
||||
/** @deprecated Please use FileUtils::getDataFromFile or FileUtils::getStringFromFile instead. */
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package com.chukong.cocosplay.client;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
public class CocosPlayClient {
|
||||
|
||||
public static boolean init(Activity activity, boolean isDemo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isDemo() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isNotifyFileLoadedEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void notifyFileLoaded(String filePath) {
|
||||
|
||||
}
|
||||
|
||||
public static void updateAssets(String filePath) {
|
||||
|
||||
}
|
||||
|
||||
public static String getGameRoot() {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static native String[] getSearchPaths();
|
||||
}
|
|
@ -28,6 +28,7 @@ import javax.microedition.khronos.egl.EGLConfig;
|
|||
import javax.microedition.khronos.egl.EGLDisplay;
|
||||
|
||||
import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener;
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
@ -216,6 +217,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
|
|||
@Override
|
||||
protected void onCreate(final Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
CocosPlayClient.init(this, false);
|
||||
|
||||
onLoadNativeLibraries();
|
||||
|
||||
|
|
|
@ -30,6 +30,8 @@ import java.util.LinkedHashSet;
|
|||
import java.util.Set;
|
||||
import java.lang.Runnable;
|
||||
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
@ -83,7 +85,13 @@ public class Cocos2dxHelper {
|
|||
Cocos2dxHelper.sCocos2dxHelperListener = (Cocos2dxHelperListener)activity;
|
||||
|
||||
Cocos2dxHelper.sPackageName = applicationInfo.packageName;
|
||||
Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath();
|
||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||
Cocos2dxHelper.sFileDirectory = CocosPlayClient.getGameRoot();
|
||||
}
|
||||
else {
|
||||
Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath();
|
||||
}
|
||||
|
||||
Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);
|
||||
|
||||
Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(activity);
|
||||
|
|
|
@ -27,6 +27,8 @@ package org.cocos2dx.lib;
|
|||
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.media.MediaPlayer;
|
||||
|
@ -245,6 +247,10 @@ public class Cocos2dxMusic {
|
|||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
|
||||
try {
|
||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||
CocosPlayClient.updateAssets(path);
|
||||
}
|
||||
CocosPlayClient.notifyFileLoaded(path);
|
||||
if (path.startsWith("/")) {
|
||||
final FileInputStream fis = new FileInputStream(path);
|
||||
mediaPlayer.setDataSource(fis.getFD());
|
||||
|
|
|
@ -31,6 +31,8 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
import android.media.SoundPool;
|
||||
|
@ -112,6 +114,10 @@ public class Cocos2dxSound {
|
|||
// ===========================================================
|
||||
|
||||
public int preloadEffect(final String path) {
|
||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||
CocosPlayClient.updateAssets(path);
|
||||
}
|
||||
CocosPlayClient.notifyFileLoaded(path);
|
||||
Integer soundID = this.mPathSoundIDMap.get(path);
|
||||
|
||||
if (soundID == null) {
|
||||
|
|
|
@ -37,6 +37,8 @@ import android.widget.MediaController.MediaPlayerControl;
|
|||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl {
|
||||
private String TAG = "VideoView";
|
||||
|
||||
|
@ -212,14 +214,18 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
|
|||
private String fileName = null;
|
||||
private String assetResourceRoot = "assets/";
|
||||
public void setVideoFileName(String path) {
|
||||
if (path.startsWith(assetResourceRoot)) {
|
||||
path = path.substring(assetResourceRoot.length());
|
||||
}
|
||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||
CocosPlayClient.updateAssets(path);
|
||||
}
|
||||
CocosPlayClient.notifyFileLoaded(path);
|
||||
if (path.startsWith("/")) {
|
||||
isAssetRouse = false;
|
||||
setVideoURI(Uri.parse(path),null);
|
||||
}
|
||||
else {
|
||||
if (path.startsWith(assetResourceRoot)) {
|
||||
path = path.substring(assetResourceRoot.length());
|
||||
}
|
||||
fileName = path;
|
||||
isAssetRouse = true;
|
||||
setVideoURI(Uri.parse(path),null);
|
||||
|
|
|
@ -10,6 +10,8 @@ import java.util.concurrent.Callable;
|
|||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import com.chukong.cocosplay.client.CocosPlayClient;
|
||||
|
||||
|
||||
public class Cocos2dxWebViewHelper {
|
||||
private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName();
|
||||
|
@ -164,6 +166,10 @@ public class Cocos2dxWebViewHelper {
|
|||
|
||||
@SuppressWarnings("unused")
|
||||
public static void loadFile(final int index, final String filePath) {
|
||||
if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
|
||||
CocosPlayClient.updateAssets(filePath);
|
||||
}
|
||||
CocosPlayClient.notifyFileLoaded(filePath);
|
||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
|
|
@ -0,0 +1,420 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#include "CocosPlayClient.h"
|
||||
#include "cocos2d.h"
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||
#include <map>
|
||||
#include "jni/JniHelper.h"
|
||||
#include "platform/CCCommon.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
#define LOG_TAG "CocosPlayClient.cpp"
|
||||
#if COCOS2D_DEBUG
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
|
||||
#else
|
||||
#define LOGD(...)
|
||||
#endif
|
||||
|
||||
static std::string __gameRootPath;
|
||||
static std::unordered_map<std::string, bool> __fileExistsCaches;
|
||||
static bool __isCocosPlayInited = false;
|
||||
static bool __isCocosPlayEnabled = false;
|
||||
static bool __isDemo = false;
|
||||
static bool __isNotifyFileLoadedEnabled = false;
|
||||
static jobject __classLoader;
|
||||
static jmethodID __findClassMethod;
|
||||
static pthread_key_t __threadKey;
|
||||
|
||||
|
||||
#define COCOSPLAYCLIENT_CLASS_NAME "com/chukong/cocosplay/client/CocosPlayClient"
|
||||
|
||||
extern "C" {
|
||||
jobjectArray Java_com_chukong_cocosplay_client_CocosPlayClient_getSearchPaths(JNIEnv* env, jobject thiz)
|
||||
{
|
||||
auto stringClass = env->FindClass("java/lang/String");
|
||||
auto& paths = cocos2d::FileUtils::getInstance()->getSearchPaths();
|
||||
auto count = paths.size();
|
||||
auto pathArray = env->NewObjectArray(count, stringClass, 0);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
env->SetObjectArrayElement(pathArray, i, env->NewStringUTF(paths[i].c_str()));
|
||||
}
|
||||
|
||||
return pathArray;
|
||||
}
|
||||
}
|
||||
|
||||
namespace cocosplay {
|
||||
|
||||
static void detachCurrentThread(void *env) {
|
||||
JniHelper::getJavaVM()->DetachCurrentThread();
|
||||
}
|
||||
|
||||
static bool getEnv(JNIEnv **env)
|
||||
{
|
||||
bool bRet = false;
|
||||
|
||||
switch(JniHelper::getJavaVM()->GetEnv((void**)env, JNI_VERSION_1_4))
|
||||
{
|
||||
case JNI_OK:
|
||||
bRet = true;
|
||||
break;
|
||||
case JNI_EDETACHED:
|
||||
pthread_key_create (&__threadKey, detachCurrentThread);
|
||||
if (JniHelper::getJavaVM()->AttachCurrentThread(env, 0) < 0)
|
||||
{
|
||||
LOGD("%s", "Failed to get the environment using AttachCurrentThread()");
|
||||
break;
|
||||
}
|
||||
if (pthread_getspecific(__threadKey) == NULL) {
|
||||
pthread_setspecific(__threadKey, env);
|
||||
}
|
||||
bRet = true;
|
||||
break;
|
||||
default:
|
||||
LOGD("%s", "Failed to get the environment using GetEnv()");
|
||||
break;
|
||||
}
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
static void initClassLoaderForMultiThread()
|
||||
{
|
||||
JNIEnv *env = 0;
|
||||
do
|
||||
{
|
||||
if (! getEnv(&env))
|
||||
{
|
||||
break;
|
||||
}
|
||||
jclass cocos2dClass = env->FindClass(COCOSPLAYCLIENT_CLASS_NAME);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
LOGD("Exception initClassLoaderForMultiThread cocos2dClass is exception");
|
||||
break;
|
||||
}
|
||||
|
||||
jclass classClass = env->GetObjectClass(cocos2dClass);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
LOGD("Exception initClassLoaderForMultiThread classClass is exception");
|
||||
break;
|
||||
}
|
||||
|
||||
jclass classLoaderClass = env->FindClass("java/lang/ClassLoader");
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
LOGD("Exception initClassLoaderForMultiThread classLoaderClass");
|
||||
break;
|
||||
}
|
||||
|
||||
jmethodID getClassLoaderMethod = env->GetMethodID(classClass,"getClassLoader","()Ljava/lang/ClassLoader;");
|
||||
jobject classLoader = env->CallObjectMethod(cocos2dClass, getClassLoaderMethod);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
LOGD("Exception initClassLoaderForMultiThread classLoader");
|
||||
break;
|
||||
}
|
||||
__classLoader = env->NewGlobalRef(classLoader);
|
||||
jmethodID findClassMethod = env->GetMethodID(classLoaderClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
||||
__findClassMethod = findClassMethod;
|
||||
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
__findClassMethod = NULL;
|
||||
__classLoader = NULL;
|
||||
LOGD("Exception initClassLoaderForMultiThread findClassMethod");
|
||||
break;
|
||||
}
|
||||
}while(0);
|
||||
}
|
||||
|
||||
static jclass getClassID_(const char *className, JNIEnv *env)
|
||||
{
|
||||
JNIEnv *pEnv = env;
|
||||
jclass ret = 0;
|
||||
|
||||
do
|
||||
{
|
||||
if (! pEnv)
|
||||
{
|
||||
if (! getEnv(&pEnv))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = pEnv->FindClass(className);
|
||||
if (! ret)
|
||||
{
|
||||
if(__classLoader)
|
||||
{
|
||||
pEnv->ExceptionClear();
|
||||
jstring jstrName = (pEnv)->NewStringUTF(className);
|
||||
ret = (jclass)pEnv->CallObjectMethod(__classLoader, __findClassMethod, jstrName);
|
||||
pEnv->DeleteLocalRef(jstrName);
|
||||
if(ret) break;
|
||||
}
|
||||
|
||||
LOGD("Failed to find class of %s", className);
|
||||
break;
|
||||
}
|
||||
} while (0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool getStaticMethodInfo(cocos2d::JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode)
|
||||
{
|
||||
jmethodID methodID = 0;
|
||||
JNIEnv *pEnv = 0;
|
||||
bool bRet = false;
|
||||
|
||||
do
|
||||
{
|
||||
if (! getEnv(&pEnv))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
jclass classID = getClassID_(className, pEnv);
|
||||
if(!classID) break;
|
||||
|
||||
methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode);
|
||||
if (! methodID)
|
||||
{
|
||||
LOGD("Failed to find static method id of %s", methodName);
|
||||
break;
|
||||
}
|
||||
|
||||
methodinfo.classID = classID;
|
||||
methodinfo.env = pEnv;
|
||||
methodinfo.methodID = methodID;
|
||||
|
||||
bRet = true;
|
||||
} while (0);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
void lazyInit()
|
||||
{
|
||||
if (__isCocosPlayInited)
|
||||
return;
|
||||
|
||||
JniMethodInfo t;
|
||||
if (JniHelper::getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "isEnabled", "()Z"))
|
||||
{
|
||||
__isCocosPlayEnabled = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
LOGD("isEnabled = %d", __isCocosPlayEnabled);
|
||||
}
|
||||
|
||||
if (JniHelper::getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "isDemo", "()Z"))
|
||||
{
|
||||
__isDemo = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
LOGD("isDemo = %d",__isDemo);
|
||||
}
|
||||
|
||||
if (JniHelper::getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "isNotifyFileLoadedEnabled", "()Z"))
|
||||
{
|
||||
__isNotifyFileLoadedEnabled = t.env->CallStaticBooleanMethod(t.classID, t.methodID);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
LOGD("isNotifyFileLoadedEnabled = %d", __isNotifyFileLoadedEnabled);
|
||||
}
|
||||
|
||||
initClassLoaderForMultiThread();
|
||||
|
||||
__isCocosPlayInited = true;
|
||||
}
|
||||
|
||||
bool isEnabled()
|
||||
{
|
||||
return __isCocosPlayEnabled;
|
||||
}
|
||||
|
||||
bool isDemo()
|
||||
{
|
||||
return __isDemo;
|
||||
}
|
||||
|
||||
void updateAssets(const std::string& filePath)
|
||||
{
|
||||
if (!__isCocosPlayInited)
|
||||
{
|
||||
lazyInit();
|
||||
}
|
||||
if (!__isCocosPlayEnabled)
|
||||
{
|
||||
LOGD("ERROR: CocosPlayClient isn't enabled!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (__isDemo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fileExists(filePath))
|
||||
{
|
||||
LOGD("file ( %s ) doesn't exist, updateAssets cancelled", filePath.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JniMethodInfo t;
|
||||
if (getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "updateAssets", "(Ljava/lang/String;)V"))
|
||||
{
|
||||
jstring stringArg = t.env->NewStringUTF(filePath.c_str());
|
||||
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg);
|
||||
t.env->DeleteLocalRef(stringArg);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
}
|
||||
|
||||
LOGD("updateAssets (%s) OK!", filePath.c_str());
|
||||
}
|
||||
|
||||
bool fileExists(const std::string& filePath)
|
||||
{
|
||||
auto iter = __fileExistsCaches.find(filePath);
|
||||
if (iter != __fileExistsCaches.end())
|
||||
{
|
||||
LOGD("Return file path ( %s ) in cache!", filePath.c_str());
|
||||
if(!iter->second)
|
||||
{
|
||||
auto fp = fopen(filePath.c_str(), "r");
|
||||
if (fp)
|
||||
{
|
||||
iter->second = true;
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
bool ret = false;
|
||||
JniMethodInfo t;
|
||||
if (getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "fileExists", "(Ljava/lang/String;)Z"))
|
||||
{
|
||||
jstring stringArg = t.env->NewStringUTF(filePath.c_str());
|
||||
ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg);
|
||||
t.env->DeleteLocalRef(stringArg);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
}
|
||||
|
||||
__fileExistsCaches[filePath] = ret;
|
||||
|
||||
LOGD("fileExists return (%d), path (%s)!", ret, filePath.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
void notifyFileLoaded(const std::string& filePath)
|
||||
{
|
||||
if (!__isNotifyFileLoadedEnabled)
|
||||
return;
|
||||
|
||||
JniMethodInfo t;
|
||||
if (getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "notifyFileLoaded", "(Ljava/lang/String;)V"))
|
||||
{
|
||||
jstring stringArg = t.env->NewStringUTF(filePath.c_str());
|
||||
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg);
|
||||
t.env->DeleteLocalRef(stringArg);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
}
|
||||
}
|
||||
|
||||
std::string getGameRoot()
|
||||
{
|
||||
if (!__isCocosPlayEnabled)
|
||||
{
|
||||
LOGD("ERROR: CocosPlayClient isn't enabled!");
|
||||
return "";
|
||||
}
|
||||
|
||||
if (__gameRootPath.empty())
|
||||
{
|
||||
JniMethodInfo t;
|
||||
if (JniHelper::getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "getGameRoot", "()Ljava/lang/String;"))
|
||||
{
|
||||
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
|
||||
__gameRootPath = JniHelper::jstring2string(str);
|
||||
t.env->DeleteLocalRef(str);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
}
|
||||
LOGD("GameRoot : %s", __gameRootPath.c_str());
|
||||
}
|
||||
return __gameRootPath;
|
||||
}
|
||||
|
||||
void purgeCachedEntries()
|
||||
{
|
||||
__fileExistsCaches.clear();
|
||||
}
|
||||
|
||||
void purgeCachedByFile(const std::string& filePath)
|
||||
{
|
||||
__fileExistsCaches.erase(filePath);
|
||||
}
|
||||
|
||||
void notifyDemoEnded()
|
||||
{
|
||||
JniMethodInfo t;
|
||||
if (JniHelper::getStaticMethodInfo(t, COCOSPLAYCLIENT_CLASS_NAME, "notifyDemoEnded", "()V"))
|
||||
{
|
||||
t.env->CallStaticVoidMethod(t.classID, t.methodID);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
LOGD("Game demo was ended!");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace cocosplay {
|
||||
|
||||
#else
|
||||
|
||||
namespace cocosplay {
|
||||
|
||||
bool isEnabled() { return false; }
|
||||
bool isDemo() { return false; }
|
||||
void updateAssets(const std::string& filePath) {}
|
||||
bool fileExists(const std::string& filePath) { return false; }
|
||||
void notifyFileLoaded(const std::string& filePath) {}
|
||||
std::string getGameRoot() { return ""; }
|
||||
void purgeCachedEntries() {}
|
||||
void notifyDemoEnded() {}
|
||||
void purgeCachedByFile(const std::string& filePath){}
|
||||
} // namespace cocosplay {
|
||||
|
||||
#endif
|
|
@ -0,0 +1,86 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
#ifndef __COCOSPLAYCLIENT_H__
|
||||
#define __COCOSPLAYCLIENT_H__
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cocosplay {
|
||||
|
||||
void lazyInit();
|
||||
|
||||
/**
|
||||
* Checks whether CocosPlay is enabled
|
||||
*/
|
||||
bool isEnabled();
|
||||
|
||||
/**
|
||||
* Checks whether CocosPlay is in demo mode
|
||||
*/
|
||||
bool isDemo();
|
||||
|
||||
/**
|
||||
* Updates assets by filePath, if the file doesn't exist, CocosPlay will show a progress page of downloading.
|
||||
* And this interface will be hung up until the scene package was downloaded.
|
||||
*/
|
||||
void updateAssets(const std::string& filePath);
|
||||
|
||||
/**
|
||||
* Checks whether the file exists
|
||||
*/
|
||||
bool fileExists(const std::string& filePath);
|
||||
|
||||
/**
|
||||
* Notifies to Cocos Play SDK that a file was loaded
|
||||
* It will do nothing if game doesn't run on Cocos Play
|
||||
*/
|
||||
void notifyFileLoaded(const std::string& filePath);
|
||||
|
||||
/**
|
||||
* Gets the resource root path of current game
|
||||
* @return A writable path of current game
|
||||
*/
|
||||
std::string getGameRoot();
|
||||
|
||||
/**
|
||||
* Purges the file searching cache.
|
||||
*
|
||||
* @note It should be invoked after the resources were updated.
|
||||
* For instance, it could be used when there is a small update in games.
|
||||
*/
|
||||
void purgeCachedEntries();
|
||||
|
||||
/**
|
||||
* Purges the file searching cache by giving file path.
|
||||
*/
|
||||
void purgeCachedByFile(const std::string& filePath);
|
||||
|
||||
/**
|
||||
* Notifies that the game demo was ended
|
||||
*/
|
||||
void notifyDemoEnded();
|
||||
|
||||
} // namespace cocosplay {
|
||||
|
||||
#endif // __COCOSPLAYCLIENT_H__
|
Loading…
Reference in New Issue