Add setEventTrackingEnable for event tracking

This commit is contained in:
yinjimmy 2015-01-12 15:18:30 +08:00
parent dea3cf9863
commit 53cc07a2f6
6 changed files with 79 additions and 306 deletions

View File

@ -58,6 +58,7 @@ bool AppDelegate::applicationDidFinishLaunching()
//register_custom_function(stack->getLuaState());
// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
RuntimeEngine::getInstance()->setEventTrackingEnable(true);
RuntimeEngine::getInstance()->start();
cocos2d::log("iShow!");
@ -79,4 +80,3 @@ void AppDelegate::applicationWillEnterForeground()
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

View File

@ -35,6 +35,12 @@ THE SOFTWARE.
#include "RuntimeLuaImpl.h"
#include "RuntimeCCSImpl.h"
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
#include "DeviceEx.h"
#include "network/CCHTTPRequest.h"
#include "xxhash/xxhash.h"
#endif
#include <vector>
std::string g_projectPath;
@ -107,7 +113,8 @@ void resetDesignResolution()
RuntimeEngine::RuntimeEngine()
: _runtime(nullptr)
, _call(nullptr)
, _eventTrackingEnable(false)
, _launchEvent("empty")
{
}
@ -141,21 +148,25 @@ void RuntimeEngine::setupRuntime()
if ((entryFile.rfind(".lua") != std::string::npos) ||
(entryFile.rfind(".luac") != std::string::npos))
{
_launchEvent = "lua";
_runtime = RuntimeLuaImpl::create();
}
// Js
else if ((entryFile.rfind(".js") != std::string::npos) ||
(entryFile.rfind(".jsc") != std::string::npos))
{
_launchEvent = "js";
}
// csb
else if ((entryFile.rfind(".csb") != std::string::npos))
{
_launchEvent = "ccs";
_runtime = RuntimeCCSImpl::create();
}
// csd
else if ((entryFile.rfind(".csd") != std::string::npos))
{
_launchEvent = "ccs";
_runtime = RuntimeCCSImpl::create();
}
}
@ -204,10 +215,13 @@ void RuntimeEngine::setProjectPath(const std::string &workPath)
void RuntimeEngine::startScript(const std::string &args)
{
resetDesignResolution();
if (_runtime)
{
_runtime->startScript(args);
}
trackLaunchEvent();
}
void RuntimeEngine::start()
@ -238,8 +252,8 @@ void RuntimeEngine::start()
//
if (_project.getDebuggerType() == kCCRuntimeDebuggerNone)
{
_call = StartupCall::create();
_call->startup();
setupRuntime();
startScript(_project.getScriptFileRealPath());
}
else
{
@ -259,6 +273,11 @@ void RuntimeEngine::end()
// FileServer::purge();
}
void RuntimeEngine::setEventTrackingEnable(bool enable)
{
_eventTrackingEnable = enable;
}
RuntimeProtocol* RuntimeEngine::getRuntime()
{
return _runtime;
@ -299,4 +318,51 @@ void RuntimeEngine::updateConfigParser()
parser->setEntryFile(entryFile);
parser->setBindAddress(_project.getBindAddress());
}
}
//
// NOTE: track event on windows / mac platform
//
void RuntimeEngine::trackEvent(const std::string &eventName)
{
if (!_eventTrackingEnable)
{
return ;
}
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
const char *platform = "win";
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
const char *platform = "mac";
#else
const char *platform = "UNKNOWN";
#endif
char cidBuf[64] = {0};
auto guid = player::DeviceEx::getInstance()->getUserGUID();
snprintf(cidBuf, sizeof(cidBuf), "%x", XXH32(guid.c_str(), (int)guid.length(), 0));
auto request = extra::HTTPRequest::createWithUrl(NULL,
"http://www.google-analytics.com/collect",
kCCHTTPRequestMethodPOST);
request->addPOSTValue("v", "1");
request->addPOSTValue("tid", "UA-58200293-1");
request->addPOSTValue("cid", cidBuf);
request->addPOSTValue("t", "event");
request->addPOSTValue("an", "simulator");
request->addPOSTValue("av", cocos2dVersion());
request->addPOSTValue("ec", platform);
request->addPOSTValue("ea", eventName.c_str());
request->start();
#endif // ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
}
void RuntimeEngine::trackLaunchEvent()
{
trackEvent(_launchEvent);
}

View File

