Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Prelude
Changes Proposed
Note: This is a feature that I initially developed for personal use (drafted in 2015, in use in a much less refined form since 2017), but since there seems to be some interest in having it as part of the Hercules core, I decided to clean up the code a bit so that it's more usable and release it as part of Hercules.
This adds support for conditional comments in scripts.
Conditional comments are similar to #ifdef directives in the C preprocessor, allowing to exclude parts of a script depending on whether a certain flag is enabled or not.
The feature is designed to offer backward compatibility to scripts that use it, letting older engines seamlessly ignore the conditional part as if it was a comment.
Syntax
A conditional comment begins with
/*@CONDITION
followed by whitespace or*/
and ends with the first*/
sequence encountered on a separate line, but the sequence//@*/
at the beginning of a line (optionally with indentation) is recommended as a terminator, since it provides compatibility with older versions of the script engine that don't support conditional comments.The condition flag can be negated by prefixing it with an exclamation mark (
!
).Condition flags consist of an uppercase letter followed by a sequence of uppercase letters, digits and underscores and are at least three characters long.
Note: conditional comments cannot be nested (in the same way how block comments cannot be nested).
Examples
The following block is only parsed if FLAG is defined. If not defined, or when loaded by an older engine, the block is treated as a block comment:
The following block is only parsed if FLAG is not defined. If defined, or when loaded by an older engine, the block is treated as a block comment:
The following block is only parsed if FLAG is defined. If not defined, the block is treated as a block comment. When loaded by an older engine, the block is also parsed:
The following block is only parsed if FLAG is not defined. If defined, the block is treated as a block comment. When loaded by an older engine, the block is also parsed.
Condition flags
The Hercules core provides the following conditional comment flags (plugins may provide more):
TRUE
HERCULES
FALSE
RENEWAL
PRERENEWAL
LOADGMSCRIPTS
script_configuration.load_gm_scripts
istrue
Applications
A first application of the flags is included in this pull request: conditionally loading the GM management scripts (that may be considered by some as a security hazard) based on a server setting.
When the
script_configuration.load_gm_scripts
flag in the script configuration is set tofalse
, theLOADGMSCRIPTS
conditional feature is not defined in the script engine.Compliant scripts put their GM management NPCs in a conditional block controlled by that flag (all the built-in scripts have been updated to support this feature).
Plugins
Plugins can define conditional flags at runtime by calling script->declare_conditional_feature("FLAG_NAME"), allowing scripts to behave differently when a plugin is loaded.
One such example is the naviluagenerator plugin in the StaffPlugins repository. The plugin defines the
NAVILUAGENERATOR
feature and compliant scripts can define conditional code to generate better navi lua scripts as follows:Example exposing a warper NPC as a normal warp to the navi lua:
Example hiding a super secret quest NPC from the navi system:
Example exposing a scripted mob spawn as a normal monster to the navi system:
I have a number of such overrides already written, but those will be part of a separate release to keep this PR shorter.
Issues addressed: N/A