void TwoDReplay::start() { if (file) return; file = fopen("data.tdr", "w"); // first: write header r2d_file_header_t header; memset(&header, 0, sizeof(header)); strncpy(header.fileformat, R2DFILEFORMAT, 8); header.num_rigs = BeamFactory::getSingleton().getTruckCount(); fwrite(&header, 1, sizeof(header), file); // second: write entry for every truck for (int t = 0; t < header.num_rigs; t++) { Beam *v = BeamFactory::getSingleton().getTruck(t); r2d_file_rig_t rig; memset(&rig, 0, sizeof(rig)); rig.num = t; if (v) { strncpy(rig.name, v->getTruckName().c_str(), std::min<int>(v->getTruckName().size(), 255)); strncpy(rig.filename, v->getTruckFileName().c_str(), std::min<int>(v->getTruckFileName().size(), 255)); strncpy(rig.hash, v->getTruckHash().c_str(), std::min<int>(v->getTruckHash().size(), 255)); rig.num_wheels = v->free_wheel; } fwrite(&rig, 1, sizeof(rig), file); // write wheel setup if (v && rig.num_wheels > 0) { r2d_file_wheel_setup_t ws; for (int w=0; w < rig.num_wheels; w++) { memset(&ws, 0, sizeof(ws)); ws.width = v->wheels[w].width; ws.radius = v->wheels[w].radius; ws.type = v->wheels[w].propulsed; fwrite(&ws, 1, sizeof(ws), file); } } } // third: header done, now the data }
int GameScript::useOnlineAPIDirectly(OnlineAPIParams_t params) { #ifdef USE_CURL struct curlMemoryStruct chunk; chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */ chunk.size = 0; /* no data at this point */ // construct post fields struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; curl_global_init(CURL_GLOBAL_ALL); std::map<String, AngelScript::CScriptDictionary::valueStruct>::const_iterator it; for (it = params.dict->dict.begin(); it != params.dict->dict.end(); it++) { int typeId = it->second.typeId; if (typeId == mse->getEngine()->GetTypeIdByDecl("string")) { // its a String String *str = (String *)it->second.valueObj; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, str->c_str(), CURLFORM_END); } else if (typeId == AngelScript::asTYPEID_INT8 \ || typeId == AngelScript::asTYPEID_INT16 \ || typeId == AngelScript::asTYPEID_INT32 \ || typeId == AngelScript::asTYPEID_INT64) { // its an integer curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, TOSTRING((int)it->second.valueInt).c_str(), CURLFORM_END); } else if (typeId == AngelScript::asTYPEID_UINT8 \ || typeId == AngelScript::asTYPEID_UINT16 \ || typeId == AngelScript::asTYPEID_UINT32 \ || typeId == AngelScript::asTYPEID_UINT64) { // its an unsigned integer curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, TOSTRING((unsigned int)it->second.valueInt).c_str(), CURLFORM_END); } else if (typeId == AngelScript::asTYPEID_FLOAT || typeId == AngelScript::asTYPEID_DOUBLE) { // its a float or double curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, it->first.c_str(), CURLFORM_COPYCONTENTS, TOSTRING((float)it->second.valueFlt).c_str(), CURLFORM_END); } } // add some hard coded values //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_Name", CURLFORM_COPYCONTENTS, gEnv->frameListener->terrainName.c_str(), CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_FileName", CURLFORM_COPYCONTENTS, gEnv->frameListener->terrainFileName.c_str(), CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_FileHash", CURLFORM_COPYCONTENTS, gEnv->frameListener->terrainFileHash.c_str(), CURLFORM_END); //curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_ModHash", CURLFORM_COPYCONTENTS, gEnv->frameListener->terrainModHash.c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_ScriptName", CURLFORM_COPYCONTENTS, mse->getScriptName().c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "terrain_ScriptHash", CURLFORM_COPYCONTENTS, mse->getScriptHash().c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "User_NickName", CURLFORM_COPYCONTENTS, SSETTING("Nickname", "Anonymous").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "User_Language", CURLFORM_COPYCONTENTS, SSETTING("Language", "English").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "User_Token", CURLFORM_COPYCONTENTS, SSETTING("User Token Hash", "-").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "RoR_VersionString", CURLFORM_COPYCONTENTS, ROR_VERSION_STRING, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "RoR_ProtocolVersion", CURLFORM_COPYCONTENTS, RORNET_VERSION, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "RoR_BinaryHash", CURLFORM_COPYCONTENTS, SSETTING("BinaryHash", "-").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "RoR_GUID", CURLFORM_COPYCONTENTS, SSETTING("GUID", "-").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "MP_ServerName", CURLFORM_COPYCONTENTS, SSETTING("Server name", "-").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "MP_ServerPort", CURLFORM_COPYCONTENTS, SSETTING("Server port", "-").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "MP_NetworkEnabled", CURLFORM_COPYCONTENTS, SSETTING("Network enable", "No").c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "APIProtocolVersion", CURLFORM_COPYCONTENTS, "2", CURLFORM_END); if (BeamFactory::getSingleton().getCurrentTruck()) { Beam *truck = BeamFactory::getSingleton().getCurrentTruck(); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "Truck_Name", CURLFORM_COPYCONTENTS, truck->getTruckName().c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "Truck_FileName", CURLFORM_COPYCONTENTS, truck->getTruckFileName().c_str(), CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "Truck_Hash", CURLFORM_COPYCONTENTS, truck->getTruckHash().c_str(), CURLFORM_END); // look for any locked trucks int i = 0; for (std::vector<hook_t>::iterator it = truck->hooks.begin(); it != truck->hooks.end(); it++, i++) { Beam *trailer = it->lockTruck; if (trailer && trailer->getTruckName() != trailer->getTruckName()) { String name = "Trailer_" + TOSTRING(i) + "_Name"; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, name.c_str(), CURLFORM_COPYCONTENTS, trailer->getTruckName().c_str(), CURLFORM_END); String filename = "Trailer_" + TOSTRING(i) + "_FileName"; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, filename.c_str(), CURLFORM_COPYCONTENTS, trailer->getTruckFileName().c_str(), CURLFORM_END); String hash = "Trailer_" + TOSTRING(i) + "_Hash"; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, hash.c_str(), CURLFORM_COPYCONTENTS, trailer->getTruckHash().c_str(), CURLFORM_END); } } curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "Trailer_Count", CURLFORM_COPYCONTENTS, TOSTRING(i).c_str(), CURLFORM_END); } const RenderTarget::FrameStats& stats = RoR::Application::GetOgreSubsystem()->GetRenderWindow()->getStatistics(); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "AVG_FPS", CURLFORM_COPYCONTENTS, TOSTRING(stats.avgFPS).c_str(), CURLFORM_END); CURLcode res; CURL *curl = curl_easy_init(); if (!curl) { LOG("ERROR: failed to init curl"); return 1; } char *curl_err_str[CURL_ERROR_SIZE]; memset(curl_err_str, 0, CURL_ERROR_SIZE); String url = "http://" + String(REPO_SERVER) + params.apiquery; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); /* send all data to this function */ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteMemoryCallback); /* we pass our 'chunk' struct to the callback function */ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // set post options curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); // logging stuff //curl_easy_setopt(curl, CURLOPT_STDERR, LogManager::getsin InstallerLog::getSingleton()->getLogFilePtr()); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str[0]); // http related settings curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // follow redirects curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // set the Referrer: field in requests where it follows a Location: redirect. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20); curl_easy_setopt(curl, CURLOPT_USERAGENT, "RoR"); curl_easy_setopt(curl, CURLOPT_FILETIME, 1); // TO BE DONE: ADD SSL // see: http://curl.haxx.se/libcurl/c/simplessl.html // curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,1L); res = curl_easy_perform(curl); curl_easy_cleanup(curl); //printf("%lu bytes retrieved\n", (long)chunk.size); curl_formfree(formpost); String result; if (chunk.memory) { // convert memory into String now result = String(chunk.memory); // then free free(chunk.memory); } /* we're done with libcurl, so clean it up */ curl_global_cleanup(); if (res != CURLE_OK) { const char *errstr = curl_easy_strerror(res); result = "ERROR: " + String(errstr); } LOG("online API result: " + result); #ifdef USE_MYGUI Console *con = RoR::Application::GetConsole(); if (con) { con->putMessage(Console::CONSOLE_MSGTYPE_HIGHSCORE, Console::CONSOLE_SYSTEM_NOTICE, ANSI_TO_UTF(result)); RoR::Application::GetGuiManager()->PushNotification("Script:", ANSI_TO_UTF(result)); } #endif // USE_MYGUI #endif //USE_CURL return 0; }