@ -45,7 +45,7 @@ const char* getRuntimeVersion();
//
// RuntimeEngine
//
#include "StartCall.h"
#include "ProjectConfig/ProjectConfig.h"
class RuntimeProtocol;
class RuntimeEngine
{
@ -60,6 +60,7 @@ public:
void startScript(const std::string &args);
void start();
void end();
void setEventTrackingEnable(bool enable);
RuntimeProtocol *getRuntime();
private:
@ -68,9 +69,14 @@ private:
void showUI();
void updateConfigParser();
//
void trackEvent(const std::string &eventName);
void trackLaunchEvent();
RuntimeProtocol *_runtime;
StartupCall *_call;
ProjectConfig _project;
bool _eventTrackingEnable; // false default
std::string _launchEvent;
};
#endif // _RUNTIME__H_

View File

@ -1,250 +0,0 @@
//
// StartCall.cpp
// simulator
//
//
#include "StartCall.h"
#include "CCLuaEngine.h"
#include "cocostudio/CocoStudio.h"
#include "Runtime.h"
#include "ConfigParser.h"
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
#include "DeviceEx.h"
#include "network/CCHTTPRequest.h"
#endif
#include "xxhash/xxhash.h"
USING_NS_CC;
// ----------------------------------------
StartupCall *StartupCall::create()
{
StartupCall *call = new StartupCall();
call->autorelease();
return call;
}
StartupCall::StartupCall()
: _launchEvent("empty")
{
}
static bool endWithString(const std::string &buf, const std::string &suffix)
{
return ((buf.find(suffix) + suffix.length()) == buf.length());
}
void StartupCall::startup()
{
auto engine = LuaEngine::getInstance();
auto stack = engine->getLuaStack();
const ProjectConfig &project = RuntimeEngine::getInstance()->getProjectConfig();
// set search path
string path = FileUtils::getInstance()->fullPathForFilename(project.getScriptFileRealPath().c_str());
size_t pos;
while ((pos = path.find_first_of("\\")) != std::string::npos)
{
path.replace(pos, 1, "/");
}
size_t p = path.find_last_of("/");
string workdir;
if (p != path.npos)
{
workdir = path.substr(0, p);
stack->addSearchPath(workdir.c_str());
FileUtils::getInstance()->addSearchPath(workdir);
}
// update search pathes
FileUtils::getInstance()->addSearchPath(project.getProjectDir());
auto &customizedPathes = project.getSearchPath();
for (auto &path : customizedPathes)
{
FileUtils::getInstance()->addSearchPath(path);
}
updateConfigParser(project);
updatePreviewFuncForPath(path);
// launch
if (project.getDebuggerType() == kCCRuntimeDebuggerNone)
{
_previewFunc(path);
}
// track start event
trackLaunchEvent();
}
//
// NOTE: track event on windows / mac platform
//
void StartupCall::trackEvent(const char *eventName)
{
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
const char *platform = "win";
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
const char *platform = "mac";
#else
const char *platform = "UNKNOWN";
#endif
char cidBuf[56] = {0};
auto guid = player::DeviceEx::getInstance()->getUserGUID();
snprintf(cidBuf, sizeof(cidBuf), "%x", XXH32(guid.c_str(), (int)guid.length(), 0));
auto request = extra::HTTPRequest::createWithUrl(NULL,
"http://www.google-analytics.com/collect",
kCCHTTPRequestMethodPOST);
request->addPOSTValue("v", "1");
request->addPOSTValue("tid", "UA-58200293-1");
request->addPOSTValue("cid", cidBuf);
request->addPOSTValue("t", "event");
request->addPOSTValue("an", "simulator");
request->addPOSTValue("av", cocos2dVersion());
request->addPOSTValue("ec", platform);
request->addPOSTValue("ea", eventName);
request->start();
#endif // ((CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC))
}
void StartupCall::trackLaunchEvent()
{
trackEvent(_launchEvent.c_str());
}
void StartupCall::onPreviewCocosCSD(const std::string &path)
{
std::string filepath = path;
if (filepath.empty())
{
filepath = ConfigParser::getInstance()->getEntryFile();
}
CCLOG("------------------------------------------------");
CCLOG("LOAD Cocos Studio FILE (.csd): %s", filepath.c_str());
CCLOG("------------------------------------------------");
auto node = CSLoader::getInstance()->createNodeWithFlatBuffersForSimulator(filepath.c_str());
auto action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersForSimulator(filepath.c_str());
if (action)
{
node->runAction(action);
action->gotoFrameAndPlay(0);
}
if (node)
{
if (Director::getInstance()->getRunningScene())
{
auto scene = Scene::create();
scene->addChild(node);
Director::getInstance()->replaceScene(scene);
}
else
{
auto scene = Scene::create();
scene->addChild(node);
Director::getInstance()->runWithScene(scene);
}
}
}
void StartupCall::onPreviewCocosCSB(const std::string &path)
{
std::string filepath = path;
if (filepath.empty())
{
filepath = ConfigParser::getInstance()->getEntryFile();
}
CCLOG("\n------------------------------------------------\n");
CCLOG("[WARNING]: using **SUITABLE** Cocos Studio generate csb file!!");
CCLOG("LOAD Cocos Studio FILE (.csb): %s", filepath.c_str());
CCLOG("\n------------------------------------------------\n");
auto node = CSLoader::getInstance()->createNode(filepath);
if (node)
{
if (Director::getInstance()->getRunningScene())
{
auto scene = Scene::create();
scene->addChild(node);
Director::getInstance()->replaceScene(scene);
}
else
{
auto scene = Scene::create();
scene->addChild(node);
Director::getInstance()->runWithScene(scene);
}
}
}
void StartupCall::onPreviewLua(const std::string &path)
{
CCLOG("------------------------------------------------");
CCLOG("LOAD Lua FILE: %s", path.c_str());
CCLOG("------------------------------------------------");
LuaEngine::getInstance()->executeScriptFile(path.c_str());
}
void StartupCall::onPreviewJs(const std::string &path)
{
CCLOG("------------------------------------------------");
CCLOG("LOAD Js FILE: %s", path.c_str());
CCLOG("------------------------------------------------");
CCLOG("TODO: ");
}
void StartupCall::updateConfigParser(const ProjectConfig& project)
{
// set entry file
auto parser = ConfigParser::getInstance();
string entryFile(project.getScriptFileRealPath());
if (entryFile.find(project.getProjectDir()) != string::npos)
{
entryFile.erase(0, project.getProjectDir().length());
}
std::replace(entryFile.begin(), entryFile.end(), '\\', '/');
parser->setEntryFile(entryFile);
parser->setBindAddress(project.getBindAddress());
}
void StartupCall::updatePreviewFuncForPath(const std::string &path)
{
// set loader
_previewFunc = [](const std::string &path) { };
if (endWithString(path, ".lua") || endWithString(path, ".luac"))
{
_launchEvent = "lua";
_previewFunc = std::bind(&StartupCall::onPreviewLua, this, std::placeholders::_1);
}
else if (endWithString(path, ".csd"))
{
_launchEvent = "ccs";
_previewFunc = std::bind(&StartupCall::onPreviewCocosCSD, this, std::placeholders::_1);
}
else if (endWithString(path, ".csb"))
{
_launchEvent = "ccs";
_previewFunc = std::bind(&StartupCall::onPreviewCocosCSB, this, std::placeholders::_1);
}
else if (endWithString(path, ".js") || endWithString(path, ".jsc"))
{
_launchEvent = "js";
}
}

