]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Resources/TimelineResourceSaver.gd
Updated export config options
[wolf-seeking-sheep.git] / addons / dialogic / Resources / TimelineResourceSaver.gd
1 @tool
2 class_name DialogicTimelineFormatSaver
3 extends ResourceFormatSaver
4
5
6 func _get_recognized_extensions(_resource: Resource) -> PackedStringArray:
7         return PackedStringArray(["dtl"])
8
9
10 ## Return true if this resource should be loaded as a DialogicTimeline
11 func _recognize(resource: Resource) -> bool:
12         # Cast instead of using "is" keyword in case is a subclass
13         resource = resource as DialogicTimeline
14
15         if resource:
16                 return true
17
18         return false
19
20
21 ## Save the resource
22 ## TODO: This should use timeline.as_text(), why is this still here?
23 func _save(resource: Resource, path: String = '', _flags: int = 0) -> Error:
24         if resource.get_meta("timeline_not_saved", false):
25
26                 var timeline_as_text := ""
27                 # if events are resources, create text
28                 if resource.events_processed:
29
30                         var indent := 0
31                         for idx in range(0, len(resource.events)):
32                                 if resource.events[idx]:
33                                         var event: DialogicEvent = resource.events[idx]
34                                         if event.event_name == 'End Branch':
35                                                 indent -=1
36                                                 continue
37
38                                         for i in event.empty_lines_above:
39                                                 timeline_as_text += '\t'.repeat(indent) + '\n'
40
41                                         if event != null:
42                                                 timeline_as_text += "\t".repeat(indent)+ event.event_node_as_text + "\n"
43                                         if event.can_contain_events:
44                                                 indent += 1
45                                         if indent < 0:
46                                                 indent = 0
47
48                 # if events are string lines, just save them
49                 else:
50                         for event in resource.events:
51                                 timeline_as_text += event + "\n"
52
53                 # Now do the actual saving
54                 var file := FileAccess.open(path, FileAccess.WRITE)
55                 if !file:
56                         print("[Dialogic] Error opening file:", FileAccess.get_open_error())
57                         return ERR_CANT_OPEN
58                 file.store_string(timeline_as_text)
59                 file.close()
60
61         return OK