Skip to content

Commit

Permalink
Add Godot version to README, improve misc code
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Dec 20, 2023
1 parent e918600 commit 748c810
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_preprocessor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- if: ${{ steps.cache-godot.outputs.cache-hit != 'true' }}
name: Install Godot
run: |
wget https://github.com/godotengine/godot-builds/releases/download/${{ vars.GODOT_VERSION }}/${{ env.GODOT_EXECUTABLE }}.zip
wget -q https://github.com/godotengine/godot-builds/releases/download/${{ vars.GODOT_VERSION }}/${{ env.GODOT_EXECUTABLE }}.zip
unzip ${{ env.GODOT_EXECUTABLE }}.zip
- name: Run tests
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.godot/
export_presets.cfg

addons/gdscript_preprocessor/options.cfg
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

An export plugin for stripping comments and "conditional compilation" of GDScript.

### How to use
Compatible with Godot 4.2.

## How to use

1. Copy the `addons/gdscript_preprocessor` folder to your project.
2. Enable the plugin in the Project Settings.
3. Export the project.
4. The original scripts will not be changed, but in PCK/ZIP the scripts will be changed. Use ZIP to check the changes.
5. If any errors occurred during the export, you will see them in the Output Log.

### Important note
## Important note

Each supported platform has certain standard feature tags (plus any custom tags you specify in the export preset). However, there are some standard tags that are not known in advance. See [godotengine/godot#76990](https://github.com/godotengine/godot/issues/76990) and [godotengine/godot#76996](https://github.com/godotengine/godot/pull/76996) for details.

### Features
## Features

* Stripping comments.
* Conditional compilation directives (`#~if` and `#~endif`). They work only when exporting a project, and have no effect in the editor.
Expand All @@ -29,14 +31,14 @@ Each supported platform has certain standard feature tags (plus any custom tags
* `print_stack()`;
* also you can specify custom regexes in `addons/gdscript_preprocessor/options.cfg`.

### Limitations
## Limitations

* Built-in scripts are not properly supported yet.
* Multiple statements on the same line (`if Engine.is_editor_hint(): return`) are not recognized.
* Statements inside lambdas are not processed.
* Your code is expected to follow the [GDScript style guide](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_styleguide.html). Otherwise, regular expressions and string processing used in the plugin may not work.

### Example
## Example

Original script:

Expand All @@ -53,11 +55,11 @@ var c: int
## Comment.
func _ready() -> void:
print(1) # Comment.
# Comment.
print(1)
if OS.has_feature("debug"):
var t: int = a + b
print("Debug: t = ", t)
# Comment.
elif OS.has_feature("release"):
print("Release.")
else:
Expand Down
1 change: 0 additions & 1 deletion addons/gdscript_preprocessor/.gitignore

This file was deleted.

19 changes: 10 additions & 9 deletions addons/gdscript_preprocessor/export_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ extends EditorExportPlugin
const _Preprocessor = preload("./preprocessor.gd")

var _config_path: String
var _preprocessor: _Preprocessor = _Preprocessor.new()
var _platform: EditorExportPlatform
var _features: PackedStringArray
var _preprocessor: _Preprocessor = _Preprocessor.new()


func _init() -> void:
Expand All @@ -26,9 +26,9 @@ func _export_begin(
_path: String,
_flags: int,
) -> void:
_features = features
_preprocessor.features = features
_preprocessor.is_debug = is_debug
_preprocessor.statement_removing_regex = null

var regex: String
var config: ConfigFile = ConfigFile.new()
Expand All @@ -51,8 +51,9 @@ func _begin_customize_resources(
platform: EditorExportPlatform,
features: PackedStringArray,
) -> bool:
assert(features == _preprocessor.features)
_platform = platform
assert(_features == features)
_features = features
return true


Expand All @@ -61,14 +62,14 @@ func _get_customization_configuration_hash() -> int:


func _customize_resource(resource: Resource, path: String) -> Resource:
var gds: GDScript = resource as GDScript
if not gds:
var gdscript: GDScript = resource as GDScript
if not gdscript:
return null

if _preprocessor.preprocess(gds.source_code):
var new_gds: GDScript = GDScript.new()
new_gds.source_code = _preprocessor.result
return new_gds
if _preprocessor.preprocess(gdscript.source_code):
var new_gdscript: GDScript = GDScript.new()
new_gdscript.source_code = _preprocessor.result
return new_gdscript
else:
printerr("%s:%s - %s" % [
"<unknown>" if path.is_empty() else path,
Expand Down
38 changes: 21 additions & 17 deletions addons/gdscript_preprocessor/preprocessor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var _indent_size: int
var _root_parent: _Block # A fake parent of root.
var _block_stack: Array[_Block]
var _if_directive_stack: Array[bool]
var _output_enabled: bool = true
var _paren_stack: Array[int]

var _os_has_feature_regex: RegEx = RegEx.create_from_string(
Expand Down Expand Up @@ -86,6 +87,7 @@ func preprocess(source_code: String) -> bool:
_block_stack.clear()
_block_stack.push_back(_Block.new())
_if_directive_stack.clear()
_output_enabled = true
_paren_stack.clear()

while _position < _length:
Expand Down Expand Up @@ -122,18 +124,17 @@ func _parse_comment_line() -> void:
_position += 1
var line: String = _source.substr(from, _position - from)

if not line.begins_with("#~"): # Normal comment.
return

if line.begins_with("#~if "):
_parse_if_directive(line.trim_prefix("#~if "))
elif line == "#~endif" or line.begins_with("#~endif "): # Allow comment.
_parse_endif_directive()
else:
error_message = 'Unknown or invalid directive "%s".' % line
if line.begins_with("#~"):
if line.begins_with("#~if "):
_parse_if_directive(line.trim_prefix("#~if "))
elif line == "#~endif" or line.begins_with("#~endif "): # Allow comment.
_parse_endif_directive()
else:
error_message = 'Unknown or invalid directive "%s".' % line

_position += 1 # Consume newline.
_line += 1
if _position < _length and _source.unicode_at(_position) == _NEWLINE:
_position += 1
_line += 1


func _parse_if_directive(cond: String) -> void:
Expand All @@ -142,16 +143,18 @@ func _parse_if_directive(cond: String) -> void:
error_message = 'Invalid condition for directive "#~if".'
return
var state: bool = res == _Trilean.TRUE
if not _if_directive_stack.is_empty() and _if_directive_stack.back() == false:
if not _if_directive_stack.is_empty() and not _if_directive_stack.back():
state = false
_if_directive_stack.push_back(state)
_output_enabled = state


func _parse_endif_directive() -> void:
if _if_directive_stack.is_empty():
error_message = '"#~endif" does not have an opening counterpart.'
return
_if_directive_stack.pop_back()
_output_enabled = _if_directive_stack.is_empty() or _if_directive_stack.back()


func _parse_statement() -> void:
Expand Down Expand Up @@ -308,16 +311,17 @@ func _parse_statement() -> void:
_append(current_block.indent, "else:")
current_block.status = _Status.FINISHED
else:
if statement_removing_regex and statement_removing_regex.search(string):
if parent_block.state == _Trilean.FALSE or (statement_removing_regex
and statement_removing_regex.search(string)):
current_block.state = _Trilean.FALSE
return
_append(current_block.indent, string)
parent_block.empty = false
# Let's assume it's not a block (`func`, `if`, `for`, `while`, etc.).
# Otherwise it will be corrected when allocating a nested block.
current_block.empty = false
current_block.state = parent_block.state
current_block.status = _Status.NORMAL
if current_block.state != _Trilean.FALSE:
_append(current_block.indent, string)
parent_block.empty = false


func _parse_string(is_raw: bool) -> void:
Expand Down Expand Up @@ -367,7 +371,7 @@ func _parse_string(is_raw: bool) -> void:


func _append(indent_level: int, string: String) -> void:
if (_if_directive_stack.is_empty() or _if_directive_stack.back() == true):
if _output_enabled:
result += _indent_char_str.repeat(indent_level) + string + "\n"


Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 748c810

Please sign in to comment.