Fix dialogue manager plugin, lower resolution

This commit is contained in:
2025-06-27 18:53:08 -07:00
parent 338e303fbb
commit 39f791e2b4
43 changed files with 1565 additions and 856 deletions

View File

@@ -168,12 +168,13 @@ func import_content(path: String, prefix: String, imported_line_map: Dictionary,
for i in range(0, content.size()):
var line = content[i]
if line.strip_edges().begins_with("~ "):
var indent: String = "\t".repeat(get_indent(line))
var title = line.strip_edges().substr(2)
if "/" in line:
var bits = title.split("/")
content[i] = "~ %s/%s" % [_imported_titles[bits[0]], bits[1]]
content[i] = "%s~ %s/%s" % [indent, _imported_titles[bits[0]], bits[1]]
else:
content[i] = "~ %s/%s" % [str(path.hash()), title]
content[i] = "%s~ %s/%s" % [indent, str(path.hash()), title]
elif "=>< " in line:
var jump: String = line.substr(line.find("=>< ") + "=>< ".length()).strip_edges()
@@ -185,7 +186,7 @@ func import_content(path: String, prefix: String, imported_line_map: Dictionary,
else:
content[i] = "%s=>< %s/%s" % [line.split("=>< ")[0], title_hash, bits[1]]
elif not jump in ["END", "END!"]:
elif not jump in ["END", "END!"] and not jump.begins_with("{{"):
content[i] = "%s=>< %s/%s" % [line.split("=>< ")[0], str(path.hash()), jump]
elif "=> " in line:
@@ -198,7 +199,7 @@ func import_content(path: String, prefix: String, imported_line_map: Dictionary,
else:
content[i] = "%s=> %s/%s" % [line.split("=> ")[0], title_hash, bits[1]]
elif not jump in ["END", "END!"]:
elif not jump in ["END", "END!"] and not jump.begins_with("{{"):
content[i] = "%s=> %s/%s" % [line.split("=> ")[0], str(path.hash()), jump]
imported_paths.append(path)
@@ -249,18 +250,22 @@ func build_line_tree(raw_lines: PackedStringArray) -> DMTreeLine:
tree_line.notes = "\n".join(doc_comments)
doc_comments.clear()
# Empty lines are only kept so that we can work out groupings of things (eg. responses and
# randomised lines). Therefore we only need to keep one empty line in a row even if there
# Empty lines are only kept so that we can work out groupings of things (eg. randomised
# lines). Therefore we only need to keep one empty line in a row even if there
# are multiple. The indent of an empty line is assumed to be the same as the non-empty line
# following it. That way, grouping calculations should work.
if tree_line.type in [DMConstants.TYPE_UNKNOWN, DMConstants.TYPE_COMMENT] and raw_lines.size() > i + 1:
var next_line = raw_lines[i + 1]
if previous_line and previous_line.type in [DMConstants.TYPE_UNKNOWN, DMConstants.TYPE_COMMENT] and tree_line.type in [DMConstants.TYPE_UNKNOWN, DMConstants.TYPE_COMMENT]:
if get_line_type(next_line) in [DMConstants.TYPE_UNKNOWN, DMConstants.TYPE_COMMENT]:
continue
else:
tree_line.type = DMConstants.TYPE_UNKNOWN
tree_line.indent = get_indent(next_line)
# Nothing should be more than a single indent past its parent.
if tree_line.indent > parent_chain.size():
add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_INVALID_INDENTATION)
# Check for indentation changes
if tree_line.indent > parent_chain.size() - 1:
parent_chain.append(previous_line)
@@ -481,6 +486,7 @@ func parse_match_line(tree_line: DMTreeLine, line: DMCompiledLine, siblings: Arr
# Check that all children are when or else.
for child in tree_line.children:
if child.type == DMConstants.TYPE_WHEN: continue
if child.type == DMConstants.TYPE_UNKNOWN: continue
if child.type == DMConstants.TYPE_CONDITION and child.text.begins_with("else"): continue
result = add_error(child.line_number, child.indent, DMConstants.ERR_EXPECTED_WHEN_OR_ELSE)
@@ -569,11 +575,15 @@ func parse_response_line(tree_line: DMTreeLine, line: DMCompiledLine, siblings:
result = add_error(tree_line.line_number, condition.index, condition.error)
else:
line.expression = condition
# Extract just the raw condition text
var found: RegExMatch = regex.WRAPPED_CONDITION_REGEX.search(tree_line.text)
line.expression_text = found.strings[found.names.expression]
tree_line.text = regex.WRAPPED_CONDITION_REGEX.sub(tree_line.text, "").strip_edges()
# Find the original response in this group of responses.
var original_response: DMTreeLine = tree_line
for i in range(sibling_index - 1, 0, -1):
for i in range(sibling_index - 1, -1, -1):
if siblings[i].type == DMConstants.TYPE_RESPONSE:
original_response = siblings[i]
elif siblings[i].type != DMConstants.TYPE_UNKNOWN:
@@ -690,14 +700,32 @@ func parse_dialogue_line(tree_line: DMTreeLine, line: DMCompiledLine, siblings:
for i in range(0, tree_line.children.size()):
var child: DMTreeLine = tree_line.children[i]
if child.type == DMConstants.TYPE_DIALOGUE:
# Nested dialogue lines cannot have further nested dialogue.
if child.children.size() > 0:
add_error(child.children[0].line_number, child.children[0].indent, DMConstants.ERR_INVALID_INDENTATION)
# Mark this as a dialogue child of another dialogue line.
child.is_nested_dialogue = true
var child_line = DMCompiledLine.new("", DMConstants.TYPE_DIALOGUE)
parse_character_and_dialogue(child, child_line, [], 0, parent)
var child_static_line_id: String = extract_static_line_id(child.text)
if child_line.character != "" or child_static_line_id != "":
add_error(child.line_number, child.indent, DMConstants.ERR_UNEXPECTED_SYNTAX_ON_NESTED_DIALOGUE_LINE)
# Check that only the last child (or none) has a jump reference
if i < tree_line.children.size() - 1 and " =>" in child.text:
add_error(child.line_number, child.indent, DMConstants.ERR_NESTED_DIALOGUE_INVALID_JUMP)
if i == 0 and " =>" in tree_line.text:
add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_NESTED_DIALOGUE_INVALID_JUMP)
tree_line.text += "\n" + child.text
elif child.type == DMConstants.TYPE_UNKNOWN:
tree_line.text += "\n"
else:
result = add_error(child.line_number, child.indent, DMConstants.ERR_INVALID_INDENTATION)
# Extract the static line ID
var static_line_id: String = extract_static_line_id(tree_line.text)
if static_line_id:
tree_line.text = tree_line.text.replace("[ID:%s]" % [static_line_id], "")
tree_line.text = tree_line.text.replace(" [ID:", "[ID:").replace("[ID:%s]" % [static_line_id], "")
line.translation_key = static_line_id
# Check for simultaneous lines
@@ -730,7 +758,7 @@ func parse_dialogue_line(tree_line: DMTreeLine, line: DMCompiledLine, siblings:
if expression.size() == 0:
add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_INVALID_EXPRESSION)
elif expression[0].type == DMConstants.TYPE_ERROR:
add_error(tree_line.line_number, tree_line.indent + expression[0].index, expression[0].value)
add_error(tree_line.line_number, tree_line.indent + expression[0].i, expression[0].value)
# If the line isn't part of a weighted random group then make it point to the next
# available sibling.
@@ -817,9 +845,14 @@ func parse_character_and_dialogue(tree_line: DMTreeLine, line: DMCompiledLine, s
# Replace any newlines.
text = text.replace("\\n", "\n").strip_edges()
# If there was no manual translation key then just use the text itself
if line.translation_key == "":
line.translation_key = text
# If there was no manual translation key then just use the text itself (unless this is a
# child dialogue below another dialogue line).
if not tree_line.is_nested_dialogue and line.translation_key == "":
# Show an error if missing translations is enabled
if DMSettings.get_setting(DMSettings.MISSING_TRANSLATIONS_ARE_ERRORS, false):
result = add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_MISSING_ID)
else:
line.translation_key = text
line.text = text
@@ -829,9 +862,6 @@ func parse_character_and_dialogue(tree_line: DMTreeLine, line: DMCompiledLine, s
result = add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_DUPLICATE_ID)
else:
_known_translation_keys[line.translation_key] = line.text
# Show an error if missing translations is enabled
elif DMSettings.get_setting(DMSettings.MISSING_TRANSLATIONS_ARE_ERRORS, false):
result = add_error(tree_line.line_number, tree_line.indent, DMConstants.ERR_MISSING_ID)
return result
@@ -1009,7 +1039,7 @@ func extract_condition(text: String, is_wrapped: bool, index: int) -> Dictionary
}
elif expression[0].type == DMConstants.TYPE_ERROR:
return {
index = expression[0].index,
index = expression[0].i,
error = expression[0].value
}
else:
@@ -1037,7 +1067,7 @@ func extract_mutation(text: String) -> Dictionary:
}
elif expression[0].type == DMConstants.TYPE_ERROR:
return {
index = expression[0].index,
index = expression[0].i,
error = expression[0].value
}
else:

View File

@@ -26,6 +26,8 @@ var concurrent_lines: PackedStringArray = []
var tags: PackedStringArray = []
## The condition or mutation expression for this line.
var expression: Dictionary = {}
## The express as the raw text that was given.
var expression_text: String = ""
## The next sequential line to go to after this line.
var next_id: String = ""
## The next line to go to after this line if it is unknown and compile time.
@@ -129,6 +131,8 @@ func to_data() -> Dictionary:
d.tags = tags
if not notes.is_empty():
d.notes = notes
if not expression_text.is_empty():
d.condition_as_text = expression_text
DMConstants.TYPE_DIALOGUE:
d.text = text

View File

@@ -38,6 +38,7 @@ var TOKEN_DEFINITIONS: Dictionary = {
DMConstants.TOKEN_NUMBER: RegEx.create_from_string("^\\-?\\d+(\\.\\d+)?"),
DMConstants.TOKEN_OPERATOR: RegEx.create_from_string("^(\\+|\\-|\\*|/|%)"),
DMConstants.TOKEN_COMMA: RegEx.create_from_string("^,"),
DMConstants.TOKEN_NULL_COALESCE: RegEx.create_from_string("^\\?\\."),
DMConstants.TOKEN_DOT: RegEx.create_from_string("^\\."),
DMConstants.TOKEN_STRING: RegEx.create_from_string("^&?(\".*?\"|\'.*?\')"),
DMConstants.TOKEN_NOT: RegEx.create_from_string("^(not( |$)|!)"),

View File

@@ -2,6 +2,9 @@
class_name DMExpressionParser extends RefCounted
var include_comments: bool = false
# Reference to the common [RegEx] that the parser needs.
var regex: DMCompilerRegEx = DMCompilerRegEx.new()
@@ -25,7 +28,7 @@ func tokenise(text: String, line_type: String, index: int) -> Array:
index += 1
text = text.substr(1)
else:
return _build_token_tree_error(DMConstants.ERR_INVALID_EXPRESSION, index)
return _build_token_tree_error([], DMConstants.ERR_INVALID_EXPRESSION, index)
return _build_token_tree(tokens, line_type, "")[0]
@@ -59,7 +62,7 @@ func extract_replacements(text: String, index: int) -> Array[Dictionary]:
}
elif expression[0].type == DMConstants.TYPE_ERROR:
replacement = {
index = expression[0].index,
index = expression[0].i,
error = expression[0].value
}
else:
@@ -76,8 +79,13 @@ func extract_replacements(text: String, index: int) -> Array[Dictionary]:
# Create a token that represents an error.
func _build_token_tree_error(error: int, index: int) -> Array:
return [{ type = DMConstants.TOKEN_ERROR, value = error, index = index }]
func _build_token_tree_error(tree: Array, error: int, index: int) -> Array:
tree.insert(0, {
type = DMConstants.TOKEN_ERROR,
value = error,
i = index
})
return tree
# Convert a list of tokens into an abstract syntax tree.
@@ -91,14 +99,22 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
var error = _check_next_token(token, tokens, line_type, expected_close_token)
if error != OK:
var error_token: Dictionary = tokens[1] if tokens.size() > 1 else token
return [_build_token_tree_error(error, error_token.index), tokens]
return [_build_token_tree_error(tree, error, error_token.index), tokens]
match token.type:
DMConstants.TOKEN_COMMENT:
if include_comments:
tree.append({
type = DMConstants.TOKEN_COMMENT,
value = token.value,
i = token.index
})
DMConstants.TOKEN_FUNCTION:
var sub_tree = _build_token_tree(tokens, line_type, DMConstants.TOKEN_PARENS_CLOSE)
if sub_tree[0].size() > 0 and sub_tree[0][0].type == DMConstants.TOKEN_ERROR:
return [_build_token_tree_error(sub_tree[0][0].value, sub_tree[0][0].index), tokens]
return [_build_token_tree_error(tree, sub_tree[0][0].value, sub_tree[0][0].i), tokens]
tree.append({
type = DMConstants.TOKEN_FUNCTION,
@@ -113,11 +129,11 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
var sub_tree = _build_token_tree(tokens, line_type, DMConstants.TOKEN_BRACKET_CLOSE)
if sub_tree[0].size() > 0 and sub_tree[0][0].type == DMConstants.TOKEN_ERROR:
return [_build_token_tree_error(sub_tree[0][0].value, sub_tree[0][0].index), tokens]
return [_build_token_tree_error(tree, sub_tree[0][0].value, sub_tree[0][0].i), tokens]
var args = _tokens_to_list(sub_tree[0])
if args.size() != 1:
return [_build_token_tree_error(DMConstants.ERR_INVALID_INDEX, token.index), tokens]
return [_build_token_tree_error(tree, DMConstants.ERR_INVALID_INDEX, token.index), tokens]
tree.append({
type = DMConstants.TOKEN_DICTIONARY_REFERENCE,
@@ -132,7 +148,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
var sub_tree = _build_token_tree(tokens, line_type, DMConstants.TOKEN_BRACE_CLOSE)
if sub_tree[0].size() > 0 and sub_tree[0][0].type == DMConstants.TOKEN_ERROR:
return [_build_token_tree_error(sub_tree[0][0].value, sub_tree[0][0].index), tokens]
return [_build_token_tree_error(tree, sub_tree[0][0].value, sub_tree[0][0].i), tokens]
var t = sub_tree[0]
for i in range(0, t.size() - 2):
@@ -154,7 +170,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
var sub_tree = _build_token_tree(tokens, line_type, DMConstants.TOKEN_BRACKET_CLOSE)
if sub_tree[0].size() > 0 and sub_tree[0][0].type == DMConstants.TOKEN_ERROR:
return [_build_token_tree_error(sub_tree[0][0].value, sub_tree[0][0].index), tokens]
return [_build_token_tree_error(tree, sub_tree[0][0].value, sub_tree[0][0].i), tokens]
var type = DMConstants.TOKEN_ARRAY
var value = _tokens_to_list(sub_tree[0])
@@ -177,7 +193,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
var sub_tree = _build_token_tree(tokens, line_type, DMConstants.TOKEN_PARENS_CLOSE)
if sub_tree[0].size() > 0 and sub_tree[0][0].type == DMConstants.TOKEN_ERROR:
return [_build_token_tree_error(sub_tree[0][0].value, sub_tree[0][0].index), tokens]
return [_build_token_tree_error(tree, sub_tree[0][0].value, sub_tree[0][0].i), tokens]
tree.append({
type = DMConstants.TOKEN_GROUP,
@@ -190,7 +206,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
DMConstants.TOKEN_BRACE_CLOSE, \
DMConstants.TOKEN_BRACKET_CLOSE:
if token.type != expected_close_token:
return [_build_token_tree_error(DMConstants.ERR_UNEXPECTED_CLOSING_BRACKET, token.index), tokens]
return [_build_token_tree_error(tree, DMConstants.ERR_UNEXPECTED_CLOSING_BRACKET, token.index), tokens]
tree.append({
type = token.type,
@@ -211,10 +227,11 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
DMConstants.TOKEN_COMMA, \
DMConstants.TOKEN_COLON, \
DMConstants.TOKEN_DOT:
DMConstants.TOKEN_DOT, \
DMConstants.TOKEN_NULL_COALESCE:
tree.append({
type = token.type,
i = token.index
i = token.index
})
DMConstants.TOKEN_COMPARISON, \
@@ -230,7 +247,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
tree.append({
type = token.type,
value = value,
i = token.index
i = token.index
})
DMConstants.TOKEN_STRING:
@@ -248,7 +265,7 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
})
DMConstants.TOKEN_CONDITION:
return [_build_token_tree_error(DMConstants.ERR_UNEXPECTED_CONDITION, token.index), token]
return [_build_token_tree_error(tree, DMConstants.ERR_UNEXPECTED_CONDITION, token.index), token]
DMConstants.TOKEN_BOOL:
tree.append({
@@ -280,8 +297,8 @@ func _build_token_tree(tokens: Array[Dictionary], line_type: String, expected_cl
})
if expected_close_token != "":
var index: int = tokens[0].index if tokens.size() > 0 else 0
return [_build_token_tree_error(DMConstants.ERR_MISSING_CLOSING_BRACKET, index), tokens]
var index: int = tokens[0].i if tokens.size() > 0 else 0
return [_build_token_tree_error(tree, DMConstants.ERR_MISSING_CLOSING_BRACKET, index), tokens]
return [tree, tokens]
@@ -347,8 +364,8 @@ func _check_next_token(token: Dictionary, next_tokens: Array[Dictionary], line_t
DMConstants.TOKEN_COMPARISON, \
DMConstants.TOKEN_OPERATOR, \
DMConstants.TOKEN_COMMA, \
DMConstants.TOKEN_DOT, \
DMConstants.TOKEN_NULL_COALESCE, \
DMConstants.TOKEN_NOT, \
DMConstants.TOKEN_AND_OR, \
DMConstants.TOKEN_DICTIONARY_REFERENCE:
@@ -366,6 +383,20 @@ func _check_next_token(token: Dictionary, next_tokens: Array[Dictionary], line_t
DMConstants.TOKEN_DOT
]
DMConstants.TOKEN_COMMA:
unexpected_token_types = [
null,
DMConstants.TOKEN_COMMA,
DMConstants.TOKEN_COLON,
DMConstants.TOKEN_ASSIGNMENT,
DMConstants.TOKEN_OPERATOR,
DMConstants.TOKEN_AND_OR,
DMConstants.TOKEN_PARENS_CLOSE,
DMConstants.TOKEN_BRACE_CLOSE,
DMConstants.TOKEN_BRACKET_CLOSE,
DMConstants.TOKEN_DOT
]
DMConstants.TOKEN_COLON:
unexpected_token_types = [
DMConstants.TOKEN_COMMA,
@@ -409,7 +440,8 @@ func _check_next_token(token: Dictionary, next_tokens: Array[Dictionary], line_t
DMConstants.TOKEN_BRACKET_OPEN
]
if (expected_token_types.size() > 0 and not next_token.type in expected_token_types or unexpected_token_types.size() > 0 and next_token.type in unexpected_token_types):
if (expected_token_types.size() > 0 and not next_token.type in expected_token_types) \
or (unexpected_token_types.size() > 0 and next_token.type in unexpected_token_types):
match next_token.type:
null:
return DMConstants.ERR_UNEXPECTED_END_OF_EXPRESSION

View File

@@ -22,6 +22,8 @@ var text: String = ""
var children: Array[DMTreeLine] = []
## Any doc comments attached to this line.
var notes: String = ""
## Is this a dialogue line that is the child of another dialogue line?
var is_nested_dialogue: bool = false
func _init(initial_id: String) -> void: