[dad864]: Spell.cpp  Maximize  Restore  History

Download this file

237 lines (217 with data), 5.0 kB

  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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// spell.cpp: implementation of the Cspell class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <fcntl.h>
#include "chitem.h"
#include "spell.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cspell::Cspell()
{
extheaders=NULL;
featblocks=NULL;
extheadcount=0;
featblkcount=0;
revision=1;
}
Cspell::~Cspell()
{
KillExtHeaders();
KillFeatHeaders();
}
int Cspell::WriteSpellToFile(int fhandle, int calculate)
{
int fbc,i;
header.extheadoffs=sizeof(spell_header);
switch(revision)
{
case 20:
header.extheadoffs+=sizeof(iwd2_header);
memcpy(header.filetype,"SPL V2.0",8);
break;
default:
memcpy(header.filetype,"SPL V1 ",8); //bg1, bg2
break;
}
header.featureoffs=header.extheadoffs+header.extheadcount*sizeof(spl_ext_header);
fbc=header.featblkcount;
if(calculate)
{
for(i=0;i<extheadcount;i++)
{
fbc+=extheaders[i].fbcount;
}
return header.featureoffs+fbc*sizeof(feat_block);
}
if(fhandle<1) return -1;
if(write(fhandle,&header,sizeof(spell_header) )!=sizeof(spell_header) )
{
return -2; // short file, invalid item
}
if(revision==20)
{
if(write(fhandle,&iwd2header,sizeof(iwd2_header) )!=sizeof(iwd2_header) )
{
return -2;
}
}
for(i=0;i<extheadcount;i++)
{
extheaders[i].fboffs=(unsigned short) fbc;
if(write(fhandle, &extheaders[i], sizeof(spl_ext_header) )!=sizeof(spl_ext_header) )
{
return -2;
}
fbc+=extheaders[i].fbcount;
}
if(fbc!=featblkcount)
{
return -2;
}
for(i=0;i<featblkcount;i++)
{
if(write(fhandle, &featblocks[i], sizeof(feat_block) )!=sizeof(feat_block) )
{
return -2;
}
}
m_changed=false;
return 0;
}
CString Cspell::RetrieveResourceRef(int fh)
{
CString tmpstr;
int required;
if(fh<1) return "";
required=(BYTE *) &header.icon-(BYTE *) &header+8;
if(read(fh, &header, required)!=required)
{
close(fh);
return "";
}
close(fh);
RetrieveResref(tmpstr,header.icon);
return tmpstr;
}
long Cspell::RetrieveNameRef(int fh)
{
int required;
if(fh<1) return -1;
required=(BYTE *) &header.spellname-(BYTE *) &header+sizeof(long);
if(read(fh, &header, required)!=required)
{
close(fh);
return -1;
}
close(fh);
return header.spellname;
}
//does no apply for IWD2
long Cspell::RetrieveTypeAndLevel(int fh)
{
int required;
if(fh<1) return -1;
required=(BYTE *) &header.level-(BYTE *) &header+sizeof(long);
if(read(fh, &header, required)!=required)
{
close(fh);
return -1;
}
close(fh);
if(iwd2_structures()) return header.level;
switch(header.spelltype)
{
case 2:
return (header.level>7)?6:(header.level-1);
case 1:
return (header.level>9)?15:(header.level+6);
case 4:
return 16;
default:
return -1;
}
}
int Cspell::ReadSpellFromFile(int fhandle, long maxlen)
{
int fullsize;
int fbc;
int i;
m_changed=false;
if(fhandle<1) return -1;
if(maxlen==-1) maxlen=filelength(fhandle);
if(!maxlen) return -2; //short file, invalid item
if(read(fhandle,&header,sizeof(spell_header) )!=sizeof(spell_header) )
{
return -2; // short file, invalid item
}
if(memcmp(header.filetype,"SPL ",4) )
{
return -2;
}
if(header.revision[0]!='V')
{
return -2;
}
if(header.revision[1]=='2')
{
if(read(fhandle,&iwd2header,sizeof(iwd2_header) )!=sizeof(iwd2_header) )
{
return -2; // short file, invalid item
}
fullsize=sizeof(header)+sizeof(iwd2header);
revision=20;
}
else
{
fullsize=sizeof(header);
revision=1;
}
if(header.extheadoffs!=fullsize )
{
return -2;
}
if(header.extheadcount!=extheadcount)
{
KillExtHeaders();
extheaders=new spl_ext_header[header.extheadcount];
if(!extheaders) return -3;
extheadcount=header.extheadcount;
}
fbc=header.featblkcount;
for(i=0;i<extheadcount;i++)
{
if(read(fhandle, &extheaders[i], sizeof(spl_ext_header) )!=sizeof(spl_ext_header) )
{
return -2;
}
fbc+=extheaders[i].fbcount;
fullsize+=sizeof(spl_ext_header);
}
if(fbc!=featblkcount)
{
KillFeatHeaders();
featblocks=new feat_block[fbc];
if(!featblocks) return -3;
featblkcount=fbc;
}
for(i=0;i<featblkcount;i++)
{
if(read(fhandle, &featblocks[i], sizeof(feat_block) )!=sizeof(feat_block) )
{
return -2;
}
fullsize+=sizeof(feat_block);
}
if(maxlen!=fullsize)
{
return 1; //incorrect length
}
return 0;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks