Skip to content

Commit

Permalink
Convert project to Godot 4 (#22)
Browse files Browse the repository at this point in the history
* update GUT

* convert to godot 4 (handle null checks and types)

* fix example

* rename default converted JSON object

* update README
  • Loading branch information
viniciusgerevini authored Mar 19, 2023
1 parent cb47e20 commit 7d70dc5
Show file tree
Hide file tree
Showing 132 changed files with 8,297 additions and 4,908 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Godot-specific ignores
.godot
.import/
*/*.import
output/
Expand Down
11 changes: 11 additions & 0 deletions .gut_editor_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"background_color": "ff262626",
"compact_mode": false,
"config_file": "res://.gutconfig.json",
"dirs": [
"res://test"
Expand All @@ -9,6 +10,7 @@
"font_color": "ffcccccc",
"font_name": "CourierPrime",
"font_size": 16,
"gut_on_top": true,
"hide_orphans": false,
"ignore_pause": false,
"include_subdirs": false,
Expand All @@ -17,6 +19,15 @@
"junit_xml_timestamp": false,
"log_level": 1,
"opacity": 100,
"paint_after": 0.1,
"panel_options": {
"font_name": "CourierPrime",
"font_size": 30,
"hide_output_text": false,
"hide_result_tree": false,
"hide_settings": false,
"use_colors": false
},
"post_run_script": "",
"pre_run_script": "",
"prefix": "test_",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The Wolf: If I'm curt with you, it's because time is
sugar on top, clean the f****n' car.
```

_This branch has the source code for Godot 4. For Godot 3 check the [godot_3](https://github.com/viniciusgerevini/godot-clyde-dialogue/tree/godot_3) branch._

## Usage

The importer automatically imports `.clyde` files to be used with the interpreter. This improves performance, as the dialogue is parsed beforehand.
Expand Down
44 changes: 25 additions & 19 deletions addons/clyde/ClydeDialogue.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
extends Reference
extends RefCounted

class_name ClydeDialogue

const Interpreter = preload('./interpreter/Interpreter.gd')

class_name ClydeDialogue

signal variable_changed(name, value, previous_value)
signal event_triggered(name)
Expand All @@ -21,12 +22,16 @@ var _interpreter
# multiple dialogues in the same file.
func load_dialogue(file_name, block = null):
var file = _load_file(_get_file_path(file_name))

if file.is_empty():
return

_interpreter = Interpreter.new()
_interpreter.init(file, {
"id_suffix_lookup_separator": _config_id_suffix_lookup_separator(),
})
_interpreter.connect("variable_changed", self, "_trigger_variable_changed")
_interpreter.connect("event_triggered", self, "_trigger_event_triggered")
_interpreter.connect("variable_changed",Callable(self,"_trigger_variable_changed"))
_interpreter.connect("event_triggered",Callable(self,"_trigger_event_triggered"))
if block:
_interpreter.select_block(block)

Expand Down Expand Up @@ -80,28 +85,29 @@ func _load_file(path) -> Dictionary:
var container = _load_clyde_file(path)
return container as Dictionary

var f := File.new()
f.open(path, File.READ)
var result := JSON.parse(f.get_as_text())
var f := FileAccess.open(path, FileAccess.READ)
var json_file = JSON.new()
var parse_error = json_file.parse(f.get_as_text())
f.close()
if result.error:
printerr("Failed to parse file: ", f.get_error())
if parse_error != OK or typeof(json_file.data) != TYPE_DICTIONARY:
printerr("Failed to parse file: ", json_file.get_error_message())
return {}

return result.result as Dictionary
return json_file.data


func _load_clyde_file(path):
func _load_clyde_file(path) -> Dictionary:
var data = load(path).__data__.get_string_from_utf8()
var parsed_json = JSON.parse(data)
var json_file = JSON.new()
var parse_error = json_file.parse(data)

if OK != parsed_json.error:
var format = [parsed_json.error_line, parsed_json.error_string]
if parse_error != OK or typeof(json_file.data) != TYPE_DICTIONARY:
var format = [json_file.get_error_line(), json_file.get_error_message()]
var error_string = "%d: %s" % format
printerr("Could not parse json", error_string)
return null
return {}

return parsed_json.result
return json_file.data


func _trigger_variable_changed(name, value, previous_value):
Expand All @@ -116,13 +122,13 @@ func _get_file_path(file_name):
var p = file_name
var extension = file_name.get_extension()

if (not extension):
if (extension == ""):
p = "%s.clyde" % file_name

if p.begins_with('./') or p.begins_with('res://'):
return p

return _get_source_folder().plus_file(p)
return _get_source_folder().path_join(p)


func _get_source_folder():
Expand Down
41 changes: 26 additions & 15 deletions addons/clyde/import_plugin.gd
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
tool
@tool
extends EditorImportPlugin

const Parser = preload("./parser/Parser.gd")

func get_importer_name():
func _get_import_order():
return 1

func _get_importer_name():
return "clyde.dialogue"

func get_visible_name():

func _get_visible_name():
return "Clyde Dialogue Importer"

func get_recognized_extensions():

func _get_recognized_extensions():
return ["clyde"]

func get_save_extension():

func _get_save_extension():
return "res"

func get_resource_type():

func _get_resource_type():
return "PackedDataContainer"

func get_preset_count():

func _get_preset_count():
return 1

func get_preset_name(i):

func _get_preset_name(i):
return "Default"

func get_import_options(i):

func _get_import_options(_path, _i):
return []

func get_option_visibility(option, options):

func _get_option_visibility(_path, _option, _options):
return true

func import(source_file, save_path, options, platform_variants, gen_files):
var file = File.new()
file.open(source_file, File.READ)

func _import(source_file, save_path, options, platform_variants, gen_files):
var file = FileAccess.open(source_file, FileAccess.READ)
var clyde = file.get_as_text()
var result = parse(clyde)
file.close()

var container = PackedDataContainer.new()
container.__data__ = JSON.print(result).to_utf8()
container.__data__ = JSON.stringify(result).to_utf8_buffer()

return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], container)
return ResourceSaver.save(container, "%s.%s" % [save_path, _get_save_extension()])


func parse(input):
Expand Down
Loading

0 comments on commit 7d70dc5

Please sign in to comment.