添加游戏启动参数实现命令行批量登录(cocos2dx)

1. 启动函数中添加两个方法

GameMenu.h
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    void cmdAutoLogin();                 //配置参数自动登录
    std::string converLPSTRToStr(LPWSTR lpcwszStr);//字符转换
#endif
GameMenu.cpp
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
void GameMenu::cmdAutoLogin()
{
    int argc = 0;
    LPWSTR *argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
    if (argc > 1)
    {
        _userName = converLPSTRToStr(argv[1]);
        _userPsd = converLPSTRToStr(argv[2]);
        _isAccountLogin = true;
        string passWord = MD5_CTX::MD5String(_userPsd);
        //账号登陆
        enterGame(_userName, passWord);
    }
    LocalFree(argv);
}

std::string GameMenu::converLPSTRToStr(LPWSTR lpcwszStr)
{
    std::string str;
    DWORD dwMinSize = 0;
    LPSTR lpszStr = NULL;
    dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
    if (0 == dwMinSize)
    {
        return "";
    }
    lpszStr = new char[dwMinSize];
    WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwMinSize, NULL, FALSE);
    str = lpszStr;
    delete[] lpszStr;
    lpszStr = NULL;
    return str;
}
#endif

2. 启动结束后添加调用

GameMenu::onEnterTransitionDidFinish()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    cmdAutoLogin();
#endif

3. 添加启动脚本

autologin.bat
set projectPath=MixProject/proj.win32/Debug.win32
start %projectPath%/update.exe test01 000000
rem rem 延迟一秒避免同时写入文件报错
@ping 127.0.0.1 -n 1
start %projectPath%/update.exe test02 000000
@ping 127.0.0.1 -n 1
start %projectPath%/update.exe test03 000000

你可能感兴趣的:(添加游戏启动参数实现命令行批量登录(cocos2dx))