]> git.draconx.ca Git - upkg.git/blob - src/uobject/uobject.c
Improve GOB rebuild rules for VPATH builds.
[upkg.git] / src / uobject / uobject.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright © 2009-2012, 2015, 2020 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 <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <inttypes.h>
27 #include <assert.h>
28 #include <glib-object.h>
29
30 #include <uobject/uobject.h>
31 #include <uobject/module.h>
32 #include <uobject/package.h>
33 #include "upkg.h"
34 #include "pack.h"
35
36 #define U_OBJECT_GET_PRIV(o) \
37         G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
38
39
40 struct prop_head {
41         const char *prop_name, *struct_name;
42         unsigned long size, array_idx;
43         bool tag_msb;
44
45         enum {
46                 PROPERTY_END,
47                 PROPERTY_BYTE,
48                 PROPERTY_INTEGER,
49                 PROPERTY_BOOLEAN,
50                 PROPERTY_FLOAT,
51                 PROPERTY_OBJECT,
52                 PROPERTY_NAME,
53                 PROPERTY_STRING,
54                 PROPERTY_CLASS,
55                 PROPERTY_ARRAY,
56                 PROPERTY_STRUCT,
57                 PROPERTY_VECTOR,
58                 PROPERTY_ROTATOR,
59                 PROPERTY_STR,
60                 PROPERTY_MAP,
61                 PROPERTY_FIXEDARRAY,
62         } type;
63 };
64
65 struct u_object_priv {
66         struct upkg_file *f;
67         size_t base, len;
68
69         unsigned char buf[2048];
70         unsigned long nbuf;
71 };
72
73 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
74
75 /*
76  * Determine the actual size (in bytes) of a property, given the 3-bit encoded
77  * size from the tag byte.  Depending on the tag size, there will be 0, 1, 2
78  * or 4 additional size bytes, which are to be found in the provided buffer
79  * which is at least n bytes long.  The result is stored in *size.
80  *
81  * Returns the number of bytes that were read from the buffer, which may be 0.
82  * On failure, (i.e., there was not enough material in the buffer) 0 is stored
83  * in *size.
84  */
85 static unsigned long
86 decode_tag_size(unsigned long *size, unsigned tag_size,
87                 const unsigned char *buf, unsigned long n)
88 {
89         *size = 0;
90
91         switch (tag_size) {
92         case 0: *size =  1; return 0;
93         case 1: *size =  2; return 0;
94         case 2: *size =  4; return 0;
95         case 3: *size = 12; return 0;
96         case 4: *size = 16; return 0;
97         case 5:
98                 if (n < 1)
99                         return 0;
100                 *size = buf[0];
101                 return 1;
102         case 6:
103                 if (n < 2)
104                         return 0;
105                 *size = unpack_16_le(buf);
106                 return 2;
107         case 7:
108                 if (n < 4)
109                         return 0;
110                 *size = unpack_32_le(buf);
111                 return 4;
112         }
113
114         assert(0);
115 }
116
117 static unsigned long
118 decode_array_index(unsigned long *index, const unsigned char *buf,
119                                          unsigned long n)
120 {
121         if (n < 1)
122                 return 0;
123
124         /* TODO: Actually implement this instead of aborting. */
125         assert("FIXME" && !(buf[0] & 0x80));
126
127         *index = buf[0];
128         return 1;
129 }
130
131 /*
132  * Decode the (mostly) generic property header, filling out the struct pointed
133  * to by head.  Returns the number of bytes read from the buffer, or 0 on
134  * failure.
135  */
136 static unsigned long
137 decode_prop_header(struct upkg *upkg, struct prop_head *head,
138                    const unsigned char *buf, unsigned long n)
139 {
140         unsigned long rc, len = 0;
141         unsigned char tag_size;
142         long index;
143
144         rc = upkg_decode_index(&index, buf+len, n-len);
145         if (rc == 0)
146                 return 0;
147         if (!(head->prop_name = upkg_get_name(upkg, index)))
148                 return 0;
149         len += rc;
150
151         /* A property called "None" terminates the list, and does not have
152          * the usual header. */
153         if (!strcmp(head->prop_name, "None")) {
154                 head->type = PROPERTY_END;
155                 head->size = 0;
156                 return len;
157         }
158
159         if (n-len < 1)
160                 return 0;
161         head->tag_msb = (buf[len] >> 7) & 0x01;
162         tag_size      = (buf[len] >> 4) & 0x07;
163         head->type    = (buf[len] >> 0) & 0x0f;
164         len++;
165
166         /*
167          * TODO: Confirm the correct relative ordering of the next three
168          * fields.
169          */
170         if (head->type == PROPERTY_STRUCT) {
171                 rc = upkg_decode_index(&index, buf+len, n-len);
172                 if (rc == 0)
173                         return 0;
174                 if (!(head->struct_name = upkg_get_name(upkg, index)))
175                         return 0;
176                 len += rc;
177         }
178
179         rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
180         if (rc == 0 && head->size == 0)
181                 return 0;
182         len += rc;
183
184         head->array_idx = 0;
185         if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
186                 rc = decode_array_index(&head->array_idx, buf+len, n-len);
187                 if (rc == 0)
188                         return 0;
189                 len += rc;
190         }
191
192         return len;
193 }
194
195 /*
196  * TODO: Make this use the uobject_module stuff so packages are not loaded
197  * more than once.
198  */
199 static GTypeModule *
200 open_import_package(UObject *uo, const struct upkg_import *i)
201 {
202         GTypeModule *pkg;
203
204         assert(i->parent == NULL);
205
206         if (strcmp(i->class_package, "Core") != 0
207             || strcmp(i->class_name, "Package") != 0) {
208                 u_err(uo, "import root must be Core.Package");
209                 return NULL;
210         }
211
212         pkg = u_pkg_open(i->name);
213         if (!pkg || !g_type_module_use(pkg)) {
214                 u_err(uo, "failed to open package: %s", i->name);
215                 return NULL;
216         }
217
218         if (!U_PKG(pkg)->pkg) {
219                 g_type_module_unuse(pkg);
220                 u_err(uo, "failed to open package: %s", pkg->name);
221                 return NULL;
222         }
223
224         return pkg;
225 }
226
227 /* Find the export index of a package based on the import chain from another
228  * package.  Returns the (non-negative) offset on success, or a negative value
229  * on failure. */
230 static long resolve_import(struct upkg *pkg, const struct upkg_import *import)
231 {
232         long current, index;
233
234         if (!import->parent)
235                 return -1;
236
237         current = resolve_import(pkg, import->parent);
238         if (current != -1 && current >= 0)
239                 return -42;
240
241         index = upkg_export_find(pkg, current, import->name);
242         if (index < 0)
243                 return -42;
244
245         return index;
246 }
247
248 static GObject *get_import_object(UObject *uo, unsigned long index)
249 {
250         const struct upkg_import *import;
251         GObject *obj = NULL;
252         GTypeModule *pkg;
253         long obj_index;
254
255         import = upkg_get_import(uo->pkg_file->pkg, index);
256         if (!import) {
257                 u_err(uo, "invalid package import: %ld", index);
258                 return NULL;
259         }
260
261         /* Get the package name at the top of the hierarchy. */
262         for (const struct upkg_import *i = import; i; i = i->parent) {
263                 if (i->parent == NULL) {
264                         pkg = open_import_package(uo, i);
265                         if (!pkg)
266                                 return NULL;
267                 }
268         }
269
270         obj_index = resolve_import(U_PKG(pkg)->pkg, import);
271         if (obj_index < 0) {
272                 u_err(uo, "could not find import in package: %s", pkg->name);
273                 goto out;
274         }
275
276         obj = u_object_new_from_package(pkg, obj_index);
277 out:
278         g_type_module_unuse(pkg);
279         return obj;
280 }
281
282 GObject *u_object_get_by_link(GObject *go, long index)
283 {
284         g_return_val_if_fail(IS_U_OBJECT(go), NULL);
285         UObject *uo = U_OBJECT(go);
286
287         if (index == 0)
288                 return NULL;
289
290         if (index < 0) {
291                 return get_import_object(uo, -(index+1));
292         }
293
294         return u_object_new_from_package(uo->pkg, index-1);
295 }
296
297 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
298 {
299         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
300         GObject *obj = NULL;
301         long index;
302         int rc;
303
304         rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
305         if (rc == 0)
306                 return -1;
307
308         obj = u_object_get_by_link(G_OBJECT(uo), index);
309         g_value_init(val, U_TYPE_OBJECT);
310         g_value_take_object(val, obj);
311         return 0;
312 }
313
314 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
315 {
316         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
317         unsigned long rc, len = 0;
318         GValue val = {0};
319
320         rc = decode_prop_header(uo->pkg_file->pkg, head, priv->buf, priv->nbuf);
321         if (rc == 0)
322                 return 0;
323         len += rc;
324
325         switch (head->type) {
326         case PROPERTY_END:
327                 break;
328         case PROPERTY_BYTE:
329                 if (head->size != 1 || priv->nbuf-len < head->size)
330                         return 0;
331                 g_value_init(&val, G_TYPE_UCHAR);
332                 g_value_set_uchar(&val, priv->buf[len]);
333                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
334                 break;
335         case PROPERTY_INTEGER:
336                 if (head->size != 4 || priv->nbuf-len < head->size)
337                         return 0;
338                 g_value_init(&val, G_TYPE_ULONG);
339                 g_value_set_ulong(&val, unpack_32_le(priv->buf+len));
340                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
341                 break;
342         case PROPERTY_BOOLEAN:
343                 if (head->size != 0)
344                         return 0;
345                 g_value_init(&val, G_TYPE_BOOLEAN);
346                 g_value_set_boolean(&val, head->tag_msb);
347                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
348                 break;
349         case PROPERTY_OBJECT:
350                 rc = decode_object_property(uo, &val, len);
351                 if (rc != 0)
352                         return 0;
353                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
354
355                 /*
356                  * g_object_set_property increments refcount,
357                  * release our reference.
358                  */
359                 g_object_unref(g_value_get_object(&val));
360                 break;
361         case PROPERTY_FLOAT:
362                 if (head->size != 4 || priv->nbuf-len < head->size)
363                         return 0;
364                 g_value_init(&val, G_TYPE_FLOAT);
365                 g_value_set_float(&val, u_unpack_binary32_le(priv->buf+len));
366                 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
367                 break;
368         default:
369                 u_warn(uo, "%s: unsupported property type %u",
370                            head->prop_name, (unsigned)head->type);
371         }
372         len += head->size;
373
374         if (len > priv->nbuf) {
375                 long skip = len - priv->nbuf;
376
377                 /* XXX: Long properties are not supported yet, so just seek
378                  * past them for now. */
379                 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
380                         return 0;
381
382                 priv->nbuf = 0;
383         } else {
384                 priv->nbuf -= len;
385                 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
386         }
387
388         return len;
389 }
390
391 /* Deserialize properties from an Unreal package. */
392 static int deserialize(UObject *uo)
393 {
394         struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
395         struct upkg_file *f = uo->pkg_file;
396         unsigned long rc, tot_len = 0;
397
398         while (1) {
399                 struct prop_head head;
400
401                 /* Prime the buffer; deserialize_property assumes that there's
402                  * enough data for "small" properties already available. */
403                 if (!f->eof) {
404                         void  *buf = priv->buf + priv->nbuf;
405                         size_t amt = sizeof priv->buf - priv->nbuf;
406                         rc = upkg_export_read(f, buf, amt);
407                         if (rc == 0 && priv->nbuf == 0)
408                                 return -1;
409                         priv->nbuf += rc;
410                 }
411
412                 rc = deserialize_property(uo, &head);
413                 if (rc == 0) {
414                         return -1;
415                 }
416                 tot_len += rc;
417
418                 if (head.type == PROPERTY_END)
419                         break;
420         }
421
422         f->base += tot_len;
423         f->len  -= tot_len;
424         upkg_export_seek(f, 0, SEEK_SET);
425
426         return 0;
427 }
428
429 /*
430  * Get the full hierarchical object name for an export, used for diagnostics.
431  * The package name is passed in pname.
432  *
433  * Returns a buffer allocated by malloc on success, or NULL on failure.
434  */
435 static char *get_obj_fullname(const char *pname, const struct upkg_export *e)
436 {
437         size_t total_len = strlen(pname) + 1, len;
438         char *fullname;
439
440         assert(e != NULL);
441
442         for (const struct upkg_export *c = e; c; c = c->parent) {
443                 len = strlen(c->name) + 1;
444                 if (total_len > SIZE_MAX - len)
445                         return NULL;
446                 total_len += len;
447         }
448
449         fullname = malloc(total_len);
450         if (!fullname)
451                 return NULL;
452
453         for (const struct upkg_export *c = e; c; c = c->parent) {
454                 len = strlen(c->name);
455                 assert(total_len > len);
456
457                 total_len -= len + 1;
458                 memcpy(fullname + total_len, c->name, len);
459                 fullname[total_len + len] = c == e ? '\0' : '.';
460         }
461
462         assert(total_len == strlen(pname)+1);
463         memcpy(fullname, pname, total_len-1);
464         fullname[total_len-1] = '.';
465
466         return fullname;
467 }
468
469 int u_object_deserialize(GObject *obj, GTypeModule *pkg, unsigned long idx)
470 {
471         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
472         g_return_val_if_fail(IS_U_PKG(pkg), -1);
473
474         UObject *uo = U_OBJECT(obj);
475         struct upkg *upkg = U_PKG(pkg)->pkg;
476         const struct upkg_export *e;
477         struct upkg_file *f;
478         int rc;
479
480         g_return_val_if_fail(uo->pkg_file == NULL, -1);
481         f = upkg_export_open(upkg, idx);
482         if (!f) {
483                 return -1;
484         }
485
486         uo->pkg = pkg;
487         uo->pkg_file = f;
488
489         e = upkg_get_export(upkg, idx);
490         if (!e)
491                 return -1;
492         uo->pkg_name = get_obj_fullname(pkg->name, e);
493         if (!uo->pkg_name)
494                 return -1;
495
496         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
497         if (rc != 0) {
498                 upkg_export_close(f);
499                 uo->pkg_file = NULL;
500         }
501
502         return rc;
503 }
504
505 GObject *u_object_new_from_package(GTypeModule *pkg, unsigned long idx)
506 {
507         g_return_val_if_fail(IS_U_PKG(pkg), NULL);
508
509         const struct upkg_export *export;
510         GObject *obj = NULL;
511         GType type;
512
513         export = upkg_get_export(U_PKG(pkg)->pkg, idx);
514         if (!export) {
515                 u_err(pkg, "invalid package export: %lu", idx);
516                 return NULL;
517         }
518
519         type = u_object_module_get_class(pkg, export->class);
520         if (type) {
521                 obj = g_object_new(type, NULL);
522                 if (u_object_deserialize(obj, pkg, idx) != 0) {
523                         g_object_unref(obj);
524                         return NULL;
525                 }
526         }
527
528         return obj;
529 }
530
531 static void u_object_init(UObject *o)
532 {
533         o->pkg      = NULL;
534         o->pkg_file = NULL;
535 }
536
537 static void u_object_finalize(GObject *o)
538 {
539         UObject *uo = U_OBJECT(o);
540
541         if (uo->pkg_file) {
542                 upkg_export_close(uo->pkg_file);
543         }
544
545         free(uo->pkg_name);
546
547         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
548 }
549
550 static void u_object_class_init(UObjectClass *class)
551 {
552         g_type_class_add_private(class, sizeof (struct u_object_priv));
553         GObjectClass *go = G_OBJECT_CLASS(class);
554
555         class->deserialize = deserialize;
556         go->finalize       = u_object_finalize;
557 }
558
559 /*
560  * Prepend a prefix to a printf-style format string.  Unfortunately, there's
561  * no way to construct va_list values on the fly in C, so we cannot simply
562  * prepend %s and pass the prefix as an argument.  Any % characters in the
563  * prefix will be escaped.
564  *
565  * Returns a pointer to a newly allocated string, which should be freed by the
566  * caller, or NULL otherwise.
567  */
568 static char *prepend_fmt(const char *prefix, const char *fmt)
569 {
570         size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
571         char *new_fmt;
572
573         if (prefix_len > SIZE_MAX/2 - sizeof ": ")
574                 return NULL;
575         if (2*prefix_len > SIZE_MAX - fmt_len)
576                 return NULL;
577
578         new_fmt = malloc(2*prefix_len + fmt_len + 1);
579         if (new_fmt) {
580                 size_t offset = 0;
581
582                 for (size_t i = 0; i < prefix_len; i++) {
583                         if (prefix[i] == '%')
584                                 new_fmt[offset++] = '%';
585                         new_fmt[offset++] = prefix[i];
586                 }
587
588                 new_fmt[offset++] = ':';
589                 new_fmt[offset++] = ' ';
590                 strcpy(new_fmt+offset, fmt);
591         }
592
593         return new_fmt;
594 }
595
596 /* Logging helpers that automatically prepend the UObject class information. */
597 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
598 {
599         char *new_fmt = NULL, *obj_prefix = NULL;
600
601         if (IS_U_OBJECT(o)) {
602                 obj_prefix = U_OBJECT(o)->pkg_name;
603         } else if (G_IS_TYPE_MODULE(o)) {
604                 obj_prefix = G_TYPE_MODULE(o)->name;
605         }
606
607         if (obj_prefix) {
608                 new_fmt = prepend_fmt(obj_prefix, fmt);
609                 if (!new_fmt)
610                         g_log(G_OBJECT_TYPE_NAME(o), level, "%s", obj_prefix);
611                 else
612                         fmt = new_fmt;
613         }
614
615         g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
616         free(new_fmt);
617 }
618
619 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
620 {
621         va_list ap;
622
623         va_start(ap, fmt);
624         u_vlog_full(o, level, fmt, ap);
625         va_end(ap);
626 }