2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011 Nick Bowler
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.
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.
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/>.
26 #include <glib-object.h>
28 #include <uobject/uobject.h>
29 #include <uobject/module.h>
33 #define U_OBJECT_GET_PRIV(o) \
34 G_TYPE_INSTANCE_GET_PRIVATE(o, U_TYPE_OBJECT, struct u_object_priv)
38 const char *prop_name, *struct_name;
39 unsigned long size, array_idx;
62 struct u_object_priv {
66 unsigned char buf[2048];
70 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
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.
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
83 decode_tag_size(unsigned long *size, unsigned tag_size,
84 const unsigned char *buf, unsigned long n)
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;
102 *size = unpack_16_le(buf);
107 *size = unpack_32_le(buf);
115 decode_array_index(unsigned long *index, const unsigned char *buf,
121 /* TODO: Actually implement this instead of aborting. */
122 assert("FIXME" && !(buf[0] & 0x80));
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
134 decode_prop_header(struct upkg *upkg, struct prop_head *head,
135 const unsigned char *buf, unsigned long n)
137 unsigned long rc, len = 0;
138 unsigned char tag_size;
141 rc = upkg_decode_index(&index, buf+len, n-len);
144 if (!(head->prop_name = upkg_get_name(upkg, index)))
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;
158 head->tag_msb = (buf[len] >> 7) & 0x01;
159 tag_size = (buf[len] >> 4) & 0x07;
160 head->type = (buf[len] >> 0) & 0x0f;
164 * TODO: Confirm the correct relative ordering of the next three
167 if (head->type == PROPERTY_STRUCT) {
168 rc = upkg_decode_index(&index, buf+len, n-len);
171 if (!(head->struct_name = upkg_get_name(upkg, index)))
176 rc = decode_tag_size(&head->size, tag_size, buf+len, n-len);
177 if (rc == 0 && head->size == 0)
182 if (head->tag_msb && head->type != PROPERTY_BOOLEAN) {
183 rc = decode_array_index(&head->array_idx, buf+len, n-len);
192 static int decode_object_property(UObject *uo, GValue *val, unsigned long len)
194 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
199 rc = upkg_decode_index(&index, priv->buf+len, priv->nbuf-len);
200 if (rc == 0 || index == 0)
204 fprintf(stderr, "Imports not supported yet.\n");
206 obj = u_object_new_from_package(uo->pkg, index-1);
209 g_value_init(val, U_TYPE_OBJECT);
210 g_value_take_object(val, obj);
214 static unsigned long deserialize_property(UObject *uo, struct prop_head *head)
216 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
217 unsigned long rc, len = 0;
220 rc = decode_prop_header(uo->pkg, head, priv->buf, priv->nbuf);
225 switch (head->type) {
229 if (priv->nbuf-len < 1)
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);
235 case PROPERTY_INTEGER:
236 if (priv->nbuf-len < 4)
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);
242 case PROPERTY_BOOLEAN:
243 g_value_init(&val, G_TYPE_BOOLEAN);
244 g_value_set_boolean(&val, head->tag_msb);
245 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
247 case PROPERTY_OBJECT:
248 rc = decode_object_property(uo, &val, len);
251 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
254 fprintf(stderr, "Unhandled property type %x\n",
255 (unsigned)head->type);
259 if (len > priv->nbuf) {
260 long skip = len - priv->nbuf;
262 /* XXX: Long properties are not supported yet, so just seek
263 * past them for now. */
264 if (upkg_export_seek(uo->pkg_file, skip, SEEK_CUR) != 0)
270 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
276 /* Deserialize properties from an Unreal package. */
277 static int deserialize(UObject *uo)
279 struct u_object_priv *priv = U_OBJECT_GET_PRIV(uo);
280 struct upkg_file *f = uo->pkg_file;
281 unsigned long rc, tot_len = 0;
284 struct prop_head head;
286 /* Prime the buffer; deserialize_property assumes that there's
287 * enough data for "small" properties already available. */
289 void *buf = priv->buf + priv->nbuf;
290 size_t amt = sizeof priv->buf - priv->nbuf;
291 rc = upkg_export_read(f, buf, amt);
292 if (rc == 0 && priv->nbuf == 0)
297 rc = deserialize_property(uo, &head);
303 if (head.type == PROPERTY_END)
309 upkg_export_seek(f, 0, SEEK_SET);
314 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
316 g_return_val_if_fail(IS_U_OBJECT(obj), -1);
317 UObject *uo = U_OBJECT(obj);
321 g_return_val_if_fail(uo->pkg_file == NULL, -1);
322 f = upkg_export_open(pkg, idx);
330 rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
332 upkg_export_close(f);
339 GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx)
341 const struct upkg_export *export;
342 const char *class, *package;
346 class = upkg_export_class(upkg, idx, &package);
348 type = u_object_module_get_class(package, class);
350 obj = g_object_new(type, NULL);
351 if (u_object_deserialize(obj, upkg, idx) != 0) {
360 static void u_object_init(UObject *o)
366 static void u_object_finalize(GObject *o)
368 UObject *uo = U_OBJECT(o);
371 upkg_export_close(uo->pkg_file);
374 G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
377 static void u_object_class_init(UObjectClass *class)
379 g_type_class_add_private(class, sizeof (struct u_object_priv));
380 GObjectClass *go = G_OBJECT_CLASS(class);
382 class->deserialize = deserialize;
383 go->finalize = u_object_finalize;
387 * Prepend a prefix to a printf-style format string. Unfortunately, there's
388 * no way to construct va_list values on the fly in C, so we cannot simply
389 * prepend %s and pass the prefix as an argument. Any % characters in the
390 * prefix will be escaped.
392 * Returns a pointer to a newly allocated string, which should be freed by the
393 * caller, or NULL otherwise.
395 static char *prepend_fmt(const char *prefix, const char *fmt)
397 size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
400 if (prefix_len > SIZE_MAX/2 - sizeof ": ")
402 if (2*prefix_len > SIZE_MAX - fmt_len)
405 new_fmt = malloc(2*prefix_len + fmt_len + 1);
409 for (size_t i = 0; i < prefix_len; i++) {
410 if (prefix[i] == '%')
411 new_fmt[offset++] = '%';
412 new_fmt[offset++] = prefix[i];
415 new_fmt[offset++] = ':';
416 new_fmt[offset++] = ' ';
417 strcpy(new_fmt+offset, fmt);
423 /* Logging helpers that automatically prepend the UObject class information. */
424 void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
426 g_return_if_fail(IS_U_OBJECT(o));
427 UObject *uo = U_OBJECT(o);
428 char *new_fmt = NULL;
431 /* XXX: Currently, there's no way to get the full object name
432 * here because the object path information is discarded
433 * after opening it. In principle, there's no reason why
434 * we couldn't keep it around to improve log messages. */
435 new_fmt = prepend_fmt(uo->pkg_file->name, fmt);
437 g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
444 g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
448 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
453 u_vlog_full(o, level, fmt, ap);