summaryrefslogtreecommitdiff
path: root/PE/extract-rooms.cpp
blob: 3af349e63c6b64001c46804bc3388e42278a8fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "Main.h"
#include "Input.h"
#include "Output.h"
#include "table.h"

struct tentry {
    int sector;
    unsigned char u[4];
};

struct entry {
    int sector;
    int sizes[3];
};

#define fullsize 206213120
#define offset 0x83b78
#define N 437

CODE_BEGINS
struct entry tab[N];

virtual int startup() throw (GeneralException) {
    int i, j, s, dcount = 1;
    Input * f;
    Output * t;
    int alreadycounted = 0;
    String fn;
    char buff[2048];
    unsigned char b, a1, a2;
    struct tentry te;
    unsigned long orig, pos, truesize, ptr, sig, size;
    int found, empty;

    f = new Input("slus_006.62");
    f->seek(offset);
    
    for (i = 0; i < N; i++) {
	f->read(&te, sizeof(struct tentry));
	tab[i].sector = te.sector;
	tab[i].sizes[0] = te.u[0];
	tab[i].sizes[1] = te.u[1] | (te.u[2] & 0xf);
	tab[i].sizes[2] = (te.u[2] >> 4) | (te.u[3] << 4);
	printm(M_INFO, "entry %3i - offset: %9i, sizes = %4i %4i %4i\n", i, tab[i].sector * 2048,
	       tab[i].sizes[0], tab[i].sizes[1], tab[i].sizes[2]);
    }
    
    delete f;
    
    f = new Input("pe.img");
    
    for (i = 0; i < N; i++) {
	printm(M_INFO, "Dumping room %i\n", i);
	
	if (!tab[i].sizes[0])
	    continue;
	
	f->seek(tab[i].sector * 2048);
	for (j = 0; j < 3; j++) {
	    fn.set("rooms/room-%04i-%02i.out", i, j);
	    
	    t = new Output(fn);
	    
	    for (s = 0; s < tab[i].sizes[j]; s++) {
		f->read(buff, 2048);
		t->write(buff, 2048);
	    }
	    delete t;
	}
    }
    
    for (i = 0; i < N; i++) {
	printm(M_INFO, "Dumping script %i\n", i);

	found = empty = 0;
    
	if (!tab[i].sizes[0])
	    continue;
	
	f->seek((tab[i].sector + tab[i].sizes[0] + tab[i].sizes[1]) * 2048);
	orig = f->tell();

	fn.set("scripts/room-%04i.txt", i);
	t = new Output(fn);
	
	f->read(&truesize, 4);
	f->seek(truesize - 4, SEEK_CUR);
	
	while (!found) {
	    f->read(&ptr, 4);
	    f->seek(-8, SEEK_CUR);
	    pos = f->tell();
	    if ((ptr >> 24) == 1) {
		ptr &= 0x00ffffff;
		f->seek(orig + ptr);
		f->read(&sig, 4);
		if ((sig & 0xffff) == 0xfe) {
		    f->seek(pos);
		    f->read(&size, 4);
		    pos = ptr + orig;
		    found = 1;
		} else if (sig != 0x4f414b41) { /* AKAO */
		    delete t;
		    empty = 1;
		    break;
		}
	    }
	    f->seek(pos);
	}
	
	if (empty)
	    continue;
	
	printm(M_INFO, "Found %i bytes of text at %i\n", size, pos);
	
	for (j = 0; j < size; j++) {
	    b = f->readU8();
	    
	    if (b <= MAXCHAR) {
		t->writeU8(table[b]);
	    } else {
		switch(b) {
		case 0xf7:
		    t->writeU8('\n');
		    break;
		case 0xf8:
		    (*t) << "<PAUSE>\n";
		    break;
		case 0xf9:
		    (*t) << "\n<TCLOSE>\n";
		    break;
		case 0xfa:
		    (*t) << "<AYA>";
		    break;
		case 0xfb:
		    j++;
		    j++;
		    a1 = f->readU8();
		    switch(a1) {
		    case 0:
			(*t) << "<TAG0>";
			break;
		    case 1:
			(*t) << "<TAG1>";
			break;
		    case 9:
			a2 = f->readU8();
			(*t) << "<CHOICES " << a2 << ">\n";
			break;
		    case 7:
			a2 = f->readU8();
			(*t) << "<TIMER " << a2 << ">";
			break;
		    default:
			(*t) << "<UNKCMD " << a1 << ">";
			break;
		    }
		    break;
		case 0xfe:
		    j++;
		    b = f->readU8();
		    (*t) << "<PT" << b << ">\n";
		    break;
		case 0xff:
		    (*t) << "\n<CLOSE>\n";
		    break;
		default:
		    (*t) << String().set("<UNK %02X>", b);
		}
	    }
	}

	delete t;
    }

    delete f;
    return 0;
}
CODE_ENDS