mirror of
https://notabug.org/scuti/lib3ddevil1
synced 2024-11-29 10:12:02 +05:30
183 lines
5.6 KiB
C
183 lines
5.6 KiB
C
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "devil1pld.h"
|
|
#include "devil1tex.h"
|
|
|
|
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;
|
|
}
|
|
|
|
void splitpld(const char *filedata,
|
|
unsigned int filesize,
|
|
const struct PldHeader *ph,
|
|
const char *name) {
|
|
char *wbuffer = NULL; // write buffer that will change.
|
|
unsigned int wsize = 0; // size of wbuffer.
|
|
unsigned int i;
|
|
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
|
|
end = (i == (ph -> numOffset) - 1) ?
|
|
filesize : ph -> offsets[i + 1];
|
|
// copy sector to write buffer
|
|
wsize = end - start;
|
|
wbuffer = (char*)malloc(sizeof(char) * wsize);
|
|
memcpy(wbuffer, filedata + start, wsize);
|
|
// create a file.
|
|
writedata(name, i, wbuffer, wsize);
|
|
free(wbuffer);
|
|
}
|
|
}
|
|
|
|
char *loadfile(const char *fname, unsigned int *s) {
|
|
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: ");
|
|
return NULL;
|
|
}
|
|
// this method of determining file size doesn't work until 2 GB.
|
|
fseek(f, 0, SEEK_END);
|
|
size = ftell(f);
|
|
rewind(f);
|
|
char *buf = (char*)malloc(sizeof(char) * size);
|
|
if (buf == NULL) {
|
|
perror("Error 2: ");
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
rcnt = fread(buf, sizeof(char), size, f);
|
|
if (rcnt < size) {
|
|
perror("Error 3: ");
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
fclose(f);
|
|
*s = rcnt;
|
|
return buf;
|
|
}
|
|
|
|
bool unpackpld (const char *filedata,
|
|
unsigned int filesize,
|
|
const char *filename) {
|
|
bool status = false;
|
|
printf("fsize is %i\n", filesize);
|
|
if (filedata != NULL && filesize != 0) {
|
|
struct PldHeader *pldh = getpldh(filedata);
|
|
splitpld(filedata, filesize, pldh, filename);
|
|
destroypldh(pldh);
|
|
status = true;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
void write(const char *filename,
|
|
const char* t,
|
|
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);
|
|
if (written == 0) {
|
|
perror("texture write error");
|
|
}
|
|
}
|
|
}
|
|
|
|
void unpacktextures(const char *filedata,
|
|
unsigned int filesize,
|
|
const char *filename) {
|
|
struct TexturePack *tp = (struct TexturePack*)filedata;
|
|
struct TextureBatch **batches = NULL;
|
|
struct TextureBatchDescriptor **descripts = NULL;
|
|
batches = (struct TextureBatch**)malloc(tp -> batchNumber);
|
|
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);
|
|
unsigned int *tloc = (unsigned int*)
|
|
malloc(sizeof(unsigned int) * texturecount);
|
|
locatetextures(tloc, tp, descripts);
|
|
// 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, "%s_%d.dds", filename, i);
|
|
write(fmt, filedata + tloc[i], texsize);
|
|
}
|
|
free(fmt);
|
|
free(tloc);
|
|
free(descripts);
|
|
free(batches);
|
|
}
|
|
|
|
void exporttextures(const char *filedata,
|
|
unsigned int filesize,
|
|
const char *filename) {
|
|
struct TexturePack *p = NULL;
|
|
struct Texture *t = NULL;
|
|
struct TextureBatchDescriptor *d = NULL;
|
|
char * fmt = NULL;
|
|
if (filename == NULL || filesize == 0) {
|
|
return;
|
|
}
|
|
p = (struct TexturePack*)filedata;
|
|
fmt = (char*)malloc(strlen(filename) + 3 + 4);
|
|
unsigned int i;
|
|
unsigned int j;
|
|
unsigned int id = 0;
|
|
for (i = 0; i < p -> batchNumber; i++) {
|
|
gettexdescriptor(&d, i, filedata, filesize);
|
|
t = (struct Texture*)
|
|
malloc(sizeof(struct Texture) * (d -> texNumber));
|
|
unpacktexbatch(t, i, filedata, filesize);
|
|
for (j = 0; j < d -> texNumber; j++) {
|
|
sprintf(fmt, "%s_%d.dds", filename, id);
|
|
write(fmt, t[j].data, d -> textureSize);
|
|
id++;
|
|
}
|
|
free(t);
|
|
}
|
|
free(fmt);
|
|
return;
|
|
}
|
|
|
|
int main(int argc, char ** argv) {
|
|
char *f = argv[1];
|
|
unsigned int bufsize = 0;
|
|
char *buffer = loadfile(f, &bufsize);
|
|
// unpackpld(buffer, bufsize, f);
|
|
exporttextures(buffer, bufsize, f);
|
|
free(buffer);
|
|
return 0;
|
|
}
|
|
|