2018-04-02 02:51:12 +05:30
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-04-04 14:06:28 +05:30
|
|
|
#include "devil1pld.h"
|
|
|
|
#include "devil1tex.h"
|
|
|
|
|
2018-04-02 07:44:58 +05:30
|
|
|
bool writedata(const char *basename,
|
|
|
|
unsigned int id,
|
|
|
|
const char *data,
|
|
|
|
unsigned int dsize) {
|
|
|
|
bool status = false;
|
|
|
|
char *fn = (char*)malloc(sizeof(char) * strlen(basename) + 4);
|
|
|
|
sprintf(fn, "%s_%d", basename, id);
|
|
|
|
FILE *out = fopen(fn, "w");
|
|
|
|
if (out != NULL) {
|
|
|
|
fwrite(data, sizeof(char), dsize, out);
|
|
|
|
fclose(out);
|
|
|
|
status = true;
|
|
|
|
}
|
|
|
|
free(fn);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-04-02 14:25:07 +05:30
|
|
|
void splitpld(const char *filedata,
|
|
|
|
unsigned int filesize,
|
|
|
|
const struct PldHeader *ph,
|
|
|
|
const char *name) {
|
2018-04-02 04:47:58 +05:30
|
|
|
char *wbuffer = NULL; // write buffer that will change.
|
|
|
|
unsigned int wsize = 0; // size of wbuffer.
|
2018-04-02 07:02:54 +05:30
|
|
|
unsigned int i;
|
2018-04-02 07:44:58 +05:30
|
|
|
unsigned int start = 0; // ending offset for data
|
|
|
|
unsigned int end = 0; // ending offset for data
|
|
|
|
for (i = 0; (i + 1) < (ph -> numOffset) + 1; i++) {
|
|
|
|
start = ph -> offsets[i];
|
|
|
|
// the last offset still has some data until the end of file
|
2018-04-02 14:25:07 +05:30
|
|
|
end = (i == (ph -> numOffset) - 1) ?
|
|
|
|
filesize : ph -> offsets[i + 1];
|
2018-04-02 07:02:54 +05:30
|
|
|
// copy sector to write buffer
|
2018-04-02 07:44:58 +05:30
|
|
|
wsize = end - start;
|
2018-04-02 04:47:58 +05:30
|
|
|
wbuffer = (char*)malloc(sizeof(char) * wsize);
|
2018-04-02 14:25:07 +05:30
|
|
|
memcpy(wbuffer, filedata + start, wsize);
|
2018-04-02 07:02:54 +05:30
|
|
|
// create a file.
|
2018-04-02 07:44:58 +05:30
|
|
|
writedata(name, i, wbuffer, wsize);
|
2018-04-02 14:25:07 +05:30
|
|
|
free(wbuffer);
|
2018-04-02 04:47:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 11:54:14 +05:30
|
|
|
char *loadfile(const char *fname, unsigned int *s) {
|
2018-04-02 02:51:12 +05:30
|
|
|
FILE *f = fopen(fname, "rb");
|
|
|
|
unsigned int size = 0; // number of elements to buffer;
|
|
|
|
unsigned int rcnt = 0; // number of char's read by fread(...)
|
|
|
|
if (f == NULL) {
|
|
|
|
perror("Error 1: ");
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
// this method of determining file size doesn't work until 2 GB.
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
size = ftell(f);
|
|
|
|
rewind(f);
|
2018-04-02 07:02:54 +05:30
|
|
|
char *buf = (char*)malloc(sizeof(char) * size);
|
2018-04-02 05:49:39 +05:30
|
|
|
if (buf == NULL) {
|
2018-04-02 02:51:12 +05:30
|
|
|
perror("Error 2: ");
|
2018-04-02 05:49:39 +05:30
|
|
|
free(buf);
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
2018-04-02 05:49:39 +05:30
|
|
|
rcnt = fread(buf, sizeof(char), size, f);
|
2018-04-02 02:51:12 +05:30
|
|
|
if (rcnt < size) {
|
|
|
|
perror("Error 3: ");
|
2018-04-02 05:49:39 +05:30
|
|
|
free(buf);
|
2018-04-02 07:02:54 +05:30
|
|
|
return NULL;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
fclose(f);
|
2018-04-02 07:44:58 +05:30
|
|
|
*s = rcnt;
|
2018-04-02 07:02:54 +05:30
|
|
|
return buf;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
|
|
|
|
2018-04-02 13:57:21 +05:30
|
|
|
bool unpackpld (const char *filedata,
|
|
|
|
unsigned int filesize,
|
|
|
|
const char *filename) {
|
2018-04-02 11:54:14 +05:30
|
|
|
bool status = false;
|
2018-04-02 13:57:21 +05:30
|
|
|
printf("fsize is %i\n", filesize);
|
|
|
|
if (filedata != NULL && filesize != 0) {
|
2018-04-02 11:54:14 +05:30
|
|
|
struct PldHeader *pldh = getpldh(filedata);
|
2018-04-02 14:25:07 +05:30
|
|
|
splitpld(filedata, filesize, pldh, filename);
|
2018-04-02 13:57:21 +05:30
|
|
|
destroypldh(pldh);
|
2018-04-02 11:54:14 +05:30
|
|
|
status = true;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
2018-04-02 11:54:14 +05:30
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:08:30 +05:30
|
|
|
/* void unpacktextures(const char *filedata, unsigned int filesize) {
|
|
|
|
struct TexturePack *tp = (struct TexturePack*)filedata;
|
2018-04-04 22:42:29 +05:30
|
|
|
struct TextureBatchDescriptor **bds = NULL;
|
|
|
|
bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber);
|
2018-04-05 08:08:30 +05:30
|
|
|
gettbd(bds, filedata, filesize);
|
2018-04-04 14:06:28 +05:30
|
|
|
printtph(tp);
|
2018-04-04 22:42:29 +05:30
|
|
|
int i;
|
|
|
|
for (i = 0; i < tp -> batchNumber; i++) {
|
|
|
|
printtbd(bds[i]);
|
|
|
|
}
|
|
|
|
free(bds);
|
2018-04-05 08:08:30 +05:30
|
|
|
return;
|
2018-04-05 10:19:32 +05:30
|
|
|
|
|
|
|
const char *fmt = (const char*)malloc(strlen(filename) + 3 + 3);
|
|
|
|
strcat(fmt, filename);
|
|
|
|
strcat(fmt, "%d.dds");
|
2018-04-05 08:08:30 +05:30
|
|
|
} */
|
|
|
|
|
2018-04-05 13:40:52 +05:30
|
|
|
void write(const char *filename,
|
|
|
|
unsigned int id,
|
2018-04-06 02:57:29 +05:30
|
|
|
const char* t,
|
2018-04-05 13:40:52 +05:30
|
|
|
unsigned int size) {
|
|
|
|
if (filename == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned int written = 0;
|
|
|
|
FILE *out = fopen(filename, "wb");
|
|
|
|
if (out != NULL) {
|
|
|
|
written = fwrite(t, sizeof(unsigned char), size, out);
|
|
|
|
fclose(out);
|
2018-04-06 02:57:29 +05:30
|
|
|
/* unsigned int i;
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
printf("w %u \n", *(t + i));
|
|
|
|
} */
|
2018-04-05 13:40:52 +05:30
|
|
|
if (written == 0) {
|
|
|
|
perror("texture write error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 10:19:32 +05:30
|
|
|
void unpacktextures(const char *filedata,
|
2018-04-05 13:40:52 +05:30
|
|
|
unsigned int filesize,
|
|
|
|
const char *filename) {
|
2018-04-05 10:19:32 +05:30
|
|
|
struct TexturePack *tp = (struct TexturePack*)filedata;
|
2018-04-05 13:40:52 +05:30
|
|
|
struct TextureBatch **batches = NULL;
|
2018-04-05 10:19:32 +05:30
|
|
|
struct TextureBatchDescriptor **descripts = NULL;
|
2018-04-05 13:40:52 +05:30
|
|
|
batches = (struct TextureBatch**)malloc(tp -> batchNumber);
|
2018-04-05 10:19:32 +05:30
|
|
|
descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber);
|
|
|
|
gettbd(descripts, filedata, filesize);
|
|
|
|
// array of pointers - texture data exists in memory in filedata.
|
|
|
|
unsigned int texturecount = gettexturebatch(batches,
|
|
|
|
descripts,
|
|
|
|
filedata,
|
|
|
|
filesize);
|
2018-04-06 02:57:29 +05:30
|
|
|
unsigned int *tloc = (unsigned int*)
|
|
|
|
malloc(sizeof(unsigned int) * texturecount);
|
|
|
|
gettextures(tloc, tp, batches, descripts);
|
2018-04-05 13:40:52 +05:30
|
|
|
// add 3 to buffer for digits, & 4 for file extension
|
|
|
|
char *fmt = (char*)malloc(strlen(filename) + 3 + 4);
|
|
|
|
unsigned int texsize = 0;
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < texturecount; i++) {
|
|
|
|
texsize = descripts[i/(tp -> batchNumber)] -> textureSize;
|
|
|
|
sprintf(fmt, "test_%d.dds", i);
|
2018-04-06 02:57:29 +05:30
|
|
|
write(fmt, i, filedata + tloc[i], texsize);
|
2018-04-05 13:40:52 +05:30
|
|
|
}
|
|
|
|
free(fmt);
|
2018-04-06 02:57:29 +05:30
|
|
|
free(tloc);
|
2018-04-05 10:19:32 +05:30
|
|
|
free(descripts);
|
|
|
|
free(batches);
|
|
|
|
}
|
|
|
|
|
2018-04-05 08:08:30 +05:30
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
char *f = argv[1];
|
|
|
|
unsigned int bufsize = 0;
|
|
|
|
char *buffer = loadfile(f, &bufsize);
|
|
|
|
// unpackpld(buffer, bufsize, f);
|
2018-04-05 13:40:52 +05:30
|
|
|
unpacktextures(buffer, bufsize, f);
|
2018-04-02 13:57:21 +05:30
|
|
|
free(buffer);
|
2018-04-02 02:51:12 +05:30
|
|
|
return 0;
|
2018-04-02 04:47:58 +05:30
|
|
|
}
|
2018-04-02 07:02:54 +05:30
|
|
|
|