@papercraftgames/estragon 0.1.0-beta.10 → 0.1.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,7 @@ func to_dictionary() -> Dictionary:
53
53
  var ranges: Array[Dictionary] = []
54
54
  for segment in segments:
55
55
  ranges.append({"generated": _span(segment.generated), "original": _span(segment.original), "kind": segment.kind})
56
- return {"format": "estragon.gogo.source-map", "version": 1, "sourceName": source.name, "sourceContent": source.text, "segments": ranges}
56
+ return {"format": "estragon.gogo.source-map", "version": 1, "sourceName": source.name, "sourceContent": source.text, "generatedHash": code.sha256_text(), "segments": ranges}
57
57
 
58
58
  func serialize() -> String:
59
59
  return JSON.stringify(to_dictionary()) + "\n"
@@ -0,0 +1,256 @@
1
+ @tool
2
+ extends RefCounted
3
+ ## Authenticated compiled views. Native editing/debug navigation stays native;
4
+ ## only verified Estragon output is protected. Recovery precedes replacement.
5
+ const Components = preload("scene_components.gd")
6
+ const Compiler = preload("../compiler/compiler.gd")
7
+ const RECOVERY := "user://estragon/generated-edits"
8
+ var plugin: EditorPlugin
9
+ var interface: EditorInterface
10
+ var navigation: RefCounted
11
+ var association: RefCounted
12
+ var entries: Dictionary = {}
13
+ var panel: MenuButton
14
+ var viewer: AcceptDialog
15
+ var preview: TextEdit
16
+ var location: TextEdit
17
+ var selected := 0
18
+ var active := true
19
+ var mutating := false
20
+ var problem := ""
21
+ var presentation := ""
22
+ var action: Callable
23
+ func _init(owner: EditorPlugin, navigator: RefCounted, adapter: RefCounted) -> void:
24
+ plugin = owner
25
+ interface = owner.get_editor_interface()
26
+ navigation = navigator
27
+ association = adapter
28
+ panel = MenuButton.new()
29
+ panel.name = "GogoGeneratedView"
30
+ plugin.add_control_to_container(EditorPlugin.CONTAINER_TOOLBAR,panel)
31
+ panel.hide()
32
+ interface.get_script_editor().visibility_changed.connect(_present)
33
+ viewer = AcceptDialog.new()
34
+ viewer.title = "Recovered generated edit — copy changes into the Gogo source"
35
+ viewer.min_size = Vector2i(760,460)
36
+ var box := VBoxContainer.new()
37
+ viewer.add_child(box)
38
+ # A wrapping Label can feed an unbounded minimum height back into a
39
+ # native Window before its first layout. Keep the selectable path bounded.
40
+ location = TextEdit.new()
41
+ location.editable = false
42
+ location.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
43
+ location.custom_minimum_size = Vector2(740,110)
44
+ box.add_child(location)
45
+ preview = TextEdit.new()
46
+ preview.editable = false
47
+ preview.custom_minimum_size = Vector2(740,360)
48
+ preview.size_flags_vertical = Control.SIZE_EXPAND_FILL
49
+ box.add_child(preview)
50
+ plugin.add_child(viewer)
51
+ func _verified(path: String) -> String:
52
+ var source := Components.source_path(path)
53
+ if source.is_empty() or not FileAccess.file_exists(source) or not FileAccess.file_exists(path): return ""
54
+ var map = JSON.parse_string(FileAccess.get_file_as_string(path+".map.json"))
55
+ if not map is Dictionary: return ""
56
+ if not map.get("sourceContent") is String or not map.get("segments") is Array: return ""
57
+ var text := FileAccess.get_file_as_string(path)
58
+ if map.has("generatedHash"):
59
+ return source if map.generatedHash == text.sha256_text() else ""
60
+ # Older v1 maps have no output hash. Authenticate by deterministic native
61
+ # emission of their recorded source; never guess from a folder/basename.
62
+ var result := Compiler.compile(map.sourceContent,map.sourceName,path)
63
+ return source if result.ok and result.code == text else ""
64
+ func _display(path: String) -> String:
65
+ return FileAccess.get_file_as_string(path).replace("\r\n","\n")
66
+ func _recovery_prefix(path: String) -> String:
67
+ return (ProjectSettings.globalize_path("res://").simplify_path()+"|"+path).sha256_text()+"-"
68
+ func _latest(path: String) -> String:
69
+ var directory := DirAccess.open(RECOVERY)
70
+ if directory == null: return ""
71
+ var prefix := _recovery_prefix(path)
72
+ var latest := ""
73
+ for name in directory.get_files():
74
+ var file := RECOVERY.path_join(name)
75
+ if name.begins_with(prefix) and name.ends_with(".txt") and (latest.is_empty() or name>latest.get_file()): latest = file
76
+ return latest
77
+ func attach(editor: ScriptEditorBase, code: TextEdit, path: String) -> void:
78
+ if not active or not code is CodeEdit: return
79
+ _present()
80
+ if path.is_empty(): return
81
+ var id := code.get_instance_id()
82
+ if entries.has(id):
83
+ if entries[id].path == path: return
84
+ _detach(id)
85
+ var source := _verified(path)
86
+ if source.is_empty(): return
87
+ var changed := _changed.bind(id)
88
+ var closing := _closed.bind(id)
89
+ entries[id] = {"editor":weakref(editor),"code":weakref(code),"path":path,"source":source,"editable":code.editable,"baseline":_display(path),"recovery":_latest(path),"changed":changed,"closing":closing,"blocked":false}
90
+ code.editable = false
91
+ code.text_changed.connect(changed)
92
+ code.text_set.connect(changed)
93
+ code.tree_exiting.connect(closing)
94
+ # Includes a dirty tab restored before the addon is enabled. Native input
95
+ # is already read-only; no byte is cleared until recovery is verified.
96
+ _changed(id)
97
+ _present()
98
+ func _preserve(entry: Dictionary, text: String) -> bool:
99
+ var file := RECOVERY.path_join(_recovery_prefix(entry.path)+str(int(Time.get_unix_time_from_system()*1000000))+"-"+text.sha256_text()+".txt")
100
+ var error := DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(RECOVERY))
101
+ if error == OK:
102
+ var output := FileAccess.open(file,FileAccess.WRITE)
103
+ if output != null:
104
+ output.store_buffer(text.to_utf8_buffer())
105
+ output.flush()
106
+ error = output.get_error()
107
+ output.close()
108
+ else: error = FileAccess.get_open_error()
109
+ if error != OK or not FileAccess.file_exists(file) or FileAccess.get_file_as_bytes(file) != text.to_utf8_buffer():
110
+ entry.blocked = true
111
+ problem = entry.path+": Cannot preserve edited generated buffer. Save/Play blocked; copy the visible edit before continuing. Recovery: "+ProjectSettings.globalize_path(file)
112
+ return false
113
+ entry.recovery = file
114
+ entry.blocked = false
115
+ return true
116
+ func _changed(id: int) -> void:
117
+ if not active or mutating or not entries.has(id): return
118
+ var entry: Dictionary = entries[id]
119
+ var code = entry.code.get_ref()
120
+ if code == null: return
121
+ if code.text != entry.baseline:
122
+ # Native external reload can already contain a newly authenticated build.
123
+ if code.text == _display(entry.path) and not _verified(entry.path).is_empty(): entry.baseline = code.text
124
+ elif not _preserve(entry,code.text): _present(); return
125
+ else: _replace(entry,entry.baseline)
126
+ _present()
127
+ func _replace(entry: Dictionary, text: String) -> void:
128
+ text = text.replace("\r\n","\n")
129
+ var code = entry.code.get_ref()
130
+ if code == null: return
131
+ mutating = true
132
+ var line: int = code.get_caret_line()
133
+ var column: int = code.get_caret_column()
134
+ var vertical: float = code.scroll_vertical
135
+ var horizontal: int = code.scroll_horizontal
136
+ var breakpoints: PackedInt32Array = code.get_breakpointed_lines()
137
+ if code.text != text:
138
+ # Whole-buffer assignment clears CodeEdit gutters and emits native
139
+ # breakpoint removals. Update line contents instead, retaining native
140
+ # inspection state on existing lines (including unchanged breakpoints).
141
+ var lines := text.split("\n")
142
+ code.begin_complex_operation()
143
+ while code.get_line_count() < lines.size(): code.insert_line_at(code.get_line_count()-1,"")
144
+ while code.get_line_count() > lines.size():
145
+ var last: int = code.get_line_count()-1
146
+ code.remove_text(last-1,code.get_line(last-1).length(),last,code.get_line(last).length())
147
+ for index in range(lines.size()):
148
+ if code.get_line(index) != lines[index]: code.set_line(index,lines[index])
149
+ code.end_complex_operation()
150
+ for marked_line in breakpoints:
151
+ if marked_line < code.get_line_count() and not code.is_line_breakpointed(marked_line): code.set_line_as_breakpoint(marked_line,true)
152
+ code.set_caret_line(line)
153
+ code.set_caret_column(column)
154
+ code.scroll_vertical = vertical
155
+ code.scroll_horizontal = horizontal
156
+ code.tag_saved_version()
157
+ entry.baseline = text
158
+ mutating = false
159
+ func prepare() -> bool:
160
+ problem = ""
161
+ for editor in interface.get_script_editor().get_open_script_editors():
162
+ var code = editor.get_base_editor()
163
+ if code is CodeEdit: attach(editor,code,_path(editor,code))
164
+ for id in entries.keys():
165
+ _changed(id)
166
+ if entries.has(id) and entries[id].blocked:
167
+ if problem.is_empty(): problem = entries[id].path+": Generated edit recovery failed; save/build blocked."
168
+ _present()
169
+ return problem.is_empty()
170
+ func _path(editor: ScriptEditorBase, code: TextEdit) -> String:
171
+ var scripts := interface.get_script_editor()
172
+ var script := scripts.get_current_script()
173
+ if script != null and scripts.get_current_editor() == editor: return script.resource_path
174
+ return association._path(editor,code,false)
175
+ func refresh() -> void:
176
+ for entry in entries.values():
177
+ if not entry.blocked and not _verified(entry.path).is_empty(): _replace(entry,FileAccess.get_file_as_string(entry.path))
178
+ _present()
179
+ func _present() -> void:
180
+ if not active: return
181
+ selected = 0
182
+ var editor := interface.get_script_editor().get_current_editor()
183
+ for id in entries:
184
+ if entries[id].editor.get_ref() == editor: selected = id; break
185
+ panel.visible = selected != 0 and interface.get_script_editor().is_visible_in_tree()
186
+ if selected == 0:
187
+ # A missing source cannot authorize read-only or synchronization. Still
188
+ # explain its map claim on the selected native Script without redirecting.
189
+ var code = editor.get_base_editor() if editor != null else null
190
+ var source := Components.source_path(_path(editor,code)) if code is CodeEdit else ""
191
+ if not source.is_empty() and not FileAccess.file_exists(source):
192
+ panel.visible = interface.get_script_editor().is_visible_in_tree()
193
+ panel.text = "Gogo source missing"
194
+ panel.tooltip_text = "Unverified source association: "+source+". Restore the source; this tab remains editable."
195
+ if presentation != source:
196
+ presentation = source
197
+ var popup := panel.get_popup()
198
+ if action.is_valid() and popup.id_pressed.is_connected(action): popup.id_pressed.disconnect(action)
199
+ action = Callable()
200
+ popup.clear()
201
+ popup.add_item("Gogo source missing — restore "+source)
202
+ popup.set_item_disabled(0,true)
203
+ return
204
+ var entry: Dictionary = entries[selected]
205
+ var exists := FileAccess.file_exists(entry.source)
206
+ panel.text = "Generated: "+entry.source.get_file()
207
+ if not entry.recovery.is_empty(): panel.text = "Generated · edit recovered"
208
+ var status := "Read-only — edit the Gogo source"
209
+ if not exists: status = "Gogo source missing — restore it before editing"
210
+ elif _verified(entry.path).is_empty(): status = "Source map unavailable/changed — inspect only"
211
+ if entry.blocked:
212
+ status = "Recovery failed — Save/Play blocked"
213
+ panel.text = "Recovery failed!"
214
+ panel.tooltip_text = status+"\n"+entry.path+"\nGogo source: "+entry.source
215
+ var signature: String = str(selected)+status+entry.recovery+str(exists)
216
+ if signature == presentation: return
217
+ presentation = signature
218
+ var popup := panel.get_popup()
219
+ if action.is_valid() and popup.id_pressed.is_connected(action): popup.id_pressed.disconnect(action)
220
+ action = _action.bind(entry.source,entry.path,entry.recovery)
221
+ popup.id_pressed.connect(action,CONNECT_DEFERRED)
222
+ popup.clear()
223
+ popup.add_item(status,0)
224
+ popup.set_item_disabled(0,true)
225
+ popup.add_item("Open Gogo Source",1)
226
+ popup.set_item_disabled(1,not exists)
227
+ popup.add_separator()
228
+ popup.add_item("View Recovered Edit"+(" (preserved)" if not entry.recovery.is_empty() else ""),2)
229
+ popup.set_item_disabled(popup.get_item_index(2),entry.recovery.is_empty())
230
+ func _action(id: int, source: String, path: String, recovery: String) -> void:
231
+ if not active: return
232
+ if id == 1 and FileAccess.file_exists(source):
233
+ await navigation.open(source)
234
+ elif id == 2 and FileAccess.file_exists(recovery):
235
+ location.text = "Preserved edit for "+path+"\n"+ProjectSettings.globalize_path(recovery)+"\nOlder recovered versions remain in this directory. Copy intended changes into "+source+"."
236
+ preview.text = FileAccess.get_file_as_string(recovery)
237
+ viewer.popup_centered()
238
+ func _closed(id: int) -> void:
239
+ if entries.has(id): _detach(id)
240
+ func _detach(id: int) -> void:
241
+ var entry: Dictionary = entries[id]
242
+ var code = entry.code.get_ref()
243
+ if code != null:
244
+ for pair in [["text_changed","changed"],["text_set","changed"],["tree_exiting","closing"]]:
245
+ if code.is_connected(pair[0],entry[pair[1]]): code.disconnect(pair[0],entry[pair[1]])
246
+ code.editable = entry.editable
247
+ entries.erase(id)
248
+ func stop() -> void:
249
+ active = false
250
+ for id in entries.keys(): _detach(id)
251
+ interface.get_script_editor().visibility_changed.disconnect(_present)
252
+ if action.is_valid() and panel.get_popup().id_pressed.is_connected(action): panel.get_popup().id_pressed.disconnect(action)
253
+ action = Callable()
254
+ plugin.remove_control_from_container(EditorPlugin.CONTAINER_TOOLBAR,panel)
255
+ panel.free()
256
+ viewer.free()
@@ -0,0 +1 @@
1
+ uid://g6qi8b62ebi5
@@ -3,5 +3,5 @@
3
3
  name="Estragon"
