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);
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_OBJECT:
243 rc = decode_object_property(uo, &val, len);
246 g_object_set_property(G_OBJECT(uo), head->prop_name, &val);
249 fprintf(stderr, "Unhandled property type %x\n",
250 (unsigned)head->type);
254 if (len > priv->nbuf) {
255 long skip = len - priv->nbuf;
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)
265 memmove(priv->buf, priv->buf+len, priv->nbuf-len);
271 /* Deserialize properties from an Unreal package. */
272 static int deserialize(UObject *uo)
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;
279 struct prop_head head;
281 /* Prime the buffer; deserialize_property assumes that there's
282 * enough data for "small" properties already available. */
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)
292 rc = deserialize_property(uo, &head);
298 if (head.type == PROPERTY_END)
304 upkg_export_seek(f, 0, SEEK_SET);
309 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
311 g_return_val_if_fail(IS_U_OBJECT(obj), -1);
312 UObject *uo = U_OBJECT(obj);
316 g_return_val_if_fail(uo->pkg_file == NULL, -1);
317 f = upkg_export_open(pkg, idx);
325 rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
327 upkg_export_close(f);
334 GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx)
336 const struct upkg_export *export;
337 const char *class, *package;
341 class = upkg_export_class(upkg, idx, &package);
343 type = u_object_module_get_class(package, class);
345 obj = g_object_new(type, NULL);
346 if (u_object_deserialize(obj, upkg, idx) != 0) {
355 static void u_object_init(UObject *o)
361 static void u_object_finalize(GObject *o)
363 UObject *uo = U_OBJECT(o);
366 upkg_export_close(uo->pkg_file);
369 G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
372 static void u_object_class_init(UObjectClass *class)
374 g_type_class_add_private(class, sizeof (struct u_object_priv));
375 GObjectClass *go = G_OBJECT_CLASS(class);
377 class->deserialize = deserialize;
378 go->finalize = u_object_finalize;
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.
387 * Returns a pointer to a newly allocated string, which should be freed by the
388 * caller, or NULL otherwise.
390 static char *prepend_fmt(const char *prefix, const char *fmt)
392 size_t prefix_len = strlen(prefix), fmt_len = strlen(fmt);
395 if (prefix_len > SIZE_MAX/2 - sizeof ": ")
397 if (2*prefix_len > SIZE_MAX - fmt_len)
400 new_fmt = malloc(2*prefix_len + fmt_len + 1);
404 for (size_t i = 0; i < prefix_len; i++) {
405 if (prefix[i] == '%')
406 new_fmt[offset++] = '%';
407 new_fmt[offset++] = prefix[i];
410 new_fmt[offset++] = ':';
411 new_fmt[offset++] = ' ';
412 strcpy(new_fmt+offset, fmt);
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)
421 g_return_if_fail(IS_U_OBJECT(o));
422 UObject *uo = U_OBJECT(o);
423 char *new_fmt = NULL;
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);
432 g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
439 g_logv(G_OBJECT_TYPE_NAME(o), level, fmt, ap);
443 void u_log_full(GObject *o, GLogLevelFlags level, const char *fmt, ...)
448 u_vlog_full(o, level, fmt, ap);