diff --git a/README b/README index 2c5cd8b4..51b4b3ba 100644 --- a/README +++ b/README @@ -252,7 +252,10 @@ to support positioning of plugins precisely relative to other plugins, but it may be possible to achieve the positioning using only the priority setting. If possible, that is preferred. -Further details can be found in the File Format doc. +When the masterlist/userlist is loaded, it is scanned for entries for each of +the installed plugins. If more than one match is found, they should be merged. + +Further details can be found in the 'docs/BOSS Metadata File Syntax.html' file. Networking diff --git a/docs/BOSS Metadata File Syntax.html b/docs/BOSS Metadata File Syntax.html index fc04275b..abed0fa2 100644 --- a/docs/BOSS Metadata File Syntax.html +++ b/docs/BOSS Metadata File Syntax.html @@ -2,54 +2,53 @@ BOSS Metadata File Syntax +
+This documentation is a work in progress, covering a syntax that is also still a work in progress, and is subject to change. The definition of the priority node of the plugin data structure is incomplete. +
+

BOSS Metadata File Syntax

Contents

  1. Introduction -
  2. File Structure +
  3. Metadata File Structure
  4. Data Structures +
      +
    1. Message Data Structure +
    2. File Data Structure +
    3. Tag Data Structure +
    4. Plugin Data Structure +
    +
  5. Message Strings +
  6. Condition Strings +
      +
    1. Functions +
    2. The Negator & Junctors +
    3. Order Of Evaluation +
    +
  7. Language Codes +
  8. License

Introduction

-

The metadata file syntax is what BOSS's masterlists and userlists are written in (as of BOSS v3 - prior versions will not be covered here). If you know YAML, good news: the metadata file syntax is essentially just YAML v1.2! If you don't know YAML, then this section is for you. -

Some important points about how BOSS uses YAML. +

As of BOSS v3, the metadata file syntax is what BOSS's masterlists and userlists are written in. If you know YAML, good news: the metadata file syntax is essentially just YAML 1.2. If you don't know YAML, then its Wikipedia page is a good introduction. All you really need to know is:

+

Some important points that are more specific to how BOSS uses YAML: +

+

These docs give all example syntax in YAML's least compact representation: you can also use the compact representation, but as it is less readable, this is not recommended. +

Some properties of file paths as used by BOSS: +

+ +

Metadata File Structure

+

The root of a metadata file is a key-value map. BOSS will recognise the following keys, none of which are required: + + +
Key NameValue TypeNotes +
globalsmessage listA list of message data structures for messages that are displayed independently of any plugin. +
pluginsplugin listA list of plugin data structures, holding all the plugin metadata within the file. +
+

Other keys may also be present, but are not processed by BOSS. The message and plugin data structures are detailed in the next section. +

An example metadata file: +globals: + - type: say + content: You are using the latest version of BOSS. + condition: 'version("../BOSS/BOSS.exe", "2.1.1.0", ==)' +plugins: + - name: Armamentarium.esm + tag: + - Relev + - name: ArmamentariumFran.esm + tag: + - Relev + - name: 'Beautiful People 2ch-Ed.esm' + tag: + - Eyes + - Graphics + - Hair + - R.Relations +

Data Structures

-

While you could write whatever you wanted in a metadata file without problems, so long as it was valid YAML, BOSS expects the metadata to be laid out using a certain set of data structures, described in this section. +

While you could write whatever you wanted in a metadata file without problems, so long as it was valid YAML, it would be pointless as BOSS expects the metadata to be laid out using a certain set of data structures, described in this section.

Message Data Structure

-

Messages are given as key-value maps. All values are strings, and the type, content and lang strings are case-sensitive. The condition string has varying case sensitivity according to its content. +

Messages are given as key-value maps.
Key NameRequiredNotes
typeThe type string can be one of three keywords, see the table below for their semantics. -
contentThe actual message content string. It has a few special properties, see the Message Strings section for details. -
conditionA condition string that is evaluated to determine whether the message should be displayed: if it evaluates to true, the message is displayed, otherwise it is not. See the Condition Strings section for details. -
langWhat language this message is written in, given as an ISO 639-3 language code. The languages BOSS supports are given in the Language Codes section. If no language is given, then the message will always be displayed. If a language is given and it does not match the language BOSS is running for, the message will not be displayed, otherwise its display is dependent on the evaluation of the condition string, if present. +
contentThe actual message content string. It has a few special properties, see Message Strings for details. +
conditionA condition string that is evaluated to determine whether the message should be displayed: if it evaluates to true, the message is displayed, otherwise it is not. See Condition Strings for details. +
langWhat language this message is written in, given as an ISO 639-3 language code. The languages BOSS supports are given in Language Codes. If no language is given, then the message will always be displayed. If a language is given and it does not match the language BOSS is running for, the message will not be displayed, otherwise its display is dependent on the evaluation of the condition string, if present.

