-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtrenchbroom_game_config_file.gd
167 lines (140 loc) · 4.39 KB
/
trenchbroom_game_config_file.gd
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
156
157
158
159
160
161
162
163
164
165
166
167
class_name TrenchBroomGameConfigFile
extends Resource
tool
export(bool) var export_file : bool setget set_export_file
export(String, FILE, GLOBAL, "*.cfg") var target_file : String
export(String) var game_name := "Qodot"
export(Array, Resource) var brush_tags : Array = []
export(Array, Resource) var face_tags : Array = []
export(Resource) var face_attrib_defaults : Resource = null setget set_face_attrib_defaults
export(Array, Resource) var face_attrib_surface_flags : Array = []
export(Array, Resource) var face_attrib_content_flags : Array = []
export(Array, String) var fgd_filenames : Array = []
var base_text: String = """{
version: 3,
name: "%s",
icon: "Icon.png",
"fileformats": [
{ "format": "Standard", "initialmap": "initial_standard.map" },
{ "format": "Valve", "initialmap": "initial_valve.map" },
{ "format": "Quake2", "initialmap": "initial_quake2.map" },
{ "format": "Quake3" },
{ "format": "Quake3 (legacy)" },
{ "format": "Hexen2" },
{ "format": "Daikatana" }
],
"filesystem": {
"searchpath": ".",
"packageformat": { "extension": "pak", "format": "idpak" }
},
"textures": {
"package": { "type": "directory", "root": "textures" },
"format": { "extensions": ["bmp", "exr", "hdr", "jpeg", "jpg", "png", "tga", "webp"], "format": "image" },
"attribute": "_tb_textures"
},
"entities": {
"definitions": [ %s ],
"defaultcolor": "0.6 0.6 0.6 1.0",
"modelformats": [ "mdl", "md2", "md3", "bsp", "dkm" ]
},
"tags": {
"brush": [
%s
],
"brushface": [
%s
]
},
"faceattribs": {
"defaults": %s,
"surfaceflags": [
%s
],
"contentflags": [
%s
]
}
}
"""
func set_export_file(new_export_file : bool = true) -> void:
if new_export_file != export_file:
if not Engine.is_editor_hint():
return
if not target_file:
print("Skipping export: No target file")
return
print("Exporting TrenchBroom Game Config File to ", target_file)
var file_obj := File.new()
file_obj.open(target_file, File.WRITE)
file_obj.store_string(build_class_text())
file_obj.close()
func set_face_attrib_defaults(new_face_attrib_defaults : Resource) -> void:
if !new_face_attrib_defaults is TrenchBroomFaceAttribDefaults:
face_attrib_defaults = null
else:
face_attrib_defaults = new_face_attrib_defaults
func build_class_text() -> String:
var fgd_filename_str := ""
for fgd_filename in fgd_filenames:
fgd_filename_str += "\"%s\"" % fgd_filename
if fgd_filename != fgd_filenames[-1]:
fgd_filename_str += ", "
var brush_tags_str = parse_tags(brush_tags)
var face_tags_str = parse_tags(face_tags)
var face_defaults_str = parse_defaults(face_attrib_defaults)
var surface_flags_str = parse_flags(face_attrib_surface_flags)
var content_flags_str = parse_flags(face_attrib_content_flags)
return base_text % [
game_name,
fgd_filename_str,
brush_tags_str,
face_tags_str,
face_defaults_str,
surface_flags_str,
content_flags_str
]
static func get_match_key(tag_match_type: int) -> String:
var tag_keys = {
0: "texture",
1: "contentflag",
2: "surfaceflag",
3: "surfaceparm",
4: "classname"
}
return tag_keys[tag_match_type]
func parse_tags(tags: Array) -> String:
var tags_str := ""
for brush_tag in tags:
tags_str += "{\n"
tags_str += "\t\t\t\t\"name\": \"%s\",\n" % brush_tag.tag_name
var attribs_str := ""
for brush_tag_attrib in brush_tag.tag_attributes:
attribs_str += "\"%s\"" % brush_tag_attrib
if brush_tag_attrib != brush_tag.tag_attributes[-1]:
attribs_str += ", "
tags_str += "\t\t\t\t\"attribs\": [ %s ],\n" % attribs_str
tags_str += "\t\t\t\t\"match\": \"%s\",\n" % get_match_key(brush_tag.tag_match_type)
tags_str += "\t\t\t\t\"pattern\": \"%s\"" % brush_tag.tag_pattern
if brush_tag.texture_name != "":
tags_str += ",\n"
tags_str += "\t\t\t\t\"texture\": \"%s\"" % brush_tag.texture_name
tags_str += "\n"
tags_str += "\t\t\t}"
if brush_tag != tags[-1]:
tags_str += ","
return tags_str
func parse_flags(flags: Array) -> String:
var flags_str := ""
for attrib_flag in flags:
flags_str += "{\n"
flags_str += "\t\t\t\t\"name\": \"%s\",\n" % attrib_flag.attrib_name
flags_str += "\t\t\t\t\"description\": \"%s\"\n" % attrib_flag.attrib_description
flags_str += "\t\t\t}"
if attrib_flag != flags[-1]:
flags_str += ","
return flags_str
func parse_defaults(defaults: TrenchBroomFaceAttribDefaults) -> String:
if defaults:
return defaults.to_string().replace("\n", "\n\t\t")
else:
return "{}"