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-07 15:54:07 +05:30
|
|
|
#include "devil1geo.h"
|
2018-04-04 14:06:28 +05:30
|
|
|
|
2018-04-10 08:51:56 +05:30
|
|
|
|
|
|
|
#define TYPE_ID_LENGTH 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-04-10 08:51:56 +05:30
|
|
|
// this method of determining file size works up to 2 GB.
|
2018-04-02 02:51:12 +05:30
|
|
|
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-07 04:37:35 +05:30
|
|
|
void write(const char *filename,
|
|
|
|
const char* t,
|
|
|
|
unsigned int size) {
|
|
|
|
if (filename == NULL) {
|
|
|
|
return;
|
2018-04-02 02:51:12 +05:30
|
|
|
}
|
2018-04-07 04:37:35 +05:30
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 03:12:52 +05:30
|
|
|
|
|
|
|
bool unpackpld(const char *filedata,
|
|
|
|
unsigned int filesize,
|
|
|
|
const char *filename) {
|
2018-04-07 08:48:27 +05:30
|
|
|
if (filedata == NULL || filesize <= 0 || filename == NULL) {
|
2018-04-07 04:35:12 +05:30
|
|
|
return false;
|
|
|
|
}
|
2018-04-07 03:12:52 +05:30
|
|
|
struct PldHeader h;
|
2018-04-18 08:07:59 +05:30
|
|
|
DEVIL1PLD.getheader(&h, filedata);
|
2018-04-07 04:35:12 +05:30
|
|
|
char *fn = NULL;
|
|
|
|
fn = (char*)malloc(strlen(filename) + 3 + 4);
|
|
|
|
int size = 0;
|
2018-04-07 04:18:44 +05:30
|
|
|
unsigned int i = 0;
|
2018-04-10 09:46:20 +05:30
|
|
|
char textureid[TYPE_ID_LENGTH] = {'\0', '2', '3', 'T'};
|
2018-04-07 04:18:44 +05:30
|
|
|
for (i = 0; i < h.numOffset; i++) {
|
2018-04-10 08:51:56 +05:30
|
|
|
const char * currentfile = filedata + h.offsets[i];
|
2018-04-18 08:07:59 +05:30
|
|
|
size = DEVIL1PLD.sizeofsector(&h, i, filesize);
|
2018-04-10 09:46:20 +05:30
|
|
|
if (strncmp( currentfile, textureid, TYPE_ID_LENGTH ) == 0)
|
2018-04-10 08:51:56 +05:30
|
|
|
sprintf(fn, "%s_%d.txp", filename, i);
|
|
|
|
else
|
|
|
|
sprintf(fn, "%s_%d", filename, i);
|
|
|
|
write(fn, currentfile, size);
|
2018-04-07 03:12:52 +05:30
|
|
|
}
|
2018-04-07 04:35:12 +05:30
|
|
|
free(fn);
|
|
|
|
return true;
|
2018-04-02 11:54:14 +05:30
|
|
|
}
|
|
|
|
|
2018-04-06 10:22:56 +05:30
|
|
|
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;
|
2018-04-07 08:48:27 +05:30
|
|
|
if (filedata == NULL || filesize == 0) {
|
2018-04-06 10:22:56 +05:30
|
|
|
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++) {
|
2018-04-18 08:33:06 +05:30
|
|
|
DEVIL1TEX.getbatchdesc(&d, i, filedata, filesize);
|
2018-04-06 10:22:56 +05:30
|
|
|
t = (struct Texture*)
|
|
|
|
malloc(sizeof(struct Texture) * (d -> texNumber));
|
2018-04-18 08:33:06 +05:30
|
|
|
DEVIL1TEX.gettextures(t, i, filedata, filesize);
|
2018-04-06 10:22:56 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-08 08:07:43 +05:30
|
|
|
void extractmeshes(const char *filedata,
|
2018-04-07 15:54:07 +05:30
|
|
|
unsigned int filesize,
|
|
|
|
const char *filename) {
|
|
|
|
if (filedata == NULL || filesize <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-07 17:09:16 +05:30
|
|
|
struct Header *h = (struct Header*)filedata;
|
|
|
|
struct MeshHeader *mh = NULL;
|
2018-04-18 07:02:23 +05:30
|
|
|
struct Mesh m;
|
|
|
|
m.b = NULL;
|
2018-04-07 15:54:07 +05:30
|
|
|
unsigned int i;
|
2018-04-14 03:26:14 +05:30
|
|
|
for (i = 0; i < h -> numMesh; i++) {
|
2018-04-18 08:44:45 +05:30
|
|
|
DEVIL1GEO.getmeshheader(&mh, i, filedata);
|
2018-04-18 07:02:23 +05:30
|
|
|
m.b = (struct Batch*)malloc(sizeof(struct Batch) * (mh -> numBatch));
|
|
|
|
if (m.b != NULL) {
|
2018-04-18 08:44:45 +05:30
|
|
|
DEVIL1GEO.getmesh(&m, i, filedata);
|
2018-04-18 07:02:23 +05:30
|
|
|
// do something with mesh e.g write to file.
|
|
|
|
free(m.b);
|
|
|
|
}
|
2018-04-10 09:21:37 +05:30
|
|
|
} // end for
|
2018-04-07 15:54:07 +05:30
|
|
|
}
|
|
|
|
|
2018-04-02 11:54:14 +05:30
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
char *f = argv[1];
|
2018-04-02 13:57:21 +05:30
|
|
|
unsigned int bufsize = 0;
|
|
|
|
char *buffer = loadfile(f, &bufsize);
|
2018-04-07 15:54:07 +05:30
|
|
|
// unpackpld(buffer, bufsize, f);
|
2018-04-18 08:44:45 +05:30
|
|
|
// exporttextures(buffer, bufsize, f);
|
|
|
|
extractmeshes(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
|
|
|
|
2018-04-18 07:02:23 +05:30
|
|
|
|