4
4
  description="Native Gogo/GGX highlighting, compilation and validation before Play."
5
5
  author="Resist Design"
6
- version="0.1.0-beta.10"
6
+ version="0.1.0-beta.11"
7
7
  script="plugin.gd"
@@ -27,7 +27,8 @@ var _active: bool = false
27
27
  var _extensions_pending: bool = true
28
28
  var _rescan_pending: bool = false
29
29
  var _play_save_failed: bool = false
30
- var _generated_editors: Dictionary = {}
30
+ const GeneratedEditors = preload("generated_editors.gd")
31
+ var _generated_editors: RefCounted
31
32
  var last_report: Dictionary = {}
32
33
  var build_count: int = 0
33
34
 
@@ -50,6 +51,8 @@ func _enter_tree() -> void:
50
51
  _interface.get_script_editor().register_syntax_highlighter(_highlighter)
51
52
  _highlighter_association = HighlighterAssociation.new()
52
53
  _navigation = Navigation.new(_interface)
54
+ _generated_editors = GeneratedEditors.new(self,_navigation,_highlighter_association)
55
+ _highlighter_association.associated.connect(_generated_editors.attach)
53
56
  _services = Services.new(_interface,_navigation)
54
57
  add_child(_services)
55
58
  _services.diagnostics_changed.connect(_typing_report)
