void TMaps::LoadRespPoints(const QString& fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { throw UException("Error opening " + fileName); } QTextStream in(&file); QString line = ""; while (!line.isNull()) { line = in.readLine(); if (line.isEmpty() || line[0] == '#') { continue; } QStringList params = line.split(":"); if (params.size() != 8) { throw UException("Error parsing " + fileName); } TRespPoint point; //x:y:capture_radius:spawn_radius:is_capturable:is_main:capture_time:team point.X = utils::FromString(params[0]); point.Y = utils::FromString(params[1]); point.CaptureRadius = utils::FromString(params[2]); point.SpawnRadius = utils::FromString(params[3]); point.IsCapturable = utils::FromString(params[4]); point.IsMain = utils::FromString(params[5]); point.CaptureTime = utils::FromString(params[6]); point.Team = utils::FromString(params[7]) ? T_One : T_Second; RespPoints.push_back(point); } }
void USettings::Load(const QString& fname, const QStringList& required) { Path = fname; QFile file(fname); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { throw UException("Error opening file " + fname); } QTextStream in(&file); QString line = ""; while (!line.isNull()) { line = in.readLine().trimmed(); if (line.isEmpty() || line[0] == '#') { continue; } QStringList acc = line.split("="); if (acc.size() != 2) { continue; } Parameters.insert(acc[0].trimmed(), acc[1].trimmed()); } for (auto i = required.begin(); i != required.end(); i++) { if (Parameters.find(*i) == Parameters.end()) { throw UException("Required parameter " + *i + " not found"); } } }
void TMaps::LoadObjects(const QString& fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { throw UException("Error opening " + fileName); } QTextStream in(&file); QString line = ""; while (!line.isNull()) { line = in.readLine(); if (line.isEmpty() || line[0] == '#') { continue; } QStringList objectParams = line.split(":"); if (objectParams.size() != 4) { throw UException("Error parsing " + fileName); } int id = utils::FromString(objectParams[3]); int x = utils::FromString(objectParams[0]); int y = utils::FromString(objectParams[1]); double angle = utils::FromString(objectParams[2]); emit SpawnObject(id, x, y, angle); } }
//------------------------------------------------------------------------------ void TObjects::LoadObjects(const QString& fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { throw UException("Error opening file " + fileName); } QTextStream in(&file); QString line = ""; while (!line.isNull()) { line = in.readLine(); if (line.isEmpty() || line[0] == '#') { continue; } QStringList objParams = line.split(":"); if (objParams.size() != 5) { throw UException("Error loading objects from " + fileName); } QString objName = objParams[4]; bool ok = true; size_t id = objParams[0].toInt(&ok); if (!ok) { throw UException("Error loading objects from " + fileName); } QImage* image = new QImage("objects/" + objName + ".png"); Images.insert(objName, image); ImagesById.insert(id, image); } }
void TObjects::LoadObjects(const QString &fileName) { QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { throw UException("Error opening file " + file.fileName()); } QTextStream in(&file); QString line = ""; while (!line.isNull()) { line = in.readLine(); if (line.isEmpty() || line[0] == '#') { continue; } QStringList objParams = line.split(":"); if (objParams.size() != 5) { throw UException("Error loading objects from " + fileName); } bool ok = true; size_t id = objParams[0].toInt(&ok); if (!ok) { throw UException("Error loading objects from " + fileName); } QPoint pos; pos.setX(objParams[1].toInt(&ok)); if (!ok) { throw UException("Error loading objects from " + fileName); } pos.setY(objParams[2].toInt(&ok)); if (!ok) { throw UException("Error loading objects from " + fileName); } bool isDynamic = objParams[3] == "1" ? true : false; Objects.insert(id, pos); ObjectsIsDyn.insert(id, isDynamic); } }
vector<TSrvRecord> GetRecords(const string& host) const { ares_channel channel; int status; status = ares_init(&channel); if(status != ARES_SUCCESS) { throw UException(std::string("Failed to init ares channel: ") + ares_strerror(status)); } TCallbackInfo info; ares_query(channel, host.c_str(), ns_c_in, ns_t_srv, callback, &info); wait_ares(channel); ares_destroy(channel); if (info.Status != ARES_SUCCESS) { throw UException(std::string("Failed to make ares request: ") + ares_strerror(info.Status)); } struct ares_srv_reply* reply; status = ares_parse_srv_reply((const unsigned char*)info.Data.data(), info.Data.length(), &reply); if (info.Status != ARES_SUCCESS) { throw UException(std::string("Failed to parse response: ") + ares_strerror(status)); } vector<TSrvRecord> records; struct ares_srv_reply* next = reply; while (next != NULL) { TSrvRecord record; record.Host = next->host; record.Port = next->port; record.Priority = next->priority; record.Weight = next->weight; records.push_back(record); next = next->next; } ares_free_data(reply); return records; }
void TMaps::LoadConfig(const QString& fileName) { utils::USettings conf; conf.Load(fileName); bool ok = true; MapSize.setWidth(conf.GetParameter("width")); if (!ok) { throw UException("Error parsing " + fileName); } MapSize.setHeight(conf.GetParameter("height")); if (!ok) { throw UException("Error parsing " + fileName); } }
void TClient::LoadState() { string login = LoadFile(Config.StateDir + "/" + "account.txt"); if (login.empty()) { throw UException("failed to load login"); } StateDir = Config.StateDir + "/" + login + "/"; string state = LoadFile(StateDir + "state"); if (state.empty()) { throw UException("faile to load state"); } if (!State.ParseFromString(state)) { throw UException("failed to deserialize state"); } }
//------------------------------------------------------------------------------ QImage* TObjects::GetImageByName(const QString& imageName) { if (Images.find(imageName) == Images.end()) { throw UException("Image not found"); } return Images[imageName]; }
//------------------------------------------------------------------------------ QImage* TObjects::GetImageById(size_t id) { if (ImagesById.find(id) == ImagesById.end()) { throw UException("Image not found"); } return ImagesById[id]; }
void USettings::Load(const std::string& fname, const std::string& required) { std::ifstream file(fname); if (!file) { throw UException("Error opening file " + fname); } std::string line = ""; while (!file.eof()) { file >> line; int i=0, j=line.size()-1; while (line[i]==' ') i++; while( line[j]==' ') j--; line.erase(j+1,line.size()); line.erase(0,i); if (line == "" || line[0] == '#') { continue; } std::vector<std::string> acc; std::stringstream ss(line); std::string s = ""; while (std::getline(ss, s, '=')) acc.push_back(s); if (acc.size() != 2) { continue; } Parameters.insert(std::pair<std::string, std::string>(acc[0], acc[1])); } if ( Parameters.find(required) == Parameters.end() ) { //std::cout<<"Required parameter " + required + " not found"<<std::endl; //throw UException("Required parameter " + *i + " not found"); } }
void UGlobalHotkeys::RegisterHotkey(const UKeySequence& keySeq, size_t id) { if (keySeq.Size() == 0) { throw UException("Empty hotkeys"); } if (Registered.find(id) != Registered.end()) { UnregisterHotkey(id); } #if defined(Q_OS_WIN) size_t winMod = 0; size_t key = VK_F2; for (size_t i = 0; i != keySeq.Size(); i++) { if (keySeq[i] == Qt::Key_Control) { winMod |= MOD_CONTROL; } else if (keySeq[i] == Qt::Key_Alt) { winMod |= MOD_ALT; } else if (keySeq[i] == Qt::Key_Shift) { winMod |= MOD_SHIFT; } else if (keySeq[i] == Qt::Key_Meta) { winMod |= MOD_WIN; } else { key = QtKeyToWin(keySeq[i]); } } if (!RegisterHotKey((HWND)winId(), id, winMod, key)) { qDebug() << "Error activating hotkey!"; } else { Registered.insert(id); } #elif defined(Q_OS_LINUX) regLinuxHotkey(keySeq, id); #endif }
TFriendRef TClient::GetFriend(const std::string& login) { auto friendIt = Friends.find(login); if (friendIt == Friends.end()) { throw UException("GetFriend: friend not found"); } return friendIt->second; }
UFromStringFormat USettings::GetParameter(const QString& parameter) { if (Parameters.find(parameter) == Parameters.end()) { throw UException(QString("Parameter '%1'not found in config") .arg(parameter)); } return FromString(Parameters[parameter]); }
boost::optional<TClientInfo> TClientInfoStorage::Get(const std::string& login) { boost::optional<string> strData = Storage->Get(login); if (!strData.is_initialized()) { return boost::optional<TClientInfo>(); } TClientInfoData data; if (!data.ParseFromString(Decompress(*strData))) { throw UException("failed to parse storage data"); } TClientInfo result; result.Login = data.login(); result.EncryptedPrivateKey = data.encryptedprivatekey(); result.LoginPasswordHash = data.loginpasswordhash(); result.PublicKey = data.publickey(); for (size_t i = 0; i < data.friends_size(); ++i) { const TFriendInfoData& frnd = data.friends(i); TFriendInfo& friendInfo = result.Friends[frnd.login()]; friendInfo.Login = frnd.login(); friendInfo.EncryptedKey = frnd.encryptedkey(); friendInfo.Type = frnd.type(); friendInfo.AuthStatus = (EAuthStatus)frnd.authstatus(); friendInfo.PublicKey = frnd.publickey(); friendInfo.ServerPublicKey = frnd.serverpublickey(); if (frnd.has_offlinekey()) { assert(frnd.has_offlinekeysignature()); friendInfo.OfflineKey = frnd.offlinekey(); friendInfo.OfflineKeySignature = frnd.offlinekeysignature(); } } return result; }
string TSelfStorage::GetPublicKey() { boost::optional<string> pubKey = Storage->Get("public_key"); if (!pubKey.is_initialized()) { throw UException("no keys found"); } return *pubKey; }
string TSelfStorage::GetPrivateKey() { boost::optional<string> privKey = Storage->Get("private_key"); if (!privKey.is_initialized()) { throw UException("no keys found"); } return *privKey; }
void UColonSep::Load(const std::string &fileName) { std::ifstream file(fileName); if (!file) { throw UException("Error opening file " + fileName); } std::string line = ""; while (std::getline(file, line, '\n')) { try{ if (line.empty() || line[0] == '#') { continue; } std::vector<std::string> objParams = split(line, ':'); std::vector<UFromStringFormat> objs; for (int i = 0; i < objParams.size(); i++) { objs.push_back(objParams[i]); } Objects.push_back(objs); } catch (...) { file.close(); throw; } } }
TStringVector TMessageStorage::GetMessages(const string& login, TDuration from, TDuration to) { NKwStorage::TKwIterator it = Storage->Iterator(); string fromStr = login + ToString(from.MicroSeconds()); string toStr = login + ToString(to.MicroSeconds()); vector<string> messages; it->Seek(fromStr); while (!it->End()) { pair<string, string> value = it->Get(); if (value.first > toStr) { break; } TMessageData data; if (!data.ParseFromString(Decompress(value.second))) { throw UException("failed to parse message"); } messages.push_back(data.encryptedmessage()); it->Next(); if (it->End()) { break; } } return messages; }
quint8 FromString<quint8>(const QString& str){ bool ok = false; int res = str.toUInt(&ok); if (!ok) { throw UException("Cast error"); } return res; }
//------------------------------------------------------------------------------ const QImage& TImageStorage::GetImage(const QString& imageName) { auto it = Images.find(imageName); if (it == Images.end()) { throw UException("Image not found!"); } return *it.value(); }
UFromStringFormat UColonSep::Get(size_t row, size_t col) { if (row > (size_t)Objects.size() || col > (size_t)Objects[row].size()) { throw UException("Index out of bounds"); } return Objects[row][col]; }
//------------------------------------------------------------------------------ void TImageStorage::LoadImage(const QString& imageName, const QString& filename) { QImage* image = new QImage(filename); if (image->isNull()) { throw UException(QString("Loading failed: ") + filename); } Images.insert(imageName, image); }
QByteArray FromString<QByteArray>(const QString& str){ bool ok = false; QByteArray res = str.toUtf8(); if (!ok) { throw UException("Cast error"); } return res; }
double FromString<double>(const QString& str){ bool ok = false; double res = str.toDouble(&ok); if (!ok) { throw UException("Cast error"); } return res; }
float FromString<float>(const QString& str){ bool ok = false; float res = str.toFloat(&ok); if (!ok) { throw UException("Cast error"); } return res; }
void TClient::Login(const std::string& login) { // [email protected] lock_guard<mutex> guard(Lock); State.Clear(); if (CurrentState != CS_Disconnected) { throw UException("should be disconnected before logining"); } pair<string, string> loginHost = GetLoginHost(login); State.set_login(loginHost.first); State.set_host(loginHost.second); std::vector<TNetworkAddressRef> addresses = GetConnectionAddresses(loginHost.first, loginHost.second); if (addresses.size() == 0) { throw UException("no address found for host"); } // todo: random address selection CurrentState = CS_Logining; UdtClient->Connect(*addresses[0], false); }
ushort FromString<unsigned short>(const QString& str){ bool ok = false; ushort res = str.toUShort(&ok); if (!ok) { throw UException("Cast error"); } return res; }
unsigned long long FromString<unsigned long long>(const QString& str){ bool ok = false; unsigned long long res = str.toULongLong(&ok); if (!ok) { throw UException("Cast error"); } return res; }
Union& Configuration::getUnions(const std::wstring& name) { std::list<Union>::iterator it; for(it = this->unions.begin() ; it != this->unions.end() ; it++) { if ((*it).getName().compare(name) == 0) return (*it); } throw UException(L"invalid union"); }