summaryrefslogtreecommitdiff
path: root/src/material/material.c
blob: 65e13d2fa1e0051869f3489271d4899714151713 (plain)
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
#include <kalmia.h>
#include "util/util.h"
#include "xml/xml.h"


int kai_read_instance_effect(struct ka_instance_effect_t *dest, struct kai_tag_t *src)
{
	KAI_CHECK_TAG_TYPE("instance_effect");

	char *url = kai_tag_attr_to_dup(src, "url");
	if (url == NULL) { return -1; }

	dest->sid = kai_tag_attr_to_dup(src, "sid");
	dest->name = kai_tag_attr_to_dup(src, "name");
	dest->url = url;

	return 0;
}

void kai_release_instance_effect(struct ka_instance_effect_t i)
{
	free(i.sid);
	free(i.name);
	free(i.url);
}


int kai_read_material(struct ka_material_t *dest, struct kai_tag_t *src)
{
	KAI_CHECK_TAG_TYPE("material");

	struct kai_tag_t *child = kai_tag_get_first_child_with_type(src, "instance_effect");
	if (child == NULL) { return -1; }
	int result = kai_read_instance_effect(&(dest->instance_effect), child);
	if (result != 0) { return -1; }

	dest->id = kai_tag_attr_to_dup(src, "id");
	dest->name = kai_tag_attr_to_dup(src, "name");

	return 0;
}

void kai_release_material(struct ka_material_t m)
{
	free(m.id);
	free(m.name);
	kai_release_instance_effect(m.instance_effect);
}


int kai_read_library_materials(struct ka_library_materials_t *dest, struct kai_tag_t *src)
{
	int result;
	KAI_FILL_ARRAY_FROM_TAGS(
		result,
		dest->material, dest->material_count, struct ka_material_t,
		src, "material",
		kai_read_material, kai_release_material
	);
	if (result != 0) { return -1; }

	dest->id = kai_tag_attr_to_dup(src, "id");
	dest->name = kai_tag_attr_to_dup(src, "name");

	return 0;
}

void kai_release_library_materials(struct ka_library_materials_t l)
{
	free(l.id);
	free(l.name);
	int i;
	for (i=0; i<l.material_count; i++) {
		kai_release_material(l.material[i]);
	}
	free(l.material);
}