@@ -127,41 +130,12 @@ func _script_changed(script: Script) -> void:
127
130
  if script == null or scripts.get_current_script() != script: return
128
131
  var editor := scripts.get_current_editor()
129
132
  if editor == null: return
130
- _generated_editors[script.resource_path] = {"editor": weakref(editor), "script": weakref(script)}
133
+ _generated_editors.prepare()
131
134
  _schedule()
132
135
 
133
136
 
134
137
  func _refresh_generated_editors() -> void:
135
- # Native ScriptEditor buffers otherwise write pre-build text back into the
136
- # Script cache and even disk on shutdown. Match only public signal/current
137
- # editor associations and paths this successful build owns. Never edit Gogo.
138
- var scripts := _interface.get_script_editor()
139
- var current := scripts.get_current_script()
140
- if current != null and scripts.get_current_editor() != null:
141
- _generated_editors[current.resource_path] = {"editor": weakref(scripts.get_current_editor()), "script": weakref(current)}
142
- for output in last_report.get("outputs", []):
143
- var path := "res://" + str(output)
144
- if not _generated_editors.has(path): continue
145
- var editor = _generated_editors[path].editor.get_ref()
146
- var script = _generated_editors[path].script.get_ref()
147
- if editor == null or script == null or script.resource_path != path:
148
- _generated_editors.erase(path)
149
- continue
150
- var code = editor.get_base_editor()
151
- if not code is CodeEdit: continue
152
- var text := FileAccess.get_file_as_string(path)
153
- if code.text != text:
154
- var line: int = code.get_caret_line()
155
- var column: int = code.get_caret_column()
156
- var vertical: float = code.scroll_vertical
157
- var horizontal: int = code.scroll_horizontal
158
- code.text = text
159
- code.set_caret_line(line)
160
- code.set_caret_column(column)
161
- code.scroll_vertical = vertical
162
- code.scroll_horizontal = horizontal
163
- code.tag_saved_version()
164
-
138
+ _generated_editors.refresh()
165
139
 
