]> git.draconx.ca Git - upkg.git/blob - src/uobject.c
uobject: Namespace changes.
[upkg.git] / src / uobject.c
1 /*
2  *  upkg: tool for manipulating Unreal Tournament packages.
3  *  Copyright (C) 2009 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 2 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, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <glib-object.h>
25
26 #include <uobject/uobject.h>
27 #include "upkg.h"
28 #include "pack.h"
29
30 #define U_OBJECT_GET_PRIV(o) \
31         G_TYPE_INSTANCE_GET_PRIVATE(o, U_OBJECT_TYPE, struct u_object_priv)
32
33 enum {
34         PROPERTY_BYTE = 1,
35         PROPERTY_INTEGER,
36         PROPERTY_BOOLEAN,
37         PROPERTY_FLOAT,
38         PROPERTY_OBJECT,
39         PROPERTY_NAME,
40         PROPERTY_STRING,
41         PROPERTY_CLASS,
42         PROPERTY_ARRAY,
43         PROPERTY_STRUCT,
44         PROPERTY_VECTOR,
45         PROPERTY_ROTATOR,
46         PROPERTY_STR,
47         PROPERTY_MAP,
48         PROPERTY_FIXEDARRAY,
49 };
50
51 struct u_object_priv {
52         struct upkg_file *f;
53         size_t base, len;
54
55         unsigned char buf[2048];
56         unsigned long nbuf;
57 };
58
59 G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT);
60
61 static unsigned long
62 get_real_size(unsigned long *real, unsigned size, unsigned char *buf, size_t n)
63 {
64         assert(size < 8);
65
66         *real = 0;
67         switch (size) {
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;
73         case 5:
74                 if (n < 1) return 0;
75                 *real = *buf;
76                 return 1;
77         case 6:
78                 if (n < 2) return 0;
79                 *real = unpack_16_le(buf);
80                 return 2;
81         case 7:
82                 if (n < 4) return 0;
83                 *real = unpack_32_le(buf);
84                 return 4;
85         }
86
87         return 0;
88 }
89
90 static unsigned long
91 decode_property(UObject *o, const char *name, struct upkg_file *f, unsigned long len)
92 {
93         struct u_object_priv *priv = U_OBJECT_GET_PRIV(o);
94         unsigned long real_size, rc;
95         int type, size, top;
96         GValue val = {0};
97
98         if (priv->nbuf-len < 1)
99                 return 0;
100         
101         type = (priv->buf[len] >> 0) & 0x0f;
102         size = (priv->buf[len] >> 4) & 0x07;
103         top  = (priv->buf[len] >> 7) & 0x01;
104         len += 1;
105
106         rc = get_real_size(&real_size, size, priv->buf, priv->nbuf-len);
107         if (real_size == 0)
108                 return 0;
109         len += rc;
110
111         switch (type) {
112         case PROPERTY_BYTE:
113                 if (priv->nbuf-len < 1)
114                         return 0;
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);
118                 break;
119         case PROPERTY_INTEGER:
120                 if (priv->nbuf-len < 4)
121                         return 0;
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);
125                 break;
126         default:
127                 fprintf(stderr, "Unhandled property type %x\n", (unsigned)type);
128         }
129
130         real_size += len;
131         if (real_size + len <= priv->nbuf) {
132                 priv->nbuf -= real_size;
133                 memmove(priv->buf, priv->buf+real_size, priv->nbuf);
134         } else {
135                 long skip = real_size - priv->nbuf;
136                 if (upkg_export_seek(f, skip, SEEK_CUR) != 0)
137                         return 0;
138                 priv->nbuf = 0;
139         }
140
141         return real_size;
142 }
143
144 /* Deserialize properties from an Unreal package. */
145 static int deserialize(UObject *o, struct upkg_file *f)
146 {
147         struct u_object_priv *priv = U_OBJECT_GET_PRIV(o);
148         unsigned long rc, tot_len = 0;
149
150         while (1) {
151                 unsigned long len = 0;
152                 const char *name;
153                 long tmp;
154
155                 /* Read some data into buffer. */
156                 if (!f->eof) {
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)
161                                 return -1;
162                         priv->nbuf += rc;
163                 }
164
165                 /* Get the property name. */
166                 rc = upkg_decode_index(&tmp, priv->buf+len, priv->nbuf-len);
167                 if (rc == 0)
168                         return -1;
169                 len = rc;
170
171                 name = upkg_get_name(f->pkg, tmp);
172                 if (!name) {
173                         return -1;
174                 } else if (strcmp(name, "None") == 0) {
175                         tot_len += len;
176                         break;
177                 }
178
179                 rc = decode_property(U_OBJECT(o), name, f, len);
180                 if (rc == 0)
181                         return -1;
182                 len = rc;
183
184                 tot_len += len;
185         }
186
187         f->base += tot_len;
188         f->len  -= tot_len;
189         upkg_export_seek(f, 0, SEEK_SET);
190
191         return 0;
192 }
193
194 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
195 {
196         g_return_val_if_fail(IS_U_OBJECT(obj), -1);
197         UObject *uo = U_OBJECT(obj);
198         struct upkg_file *f;
199         int rc;
200
201         g_return_val_if_fail(uo->pkg_file == NULL, -1);
202         f = upkg_export_open(pkg, idx);
203         if (!f) {
204                 return -1;
205         }
206
207         rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo, f);
208         if (rc != 0) {
209                 upkg_export_close(f);
210         } else {
211                 uo->pkg      = pkg;
212                 uo->pkg_idx  = idx;
213                 uo->pkg_file = f;
214         }
215
216         return rc;
217 }
218
219 static void u_object_init(UObject *o)
220 {
221         o->pkg      = NULL;
222         o->pkg_file = NULL;
223         o->pkg_idx  = 0;
224 }
225
226 static void u_object_finalize(GObject *o)
227 {
228         UObject *uo = U_OBJECT(o);
229
230         if (uo->pkg_file) {
231                 upkg_export_close(uo->pkg_file);
232         }
233
234         G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
235 }
236
237 static void u_object_class_init(UObjectClass *class)
238 {
239         g_type_class_add_private(class, sizeof (struct u_object_priv));
240         GObjectClass *go = G_OBJECT_CLASS(class);
241
242         class->deserialize = deserialize;
243         go->finalize       = u_object_finalize;
244 }