]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
uobject: First stab at a generic logging mechanism.
[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 <stdarg.h>
23 #include <stdbool.h>
24 #include <inttypes.h>
25 #include <assert.h>
26 #include <glib-object.h>
27
28 #include <uobject/uobject.h>
29 #include <uobject/module.h>
30 #include "upkg.h"
31 #include "pack.h"
32
33 #define U_OBJECT_GET_PRIV(o) \
34         G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
35
36
37 struct prop_head {
38         const char *prop_name, *struct_name;
39         unsigned long size, array_idx;
40         bool tag_msb;
41
42         enum {
43                 PROPERTY_END,
44                 PROPERTY_BYTE,
45                 PROPERTY_INTEGER,
46                 PROPERTY_BOOLEAN,
47                 PROPERTY_FLOAT,
48                 PROPERTY_OBJECT,
49                 PROPERTY_NAME,
50                 PROPERTY_STRING,
51                 PROPERTY_CLASS,
52                 PROPERTY_ARRAY,
53                 PROPERTY_STRUCT,
54                 PROPERTY_VECTOR,
55                 PROPERTY_ROTATOR,
56                 PROPERTY_STR,
57                 PROPERTY_MAP,
58                 PROPERTY_FIXEDARRAY,
59         } type;
60 };
61
62 struct u_object_priv {
63         struct upkg_file *f;
64         size_t base, len;
65
66         unsigned char buf[2048];
67         unsigned long nbuf;
68 };
69
70 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
71
72 /*
73  * Determine the actual size (in bytes) of a property, given the 3-bit encoded
74  * size from the tag byte.  Depending on the tag size, there will be 0, 1, 2
75  * or 4 additional size bytes, which are to be found in the provided buffer
76  * which is at least n bytes long.  The result is stored in *size.
77  *
78  * Returns the number of bytes that were read from the buffer, which may be 0.
79  * On failure, (i.e., there was not enough material in the buffer) 0 is stored
80  * in *size.
81  */
82 static unsigned long
83 decode_tag_size(unsigned long *size, unsigned tag_size,
84                 const unsigned char *buf, unsigned long n)
85 {
86         *size = 0;
87
88         switch (tag_size) {
89         case 0: *size =  1; return 0;
90         case 1: *size =  2; return 0;
91         case 2: *size =  4; return 0;
92         case 3: *size = 12; return 0;
93         case 4: *size = 16; return 0;
94         case 5:
95                 if (n < 1)
96                         return 0;
97                 *size = buf[0];
98                 return 1;
99         case 6:
100                 if (n < 2)
101                         return 0;
102                 *size = unpack_16_le(buf);
103                 return 2;
104         case 7:
105                 if (n < 4)
106                         return 0;
107                 *size = unpack_32_le(buf);
108                 return 4;
109         }
110
111         assert(0);
112 }
113
114 static unsigned long
115 decode_array_index(unsigned long *index, const unsigned char *buf,
116                                          unsigned long n)
117 {
118         if (n < 1)
119                 return 0;
120
121         /* TODO: Actually implement this instead of aborting. */
122         assert("FIXME" && !(buf[0] & 0x80));
123
124         *index = buf[0];
125         return 1;
126 }
127
128 /*
129  * Decode the (mostly) generic property header, filling out the struct pointed
130  * to by head.  Returns the number of bytes read from the buffer, or 0 on
131  * failure.
132  */
133 static unsigned long
134 decode_prop_header(struct upkg *upkg, struct prop_head *head,
135                    const unsigned char *buf, unsigned long n)
136 {
137         unsigned long rc, len = 0;
138         unsigned char tag_size;
139         long index;
140
141         rc = upkg_decode_index(&index, buf+len, n-len);
142         if (rc == 0)
143                 return 0;
144         if (!(head->prop_name = upkg_get_name(upkg, index)))
145                 return 0;
146         len += rc;
147
148         /* A property called "None" terminates the list, and does not have
149          * the usual header. */
150         if (!strcmp(head->prop_name, "None")) {
151                 head->type = PROPERTY_END;
152                 head->size = 0;
153                 return len;
154         }
155
156         if (n-len < 1)
157                 return 0;
158         head->tag_msb = (buf[len] >> 7) & 0x01;
159         tag_size      = (buf[len] >> 4) & 0x07;
160         head->type    = (buf[len] >> 0) & 0x0f;
161         len++;
162
163         /*
164          * TODO: Confirm the correct relative ordering of the next three
165          * fields.
166          */
167         if (head->type == PROPERTY_STRUCT) {
168                 rc = upkg_decode_index(&index, buf+len, n-len);
169                 if (rc == 0)
170                         return 0;
171                 if (!(head->struct_name = upkg_get_name(upkg, index)))
172                         return 0;
173                 len += rc;
174         }
175
176         rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
177         if (head->size == 0)
178                 return 0;
179         len += rc;
180
181         head->array_idx = 0;
182         if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
183                 rc = decode_array_index(&head->array_idx, buf+len, n-len);
184                 if (rc == 0)
185                         return 0;
186                 len += rc;
187         }
188
189         return len;
190 }
191
192 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
193 {
194         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
195         GObject *obj = NULL;
196         long index;
197         int rc;
198
199         rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
200         if (rc == 0 || index == 0)
201                 return -1;
202
203         if (index < 0) {
204                 fprintf(stderr, "Imports not supported yet.\n");
205         } else {
206                 obj = u_object_new_from_package(uo->pkg, index-1);
207         }
208
209         g_value_init(val, U_TYPE_OBJECT);
210         g_value_take_object(val, obj);
211         return 0;
212 }
213
214 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
215 {
216         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
217         unsigned long rc, len = 0;
218         GValue val = {0};
219
220         rc = decode_prop_header(uo->pkg, head, priv->buf, priv->nbuf);
221         if (rc == 0)
222                 return 0;
223         len += rc;
224
225         switch (head->type) {
226         case PROPERTY_END:
227                 break;
228         case PROPERTY_BYTE:
229                 if (priv->nbuf-len < 1)
230                         return 0;
231                 g_value_init(&val, G_TYPE_UCHAR);
232                 g_value_set_uchar(&val, priv->buf[len]);
233                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
234                 break;
235         case PROPERTY_INTEGER:
236                 if (priv->nbuf-len < 4)
237                         return 0;
238                 g_value_init(&val, G_TYPE_ULONG);
239                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
240                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
241                 break;
242         case PROPERTY_OBJECT:
243                 rc = decode_object_property(uo, &val, len);
244                 if (rc != 0)
245                         return 0;
246                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
247                 break;
248         default:
249                 fprintf(stderr, "Unhandled property type %x\n",
250                                 (unsigned)head->type);
251         }
252         len += head->size;
253
254         if (len > priv->nbuf) {
255                 long skip = len - priv->nbuf;
256
257                 /* XXX: Long properties are not supported yet, so just seek
258                  * past them for now. */
259                 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
260                         return 0;
261
262                 priv->nbuf = 0;
263         } else {
264                 priv->nbuf -= len;
265                 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
266         }
267
268         return len;
269 }
270
271 /* Deserialize properties from an Unreal package. */
272 static int deserialize(UObject *uo)
273 {
274         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
275         struct upkg_file *f = uo->pkg_file;
276         unsigned long rc, tot_len = 0;
277
278         while (1) {
279                 struct prop_head head;
280
281                 /* Prime the buffer; deserialize_property assumes that there's
282                  * enough data for "small" properties already available. */
283                 if (!f->eof) {
284                         void  *buf = priv->buf + priv->nbuf;
285                         size_t amt = sizeof priv->buf - priv->nbuf;
286                         rc = upkg_export_read(f, buf, amt);
287                         if (rc == 0 && priv->nbuf == 0)
288                                 return -1;
289                         priv->nbuf += rc;
290                 }
291
292                 rc = deserialize_property(uo, &head);
293                 if (rc == 0) {
294                         return -1;
295                 }
296                 tot_len += rc;
297
298                 if (head.type == PROPERTY_END)
299                         break;
300         }
301
302         f->base += tot_len;
303         f->len  -= tot_len;
304         upkg_export_seek(f, 0, SEEK_SET);
305
306         return 0;
307 }
308
309 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
310 {
311         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
312         UObject *uo = U_OBJECT(obj);
313         struct upkg_file *f;
314         int rc;
315
316         g_return_val_if_fail(uo->pkg_file == NULL, -1);
317         f = upkg_export_open(pkg, idx);
318         if (!f) {
319                 return -1;
320         }
321
322         uo->pkg      = pkg;
323         uo->pkg_file = f;
324
325         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
326         if (rc != 0) {
327                 upkg_export_close(f);
328                 uo->pkg_file = NULL;
329         }
330
331         return rc;
332 }
333
334 GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx)
335 {
336         const struct upkg_export *export;
337         const char *class, *package;
338         GObject *obj = NULL;
339         GType type;
340
341         class = upkg_export_class(upkg, idx, &package);
342
343         type = u_object_module_get_class(package, class);
344         if (type) {
345                 obj = g_object_new(type, NULL);
346                 if (u_object_deserialize(obj, upkg, idx) != 0) {
347                         g_object_unref(obj);
348                         return NULL;
349                 }
350         }
351
352         return obj;
353 }
354
355 static void u_object_init(UObject *o)
356 {
357         o->pkg      = NULL;
358         o->pkg_file = NULL;
359 }
360
361 static void u_object_finalize(GObject *o)
362 {
363         UObject *uo = U_OBJECT(o);
364
365         if (uo->pkg_file) {
366                 upkg_export_close(uo->pkg_file);
367         }
368
369         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
370 }
371
372 static void u_object_class_init(UObjectClass *class)
373 {
374         g_type_class_add_private(class, sizeof (struct u_object_priv));
375         GObjectClass *go = G_OBJECT_CLASS(class);
376
377         class->deserialize = deserialize;
378         go->finalize       = u_object_finalize;
379 }
380
381 /*
382  * Prepend a prefix to a printf-style format string.  Unfortunately, there's
383  * no way to construct va_list values on the fly in C, so we cannot simply
384  * prepend %s and pass the prefix as an argument.  Any % characters in the
385  * prefix will be escaped.
386  *
387  * Returns a pointer to a newly allocated string, which should be freed by the
388  * caller, or NULL otherwise.
389  */
390 static char *prepend_fmt(const char *prefix, const char *fmt)
391 {
392         size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
393         char *new_fmt;
394
395         if (prefix_len > SIZE_MAX/2 - sizeof ": ")
396                 return NULL;
397         if (2*prefix_len > SIZE_MAX - fmt_len)
398                 return NULL;
399
400         new_fmt = malloc(2*prefix_len + fmt_len + 1);
401         if (new_fmt) {
402                 size_t offset = 0;
403
404                 for (size_t i = 0; i < prefix_len; i++) {
405                         if (prefix[i] == '%')
406                                 new_fmt[offset++] = '%';
407                         new_fmt[offset++] = prefix[i];
408                 }
409
410                 new_fmt[offset++] = ':';
411                 new_fmt[offset++] = ' ';
412                 strcpy(new_fmt+offset, fmt);
413         }
414
415         return new_fmt;
416 }
417
418 /* Logging helpers that automatically prepend the UObject class information. */
419 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
420 {
421         g_return_if_fail(IS_U_OBJECT(o));
422         UObject *uo = U_OBJECT(o);
423         char *new_fmt = NULL;
424
425         if (uo->pkg_file) {
426                 /* XXX: Currently, there's no way to get the full object name
427                  * here because the object path information is discarded
428                  * after opening it.  In principle, there's no reason why
429                  * we couldn't keep it around to improve log messages. */
430                 new_fmt = prepend_fmt(uo->pkg_file->name, fmt);
431                 if (!new_fmt) {
432                         g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
433                               uo->pkg_file->name);
434                 } else {
435                         fmt = new_fmt;
436                 }
437         }
438
439         g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
440         free(new_fmt);
441 }
442
443 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
444 {
445         va_list ap;
446
447         va_start(ap, fmt);
448         u_vlog_full(o, level, fmt, ap);
449         va_end(ap);
450 }