166
140
  func _filesystem_changed() -> void:
167
141
  if _services != null: _services.invalidate()
@@ -184,6 +158,7 @@ func _resource_saved(resource: Resource) -> void:
184
158
 
185
159
 
186
160
  func _save_external_data() -> void:
161
+ if _active and _generated_editors != null: _generated_editors.prepare()
187
162
  _schedule()
188
163
 
189
164
 
@@ -209,6 +184,7 @@ func _build_sources() -> bool:
209
184
  if not _active or _building:
210
185
  return false
211
186
  _timer.stop()
187
+ if not _generated_editors.prepare(): return _generated_failure()
212
188
  var saved := BufferGuard.save_and_verify(_interface.get_script_editor(), _highlighter_association._path.bind(false))
213
189
  _timer.stop()
214
190
  _play_save_failed = not saved.ok
@@ -220,8 +196,14 @@ func _build_sources() -> bool:
220
196
  return _compile()
221
197
 
222
198
 
199
+ func _generated_failure() -> bool:
200
+ last_report = {"ok":false,"outputs":[],"changed":[],"diagnostics":[_generated_editors.problem]}
201
+ _show_report()
202
+ return false
203
+
223
204
  func _compile() -> bool:
224
205
  if not _active or _building: return false
206
+ if not _generated_editors.prepare(): return _generated_failure()
225
207
  _building = true