View File

@ -1,39 +0,0 @@
//
// StartCall.h
// simulator
//
//
#ifndef __simulator__StartCall__
#define __simulator__StartCall__
#include "cocos2d.h"
#include "ProjectConfig/ProjectConfig.h"
#include "ProjectConfig/SimulatorConfig.h"
class StartupCall : public cocos2d::Ref
{
public:
static StartupCall *create();
void startup();
private:
StartupCall();
void trackEvent(const char *eventName);
void trackLaunchEvent();
void onPreviewCocosCSD(const std::string &path);
void onPreviewCocosCSB(const std::string &path);
void onPreviewLua(const std::string &path);
void onPreviewJs(const std::string &path);
void updateConfigParser(const ProjectConfig& project);
void updatePreviewFuncForPath(const std::string &path);
private:
std::function<void(const std::string &)> _previewFunc;
std::string _launchEvent;
};
#endif /* defined(__simulator__StartCall__) */

View File

@ -18,9 +18,6 @@
9F2F21C51A635F1C006B8BF1 /* RuntimeProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 9F2F21BC1A635F1C006B8BF1 /* RuntimeProtocol.h */; };
9F2F21C71A635FFF006B8BF1 /* Runtime_ios-mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9F2F21C61A635FFF006B8BF1 /* Runtime_ios-mac.mm */; };
9F2F21C81A635FFF006B8BF1 /* Runtime_ios-mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9F2F21C61A635FFF006B8BF1 /* Runtime_ios-mac.mm */; };
9F2F21CF1A63640B006B8BF1 /* StartCall.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9F2F21CD1A63640B006B8BF1 /* StartCall.cpp */; };
9F2F21D01A63640B006B8BF1 /* StartCall.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9F2F21CD1A63640B006B8BF1 /* StartCall.cpp */; };
9F2F21D11A63640B006B8BF1 /* StartCall.h in Headers */ = {isa = PBXBuildFile; fileRef = 9F2F21CE1A63640B006B8BF1 /* StartCall.h */; };
9FB638641A635BA300AAEC43 /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = 9FB638201A635BA300AAEC43 /* config.h */; };
9FB638651A635BA300AAEC43 /* extension_set.cc in Sources */ = {isa = PBXBuildFile; fileRef = 9FB638231A635BA300AAEC43 /* extension_set.cc */; };
9FB638661A635BA300AAEC43 /* extension_set.cc in Sources */ = {isa = PBXBuildFile; fileRef = 9FB638231A635BA300AAEC43 /* extension_set.cc */; };
@ -178,8 +175,6 @@
9F2F21BB1A635F1C006B8BF1 /* RuntimeProtocol.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuntimeProtocol.cpp; sourceTree = "<group>"; };
9F2F21BC1A635F1C006B8BF1 /* RuntimeProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuntimeProtocol.h; sourceTree = "<group>"; };
9F2F21C61A635FFF006B8BF1 /* Runtime_ios-mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "Runtime_ios-mac.mm"; sourceTree = "<group>"; };
9F2F21CD1A63640B006B8BF1 /* StartCall.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StartCall.cpp; sourceTree = "<group>"; };
9F2F21CE1A63640B006B8BF1 /* StartCall.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StartCall.h; sourceTree = "<group>"; };
9F7214351A5C271F00DAED06 /* liblibsimulator.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblibsimulator.a; sourceTree = BUILT_PRODUCTS_DIR; };
9F7214851A5C28BA00DAED06 /* liblibsimulator_iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = liblibsimulator_iOS.a; sourceTree = BUILT_PRODUCTS_DIR; };
9FB638201A635BA300AAEC43 /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
@ -428,8 +423,6 @@
9FB6385E1A635BA300AAEC43 /* ResData.h */,
9FB6385F1A635BA300AAEC43 /* Runtime.cpp */,
9FB638601A635BA300AAEC43 /* Runtime.h */,
9F2F21CD1A63640B006B8BF1 /* StartCall.cpp */,
9F2F21CE1A63640B006B8BF1 /* StartCall.h */,
9F2F21B71A635F1C006B8BF1 /* RuntimeCCSImpl.cpp */,
9F2F21B81A635F1C006B8BF1 /* RuntimeCCSImpl.h */,
9F2F21B91A635F1C006B8BF1 /* RuntimeLuaImpl.cpp */,
@ -576,7 +569,6 @@
9FB638B71A635BA300AAEC43 /* Runtime.h in Headers */,
9FF504BD1A5EA75D00AFDA55 /* PlayerSettings.h in Headers */,
9FF504B61A5EA75D00AFDA55 /* PlayerMenuServiceProtocol.h in Headers */,
9F2F21D11A63640B006B8BF1 /* StartCall.h in Headers */,
9FD6FC361A5D281D0028EDC6 /* ProjectConfig.h in Headers */,
9FB6386A1A635BA300AAEC43 /* generated_message_util.h in Headers */,
9FD6FC651A5D2A380028EDC6 /* PlayerFileDialogServiceMac.h in Headers */,
@ -719,7 +711,6 @@
9F2F21C71A635FFF006B8BF1 /* Runtime_ios-mac.mm in Sources */,
9FB6386B1A635BA300AAEC43 /* coded_stream.cc in Sources */,
9FF504B51A5EA75D00AFDA55 /* PlayerMenuServiceProtocol.cpp in Sources */,
9F2F21CF1A63640B006B8BF1 /* StartCall.cpp in Sources */,
9FF504AD1A5EA75D00AFDA55 /* AppEvent.cpp in Sources */,
9FB638A61A635BA300AAEC43 /* Landscape_png.cpp in Sources */,
9FD6FC601A5D2A380028EDC6 /* DeviceEx-mac.mm in Sources */,
@ -778,7 +769,6 @@
9F2F21BE1A635F1C006B8BF1 /* RuntimeCCSImpl.cpp in Sources */,
9FB638A71A635BA300AAEC43 /* Landscape_png.cpp in Sources */,
9FB638921A635BA300AAEC43 /* stringprintf.cc in Sources */,
9F2F21D01A63640B006B8BF1 /* StartCall.cpp in Sources */,
9FB638971A635BA300AAEC43 /* wire_format_lite.cc in Sources */,
9F2F21C11A635F1C006B8BF1 /* RuntimeLuaImpl.cpp in Sources */,
);