There are three message types: @@ -112,7 +173,7 @@

Example: -condition: if file("Silgrad_Tower.esm") +condition: 'file("Silgrad_Tower.esm")' type: warn content: Use either Silgrad_Tower.esm or Silgrad_Tower_T.esm but not both together. @@ -120,46 +181,42 @@ content: Use either Silgrad_Tower.esm or Silgrad_Tower_T.esm but not both togeth

File Data Structure

Not to be confused with the structure of the metadata file itself, this structure can be used to hold filenames. It has two forms: the first is a simple string, and the second is a key-value map. All values in the map are strings.

The simple form: -[filename] -

where [filename] is a filename relative to the game's Data folder. +filepath +

where filepath is a file path relative to the game's Data folder.

The map form: -
Key NameRequiredNotes
nameA filename relative to the game's Data folder. Case-insensitive. Directory components must be delimited by forwardslashes, not backslashes. -
displayA substitute string to be displayed instead of the filename in any generated messages, eg. the name of the mod the file belongs to. -
conditionA condition string that is evaluated to determine whether this file data should be used: if it evaluates to true, the data is used, otherwise it is ignored. See the Condition Strings section for details. +
nameA file path relative to the game's Data folder. +
displayA substitute string to be displayed instead of the file path in any generated messages, eg. the name of the mod the file belongs to. +
conditionA condition string that is evaluated to determine whether this file data should be used: if it evaluates to true, the data is used, otherwise it is ignored. See Condition Strings for details.

Examples: ../obse_loader.exe or name: ../obse_loader.exe -condition: if version("../obse_loader.exe", "0.0.18.0", <) +condition: 'version("../obse_loader.exe", "0.0.18.0", <)' display: OBSE v18+

Tag Data Structure

-

BOSS metadata files can contain suggestions for the addition or removal of Bash Tags, and this is the structure used for them. Like the file data structure, it has simple string and key-value map forms. All values in the map are strings. The Tag name is case-sensitive. +

BOSS metadata files can contain suggestions for the addition or removal of Bash Tags, and this is the structure used for them. Like the file data structure, it has simple string and key-value map forms. All values in the map are strings.

The simple form: -[tag] -

where [tag] is one of two things: -

+tag +

where tag is the Bash Tag, preceded by a minus sign if it is suggested for removal.

The map form:
Key NameRequiredNotes
nameA Bash Tag, preceded by a minus sign if it is suggested for removal. -
conditionA condition string that is evaluated to determine whether this Bash Tag should be suggested: if it evaluates to true, the Tag is suggested, otherwise it is ignored. See the Condition Strings section for details. +
conditionA condition string that is evaluated to determine whether this Bash Tag should be suggested: if it evaluates to true, the Tag is suggested, otherwise it is ignored. See Condition Strings for details.

Examples: Relations or name: -Relations -condition: if file("Mart's Monster Mod for OOO.esm") or if file("FCOM_Convergence.esm") +condition: "file(\"Mart's Monster Mod for OOO.esm\") or file(\"FCOM_Convergence.esm\")"

Plugin Data Structure

