forked from KoBeWi/Another-Godot-4-Converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.rb
155 lines (133 loc) · 4.23 KB
/
scene.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class Scene
CONST_LINES = ["\n", "[gd_scene", "[ext_resource"]
def initialize(file)
@bits = []
@lines = File.readlines(file)
@lines.each.with_index do |line, i|
if CONST_LINES.find{|c| line.start_with?(c)}
if @in_node &.> 0
extract_node(i)
@in_node = nil
@bits << line
elsif not @in_node
@bits << line
end
elsif line.start_with?("[node name=")
if not @in_node
@in_node = i
else
extract_node(i)
@in_node = i
end
elsif line.start_with?("[sub_resource type=")
if not @in_node
@in_node = -i
else
extract_node(i)
@in_node = -i
end
end
end
if @in_node
extract_node(@lines.length)
end
end
def extract_node(to)
if @in_node > 0
@bits << Node.new(@lines[@in_node...to])
else
@bits << Resource.new(@lines[-@in_node...to])
end
end
def to_s
@bits.collect(&:to_s).join
end
end
class Property
attr_accessor :name
attr_reader :value
def initialize(line)
property = line.match %r{(?<name>[^=]+) = (?<value>.+)}
@name = property["name"]
@value = property["value"]
do_conversions
end
def add_source(code)
if code.last.end_with?("\"\n") and not code.last.end_with?("\\\"\n")
code.last.chomp!("\"\n")
code.last << "\n"
end
code[0] = code.first.to_s.reverse.chomp("script/source = \"".reverse).reverse
@value = '"' + Script.new(code.collect{|line| line.gsub('\"', '"')}).to_s.collect{|line| line.end_with?("\n") ? line : line + "\n"}.join.gsub('"', '\"') + '"'
end
def to_s
"#{@name} = #{@value}\n" + @double.to_s
end
end
class Node
def initialize(lines)
@lines = lines
@properties = []
type = lines[0].match(%r{type="([^"]+)"})
@instance = !type
if not @instance
type = type[1]
@type = TYPE_CONVERSIONS.fetch(type, type)
lines[0].sub!("type=\"#{type}\"", "type=\"#{@type}\"")
end
@lines.collect! do |line|
if not line.include?("=") or line.start_with?("[")
line
else
Property.new(line)
end
end
do_conversions
end
def to_s
@lines.collect(&:to_s)
end
end
class Resource
def initialize(lines)
@lines = lines
@properties = []
type = lines[0].match(%r{type="([^"]+)"})[1]
@type = TYPE_CONVERSIONS.fetch(type, type)
lines[0].sub!("type=\"#{type}\"", "type=\"#{@type}\"")
@lines.collect!.with_index do |line, i|
if @in_script
if (m1 = line == "\"\n") or line.match(%r{[^\\]\"})
@script_property.add_source(@lines[@in_script..(m1 ? i - 1 : i)])
@lines.slice! (@in_script + 1)..i
@in_script = @script_property = nil
next
end
next line
end
if @in_shader
if (m1 = line == "\"\n") or line.match(%r{[^\\]\"})
@in_shader = false
end
next line
end
if not line.include?("=") or line.start_with?("[")
line
else
property = Property.new(line)
if property.name == "script/source"
@in_script = i
@script_property = property
end
if property.name == "code"
@in_shader = true
end
property
end
end
do_conversions
end
def to_s
@lines.collect(&:to_s)
end
end