-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Externalize script files #356
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e588802
Externalize script files
mwpenny 7cb86be
Return after loading custom script
mwpenny be924f6
Remove trailing whitespace
mwpenny 7ac4251
Fix spacing and erroneous commas in scripts. Also update with fix for…
mwpenny 0531933
Merge branch 'master' into refactor-scripts
mwpenny 7b25286
Merge branch 'master' into refactor-scripts
mwpenny 7f6f8dc
Remove continue() from GenerateScriptCode.cmake
mwpenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Generated script code | ||
*.vsc.cpp | ||
|
||
# Build objects | ||
build/ | ||
flibitBuild/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
SET(SCRIPTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/scripts") | ||
SET(OUT_DIR "${SCRIPTS_DIR}/generated") | ||
|
||
# Generate code for script files so they can be #included | ||
FILE(GLOB SCRIPTS "${SCRIPTS_DIR}/*.vsc") | ||
FOREACH(SCRIPT_FILE ${SCRIPTS}) | ||
GET_FILENAME_COMPONENT(SCRIPT_NAME "${SCRIPT_FILE}" NAME_WE) | ||
|
||
GET_FILENAME_COMPONENT(OUT_FILE "${SCRIPT_FILE}" NAME) | ||
SET(OUT_FILE "${OUT_DIR}/${OUT_FILE}.cpp") | ||
|
||
# Only regenerate what changed | ||
IF(EXISTS "${OUT_FILE}" AND "${OUT_FILE}" IS_NEWER_THAN "${SCRIPT_FILE}") | ||
CONTINUE() | ||
ENDIF() | ||
|
||
FILE(READ ${SCRIPT_FILE} SCRIPT_CONTENT) | ||
|
||
# Wrap script in initialization code | ||
STRING(REGEX REPLACE "\r?\n" ";" SCRIPT_LINES "${SCRIPT_CONTENT}") | ||
SET(SCRIPT_CONTENT "if (SDL_strcmp(t, \"${SCRIPT_NAME}\") == 0) {\n\tstatic const char* lines[] = {\n") | ||
FOREACH(LINE ${SCRIPT_LINES}) | ||
# Skip comment lines | ||
IF (LINE MATCHES "^#") | ||
CONTINUE() | ||
ENDIF() | ||
|
||
# Remove inline comments | ||
STRING(REGEX REPLACE "#.+$" "" LINE "${LINE}") | ||
|
||
# Surround each line in quotes and end with a comma | ||
STRING(REGEX REPLACE "^(.+)$" "\t\t\"\\1\"," LINE "${LINE}") | ||
SET(SCRIPT_CONTENT "${SCRIPT_CONTENT}\n${LINE}") | ||
ENDFOREACH() | ||
SET(SCRIPT_CONTENT "${SCRIPT_CONTENT}\n\t};\n\tfilllines(lines);\n\treturn;\n}") | ||
|
||
# Save generated file | ||
FILE(WRITE "${OUT_FILE}" "${SCRIPT_CONTENT}") | ||
MESSAGE(STATUS "Wrote ${OUT_FILE}") | ||
ENDFOREACH() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's nice that you specifically added code to deal with comments (and want to preserve them), but the scripting engine simply ignores all invalid commands, including comments. So I'm not really sure how necessary this is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's good to know. One additional benefit of this is that the size of the static string arrays will be (marginally) smaller. In any case, leaving the comments out of the generated code is cleaner to me but I could go either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the scripting engine ignore invalid text at the end of an otherwise valid line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, as long as you always close the arguments to the command with a parenthesis at the end.
That's 'cause it splits lines into arguments, using an argument separator token, which can be any one of
(
,,
, or)
(normal language rules like having to match parentheses don't apply,delay,5(
is a valid script line). If you go with the "proper" style, usually you'll have a parenthesis at the end closing off the arguments, e.g.delay(5)
. And if you have a comment, then it's fine, becausedelay(5) # comment
gets parsed asdelay
,5
, and#comment
(the engine also strips out spaces as well), anddelay()
ignores the second argument so it's all good. But if you havedelay(5 # comment
, it gets parsed asdelay
and5#comment
, which isn't what you want (but funnily enough it's fine enough because number parsing in scripts usesss_toi()
, which takes in as many digit characters as it can until it doesn't find any more, so it correctly identities that you want to delay for 5 frames).Hence my recommendation above.