@@ -167,9 +224,9 @@ condition: if file("Mart's Monster Mod for OOO.esm") or if file("FCOM_Convergenc -
Key NameData TypeRequiredNotes
namestringCan be an exact plugin filename or a regular expression using the POSIX Extended syntax. Case-insensitive. -
enabledbooleanEnables or disables use of the plugin object. Used for user rules, but no reason to use it in the masterlist. -
priorityinteger??? +
namestringCan be an exact plugin filename or a regular expression plugin filename. If the period that precedes the file extension has been escaped (eg. \.esp, \.esm), the string is treated as a regular expression, otherwise it is treated as an exact filename. +
enabledbooleanEnables or disables use of the plugin object. Used for user rules, but no reason to use it in the masterlist. If unspecified, defaults to true. +
priorityinteger??? If unspecified, defaults to 0.
afterfile listPlugins that this plugin must load after, but which are not dependencies. Used for resolving specific compatibility issues and by user rules for specifying custom plugin positions.
reqfile listFiles that this plugin requires to be present. If any of these files are missing, an error message will be displayed. The list can contain a mixture of plugins and other files, but all plugins must be given in their required load order relative to one another. Intended for use specifying implicit dependencies, as BOSS will detect a plugin's explicit masters itself.
incfile listFiles that this plugin is incompatible with. If any of these files are present, an error message will be displayed. @@ -177,7 +234,7 @@ condition: if file("Mart's Monster Mod for OOO.esm") or if file("FCOM_Convergenc
tagtag listThe Bash Tags suggested for this plugin.

Example: -name: Oscuro's_Oblivion_Overhaul.esm +name: "Oscuro's_Oblivion_Overhaul.esm" tag: - Actors.Spells - Graphics @@ -186,44 +243,13 @@ tag: - Scripts - Stats - name: -Relations - condition: if file("Mart's Monster Mod for OOO.esm") or if file("FCOM_Convergence.esm") + condition: "file(\"Mart's Monster Mod for OOO.esm\") or file(\"FCOM_Convergence.esm\")" msg: - type: say - content: Do not clean. "Dirty" edits are intentional and required for the mod to function. + content: 'Do not clean. "Dirty" edits are intentional and required for the mod to function.' -

Metadata File Structure

-

The root of a metadata file is a key-value map. BOSS will recognise the following keys, none of which are required: - - -
Key NameValue TypeNotes -
globalsmessage listA list of global messages. -
pluginsplugin list -
-

Other keys may also be present, but are not processed by BOSS. -

An example metadata file: -globals: - - type: say - content: You are using the latest version of BOSS. - condition: if version("../BOSS/BOSS.exe", "2.1.1.0", ==) -plugins: - - name: Armamentarium.esm - tag: - - Relev - - name: ArmamentariumFran.esm - tag: - - Relev - - name: Beautiful People 2ch-Ed.esm - tag: - - Eyes - - Graphics - - Hair - - R.Relations - - - - -

Message Strings

+

Message Strings

URLs in message content strings will be turned into hyperlinks in BOSS's user interface. Recognised URLs are those that start with file:, http: or https: and contain no spaces (valid URLs can't contain spaces anyway). URLs can be labelled by writing the label after the URL, with a space separating them, and enclosing the URL and label in double quotes.

Examples: http://www.example.com @@ -234,93 +260,104 @@ while becomes

example link
-

Condition Strings

-

Condition strings can be used to ensure that data is only acted on by BOSS under certain circumstances. They are similar in form to conditional expressions in programming languages, and their format is: -[keyword] [function] *( [operator] [keyword] [function] ) -

where *( ) encloses a phrase that may be repeated zero or more times. -

The [keyword] [function] pair is referred to as a condition, and two conditions joined by an operator is referred to as a compound condition. + +

Condition Strings

+

Condition strings can be used to ensure that data is only acted on by BOSS under certain circumstances. They are very similar to boolean conditional expressions in programming languages such as Python, though more limited. Their EBNF grammar is: +[ negator ], function, { junctor, [ negator ], function } ; +

The [ negator ], function grammar is referred to as a condition, and two conditions joined by an operator, ie. condition, operator, condition is referred to as a compound condition.

Unlike previous versions of BOSS, BOSS v3 caches the results of condition evaluations, so performance is not really an issue. A regular expression check will still take longer than a file check though, so use the former only when appropriate to do so. -

Keywords

-

There are two keywords: - - -
KeywordDescription -
ifIf the following [function] evaluates to true, the condition will be true. Otherwise, the condition will be false. -
ifnotIf the following [function] evaluates to false, the condition will be true. Otherwise, the condition will be false. -
- -

Functions

+

Functions

There are several conditions that can be tested for using the functions detailed in the table below. -
FunctionSyntaxDescription
Filefile("[filepath]")Returns true if [filepath] is installed, false otherwise. -
Filefile("[regex]")Returns true if a file matching [regex] is found, false otherwise. -
Checksumchecksum("[filepath]", [checksum])Returns true if the calculated checksum of [filepath] matches [checksum], false otherwise. If [filepath] does not exist, returns false. -
Versionversion("[filepath]", "[version]", [comparator])Returns true if the expression [version] [comparator] [actual version of [filepath]] holds true, false otherwise. If [filepath] does not exist and [comparator] is ==, > or >=, returns false. If [filepath] does not exist and [comparator] is !=, < or <=, returns true. -
Plugin Active Statusactive("[filepath]")Returns true if [filepath] is a .esp or .esm file that is both installed and active, false otherwise. +
Filefile("filepath")Returns true if filepath is installed, false otherwise. +
Fileregex("regex")Returns true if a file matching regex is found, false otherwise. +
Checksumchecksum("filepath", checksum)Returns true if the calculated checksum of filepath matches checksum, false otherwise. If filepath does not exist, returns false. +
Versionversion("filepath", "version", comparator)Returns true if the boolean expression version comparator (actual version of filepath) holds true, false otherwise. If filepath does not exist and comparator is ==, > or >=, returns false. If filepath does not exist and comparator is !=, < or <=, returns true. +
Plugin Active Statusactive("filepath")Returns true if filepath is a .esp or .esm file that is both installed and active, false otherwise.
-

The different types of condition variable described below. Only version strings are case-sensitive. +

The different types of variable used in the above functions are described in more detail below. - -
Variable TypeSyntax Description +
Variable TypeDescription
[filepath]A file path or regular expression string to match file paths to. Must not contain a double quote character ". Evaluated relative to the game's Data folder. If a regex, the string cannot contain any directory components, ie. matches can only exist in the Data folder. Paths must use forwardslashes to delimit directory components. -
[regex]A regular expression string to match filenames to. Evaluated relative to the game's Data folder. The string cannot contain any directory components. The syntax used must be POSIX Extended. -
[checksum]A string of hexadecimal digits representing an unsigned integer that is the data checksum of a file. BOSS displays the checksums of plugins in its user interface after running. -
[version]A string of characters representing the version of a plugin or executable. BOSS displays the versions of plugins in its user interface after running. -
[comparator]A comparison operator, ie. one of ==, !=, <, >, <= or >=, meaning "is equal to", "is not equal to", "is less than", "is greater than", "is less than or equal to" and "is greater than or equal to" respectively. +
filepathA Windows file path. +
regexA regular expression string to match file paths to. +
checksumA string of hexadecimal digits representing an unsigned integer that is the data checksum of a file. BOSS displays the checksums of plugins in its user interface after running. +
versionA string of characters representing the version of a plugin or executable. BOSS displays the versions of plugins in its user interface after running. +
comparatorA comparison operator, ie. one of ==, !=, <, >, <= or >=, meaning "is equal to", "is not equal to", "is less than", "is greater than", "is less than or equal to" and "is greater than or equal to" respectively.
-

Operators

-

There are two operators, which both operate on the conditions to their immediate left and right: +

The Negator & Junctors

+

The negator, or logical negation operator, inverts the value of the function that follows it. Its inclusion is optional, and its syntax is simply: +not +

Below is a truth table for the negator. - -
OperatorSyntaxDescription +
Value of functionValue of not function
Logical ANDandBoth condition must evaluate to true for the compound condition to evaluate to true. If not, the compound condition evaluates to false. -
Logical ORorOne condition or both conditions must evaluate to true for the compound condition to evaluate to true. If neither condition is true, then the compound condition evaluates to false. +
falsetrue +
truefalse
-

Order Of Evaluation

-

When a condition string is evaluated, its constituent parts are evaluated in the following order: +

There are two supported junctors, the logical conjunction (logical and operator) and the logical disjunction (logical or operator), that operate on the expressions to their left and right. + + +
JunctorSyntaxDescription +
Logical ConjunctionandBoth conditions must evaluate to true for the compound condition to evaluate to true. If not, the compound condition evaluates to false. +
Logical DisjunctionorOne condition or both conditions must evaluate to true for the compound condition to evaluate to true. If neither condition is true, then the compound condition evaluates to false. +
+

The following is a truth table for the junctors: + + +
Value of condition1Value of condition2Value of condition1 and condition2Value of condition1 or condition2 +
truetruetruetrue +
truefalsefalsetrue +
falsetruefalsetrue +
falsefalsefalsefalse +
+ +

Order Of Evaluation

+

When a condition string is evaluated, its constituent parts are evaluated according to the standard precedence rules of operator logic, ie.

    -
  1. The function is evaluated. -
  2. The keyword-function pair (condition) is evaluated, with the keyword modifying the result of the function's evaluation if necessary. -
  3. If AND operators are present, then they are evaluated using the evaluation results of the conditions to their immediate left and right, reading from left to right. -
  4. If OR operators are present, then they are evaluated using the evaluation results of the AND operators (if present) or conditions to their immediate left and right, reading from left to right. +
  5. Function +
  6. Negator +
  7. Logical and +
  8. Logical or
-

In case the words are confusing, here's a table of generic condition string structures and how they get evaluated, using brackets to display precedence. To reduce the number of brackets visible in the more complicated examples, the [keyword] [function] pair is replaced with C after the first example. +

Below is a table that lists some combinations of conditions, showing for each the order of evaluation by explicit bracketing. To make it easier to read, the keyword function pair is replaced with C after the first example. -
String StructureEvaluated As
[keyword] [function]( [keyword] ( [function] ) ) -
C and C( C and C ) -
C or C( C or C ) -
C and C and C( ( C and C ) and C ) -
C or C or C( ( C or C ) or C ) -
C and C or C( ( C and C ) or C ) -
C or C and C( C or ( C and C ) ) -
C and C and C and C( ( ( C and C ) and C ) and C ) -
C and C and C or C( ( ( C and C ) and C ) or C ) -
C and C or C and C( ( C and C ) or ( C and C ) ) -
C and C or C or C( ( ( C and C ) or C ) or C) -
C or C or C or C( ( ( C or C ) or C ) or C ) -
C or C or C and C( ( C or C ) or ( C and C ) ) -
C or C and C or C( ( C or ( C and C ) ) or C ) -
C or C and C and C( C or ( ( C and C ) and C ) ) +
keyword function( keyword ( function ) ) +
C and C( C and C ) +
C or C( C or C ) +
C and C and C( ( C and C ) and C ) +
C or C or C( ( C or C ) or C ) +
C and C or C( ( C and C ) or C ) +
C or C and C( C or ( C and C ) ) +
C and C and C and C( ( ( C and C ) and C ) and C ) +
C and C and C or C( ( ( C and C ) and C ) or C ) +
C and C or C and C( ( C and C ) or ( C and C ) ) +
C and C or C or C( ( ( C and C ) or C ) or C) +
C or C or C or C( ( ( C or C ) or C ) or C ) +
C or C or C and C( ( C or C ) or ( C and C ) ) +
C or C and C or C( ( C or ( C and C ) ) or C ) +
C or C and C and C( C or ( ( C and C ) and C ) )
-

You can also explicitly enclose any condition in brackets to override the evaluation order, eg. C and ( C or C ) is evaluated as C and ( C or C ) (contrast with the unbracketed string above). +

You can also explicitly enclose any condition in brackets to override the evaluation order, eg. C and ( C or C ) is evaluated as C and ( C or C ), in contrast to the unbracketed string above. -

Language Codes

+

Language Codes

BOSS supports the following languages and language codes:
LanguageISO 639-3 Code
Englisheng +
Russianrus

License

This document is part of the BOSS documentation.
-Copyright (C) 2011 BOSS Development Team.
+Copyright (C) 2012–2013 WrinklyNinja
See the file BOSS ReadMe.html for copying conditions. diff --git a/src/parsers.h b/src/parsers.h index d9d8beb7..4b1b1b76 100644 --- a/src/parsers.h +++ b/src/parsers.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -228,7 +229,7 @@ namespace YAML { out << BeginMap << Key << "name" << rhs.Name(); - if (!rhs.Condition().empty()) + if (rhs.IsConditional()) out << Key << "condition" << rhs.Condition(); if (!rhs.DisplayName().empty()) @@ -300,50 +301,69 @@ namespace boss { condition_grammar() : condition_grammar::base_type(expression, "condition grammar") { expression = - andStatement [qi::labels::_val = qi::labels::_1] - >> *((qi::lit("or") >> andStatement) [qi::labels::_val = qi::labels::_val || qi::labels::_1]) + compound [qi::labels::_val = qi::labels::_1] + >> *((qi::lit("or") >> compound) [qi::labels::_val = qi::labels::_val || qi::labels::_1]) ; - andStatement = + compound = condition [qi::labels::_val = qi::labels::_1] >> *((qi::lit("and") >> condition) [qi::labels::_val = qi::labels::_val && qi::labels::_1]) ; condition = - ( qi::lit("if") >> type ) [qi::labels::_val = qi::labels::_1] - | ( qi::lit("ifnot") >> type ) [qi::labels::_val = !qi::labels::_1] - | ( '(' >> expression >> ')' ) [qi::labels::_val = qi::labels::_1] //This *should* handle the "brackets override operators" rule. - ; + function [qi::labels::_val = qi::labels::_1] + | ( qi::lit("not") > function ) [qi::labels::_val = !qi::labels::_1] + | ( '(' > expression > ')' ) [qi::labels::_val = qi::labels::_1] + ; - type = - ( "file(" > quotedStr > ')' ) [phoenix::bind(&condition_grammar::CheckFile, this, qi::labels::_val, qi::labels::_1)] - | ( "checksum(" > quotedStr > ',' > qi::hex > ')' ) [phoenix::bind(&condition_grammar::CheckSum, this, qi::labels::_val, qi::labels::_1, qi::labels::_2)] - | ( "version(" > quotedStr > ',' > quotedStr > ',' > comparator > ')' ) [phoenix::bind(&condition_grammar::CheckVersion, this, qi::labels::_val, qi::labels::_1, qi::labels::_2, qi::labels::_3)] - | ( "active(" > quotedStr > ')' ) [phoenix::bind(&condition_grammar::CheckActive, this, qi::labels::_val, qi::labels::_1)] + function = + ( "file(" > filePath > ')' ) [phoenix::bind(&condition_grammar::CheckFile, this, qi::labels::_val, qi::labels::_1)] + | ( "regex(" > quotedStr > ')' ) [phoenix::bind(&condition_grammar::CheckRegex, this, qi::labels::_val, qi::labels::_1)] + | ( "checksum(" > filePath > ',' > qi::hex > ')' ) [phoenix::bind(&condition_grammar::CheckSum, this, qi::labels::_val, qi::labels::_1, qi::labels::_2)] + | ( "version(" > filePath > ',' > quotedStr > ',' > comparator > ')' ) [phoenix::bind(&condition_grammar::CheckVersion, this, qi::labels::_val, qi::labels::_1, qi::labels::_2, qi::labels::_3)] + | ( "active(" > filePath > ')' ) [phoenix::bind(&condition_grammar::CheckActive, this, qi::labels::_val, qi::labels::_1)] ; quotedStr %= '"' > +(unicode::char_ - '"') > '"'; + filePath %= '"' > +(unicode::char_ - invalidPathChars) > '"'; + + invalidPathChars %= + unicode::char_(':') + | unicode::char_('*') + | unicode::char_('?') + | unicode::char_('"') + | unicode::char_('<') + | unicode::char_('>') + | unicode::char_('|') + ; + comparator %= - unicode::string("==") - | unicode::string("!=") - | unicode::string("<") - | unicode::string(">") - | unicode::string("<=") - | unicode::string(">=") - ; + unicode::string("==") + | unicode::string("!=") + | unicode::string("<") + | unicode::string(">") + | unicode::string("<=") + | unicode::string(">=") + ; expression.name("expression"); + compound.name("compound condition"); condition.name("condition"); - type.name("condition type"); + function.name("function"); quotedStr.name("quoted string"); + filePath.name("file path"); comparator.name("comparator"); + invalidPathChars.name("invalid file path characters"); qi::on_error(expression, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); + qi::on_error(compound, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); qi::on_error(condition, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); - qi::on_error(type, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); + qi::on_error(function, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); qi::on_error(quotedStr, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); + qi::on_error(filePath, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); qi::on_error(comparator, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); + qi::on_error(invalidPathChars, phoenix::bind(&condition_grammar::SyntaxError, this, qi::labels::_1, qi::labels::_2, qi::labels::_3, qi::labels::_4)); } void SetGame(boss::Game& g) { @@ -351,40 +371,84 @@ namespace boss { } private: - qi::rule expression, andStatement, condition, type; - qi::rule quotedStr, comparator; + qi::rule expression, compound, condition, function; + qi::rule quotedStr, filePath, comparator; + qi::rule invalidPathChars; boss::Game * game; //Eval's regex and exact paths. Check for files and ghosted plugins. void CheckFile(bool& result, const std::string& file) { - if (boost::contains(file, "\\.")) { //Regex. Only supports filenames right now. - result = false; - boost::regex regex; - try { - regex = boost::regex(file, boost::regex::extended|boost::regex::icase); - } catch (boost::regex_error e) { - throw std::runtime_error("The regex string \"" + file + "\" is invalid."); - // LOG_ERROR("\"%s\" is not a valid regular expression. Item skipped.", reg.c_str()); - } - for (fs::directory_iterator itr(game->DataPath()); itr != fs::directory_iterator(); ++itr) { - if (fs::is_regular_file(itr->status())) { - if (boost::regex_match(itr->path().filename().string(), regex)) { - result = true; - break; - } - } - } + if (!IsSafePath(file)) + throw std::runtime_error("The file path \"" + file + "\" is invalid."); - - } else if (IsPlugin(file)) + if (IsPlugin(file)) result = boost::filesystem::exists(game->DataPath() / file) || boost::filesystem::exists(game->DataPath() / (file + ".ghost")); else result = boost::filesystem::exists(game->DataPath() / file); } + void CheckRegex(bool& result, const std::string& regexStr) { + result = false; + //Can't support a regex string where all path components may be regex, since this could + //lead to massive scanning if an unfortunately-named directory is encountered. + //As such, only the filename portion can be a regex. Need to separate that from the rest + //of the string. + + /* Look for directory separators: in non-regex strings, they are '/' and '\'. In regex, + the backslash is special so must be escaped using another backslash, so look for '/' and "\\". + In C++ string literals, the backslash must be escaped once more to give "\\\\". + Split the regex with another regex! */ + + //Need to also check if the regex is for a safe path. + + boost::regex sepReg("/|(\\\\)", boost::regex::extended); + + std::vector components; + boost::algorithm::split_regex(components, regexStr, sepReg); + + std::string filename = components.back(); + components.pop_back(); + + std::string parent; + for (std::vector::const_iterator it=components.begin(), endIt=components.end()--; it != endIt; ++it) { + if (*it == ".") + continue; + + parent += *it + '/'; + } + + if (boost::contains(parent, "../../")) + throw std::runtime_error("The folder path \"" + parent + "\" is invalid."); + + //Now we have a valid parent path and a regex filename. Check that + //the parent path exists and is a directory. + + boost::filesystem::path parent_path = game->DataPath() / parent; + if (!boost::filesystem::exists(parent_path) || !boost::filesystem::is_directory(parent_path)) + return; + + boost::regex regex; + try { + regex = boost::regex(filename, boost::regex::extended|boost::regex::icase); + } catch (boost::regex_error e) { + throw std::runtime_error("The regex string \"" + filename + "\" is invalid."); + } + + for (fs::directory_iterator itr(parent_path); itr != fs::directory_iterator(); ++itr) { + if (boost::regex_match(itr->path().filename().string(), regex)) { + result = true; + return; + } + } + } + void CheckSum(bool& result, const std::string& file, const uint32_t checksum) { + + if (!IsSafePath(file)) + throw std::runtime_error("The file path \"" + file + "\" is invalid."); + uint32_t crc; boost::unordered_map::iterator it = game->crcCache.find(boost::to_lower_copy(file)); @@ -442,6 +506,20 @@ namespace boss { throw std::runtime_error("Error parsing condition at \"" + context + "\", expected \"" + what.tag + "\""); } + + //Checks that the path (not regex) doesn't go outside any game folders. + bool IsSafePath(const std::string& path) { + std::vector components; + boost::split(components, path, boost::is_any_of("/\\")); + components.pop_back(); + std::string parent_path; + for (std::vector::const_iterator it=components.begin(), endIt=components.end()--; it != endIt; ++it) { + if (*it == ".") + continue; + parent_path += *it + '/'; + } + return !boost::contains(parent_path, "../../"); + } }; bool ConditionalData::EvalCondition(boss::Game& game) const { diff --git a/src/tester.cpp b/src/tester.cpp index b2a77f14..651d11c2 100644 --- a/src/tester.cpp +++ b/src/tester.cpp @@ -23,7 +23,7 @@ int main() { cout << "Testing masterlist parser." << endl; - YAML::Node test = YAML::LoadFile("masterlist-example.yaml"); + YAML::Node test = YAML::LoadFile("masterlist.yaml"); list globalMessages; if (test["globals"]) {