3 * upkg: tool for manipulating Unreal Tournament packages.
4 * Copyright © 2009-2011 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <uobject/loadable.h>
26 #include <uobject/exportable.h>
27 #include <engine/palette.h>
33 #include <uobject/uobject.h>
35 struct engine_texture_data {
36 unsigned long width, height;
38 unsigned long datalen;
45 struct engine_texture_data *decode_mipmap(UObject *uo)
47 struct upkg_file *f = uo->pkg_file;
48 struct engine_texture_data *data;
49 size_t rc, pos = 0, buflen;
50 unsigned long end_offset;
51 unsigned char buf[32];
54 buflen = upkg_export_read(f, buf, sizeof buf);
55 if (f->pkg->version >= 63) {
57 * There's an offset to the end of the image data here; not
58 * clear why it's useful since it's implied by the very next
59 * field anyway. However, we will nevertheless validate this
64 end_offset = unpack_32_le(buf);
68 rc = upkg_decode_index(&datalen, buf+pos, buflen-pos);
69 if (rc == 0 || datalen < 0)
73 data = malloc(sizeof *data + datalen);
76 data->datalen = datalen;
78 /* Rewind to the start of the image data, and slurp it in. */
79 if (upkg_export_seek(f, -(long)(buflen-pos), SEEK_CUR) != 0)
81 rc = upkg_export_read(f, data->data, data->datalen);
82 if (rc != data->datalen)
85 /* At this point, the current file offset should match the one recorded
87 if (f->pkg->version >= 63 && end_offset != (f->base + f->offset)) {
88 u_err(uo, "mipmap end offset does not match data size");
92 /* Read in the remaining fields */
93 buflen = upkg_export_read(f, buf, 10);
97 data->width = unpack_32_le(buf+0);
98 data->height = unpack_32_le(buf+4);
99 /* Last 2 bytes seem to be simply the base-2 log of width/height for
100 * whatever reason. Ignore them for now. */
109 class Engine:Texture from U:Object (dynamic)
110 (interface U:Object:Exportable)
111 (interface U:Object:Loadable)
113 private unsigned USize;
114 private unsigned UClamp;
115 private unsigned UBits;
116 private unsigned VSize;
117 private unsigned VClamp;
118 private unsigned VBits;
120 private Engine:Palette *Palette;
121 private Engine:Texture *DetailTexture;
123 private struct engine_texture_data **mipmap_data;
124 private unsigned char mipmap_count;
126 interface U:Object:Loadable
127 private int load(U:Object *uo)
129 struct engine_texture_data **data;
130 Self *self = SELF(uo);
132 if (upkg_export_seek(uo->pkg_file, 0, SEEK_SET) != 0)
135 data = malloc(self->_priv->mipmap_count * sizeof *data);
139 for (int i = 0; i < self->_priv->mipmap_count; i++) {
140 data[i] = decode_mipmap(uo);
142 u_err(uo, "error decoding mipmap level %d", i);
144 /* Unwind the allocations. */
152 self->_priv->mipmap_data = data;
156 interface U:Object:Loadable
157 private void unload(U:Object *uo)
159 Self *self = SELF(uo);
161 for (int i = 0; i < self->_priv->mipmap_count; i++)
162 free(self->_priv->mipmap_data[i]);
163 free(self->_priv->mipmap_data);
164 self->_priv->mipmap_data = NULL;
167 interface U:Object:Exportable
168 private int export_name(U:Object *uo, char *buf, size_t n)
170 return snprintf(buf, n, "%s.pcx", uo->pkg_file->name);
173 interface U:Object:Exportable
174 private int export(U:Object *uo, FILE *f)
176 Self *self = SELF(uo);
177 struct engine_texture_data *data;
178 struct pcx_head head;
180 if (!self->_priv->mipmap_data || !self->_priv->Palette)
182 data = self->_priv->mipmap_data[0];
184 head.width = data->width;
185 head.height = data->height;
187 if (pcx_init_header(&head) != 0)
189 if (fwrite(head.encoded, sizeof head.encoded, 1, f) != 1)
192 for (unsigned i = 0; i < head.height; i++) {
193 if (pcx_write_scanline(&head, data->data+i*data->width,
199 if (pcx_write_palette(self->_priv->Palette, f) != 0)
205 override (U:Object) int deserialize(U:Object *uo)
207 struct upkg_file *f = uo->pkg_file;
208 Self *self = SELF(uo);
212 if (upkg_export_read(f, &self->_priv->mipmap_count, 1) != 1)
214 if (self->_priv->mipmap_count == 0)
219 upkg_export_seek(f, 0, SEEK_SET);
226 , blurb = "Width of the texture."
250 , blurb = "Height of the texture."
273 property OBJECT Palette
275 , blurb = "Reference to the texture's palette."
276 , object_type = Engine:Palette
280 property OBJECT DetailTexture
281 ( nick = "Detail Texture"
282 , blurb = "Reference to the corresponding detail texture."
283 , object_type = Engine:Texture