aboutsummaryrefslogtreecommitdiff
path: root/src/obj.c
blob: 71c534c85b2f4aa971579448c46db61b9af6b75e (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
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
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   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 <http://www.gnu.org/licenses/>.
 */

#include "globals.h"

#include "hash.h"
#include "obj.h"

/* map of class names -> object classes */
static void *obj_class_map = NULL;

static obj_id idcounter = 1;

obj_id obj_get_idcounter(void)
{
    return idcounter;
}

void obj_set_idcounter(obj_id c)
{
    idcounter = c;
}

struct object_t *obj_new(const char *class_name)
{
    if(!obj_class_map)
    {
        extern const struct obj_class_t netcosm_obj_classes[];
        extern const size_t netcosm_obj_classes_sz;
        obj_class_map = hash_init(netcosm_obj_classes_sz / 2 + 1,
                                  hash_djb,
                                  compare_strings);
        for(unsigned i = 0; i < netcosm_obj_classes_sz; ++i)
        {
            if(hash_insert(obj_class_map,
                           netcosm_obj_classes[i].class_name,
                           netcosm_obj_classes + i))
                error("duplicate object class name");
        }
    }

    struct object_t *obj = calloc(1, sizeof(struct object_t));

    obj->class = hash_lookup(obj_class_map, class_name);

    if(!obj->class)
    {
        free(obj);
        error("unknown object class '%s'", class_name);
    }

    obj->id = idcounter++;
    obj->refcount = 1;

    return obj;
}

void obj_write(int fd, struct object_t *obj)
{
    write_string(fd, obj->class->class_name);

    write_uint64(fd, obj->id);

    write_string(fd, obj->name);
    write_bool(fd, obj->list);

    if(obj->class->hook_serialize)
        obj->class->hook_serialize(fd, obj);
}

struct object_t *obj_read(int fd)
{
    char *class_name = read_string(fd);
    struct object_t *obj = obj_new(class_name);
    free(class_name);

    obj->id = read_uint64(fd);

    obj->name = read_string(fd);
    obj->list = read_bool(fd);
    if(obj->class->hook_deserialize)
        obj->class->hook_deserialize(fd, obj);

    return obj;
}

struct object_t *obj_copy(struct object_t *obj)
{
    struct object_t *ret = obj_new(obj->class->class_name);
    ret->name = strdup(obj->name);
    ret->list = obj->list;
    ret->userdata = obj->class->hook_dupdata(obj);
    return ret;
}

struct object_t *obj_dup(struct object_t *obj)
{
    debugf("Adding an object reference for #%lu.\n", obj->id);
    ++obj->refcount;
    return obj;
}

void obj_free(void *ptr)
{
    struct object_t *obj = ptr;
    --obj->refcount;

    debugf("Freeing an object reference for #%lu (%s, %d).\n", obj->id, obj->name, obj->refcount);

    if(!obj->refcount)
    {
        debugf("Freeing object #%lu\n", obj->id);
        if(obj->class->hook_destroy)
            obj->class->hook_destroy(obj);

        free(obj->name);
        free(obj);
    }
}

void obj_shutdown(void)
{
    hash_free(obj_class_map);
    obj_class_map = NULL;
}

int obj_compare(const void *a, const void *b)
{
    const struct object_t *c = a, *d = b;
    return !(c->id == d->id);
}