226
208
  build_count += 1
227
209
  last_report = BuildService.run_build()
@@ -276,6 +258,10 @@ func _exit_tree() -> void:
276
258
  _creation_menu.create = Callable()
277
259
  _creation_menu = null
278
260
  remove_tool_menu_item("Add Missing Gogo Imports")
261
+ if _generated_editors != null:
262
+ _highlighter_association.associated.disconnect(_generated_editors.attach)
263
+ _generated_editors.stop()
264
+ _generated_editors = null
279
265
  if _navigation != null: _navigation.stop()
280
266
  if _new_script != null: _new_script.stop(); _new_script = null
281
267
  if _services != null: _services.stop(); _services.free(); _services = null
@@ -293,7 +279,6 @@ func _exit_tree() -> void:
293
279
  var scripts := _interface.get_script_editor()
294
280
  if scripts.editor_script_changed.is_connected(_script_changed):
295
281
  scripts.editor_script_changed.disconnect(_script_changed)
296
- _generated_editors.clear()
297
282
  if resource_saved.is_connected(_resource_saved):
298
283
  resource_saved.disconnect(_resource_saved)
299
284
  if _timer != null:
@@ -58,12 +58,14 @@ static func _res(path: String) -> String:
58
58
  return path if path.begins_with("res://") else "res://"+path
59
59
  static func source_for(script: Script) -> String:
60
60
  if script == null or script.resource_path.is_empty(): return ""
61
- var path := script.resource_path
61
+ return source_path(script.resource_path)
62
+ static func source_path(path: String) -> String:
63
+ if not path.begins_with("res://") or path.contains("::") or path.simplify_path() != path: return ""
62
64
  if not FileAccess.file_exists(path+".map.json"): return ""
63
65
  var map = JSON.parse_string(FileAccess.get_file_as_string(path+".map.json"))
64
66
  if not map is Dictionary or map.get("format") != "estragon.gogo.source-map" or map.get("version") != 1 or not map.get("sourceName") is String: return ""
65
67
  var source := _res(map.sourceName)
66
- return source if Build.output_for(source) == path else ""
68
+ return source if source.ends_with(".gogo") and source.simplify_path() == source and Build.output_for(source) == path else ""
67
69
  static func eligible(script: Script) -> bool:
68
70
  if not script is GDScript: return false
69
71
  var base := script.get_instance_base_type()
package/package.json CHANGED
@@ -29,5 +29,5 @@
29
29
  "tag": "beta",
30
30
  "registry": "https://registry.npmjs.org/"
31
31
  },
32
- "version": "0.1.0-beta.10"
32
+ "version": "0.1.0-beta.11"
33
33
  }