2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright (C) 2009 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <glib-object.h>
26 #include <uobject/uobject.h>
30 #define U_OBJECT_GET_PRIV(o) \
31 G_TYPE_INSTANCE_GET_PRIVATE(o, U_OBJECT_TYPE, struct u_object_priv)
51 struct u_object_priv {
55 unsigned char buf[2048];
59 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
62 get_real_size(unsigned long *real, unsigned size, unsigned char *buf, size_t n)
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;
79 *real = unpack_16_le(buf);
83 *real = unpack_32_le(buf);
91 decode_property(UObject *o, const char *name, struct upkg_file *f, unsigned long len)
93 struct u_object_priv *priv = U_OBJECT_GET_PRIV(o);
94 unsigned long real_size, rc;
98 if (priv->nbuf-len < 1)
101 type = (priv->buf[len] >> 0) & 0x0f;
102 size = (priv->buf[len] >> 4) & 0x07;
103 top = (priv->buf[len] >> 7) & 0x01;
106 rc = get_real_size(&real_size, size, priv->buf, priv->nbuf-len);
113 if (priv->nbuf-len < 1)
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);
119 case PROPERTY_INTEGER:
120 if (priv->nbuf-len < 4)
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);
127 fprintf(stderr, "Unhandled property type %x\n", (unsigned)type);
131 if (real_size + len <= priv->nbuf) {
132 priv->nbuf -= real_size;
133 memmove(priv->buf, priv->buf+real_size, priv->nbuf);
135 long skip = real_size - priv->nbuf;
136 if (upkg_export_seek(f, skip, SEEK_CUR) != 0)
144 /* Deserialize properties from an Unreal package. */
145 static int deserialize(UObject *o, struct upkg_file *f)
147 struct u_object_priv *priv = U_OBJECT_GET_PRIV(o);
148 unsigned long rc, tot_len = 0;
151 unsigned long len = 0;
155 /* Read some data into buffer. */
157 void *buf = priv->buf + priv->nbuf;
158 size_t amt = sizeof priv->buf - priv->nbuf;
159 rc = upkg_export_read(f, buf, amt);
160 if (rc == 0 && priv->nbuf == 0)
165 /* Get the property name. */
166 rc = upkg_decode_index(&tmp, priv->buf+len, priv->nbuf-len);
171 name = upkg_get_name(f->pkg, tmp);
174 } else if (strcmp(name, "None") == 0) {
179 rc = decode_property(U_OBJECT(o), name, f, len);
189 upkg_export_seek(f, 0, SEEK_SET);
194 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
196 g_return_val_if_fail(IS_U_OBJECT(obj), -1);
197 UObject *uo = U_OBJECT(obj);
201 g_return_val_if_fail(uo->pkg_file == NULL, -1);
202 f = upkg_export_open(pkg, idx);
207 rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo, f);
209 upkg_export_close(f);
219 static void u_object_init(UObject *o)
226 static void u_object_finalize(GObject *o)
228 UObject *uo = U_OBJECT(o);
231 upkg_export_close(uo->pkg_file);
234 G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
237 static void u_object_class_init(UObjectClass *class)
239 g_type_class_add_private(class, sizeof (struct u_object_priv));
240 GObjectClass *go = G_OBJECT_CLASS(class);
242 class->deserialize = deserialize;
243 go->finalize = u_object_finalize;