void ppPragma::HandleSTDC(Tokenizer &tk) { const Token *token = tk.Next(); if (token && token->IsIdentifier()) { std::string name = token->GetId(); const Token *tokenCmd = tk.Next(); if (tokenCmd && tokenCmd->IsIdentifier()) { const char *val = tokenCmd->GetId().c_str(); bool on; bool valid = false; if (!strcmp(val, "ON")) { valid = true; on = true; } else if (!strcmp(val, "OFF")) { valid = true; on = false; } if (valid) { if (name == "FENV_ACCESS") FenvAccess::Instance()->Add(on); else if (name == "CX_LIMITED_RANGE") CXLimitedRange::Instance()->Add(on); else if (name == "FP_CONTRACT") FPContract::Instance()->Add(on); } } } }
void ppPragma::HandlePack(Tokenizer &tk) { const Token *tok = tk.Next(); if (tok && tok->GetKeyword() == openpa) { tok = tk.Next(); int val = -1; if (tok) if (tok->IsNumeric()) { val = tok->GetInteger(); } tok = tk.Next(); if (tok) if (tok->GetKeyword() == closepa) { if (val <= 0) { Packing::Instance()->Remove(); } else { Packing::Instance()->Add(val); } } } }
void ppPragma::HandleSR(Tokenizer &tk, bool startup) { const Token *name = tk.Next(); if (name && name->IsIdentifier()) { std::string id = name->GetId(); const Token *prio = tk.Next(); if (prio && prio->IsNumeric() && !prio->IsFloating()) { Startups::Instance()->Add(id, prio->GetInteger(), startup); } } }
void ppPragma::HandleAlias(Tokenizer &tk) { const Token *name = tk.Next(); if (name && name->IsIdentifier()) { std::string id = name->GetId(); const Token *alias = tk.Next(); if (alias && alias->GetKeyword() == eq) { alias = tk.Next(); if (alias && alias->IsIdentifier()) { Aliases::Instance()->Add(id, alias->GetId()); } } } }
void CommandParser::ArenaCombat(cstring str) { vector<Arena::Enemy> units; Tokenizer t; t.FromString(str); bool any[2] = { false,false }; try { bool side = false; t.Next(); while(!t.IsEof()) { if(t.IsItem("vs")) { side = true; t.Next(); } uint count = 1; if(t.IsInt()) { count = t.GetInt(); if(count < 1) t.Throw("Invalid count %d.", count); t.Next(); } const string& id = t.MustGetItem(); UnitData* ud = UnitData::TryGet(id); if(!ud || IS_SET(ud->flags, F_SECRET)) t.Throw("Missing unit '%s'.", id.c_str()); else if(ud->group == G_PLAYER) t.Throw("Unit '%s' can't be spawned.", id.c_str()); t.Next(); int level = -2; if(t.IsItem("lvl")) { t.Next(); level = t.MustGetInt(); t.Next(); } units.push_back({ ud, count, level, side }); any[side ? 1 : 0] = true; } } catch(const Tokenizer::Exception& e) { Msg("Broken units list: %s", e.ToString()); return; } if(units.size() == 0) Msg("Empty units list."); else if(!any[0] || !any[1]) Msg("Missing other units."); else Game::Get().arena->SpawnUnit(units); }
static std::vector<Statement*> ParseStatements(const String& input, bool allowSpaces = false) { String string = input; // Remove spaces since they should be ignored (i.e. variables should not be split) if (allowSpaces) string.erase(std::remove(string.begin(), string.end(), ' '), string.end()); string += ";"; Tokenizer tokenizer; tokenizer.AddEndChars(";"); tokenizer.AddOperatorChars("<=>&|\\/"); tokenizer.AddWhitespaceChars(" \n\r\t"); tokenizer.SetString(string); Token* lastToken = nullptr; Token token; Statement* statement = nullptr; std::vector<Statement*> statements; while (tokenizer.Next(token)) { if (lastToken == nullptr || lastToken->type == Token::Type::END_STATEMENT) { statement = new Statement(); statements.push_back(statement); } lastToken = &token; switch (token.type) { case Token::Type::IDENTIFIER: statement->identifiers.push_back(token.token); break; case Token::Type::OPERATOR: statement->operators.push_back(ParseOperator(token.token)); break; } } return statements; }
//================================================================================================= bool RunInstallScripts() { Info("Reading install scripts."); WIN32_FIND_DATA data; HANDLE find = FindFirstFile(Format("%s/install/*.txt", g_system_dir.c_str()), &data); if(find == INVALID_HANDLE_VALUE) return true; vector<InstallScript> scripts; Tokenizer t; t.AddKeyword("install", 0); t.AddKeyword("version", 1); t.AddKeyword("remove", 2); do { int major, minor, patch; // read file to find version info try { if(t.FromFile(Format("%s/install/%s", g_system_dir.c_str(), data.cFileName))) { t.Next(); if(t.MustGetKeywordId() == 2) { // old install script if(sscanf_s(data.cFileName, "%d.%d.%d.txt", &major, &minor, &patch) != 3) { if(sscanf_s(data.cFileName, "%d.%d.txt", &major, &minor) == 2) patch = 0; else { // unknown version major = 0; minor = 0; patch = 0; } } } else { t.AssertKeyword(0); t.Next(); if(t.MustGetInt() != 1) t.Throw(Format("Unknown install script version '%d'.", t.MustGetInt())); t.Next(); t.AssertKeyword(1); t.Next(); major = t.MustGetInt(); t.Next(); minor = t.MustGetInt(); t.Next(); patch = t.MustGetInt(); } InstallScript& s = Add1(scripts); s.filename = data.cFileName; s.version = (((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF)); } } catch(const Tokenizer::Exception& e) { Warn("Unknown install script '%s': %s", data.cFileName, e.ToString()); } } while(FindNextFile(find, &data)); FindClose(find); if(scripts.empty()) return true; std::sort(scripts.begin(), scripts.end()); GetModuleFileName(nullptr, BUF, 256); char buf[512], buf2[512]; char* filename; GetFullPathName(BUF, 512, buf, &filename); *filename = 0; DWORD len = strlen(buf); LocalString s, s2; for(vector<InstallScript>::iterator it = scripts.begin(), end = scripts.end(); it != end; ++it) { cstring path = Format("%s/install/%s", g_system_dir.c_str(), it->filename.c_str()); try { if(!t.FromFile(path)) { Error("Failed to load install script '%s'.", it->filename.c_str()); continue; } Info("Using install script %s.", it->filename.c_str()); t.Next(); t.AssertKeyword(); if(t.MustGetKeywordId() == 0) { // skip install 1, version X Y Z W t.Next(); t.AssertInt(); t.Next(); t.AssertKeyword(1); t.Next(); t.AssertInt(); t.Next(); t.AssertInt(); t.Next(); t.AssertInt(); t.Next(); t.AssertInt(); t.Next(); } while(true) { if(t.IsEof()) break; t.AssertKeyword(2); t.Next(); s2 = t.MustGetString(); if(GetFullPathName(s2->c_str(), 512, buf2, nullptr) == 0 || strncmp(buf, buf2, len) != 0) { Error("Invalid file path '%s'.", s2->c_str()); return false; } DeleteFile(buf2); t.Next(); } DeleteFile(path); } catch(cstring err) { Error("Failed to parse install script '%s': %s", path, err); } } return true; }