]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
uobject: Add a helper function to load objects from packages.
[upkg.git] / src / uobject / uobject.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <glib-object.h>
24
25 #include <uobject/uobject.h>
26 #include <uobject/module.h>
27 #include "upkg.h"
28 #include "pack.h"
29
30 #define U_OBJECT_GET_PRIV(o) \
31         G_TYPE_INSTANCE_GET_PRIVATE(o, U_OBJECT_TYPE, struct u_object_priv)
32
33 enum {
34         PROPERTY_BYTE = 1,
35         PROPERTY_INTEGER,
36         PROPERTY_BOOLEAN,
37         PROPERTY_FLOAT,
38         PROPERTY_OBJECT,
39         PROPERTY_NAME,
40         PROPERTY_STRING,
41         PROPERTY_CLASS,
42         PROPERTY_ARRAY,
43         PROPERTY_STRUCT,
44         PROPERTY_VECTOR,
45         PROPERTY_ROTATOR,
46         PROPERTY_STR,
47         PROPERTY_MAP,
48         PROPERTY_FIXEDARRAY,
49 };
50
51 struct u_object_priv {
52         struct upkg_file *f;
53         size_t base, len;
54
55         unsigned char buf[2048];
56         unsigned long nbuf;
57 };
58
59 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
60
61 static unsigned long
62 get_real_size(unsigned long *real, unsigned size, unsigned char *buf, size_t n)
63 {
64         assert(size < 8);
65
66         *real = 0;
67         switch (size) {
68         case 0: *real =  1; return 0;
69         case 1: *real =  2; return 0;
70         case 2: *real =  4; return 0;
71         case 3: *real = 12; return 0;
72         case 4: *real = 16; return 0;
73         case 5:
74                 if (n < 1) return 0;
75                 *real = *buf;
76                 return 1;
77         case 6:
78                 if (n < 2) return 0;
79                 *real = unpack_16_le(buf);
80                 return 2;
81         case 7:
82                 if (n < 4) return 0;
83                 *real = unpack_32_le(buf);
84                 return 4;
85         }
86
87         return 0;
88 }
89
90 static unsigned long
91 decode_property(UObject *o, const char *name, struct upkg_file *f, unsigned long len)
92 {
93         struct u_object_priv *priv = U_OBJECT_GET_PRIV(o);
94         unsigned long real_size, rc;
95         int type, size, top;
96         GValue val = {0};
97
98         if (priv->nbuf-len < 1)
99                 return 0;
100         
101         type = (priv->buf[len] >> 0) & 0x0f;
102         size = (priv->buf[len] >> 4) & 0x07;
103         top  = (priv->buf[len] >> 7) & 0x01;
104         len += 1;
105
106         rc = get_real_size(&real_size, size, priv->buf, priv->nbuf-len);
107         if (real_size == 0)
108                 return 0;
109         len += rc;
110
111         switch (type) {
112         case PROPERTY_BYTE:
113                 if (priv->nbuf-len < 1)
114                         return 0;
115                 g_value_init(&val, G_TYPE_UCHAR);
116                 g_value_set_uchar(&val, priv->buf[len]);
117                 g_object_set_property(G_OBJECT(o), name, &val);
118                 break;
119         case PROPERTY_INTEGER:
120                 if (priv->nbuf-len < 4)
121                         return 0;
122                 g_value_init(&val, G_TYPE_ULONG);
123                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
124                 g_object_set_property(G_OBJECT(o), name, &val);
125                 break;
126         default:
127                 fprintf(stderr, "Unhandled property type %x\n", (unsigned)type);
128         }
129
130         real_size += len;
131         if (real_size + len <= priv->nbuf) {
132                 priv->nbuf -= real_size;
133                 memmove(priv->buf, priv->buf+real_size, priv->nbuf);
134         } else {
135                 long skip = real_size - priv->nbuf;
136                 if (upkg_export_seek(f, skip, SEEK_CUR) != 0)
137                         return 0;
138                 priv->nbuf = 0;
139         }
140
141         return real_size;
142 }
143
144 /* Deserialize properties from an Unreal package. */
145 static int deserialize(UObject *uo)
146 {
147         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
148         struct upkg_file *f = uo->pkg_file;
149         unsigned long rc, tot_len = 0;
150
151         while (1) {
152                 unsigned long len = 0;
153                 const char *name;
154                 long tmp;
155
156                 /* Read some data into buffer. */
157                 if (!f->eof) {
158                         void  *buf = priv->buf + priv->nbuf;
159                         size_t amt = sizeof priv->buf - priv->nbuf;
160                         rc = upkg_export_read(f, buf, amt);
161                         if (rc == 0 && priv->nbuf == 0)
162                                 return -1;
163                         priv->nbuf += rc;
164                 }
165
166                 /* Get the property name. */
167                 rc = upkg_decode_index(&tmp, priv->buf+len, priv->nbuf-len);
168                 if (rc == 0)
169                         return -1;
170                 len = rc;
171
172                 name = upkg_get_name(uo->pkg, tmp);
173                 if (!name) {
174                         return -1;
175                 } else if (strcmp(name, "None") == 0) {
176                         tot_len += len;
177                         break;
178                 }
179
180                 rc = decode_property(uo, name, f, len);
181                 if (rc == 0)
182                         return -1;
183                 len = rc;
184
185                 tot_len += len;
186         }
187
188         f->base += tot_len;
189         f->len  -= tot_len;
190         upkg_export_seek(f, 0, SEEK_SET);
191
192         return 0;
193 }
194
195 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
196 {
197         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
198         UObject *uo = U_OBJECT(obj);
199         struct upkg_file *f;
200         int rc;
201
202         g_return_val_if_fail(uo->pkg_file == NULL, -1);
203         f = upkg_export_open(pkg, idx);
204         if (!f) {
205                 return -1;
206         }
207
208         uo->pkg      = pkg;
209         uo->pkg_file = f;
210
211         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
212         if (rc != 0) {
213                 upkg_export_close(f);
214                 uo->pkg_file = NULL;
215         }
216
217         return rc;
218 }
219
220 GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx)
221 {
222         const struct upkg_export *export;
223         const char *class, *package;
224         GObject *obj = NULL;
225         GType type;
226
227         class = upkg_export_class(upkg, idx, &package);
228
229         type = u_object_module_get_class(package, class);
230         if (type) {
231                 obj = g_object_new(type, NULL);
232                 if (u_object_deserialize(obj, upkg, idx) != 0) {
233                         g_object_unref(obj);
234                         return NULL;
235                 }
236         }
237
238         return obj;
239 }
240
241 static void u_object_init(UObject *o)
242 {
243         o->pkg      = NULL;
244         o->pkg_file = NULL;
245 }
246
247 static void u_object_finalize(GObject *o)
248 {
249         UObject *uo = U_OBJECT(o);
250
251         if (uo->pkg_file) {
252                 upkg_export_close(uo->pkg_file);
253         }
254
255         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
256 }
257
258 static void u_object_class_init(UObjectClass *class)
259 {
260         g_type_class_add_private(class, sizeof (struct u_object_priv));
261         GObjectClass *go = G_OBJECT_CLASS(class);
262
263         class->deserialize = deserialize;
264         go->finalize       = u_object_finalize;
265 }