|  | 
|  | 1 | +extends Object | 
|  | 2 | +class_name Collection | 
|  | 3 | +## | 
|  | 4 | +## This class is used for getting a collection of datatable. | 
|  | 5 | +## | 
|  | 6 | +## To get the collection, you need the path to your collection file (should end by ".tableCollection.res", or just ".res" if you use the old collection system)[br] | 
|  | 7 | +## [br] | 
|  | 8 | +## Example: | 
|  | 9 | +## [codeblock] | 
|  | 10 | +## func _ready(): | 
|  | 11 | +##      | 
|  | 12 | +##     var collection: Collection = Collection.new("res://datatable.tableCollection.res") | 
|  | 13 | +##      | 
|  | 14 | +##     if !collection.has_table("myTable"): # Checking if the table exist | 
|  | 15 | +##         push_error("The table 'myTable' doesn't exist!") | 
|  | 16 | +##         return | 
|  | 17 | +##      | 
|  | 18 | +##     var datatable: Datatable = collection.get_table("myTable") # Getting the table with the name "myTable" | 
|  | 19 | +##      | 
|  | 20 | +##     if !datatable.has_item("myItem"): # Checking if the item exist inside the table "myTable" | 
|  | 21 | +##         push_error("The table 'myTable' doesn't have 'myItem' key!") | 
|  | 22 | +##         return | 
|  | 23 | +##      | 
|  | 24 | +##     var item: Dictionary = datatable.get_item("myItem") # Getting the item "myItem" inside "myTable" | 
|  | 25 | +##      | 
|  | 26 | +##     print(item) # print the result | 
|  | 27 | +## [/codeblock] | 
|  | 28 | +## [b] | 
|  | 29 | +## You can copy a personalised template code for your own item / table by right clicking on your table / item | 
|  | 30 | +## [/b] | 
|  | 31 | + | 
|  | 32 | +var _collection_path: String | 
|  | 33 | + | 
|  | 34 | +var _collection: Object | 
|  | 35 | + | 
|  | 36 | +const _collection_required_key = ["table", "type"] | 
|  | 37 | + | 
|  | 38 | +func _init(collection_path: String): | 
|  | 39 | +	 | 
|  | 40 | +	if !ResourceLoader.exists(collection_path, "PackedDataContainer"): | 
|  | 41 | +		assert(false, str("The path ", collection_path, " isn't an collection!")) | 
|  | 42 | +	 | 
|  | 43 | +	var collection = ResourceLoader.load(collection_path) | 
|  | 44 | +	 | 
|  | 45 | +	var required_key: int = 0 | 
|  | 46 | +	 | 
|  | 47 | +	for i in collection: | 
|  | 48 | +		if _collection_required_key.has(i): | 
|  | 49 | +			required_key += 1 | 
|  | 50 | +	 | 
|  | 51 | +	if required_key != _collection_required_key.size(): | 
|  | 52 | +		assert(false, str("The path ", collection_path, " isn't a valid collection file! (missing key)")) | 
|  | 53 | +	 | 
|  | 54 | +	_collection = collection | 
|  | 55 | +	_collection_path = collection_path | 
|  | 56 | + | 
|  | 57 | +## Get the table with the given name inside this collection | 
|  | 58 | +func get_table(table_name: String) -> Datatable: | 
|  | 59 | +	 | 
|  | 60 | +	if has_table(table_name): | 
|  | 61 | +		var table = Datatable.new(_collection, table_name) | 
|  | 62 | +		 | 
|  | 63 | +		table.table_saved.connect(_on_save_collection) | 
|  | 64 | +		 | 
|  | 65 | +		return table | 
|  | 66 | +	 | 
|  | 67 | +	return null | 
|  | 68 | + | 
|  | 69 | +## Check if the table exist inside this collection[br] | 
|  | 70 | +## For your information: This function is called each time you try to get a table! | 
|  | 71 | +func has_table(table_name: String) -> bool: | 
|  | 72 | +	 | 
|  | 73 | +	var data = _get_table_datas() | 
|  | 74 | +	 | 
|  | 75 | +	return data.has(table_name) | 
|  | 76 | + | 
|  | 77 | +## Check if the structure exist inside this collection[br] | 
|  | 78 | +## For your information: This function is called each time you try to get a structure! | 
|  | 79 | +func has_struct(struct_name: String) -> bool: | 
|  | 80 | +	 | 
|  | 81 | +	var data = _get_table_structs() | 
|  | 82 | +	 | 
|  | 83 | +	return data.has(struct_name) | 
|  | 84 | + | 
|  | 85 | +## Return the structure data. | 
|  | 86 | +## [br] | 
|  | 87 | +## You can use that to manually check the data that you can get from a table | 
|  | 88 | +## Dictionary key:[br] | 
|  | 89 | +## - name[br] | 
|  | 90 | +## - params[br] | 
|  | 91 | +## - params.[b](key)[/b].name[br] | 
|  | 92 | +## - params.[b](key)[/b].size[br] | 
|  | 93 | +## - params.[b](key)[/b].type[br] | 
|  | 94 | +## [br] | 
|  | 95 | +## Example: | 
|  | 96 | +## [codeblock] | 
|  | 97 | +## { | 
|  | 98 | +##     "name": "Simple structure", | 
|  | 99 | +##     "params": { | 
|  | 100 | +##         "A string": { | 
|  | 101 | +##             "name": "A String", | 
|  | 102 | +##             "size": 0, | 
|  | 103 | +##             "type": 0 | 
|  | 104 | +##         }, | 
|  | 105 | +##         "Lot of float": { | 
|  | 106 | +##             "name": "An array of float", | 
|  | 107 | +##             "size": 1, | 
|  | 108 | +##             "type": 2 | 
|  | 109 | +##         } | 
|  | 110 | +##     } | 
|  | 111 | +## } | 
|  | 112 | +## [/codeblock] | 
|  | 113 | +func get_struct(struct_name: String) -> Dictionary: | 
|  | 114 | +	 | 
|  | 115 | +	if has_struct(struct_name): | 
|  | 116 | +		return _get_table_structs()[struct_name] | 
|  | 117 | +	 | 
|  | 118 | +	return {"error": str("The structure ",struct_name," doesn't exist!")} | 
|  | 119 | + | 
|  | 120 | +# Return the tables list of the datatable | 
|  | 121 | +func _get_table_datas()->Dictionary: | 
|  | 122 | +	 | 
|  | 123 | +	var table_datas = {} | 
|  | 124 | +	 | 
|  | 125 | +	var packedData = _collection | 
|  | 126 | +	 | 
|  | 127 | +	var table_data = packedData['table'] | 
|  | 128 | +	 | 
|  | 129 | +	for main_key in table_data: | 
|  | 130 | +		 | 
|  | 131 | +		var data = table_data[main_key] | 
|  | 132 | +		 | 
|  | 133 | +		table_datas[main_key] = {"size": data['size'], "structure": data['structure'], "rows": {}} | 
|  | 134 | +		 | 
|  | 135 | +		 | 
|  | 136 | +		for row_key in data['rows']: | 
|  | 137 | +			 | 
|  | 138 | +			var row_data = data['rows'][row_key] # name, columns | 
|  | 139 | +			 | 
|  | 140 | +			 | 
|  | 141 | +			table_datas[main_key]['rows'][row_key] = {} | 
|  | 142 | +			 | 
|  | 143 | +			 | 
|  | 144 | +			for column_key in row_data['columns']: | 
|  | 145 | +				 | 
|  | 146 | +				var column_data = row_data['columns'][column_key] # name, value, type | 
|  | 147 | +				 | 
|  | 148 | +				 | 
|  | 149 | +				# As the "size" object has been created after first release, we do a check so if the size | 
|  | 150 | +				# is not inside the data, we add it with the default value | 
|  | 151 | +				if column_data.size() == 3: | 
|  | 152 | +					column_data['size'] = Datatable.ItemSize.SIZE_SINGLE | 
|  | 153 | +				 | 
|  | 154 | +				if column_data['size'] == Datatable.ItemSize.SIZE_ARRAY: | 
|  | 155 | +					var arr_raw: String = column_data['value'] | 
|  | 156 | +					 | 
|  | 157 | +					if arr_raw.begins_with(" ARR/ "): | 
|  | 158 | +						arr_raw = arr_raw.replace(" ARR/ ", "") | 
|  | 159 | +					 | 
|  | 160 | +					var arr: PackedStringArray = arr_raw.split(";") | 
|  | 161 | +					 | 
|  | 162 | +					var arr_value = [] | 
|  | 163 | +					 | 
|  | 164 | +					if arr.size() == 1: | 
|  | 165 | +						if arr[0].is_empty(): | 
|  | 166 | +							table_datas[main_key]['rows'][row_key][column_key] = arr_value | 
|  | 167 | +							continue | 
|  | 168 | +					 | 
|  | 169 | +					for item in arr: | 
|  | 170 | +						 | 
|  | 171 | +						var arr_item = Datatable._convert_complex_string_to_data(column_data["type"], item) | 
|  | 172 | +						 | 
|  | 173 | +						arr_value.append(arr_item) | 
|  | 174 | +					 | 
|  | 175 | +					table_datas[main_key]['rows'][row_key][column_key] = arr_value | 
|  | 176 | +					continue | 
|  | 177 | +				 | 
|  | 178 | +				table_datas[main_key]['rows'][row_key][column_key] = Datatable._convert_complex_string_to_data(column_data['type'], column_data['value']) | 
|  | 179 | + | 
|  | 180 | +	 | 
|  | 181 | +	return table_datas | 
|  | 182 | + | 
|  | 183 | +# Return the structs list of the datatable | 
|  | 184 | +func _get_table_structs()->Dictionary: | 
|  | 185 | +	 | 
|  | 186 | +	var table_types = {} | 
|  | 187 | +	 | 
|  | 188 | +	var packedData = _collection | 
|  | 189 | +	 | 
|  | 190 | +	var type_data = packedData['type'] | 
|  | 191 | +	 | 
|  | 192 | +	for main_key in type_data: # name of type | 
|  | 193 | +		 | 
|  | 194 | +		var type = type_data[main_key] | 
|  | 195 | +		 | 
|  | 196 | +		 | 
|  | 197 | +		table_types[main_key] = {"name": type['name'], "params":{}} | 
|  | 198 | +		 | 
|  | 199 | +		for param_key in type['params']: | 
|  | 200 | +			 | 
|  | 201 | +			var param = type["params"][param_key] | 
|  | 202 | +			 | 
|  | 203 | +			 | 
|  | 204 | +			table_types[main_key]["params"][param_key] = {"name":param['name'], "type": param['type'], "size": param['size']} | 
|  | 205 | +	 | 
|  | 206 | +	return table_types | 
|  | 207 | + | 
|  | 208 | +# response for the save_data signal emitted by the Datatable object | 
|  | 209 | +func _on_save_collection(data:PackedDataContainer): | 
|  | 210 | +	 | 
|  | 211 | +	ResourceSaver.save(data, _collection_path) | 
0 commit comments