mirror of
https://notabug.org/scuti/lib3ddevil1
synced 2024-11-26 00:38:55 +05:30
154 lines
4.1 KiB
C
154 lines
4.1 KiB
C
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "devil1pld.h"
|
|
#include "devil1tex.h"
|
|
#include "devil1geo.h"
|
|
|
|
|
|
#define TYPE_ID_LENGTH 4
|
|
|
|
|
|
|
|
|
|
|
|
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 works up to 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;
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
|
|
bool unpackpld(const char *filedata,
|
|
unsigned int filesize,
|
|
const char *filename) {
|
|
if (filedata == NULL || filesize <= 0 || filename == NULL) {
|
|
return false;
|
|
}
|
|
struct PldHeader h;
|
|
DEVIL1PLD.getheader(&h, filedata);
|
|
char *fn = NULL;
|
|
fn = (char*)malloc(strlen(filename) + 3 + 4);
|
|
int size = 0;
|
|
unsigned int i = 0;
|
|
char textureid[TYPE_ID_LENGTH] = {'\0', '2', '3', 'T'};
|
|
for (i = 0; i < h.numOffset; i++) {
|
|
const char * currentfile = filedata + h.offsets[i];
|
|
size = DEVIL1PLD.sizeofsector(&h, i, filesize);
|
|
if (strncmp( currentfile, textureid, TYPE_ID_LENGTH ) == 0)
|
|
sprintf(fn, "%s_%d.txp", filename, i);
|
|
else
|
|
sprintf(fn, "%s_%d", filename, i);
|
|
write(fn, currentfile, size);
|
|
}
|
|
free(fn);
|
|
return true;
|
|
}
|
|
|
|
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 (filedata == 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++) {
|
|
DEVIL1TEX.getbatchdesc(&d, i, filedata, filesize);
|
|
t = (struct Texture*)
|
|
malloc(sizeof(struct Texture) * (d -> texNumber));
|
|
DEVIL1TEX.gettextures(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;
|
|
}
|
|
|
|
void extractmeshes(const char *filedata,
|
|
unsigned int filesize,
|
|
const char *filename) {
|
|
if (filedata == NULL || filesize <= 0) {
|
|
return;
|
|
}
|
|
struct Header *h = (struct Header*)filedata;
|
|
struct MeshHeader *mh = NULL;
|
|
struct Mesh m;
|
|
m.b = NULL;
|
|
unsigned int i;
|
|
for (i = 0; i < h -> numMesh; i++) {
|
|
getmeshheader(&mh, i, filedata);
|
|
m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch));
|
|
if (m.b != NULL) {
|
|
getmesh(&m, i, filedata);
|
|
// do something with mesh e.g write to file.
|
|
free(m.b);
|
|
}
|
|
} // end for
|
|
}
|
|
|
|
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);
|
|
// extractmeshes(buffer, bufsize, f);
|
|
free(buffer);
|
|
return 0;
|
|
}
|
|
|
|
|