-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclsKDF2.cls
591 lines (449 loc) · 14.5 KB
/
clsKDF2.cls
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsKDF2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'-------------------------------------
'--> KenamicK Data File Pack 2
'--> by Peter "Pro-XeX" Petrov
'--> KenamicK Entertainment 1998-2002
'-------------------------------------
Private Type stKDFile
strName As String ' file name
strInfo As String ' extended file info
lPosition As Long ' position in packet
End Type
Private Const KDF_IDENTITY = "KDF" ' packet identity
Private Const KDF_TAGIDENTITY = "KDF_TAG" ' packet identity
Private Const KDF_VERSION = 20 ' packet version (2.0)
Dim m_arKDFile() As stKDFile ' array of all the files
Dim m_arKDFilePath() As String ' file path ( not part of the main data structure )
Dim m_lnumFiles As Long ' number of files in packet
Dim m_strPacketName As String ' packet file name
Dim m_strLog As String ' log messages
Dim m_bTagOpened As Boolean ' tag opened flag
'////////////////////////////////////////////////////////////////
'//// On initialization
'////////////////////////////////////////////////////////////////
Private Sub _
Class_Initialize()
AppendToLog ("Packeger Class Ready.")
Call Reset
End Sub
'////////////////////////////////////////////////////////////////
'//// On termination
'////////////////////////////////////////////////////////////////
Private Sub _
Class_Terminate()
Call Reset
End Sub
'////////////////////////////////////////////////////////////////
'//// List all entires
'//// LISTBOX lstBox - a valid listbox object
'////////////////////////////////////////////////////////////////
Public Sub _
ListAll(lstBox As ListBox)
Dim cn As Long
' clear previous contents
lstBox.Clear
' list entries
For cn = 0 To m_lnumFiles
lstBox.AddItem (m_arKDFile(cn).strName)
Next
End Sub
'////////////////////////////////////////////////////////////////
'//// Clear all arrays
'////////////////////////////////////////////////////////////////
Private Sub _
Reset()
AppendToLog ("Reseting arrays and vars.")
' reset file counter
m_lnumFiles = -1
' reset tag flag
m_bTagOpened = False
' clear array
Erase m_arKDFile()
Erase m_arKDFilePath()
End Sub
'////////////////////////////////////////////////////////////////
'//// Add message to local log
'//// STRING strMsg - text message to add
'////////////////////////////////////////////////////////////////
Private Sub _
AppendToLog(strMsg As String)
m_strLog = m_strLog & vbCrLf & strMsg
End Sub
'////////////////////////////////////////////////////////////////
'//// Load Tag
'//// STRING strFileName - path&filename to load
'////////////////////////////////////////////////////////////////
Public Function _
LoadTag(strFileName As String) As Boolean
' tarp local errors
On Local Error GoTo KDFTAGLOADERROR
Dim cn As Integer
Dim hFile As Integer
Dim strIdentity As String
' reset arrays and counter
Call Reset
' get free file handle
hFile = freefile()
AppendToLog ("Opening tag " & strFileName)
Open (strFileName) For Binary Access Read As #hFile
' get file identity
strIdentity = Space$(Len(KDF_TAGIDENTITY))
Get #hFile, , strIdentity
If (strIdentity <> KDF_TAGIDENTITY) Then
AppendToLog ("Invalid tag file!")
GoTo KDFTAGLOADERROR
End If
' get num of files
Get #hFile, , m_lnumFiles
' resize array
ReDim m_arKDFile(m_lnumFiles)
' load all entries
For cn = 0 To m_lnumFiles
Get #hFile, , m_arKDFile(cn)
Next
Close #hFile
m_bTagOpened = True
LoadTag = True
AppendToLog ("Tag Opened...")
Exit Function
KDFTAGLOADERROR:
LoadTag = False
AppendToLog ("Error opening tag " & strFileName)
m_bTagOpened = False
End Function
'////////////////////////////////////////////////////////////////
'//// Load packet
'//// STRING strFileName - path&filename to load
'////////////////////////////////////////////////////////////////
Public Function _
LoadPacket(strFileName As String) As Boolean
' trap local errors
On Local Error GoTo KDFLOADERROR
Dim hFile As Integer
Dim hFile2 As Integer
Dim strIdentity As String
Dim nVersion As Integer
' see if tag is opened
If (Not m_bTagOpened) Then
AppendToLog ("Tag file is not opened!")
GoTo KDFLOADERROR
End If
AppendToLog ("Opening packet " & strFileName)
hFile = freefile()
' open packet
Open (strFileName) For Binary Access Read As #hFile
' get identity
strIdentity = Space$(Len(KDF_IDENTITY))
Get #hFile, , strIdentity
' check identity
If (strIdentity <> KDF_IDENTITY) Then
AppendToLog ("Not a valid KDF file!")
GoTo KDFLOADERROR
End If
' get version
Get #hFile, , nVersion
If (nVersion > KDF_VERSION) Then
AppendToLog ("KDF Packet version is higher than the supported!")
GoTo KDFLOADERROR
End If
'...
Close #hFile
' set packet name
m_strPacketName = strFileName
' success
LoadPacket = True
AppendToLog ("Loading packet successful...")
Exit Function
KDFLOADERROR:
'...
AppendToLog ("Error loading packet " & strFileName)
LoadPacket = False
End Function
'////////////////////////////////////////////////////////////////
'//// Save Tag
'//// STRING strFileName - path&filename to save
'////////////////////////////////////////////////////////////////
Public Function _
SaveTag(strFileName As String) As Boolean
' tarp local errors
On Local Error GoTo KDFTAGSAVERROR
Dim cn As Integer
Dim hFile As Integer
' check enties count
If (m_lnumFiles < 0) Then
AppendToLog ("No files in packet!")
Exit Function
End If
' get free file handle
hFile = freefile()
AppendToLog ("Saving tag " & strFileName)
' check for extension
If (Not CBool(InStr(Right$(strFileName, 4), "."))) Then
strFileName = strFileName & ".kdf"
End If
Open (strFileName) For Binary Access Write As #hFile
' save tag
Put #hFile, , KDF_TAGIDENTITY
' save num of entires
Put #hFile, , m_lnumFiles
' save all entries
For cn = 0 To m_lnumFiles
Put #hFile, , m_arKDFile(cn)
Next
Close #hFile
SaveTag = True
AppendToLog ("Saving tag successful...")
Exit Function
KDFTAGSAVERROR:
SaveTag = False
AppendToLog ("Error saving tag " & strFileName)
End Function
'////////////////////////////////////////////////////////////////
'//// Save packet
'//// STRING strFileName - path&filename to save
'////////////////////////////////////////////////////////////////
Public Function _
SavePacket(strFileName As String) As Boolean
' trap local errors
On Local Error GoTo KDFSAVERROR
Dim cn As Long
Dim hFile As Integer
Dim hFile2 As Integer
Dim arData() As Byte
' check enties count
If (m_lnumFiles < 0) Then
AppendToLog ("No files in packet!")
Exit Function
End If
' get free file handles
hFile = freefile()
hFile2 = freefile() + 1
AppendToLog ("Saving packet " & strFileName)
' check for extension
If (Not CBool(InStr(Right$(strFileName, 4), "."))) Then
strFileName = strFileName & ".kdf"
End If
Open (strFileName) For Binary Access Write As #hFile
' save header
Put #hFile, , KDF_IDENTITY
' save version
Put #hFile, , KDF_VERSION
For cn = 0 To m_lnumFiles
' open KDFile entry
Open (m_arKDFilePath(cn)) For Binary Access Read As #hFile2
' resize data array
ReDim arData((LOF(hFile2) - 1))
' get data
Get #hFile2, , arData()
Close #hFile2
' save position in packet
m_arKDFile(cn).lPosition = Seek(hFile)
' save data
Put #hFile, , arData()
Next cn
Close #hFile
SavePacket = True
AppendToLog ("Packet saved successfully...")
Exit Function
KDFSAVERROR:
'...
SavePacket = False
AppendToLog ("Error saving packet " & strFileName)
End Function
'////////////////////////////////////////////////////////////////
'//// Extarct File from packet
'//// STRING strFileName - path&filename to load
'//// STRING strDestPath - destination path&
'///// ...not finished
'////////////////////////////////////////////////////////////////
Public Function _
ExtractFile(strFileName As String, strDestPath As String) As Boolean
On Local Error GoTo EXFILERROR
'...
EXFILERROR:
'...
ExtractFile = False
End Function
'////////////////////////////////////////////////////////////////
'//// Delete file from packet
'//// STRING strFileName - path&filename to delete from packet
'////////////////////////////////////////////////////////////////
Public Function _
DeleteFile(strFileName As String)
Dim i As Long
Dim j As Long
Dim bFound As Boolean
' check enties count
If (m_lnumFiles < 0) Then
AppendToLog ("No files in packet!")
Exit Function
End If
AppendToLog ("Removing " & strFileName)
i = 0
j = 0
' loop trough all entries
Do While (i <= m_lnumFiles)
' look for file
If (m_arKDFile(i).strName = strFileName) Then
'' exchangle files & paths
'm_arKDFile(i) = m_arKDFile(m_lnumFiles)
'm_arKDFilePath(i) = m_arKDFilePath(m_lnumFiles)
bFound = True
' kill this entry
For j = i To (m_lnumFiles - 1)
m_arKDFile(i) = m_arKDFile(i + 1)
m_arKDFilePath(i) = m_arKDFilePath(i + 1)
Next
' decrement file counter
m_lnumFiles = m_lnumFiles - 1
' reset if less than 0
If (m_lnumFiles < 0) Then
Call Reset
End If
' resize arrays
ReDim Preserve m_arKDFile(m_lnumFiles)
ReDim Preserve m_arKDFilePath(m_lnumFiles)
' job done, exit stage...left ;)
Exit Do
End If
' increment counter
i = i + 1
Loop
If (bFound) Then
AppendToLog (strFileName & " was removed sucessfully.")
Else
AppendToLog ("Could not find " & strFileName & " in archive!")
End If
End Function
'////////////////////////////////////////////////////////////////
'//// Add file to packet
'//// STRING strFileName - path&filename to add to packet
'//// STRING strInfo - filename info
'//// Returns: index in the kdf array
'////////////////////////////////////////////////////////////////
Public Function _
AddFile(strFileName As String, _
Optional strInfo As String = "") As Long
' incremenet files counter
m_lnumFiles = m_lnumFiles + 1
' resize array
ReDim Preserve m_arKDFile(m_lnumFiles)
ReDim Preserve m_arKDFilePath(m_lnumFiles)
' fill info
Dim llbl As Long ' last backlash located@
Dim strFile As String
Dim cn As Integer
AppendToLog ("Adding " & strFileName)
' extract file name from file path
Do While (cn < Len(strFileName))
cn = llbl + 1
llbl = InStr(cn, strFileName, "\")
' no more backlashes located
If (llbl = 0) Then
strFile = Mid$(strFileName, cn)
Exit Do
End If
Loop
m_arKDFile(m_lnumFiles).strName = strFile
m_arKDFile(m_lnumFiles).strInfo = strInfo
m_arKDFilePath(m_lnumFiles) = strFileName
' return
AddFile = m_lnumFiles
AppendToLog (strFileName & " was added successfully.")
End Function
' *_*_-_*----- proprties -----*_-_*_*
'////////////////////////////////////////////////////////////////
'//// Retrieve loaded packet name
'////////////////////////////////////////////////////////////////
Public Property Get _
GetPacketName() As String
GetPacketName = m_strPacketName
End Property
'////////////////////////////////////////////////////////////////
'//// Get entry position from it's name
'//// STRING strFileName - valid file name
'////////////////////////////////////////////////////////////////
Public Property Get _
GetEntryPositionFromName(strFileName As String) As Long
Dim cn As Integer
Dim bFound As Boolean
' check for opened tag
If (Not m_bTagOpened Or _
m_lnumFiles < 0) Then Exit Property
For cn = 0 To m_lnumFiles
'Stop
If (m_arKDFile(cn).strName = strFileName) Then
bFound = True
GetEntryPositionFromName = m_arKDFile(cn).lPosition
Exit For
End If
Next cn
' check for file found
If (Not bFound) Then GetEntryPositionFromName = -1
End Property
'////////////////////////////////////////////////////////////////
'//// Get entry position from it's index
'//// LONG lNum - valid index
'////////////////////////////////////////////////////////////////
Public Property Get _
GetEntryPositionFromIndex(lNum As Long) As Long
If (lNum < 0 Or lNum > m_lnumFiles) Then
GetEntryPositionFromIndex = -1
Exit Property
End If
GetEntryPositionFromIndex = m_arKDFile(lNum).lPosition
End Property
'////////////////////////////////////////////////////////////////
'//// Get entry info from it's index
'//// LONG lNum - valid index
'////////////////////////////////////////////////////////////////
Public Property Get _
GetEntryInfo(lNum As Long) As String
If (lNum < 0 Or lNum > m_lnumFiles) Then
GetEntryInfo = ""
Exit Property
End If
GetEntryInfo = m_arKDFile(lNum).strInfo
End Property
'////////////////////////////////////////////////////////////////
'//// Get entry name from it's index
'//// LONG lNum - valid index
'////////////////////////////////////////////////////////////////
Public Property Get _
GetEntryName(lNum As Long) As String
If (lNum < 0 Or lNum > m_lnumFiles) Then
GetEntryName = ""
Exit Property
End If
GetEntryName = m_arKDFile(lNum).strName
End Property
'////////////////////////////////////////////////////////////////
'//// Get class status/log
'////////////////////////////////////////////////////////////////
Public Property Get _
GetLog() As String
GetLog = m_strLog
End Property
'////////////////////////////////////////////////////////////////
'//// Get version
'////////////////////////////////////////////////////////////////
Public Property Get _
GetVersion() As String
Dim strTemp As String
strTemp = CStr(KDF_VERSION)
strTemp = Left$(strTemp, 1) & "." & Mid$(strTemp, 2)
GetVersion = strTemp
End Property