""" NBT Parser for Godot Engine Copyright (C) 2022 Jason Table This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ class_name SNBT enum { TAG_End, TAG_Byte, TAG_Short, TAG_Int, TAG_Long, TAG_Float, TAG_Double, TAG_Byte_Array, TAG_String, TAG_List, TAG_Compound, TAG_Int_Array, TAG_Long_Array, } var stream := "" #code for writing snbt files is below func to_snbt(data:Dictionary) -> String: #create stream stream = "" #validate and parse dictionary data if data.size() != 1: printerr("Invalid. Root dictonary must contain one entry") return "" _write_compound(data) return stream func _write_data_type(data:Dictionary): var type = data.type if type == TAG_Byte: return _write_byte(data.data) elif type == TAG_Short: return _write_short(data.data) elif type == TAG_Int: return _write_int(data.data) elif type == TAG_Long: return _write_long(data.data) elif type == TAG_Float: return _write_float(data.data) elif type == TAG_Double: return _write_double(data.data) elif type == TAG_Byte_Array: return _write_byte_array(data.data) elif type == TAG_String: return _write_string(data.data) elif type == TAG_List: return _write_list(data.data) elif type == TAG_Compound: return _write_compound(data.data) elif type == TAG_Int_Array: return _write_int_array(data.data) elif type == TAG_Long_Array: return _write_long_array(data.data) func _write_byte(data:int): stream += str(data,"b") func _write_short(data:int): stream += str(data,"s") func _write_int(data:int): stream += str(data) func _write_long(data:int): stream += str(data,"l") func _write_float(data:float): stream += str(data,"f") func _write_double(data:float): stream += str(data,"d") func _write_byte_array(data:PoolByteArray): stream += "[B;" for i in data: stream += str(i,"b,") stream = stream.trim_suffix(",") stream += "]" func _write_string(data:String): var escaped = data.replace('"','\\"') stream += str('"',escaped,'"') func _write_list(data:Array): stream += "[" for i in data: _write_data_type(i) stream += "," stream = stream.trim_suffix(",") stream += "]" func _write_compound(data:Dictionary): stream += "{" for each in data.keys(): var key:String = each stream += str(key,":") var type = data[key].type _write_data_type(data[key]) stream += "," # Write end of compound tag: stream = stream.trim_suffix(",") stream += "}" func _write_int_array(data:PoolIntArray): stream += "[I;" for i in data: stream += str(i,",") stream = stream.trim_suffix(",") stream += "]" func _write_long_array(data:Array): stream += "[L;" for i in data: stream += str(i,"l,") stream = stream.trim_suffix(",") stream += "]"