2014-01-07 11:47:11 +08:00
|
|
|
/***************************************************************************
|
|
|
|
Copyright (c) 2012 Zynga Inc.
|
2013-01-07 18:06:53 +08:00
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologic Inc.
|
2013-01-07 18:06:53 +08:00
|
|
|
|
2013-01-07 17:22:06 +08:00
|
|
|
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.
|
2014-01-07 11:47:11 +08:00
|
|
|
***************************************************************************/
|
2013-01-07 17:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Storage support for the JS Bindings for iOS.
|
|
|
|
Works on cocos2d-iphone and cocos2d-x.
|
|
|
|
*/
|
|
|
|
|
2014-01-20 23:01:44 +08:00
|
|
|
#include "LocalStorage.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCPlatformMacros.h"
|
2013-01-07 17:22:06 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "jni.h"
|
|
|
|
#include "jni/JniHelper.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
static int _initialized = 0;
|
|
|
|
|
|
|
|
static void splitFilename (std::string& str)
|
|
|
|
{
|
|
|
|
size_t found = 0;
|
|
|
|
found=str.find_last_of("/\\");
|
|
|
|
if (found != std::string::npos)
|
|
|
|
{
|
|
|
|
str = str.substr(found+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageInit( const std::string& fullpath)
|
2013-01-07 17:22:06 +08:00
|
|
|
{
|
2014-01-20 22:45:40 +08:00
|
|
|
if (fullpath.empty())
|
2014-01-20 22:31:56 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( ! _initialized )
|
|
|
|
{
|
|
|
|
JniMethodInfo t;
|
|
|
|
|
|
|
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "init", "(Ljava/lang/String;Ljava/lang/String;)Z")) {
|
|
|
|
std::string strDBFilename = fullpath;
|
|
|
|
splitFilename(strDBFilename);
|
|
|
|
jstring jdbName = t.env->NewStringUTF(strDBFilename.c_str());
|
|
|
|
jstring jtableName = t.env->NewStringUTF("data");
|
|
|
|
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, jdbName, jtableName);
|
|
|
|
t.env->DeleteLocalRef(jdbName);
|
|
|
|
t.env->DeleteLocalRef(jtableName);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
if (ret) {
|
|
|
|
_initialized = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 17:22:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void localStorageFree()
|
|
|
|
{
|
|
|
|
if( _initialized ) {
|
|
|
|
|
|
|
|
JniMethodInfo t;
|
|
|
|
|
|
|
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "destory", "()V"))
|
|
|
|
{
|
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialized = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** sets an item in the LS */
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageSetItem( const std::string& key, const std::string& value)
|
2013-01-07 17:22:06 +08:00
|
|
|
{
|
|
|
|
assert( _initialized );
|
|
|
|
|
|
|
|
JniMethodInfo t;
|
|
|
|
|
|
|
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "setItem", "(Ljava/lang/String;Ljava/lang/String;)V")) {
|
2014-01-20 22:31:56 +08:00
|
|
|
jstring jkey = t.env->NewStringUTF(key.c_str());
|
|
|
|
jstring jvalue = t.env->NewStringUTF(value.c_str());
|
2013-01-07 17:22:06 +08:00
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey, jvalue);
|
|
|
|
t.env->DeleteLocalRef(jkey);
|
|
|
|
t.env->DeleteLocalRef(jvalue);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** gets an item from the LS */
|
2014-01-20 22:31:56 +08:00
|
|
|
std::string localStorageGetItem( const std::string& key )
|
2013-01-07 17:22:06 +08:00
|
|
|
{
|
|
|
|
assert( _initialized );
|
|
|
|
JniMethodInfo t;
|
2014-01-20 22:31:56 +08:00
|
|
|
|
|
|
|
std::string ret;
|
2013-01-07 17:22:06 +08:00
|
|
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) {
|
2014-01-20 22:31:56 +08:00
|
|
|
jstring jkey = t.env->NewStringUTF(key.c_str());
|
|
|
|
jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);
|
|
|
|
ret = JniHelper::jstring2string(jret);
|
|
|
|
t.env->DeleteLocalRef(jret);
|
2013-01-07 17:22:06 +08:00
|
|
|
t.env->DeleteLocalRef(jkey);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
2014-01-20 22:31:56 +08:00
|
|
|
return ret;
|
2013-01-07 17:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** removes an item from the LS */
|
2014-01-20 22:31:56 +08:00
|
|
|
void localStorageRemoveItem( const std::string& key )
|
2013-01-07 17:22:06 +08:00
|
|
|
{
|
|
|
|
assert( _initialized );
|
|
|
|
JniMethodInfo t;
|
|
|
|
|
|
|
|
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "removeItem", "(Ljava/lang/String;)V")) {
|
2014-01-20 22:31:56 +08:00
|
|
|
jstring jkey = t.env->NewStringUTF(key.c_str());
|
2013-01-07 17:22:06 +08:00
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey);
|
|
|
|
t.env->DeleteLocalRef(jkey);
|
|
|
|
t.env->DeleteLocalRef(t.classID);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|