-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1621 lines (1406 loc) · 49.7 KB
/
main.cpp
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// (c) hikami, aka longod
// http://social.bioware.com/project/4206/
// http://social.bioware.com/wiki/datoolset/index.php/GFF
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#include <tchar.h>
#ifdef _DEBUG
#define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define NEW new
#endif
#include <iostream>
#include <fstream>
#include <sstream>
#include <cassert>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <hash_map>
using namespace std;
template <class T>
class GFF_List;
// win32
typedef unsigned char u8;
typedef char s8;
typedef unsigned short u16;
typedef short s16;
typedef unsigned int u32;
typedef int s32;
typedef unsigned long long u64;
typedef long long s64;
typedef float f32;
typedef double f64;
typedef GFF_List<wchar_t> String;
bool g_x360 = false; // big endian
bool g_ignoreEmptyLine = false;
bool g_addIDPrefix = false;
bool g_usingXML = false;
bool g_usingTroika = false;
namespace bit {
u32 swapU32( u32 d ) {
if ( !g_x360 ) {
return d;
}
return (((((d) & 0x000000ff) << 24) | (((d) & 0x0000ff00) << 8) | (((d) & 0x00ff0000) >> 8) | (((d) & 0xff000000) >> 24) ));
}
u16 swapU16( u16 d ) {
if ( !g_x360 ) {
return d;
}
u8 temp[2];
u8* p = reinterpret_cast<u8*>( &d );
temp[0] = p[1];
temp[1] = p[0];
u16* pp = reinterpret_cast<u16*>(temp);
return *pp;
}
wchar_t swapU16( wchar_t d ) {
if ( !g_x360 ) {
return d;
}
u8 temp[2];
u8* p = reinterpret_cast<u8*>( &d );
temp[0] = p[1];
temp[1] = p[0];
wchar_t* pp = reinterpret_cast<wchar_t*>(temp);
return *pp;
}
}
namespace file {
bool read( const char *file, char *&buff, size_t *size ) {
assert(file);
assert(size);
assert(buff == NULL);
ifstream infile( file, ifstream::binary );
if ( infile.fail() ) {
return false;
}
infile.seekg( 0, ifstream::end );
size_t s = static_cast< size_t >(infile.tellg());
infile.seekg( 0, ifstream::beg );
buff = NEW char[s];
infile.read( buff, static_cast<streamsize>(s) );
infile.close();
if ( size ) {
*size = s;
}
return true;
}
bool write( const char *file, const char *buff, size_t size ) {
assert(file);
assert(buff);
ofstream outfile( file, ostream::binary );
if ( outfile.fail() ) {
return false;
}
outfile.write( buff, static_cast<streamsize>(size) );
outfile.close();
return true;
}
bool writeApp( const char *file, const char *buff, size_t size ) {
assert(file);
assert(buff);
ofstream outfile( file, ostream::binary | ostream::app );
if ( outfile.fail() ) {
return false;
}
outfile.write( buff, static_cast<streamsize>(size) );
outfile.close();
return true;
}
}
enum FieldDataTypes : unsigned short {
UINT8 = 0,
INT8 = 1,
UINT16 = 2,
INT16 = 3,
UINT32 = 4,
INT32 = 5,
UINT64 = 6,
INT64 = 7,
FLOAT32 = 8,
FLOAT64 = 9,
Vector3f = 10,
Vector4f = 12,
Quaternionf = 13,
ECString = 14,
Color4f = 15,
Matrix4x4f = 16,
TlkString = 17,
Generic = 0xFFFF,
};
static const char* g_fieldDataType[] =
{
"UINT8 = 0",
"INT8 = 1",
"UINT16 = 2",
"INT16 = 3",
"UINT32 = 4",
"INT32 = 5",
"UINT64 = 6",
"INT64 = 7",
"FLOAT32 = 8",
"FLOAT64 = 9",
"Vector3f = 10",
"UNKNOWN = 11",
"Vector4f = 12",
"Quaternionf = 13",
"ECString = 14",
"Color4f = 15",
"Matrix4x4f = 16",
"TlkString = 17",
};
enum FieldFlag : unsigned short {
LIST = 0x8000,
STRUCT = 0x4000,
REFERENCE = 0x2000,
};
// header
struct GFF_Header {
u32 GFFMagicNumber;
u32 GFFVersion;
u32 TargetPlatform;
u32 FileType;
u32 FileVersion;
u32 StructCount;
u32 DataOffset;
//The first five fields are always in big endian and never byteswapped.
//This keeps those fields human readable on any machine.
};
struct GFF_Struct {
union{
u32 StructType;
s8 Type[ sizeof(u32) ];
};
u32 FieldCount;
u32 FieldOffset;
u32 StructSize;
};
struct GFF_Field {
u32 Label;
union {
u32 FieldType;
struct {
u16 TypeID;
u16 Flags;
} Type;
};
u32 Index;
};
struct HTLK {
u32 tag; // unknown 0 but tlkの値と同じ意味を持つはず
u32 dictOffset; // unknown 1
u32 bitOffset; // unknown 2
};
struct HSTR {
u32 id;
u32 ptr; // このオフセット先は文字列ではない
};
struct HSTRChunk {
HSTRChunk () : id( 0xFFFFFFFF ), offset( 0xFFFFFFFF )/*, length( 0 ), ptr( NULL ) */{}
u32 id;
u32 offset;
//u32 length; // u32でいいかどうかは不明、バイト単位かも
//u32* ptr;
bool operator < ( const HSTRChunk &a ) const {
return id < a.id;
}
};
struct HNode {
HNode() : left(0xFFFFFFFF), right(0xFFFFFFFF) {
}
s32 left;
s32 right;
};
struct TLKEntry {
TLKEntry() : offset( 0 ) {
}
TLKEntry( const TLKEntry& copy ) : offset( 0 ) {
this->id = copy.id;
this->str = copy.str;
}
u32 offset;
wstring id; // copyしまくりだが
wstring str;
std::vector<u8> bit;
//wchar_t* ptr;
//u32 length;
};
class HuffmanNode {
public:
HuffmanNode() : data( 0 ), count( 0 ), ID(0), left( NULL ), right(NULL) {}
explicit HuffmanNode( wchar_t d, u32 c ) : ID(0), left( NULL ), right(NULL) { data = d; count = c; }
explicit HuffmanNode( HuffmanNode* l, HuffmanNode* r ) : data( 0xFFFF ), ID(0) {
count = l->count + r->count;
left = l;
right = r;
}
virtual ~HuffmanNode() {
if ( left ) {
delete left;
left = NULL;
}
if ( right ) {
delete right;
right = NULL;
}
}
wchar_t data;
u32 count;
u32 ID;
HuffmanNode* left;
HuffmanNode* right;
bool operator < ( const HuffmanNode &a ) const {
return count < a.count;
}
bool operator <= ( const HuffmanNode &a ) const {
return count <= a.count;
}
};
template<typename T>
bool lesser_ptr (T * lhs, T * rhs) {
return ((*lhs) < (*rhs));
}
bool lesser_ptr (HuffmanNode * lhs, HuffmanNode * rhs) {
return ((*lhs) < (*rhs));
}
class CompHuffmanNode {
public:
bool operator () (const HuffmanNode * lhs, const HuffmanNode * rhs) const{
return ((*lhs) < (*rhs));
}
};
void traverseHuffmanTree( HuffmanNode* node, std::vector<u8>& code, stdext::hash_map<wchar_t, std::vector<u8> >& huffmanCodes ) {
if ( node->left == node->right ) {
huffmanCodes.insert( std::pair< wchar_t, std::vector<u8> >( node->data, code ) );
} else {
code.push_back( 0 );
traverseHuffmanTree( node->left, code, huffmanCodes );
code.pop_back();
code.push_back( 1 );
traverseHuffmanTree( node->right, code, huffmanCodes );
code.pop_back();
}
}
enum Mode {
Mode_Compress,
Mode_Decompress,
Mode_None,
};
// xml
const wchar_t* xml_linefeed = L"\r\n";
const wchar_t* xml_head = L"<?xml version=\"1.0\" encoding=\"UTF-16\"?>";
const wchar_t* xml_listbeg = L"<tlkList>"; // バージョンいる?変わるとしても位置情報くらいだが あるとまともなパーサ(これが面倒)じゃないと面倒
const wchar_t* xml_listend = L"</tlkList>";
const wchar_t* xml_chunkbeg = L"<tlkElement>";
const wchar_t* xml_chunkend = L"</tlkElement>";
const wchar_t* xml_idbeg = L"<tlkID>";
const wchar_t* xml_idend = L"</tlkID>";
const wchar_t* xml_textbeg = L"<tlkString>";
const wchar_t* xml_textend = L"</tlkString>";
const wchar_t* xml_whitespace = L" ";
bool parseXML( std::list<TLKEntry>& entry_list, const wchar_t* buff, u32 size ) {
std::wstring xml(buff); // メモリどか食いだが文字列比較がめんどい
size_t pos = 0;
size_t eol = 0;
// find header
{
std::wstring::size_type index = xml.find( xml_head );
if ( std::wstring::npos == index ) {
// not found xml header
return false;
}
pos = index + wcslen( xml_head );
}
// find list
{
std::wstring::size_type start = xml.find( xml_listbeg, pos );
if ( std::wstring::npos == start ) {
return false;
}
pos = start + wcslen( xml_listbeg );
std::wstring::size_type end = xml.find( xml_listend, pos );
if ( std::wstring::npos == end ) {
return false;
}
eol = end;
}
size_t chunkendlen = wcslen( xml_chunkend );
size_t idbeglen = wcslen( xml_idbeg );
size_t textbeglen = wcslen( xml_textbeg );
while( pos < eol ) {
std::wstring::size_type start = xml.find( xml_chunkbeg, pos );
if ( std::wstring::npos == start ) {
break;
//return false;
}
pos = start + wcslen( xml_listbeg );
std::wstring::size_type end = xml.find( xml_chunkend, pos );
if ( std::wstring::npos == end || end < pos ) {
return false;
}
// find id
TLKEntry temp;
bool findID = false;
{
std::wstring::size_type s = xml.find( xml_idbeg, pos );
if ( std::wstring::npos == s || end < s ) {
//return false;
} else {
size_t beg = s + idbeglen;
std::wstring::size_type e = xml.find( xml_idend, beg );
if ( std::wstring::npos == e || e < beg ) {
return false;
}
temp.id = xml.substr( beg, e - beg );
findID = true;
}
}
// find string
bool findText= false;
{
std::wstring::size_type s = xml.find( xml_textbeg, pos );
if ( std::wstring::npos == s || end < s ) {
//return false;
} else {
size_t beg = s + textbeglen;
std::wstring::size_type e = xml.find( xml_textend, beg );
if ( std::wstring::npos == e || e < beg ) {
return false;
}
temp.str = xml.substr( beg, e - beg );;
findText = true;
}
}
if ( findID ) {
entry_list.push_back( temp );
}
pos = end + chunkendlen;
}
return true;
}
bool parseTroika( std::list<TLKEntry>& entry_list, const wchar_t* buff, u32 size ) {
TLKEntry temp;
bool isID = false;
bool isIDdone = false;
bool isStr = false;
for ( u32 i = 0; i < size; ++i ) {
wchar_t c = buff[ i ];
switch ( c )
{
case L'{':
if ( !isID && !isIDdone ) {
isID = true;
temp.id.clear();
} else if ( !isStr ) {
isStr = true;
temp.str.clear();
} else {
// error or ignore
}
break;
case L'}':
if ( isID ) {
isID = false;
isIDdone = true;
} else if ( isStr ) {
isStr = false;
isIDdone = false;
entry_list.push_back( temp );
} else {
// error or ignore
}
break;
default:
if ( isID ) {
temp.id += c;
} else if ( isStr ) {
temp.str += c;
}
break;
}
}
return true;
}
bool parseText( std::list<TLKEntry>& entry_list, const wchar_t* buff, u32 size ) {
bool isID = false;
bool isStr = false;
int cnt = 0; // return count;
TLKEntry temp;
for ( u32 i = 0; i < size; ++i ) {
wchar_t c = buff[ i ];
switch ( c ) {
case L'{':
isID = true;
temp.id.clear();
break;
case L'}':
isID = false;
isStr = true;
cnt = 0;
temp.str.clear();
break;
case L'\r':
break;
case L'\n':
case L'\0':
if ( isStr ) {
if ( cnt > 0 ) {
// 末尾に\r\nが余分入る
entry_list.push_back( temp ); //コピーされんのか
temp.id.clear();
temp.str.clear();
isStr = false;
}
++cnt;
}
break;
default:
if ( isID ) {
temp.id += c;
}
if ( isStr ) {
// \r\n置換
if ( c == L'\\' && (i + 1) < size ) {
wchar_t next = buff[ i + 1 ];
if ( next == L'n' ) {
temp.str += L'\n';
++i;
} else if ( next == L'r' ) {
temp.str += L'\r';
++i;
} else {
temp.str += c;
}
} else {
temp.str += c;
}
// ここでアルファベットをカウントしたいが、\r\nが増えるなあ
}
break;
}
}
return true;
}
int convertTLKintoTXT( const char* input_path, const char* output_path ) {
cout << "---- Converting TLK into TXT. --------------------------------" << endl;
cout << "\treading input file." << endl;
char* bin = NULL;
size_t size = 0;
bool isRead = file::read( input_path, bin, &size );
if ( !isRead ) {
// open error
return -3;
}
// header chack
GFF_Header* header = reinterpret_cast<GFF_Header*>(bin);
{
bool checkHeader = true;
if ( header->GFFMagicNumber != ' FFG' ) {
checkHeader = false;
}
if ( header->GFFVersion != '0.4V' ) {
checkHeader = false;
}
if ( header->TargetPlatform == ' CP' ) {
} else if ( header->TargetPlatform == '063X' ){
#if 0 // disalbe BE
g_x360 = true;
#else
checkHeader = false;
#endif
} else {
checkHeader = false;
}
if ( header->FileType != ' KLT' ) {
checkHeader = false;
}
if ( header->FileVersion != '5.0V' ) {
checkHeader = false;
}
if ( !checkHeader ) {
// ignore file format
if ( bin ) {
delete[] bin;
bin = NULL;
}
return -5;
}
}
u32 structCount = bit::swapU32( header->StructCount );
u32 dataOffset = bit::swapU32( header->DataOffset );
#if 0
cout << endl;
cout << L"StructCount: " << structCount << endl;
cout << L"DataOffset: 0x" << hex << dataOffset << dec << endl;
cout << endl;
#endif
u8* struct_head = reinterpret_cast<u8*>(header + 1);
GFF_Struct* struct_array = reinterpret_cast<GFF_Struct*>(header + 1);
// field
#if 0
GFF_Struct* sptr = struct_array;
for( u32 i = 0; i < structCount; ++i ) {
++sptr;
}
GFF_Field* field_array = reinterpret_cast<GFF_Field*>(sptr);
GFF_Field* fptr = field_array;
GFF_Field* fp = fptr;
for( u32 i = 0; i < structCount; ++i ) {
GFF_Struct* sp = &(struct_array[ i ]);
cout << "Struct: "<< i << endl;
char type[5];
strncpy_s( type, 5, sp->Type, 4 );
cout << "\tType: " << type << endl;
u32 fieldCount = bit::swapU32( sp->FieldCount );
cout << "\tFieldCount: " << fieldCount << endl;
u32 fieldOffset = bit::swapU32( sp->FieldOffset );
cout << "\tFieldOffset: " << fieldOffset << endl;
u32 structSize = bit::swapU32( sp->StructSize );
cout << "\tStructSize: " << structSize << endl;
cout << endl;
GFF_Field* fp = reinterpret_cast<GFF_Field*>( bin + fieldOffset );
for ( u32 j = 0; j < fieldCount; ++j ) {
cout << "\tField: "<< j << endl;
cout << "\t\tLabel: "<< bit::swapU32(fp->Label) << endl;
GFF_Field type;
type.FieldType = bit::swapU32(fp->FieldType);
cout << "\t\tFieldType: "<< type.FieldType << endl;
if ( type.Type.TypeID < 17 ) {
cout << "\t\t\tTypeID: "<< g_fieldDataType[ type.Type.TypeID ] << endl;
} else if ( type.Type.TypeID == 0xFFFF ) {
cout << "\t\t\tTypeID: "<< L"Generic = 0xFFFF" << endl;
} else {
cout << "\t\t\tTypeID: "<< type.Type.TypeID << L" (UNKNOWN)" << endl;
}
cout << "\t\t\tFlags: "<< type.Type.Flags;
if ( type.Type.Flags & LIST ) {
cout << " (List)";
}
if ( type.Type.Flags & STRUCT ) {
cout << " (Struct)";
}
if ( type.Type.Flags & REFERENCE ) {
cout << " (Reference)";
}
cout << endl;
cout << "\t\tIndex: "<< bit::swapU32(fp->Index) << endl;
//fptr;
++fp;
}
cout << endl;
}
cout << endl;
#endif
cout << "\tcreating tree." << endl;
u8* raw = reinterpret_cast<u8*>( bin + dataOffset );
//char* p = raw;
u32* p32 = reinterpret_cast<u32*>(raw);
HTLK* ptlk = reinterpret_cast<HTLK*>( raw );
HTLK htlk;
htlk.tag = bit::swapU32( ptlk->tag );
htlk.dictOffset = bit::swapU32( ptlk->dictOffset );
htlk.bitOffset = bit::swapU32( ptlk->bitOffset );
// create dictionary
u8* p0 = raw + htlk.dictOffset; // s32のlist
u32* pval0len = reinterpret_cast<u32*>( p0 );
u32 val0len = bit::swapU32( *pval0len );
s32* pval0 = reinterpret_cast<s32*>( p0 + 4 );
u8* pDictStart = reinterpret_cast<u8*>(pval0);
std::vector<HNode> nodes;
u32 nodesize = val0len / 2;
nodes.reserve( nodesize );
nodes.resize( nodesize );
for ( u32 i = 0; i < nodesize; ++i ) {
u32 left = bit::swapU32( pval0[ i * 2 ] );
u32 right = bit::swapU32( pval0[ i * 2 + 1 ] );
nodes[ i ].left = left;
nodes[ i ].right = right;
}
cout << "\tcreating bits." << endl;
// create bits array
u8* p1 = raw + htlk.bitOffset; // u32のlist
u32* pval1len = reinterpret_cast<u32*>( p1 );
u32 val1len = bit::swapU32( *pval1len );
u32* pval1 = reinterpret_cast<u32*>( p1 + 4 );
u8* pDataStart = reinterpret_cast<u8*>(pval1);
std::vector<u8> bit_array;
bit_array.reserve( val1len * 4 * 8 ); // 1unsigned -> 4byte -> 32bit
bit_array.resize( val1len * 4 * 8 );
for ( u32 i = 0; i < val1len; ++i ) {
// たぶん4バイト単位で並べ替えないと駄目
u32 data = bit::swapU32( pval1[ i ] );
u8* p = reinterpret_cast<u8*>(&data);
for ( u32 j = 0; j < 4; ++j ) {
u8 d = p[ j ];
// forでマスクしたいが手動でやろう
u8 bits[8];
bits[ 0 ] = (d & 0x01) ? 1 : 0;
bits[ 1 ] = (d & 0x02) ? 1 : 0;
bits[ 2 ] = (d & 0x04) ? 1 : 0;
bits[ 3 ] = (d & 0x08) ? 1 : 0;
bits[ 4 ] = (d & 0x10) ? 1 : 0;
bits[ 5 ] = (d & 0x20) ? 1 : 0;
bits[ 6 ] = (d & 0x40) ? 1 : 0;
bits[ 7 ] = (d & 0x80) ? 1 : 0;
u32 index = i * 4 + j;
index *= 8; // to bit
for ( u32 k = 0; k < 8; ++k ) {
bit_array[ index + k ] = bits[ k ];
}
}
}
cout << "\tcreating ID & offset pairs." << endl;
// create id:string bits offset
u32* pstrlen = reinterpret_cast<u32*>(ptlk + 1);
u32 strlen = bit::swapU32( *pstrlen );
HSTR* pstr = reinterpret_cast<HSTR*>(pstrlen + 1);
std::vector<HSTRChunk> hstr_array;
hstr_array.reserve( strlen );
u32 discardCount = 0;
for ( u32 i = 0; i < strlen; ++i ){
HSTR& hs = pstr[i];
HSTRChunk chunk;
// 0.5で無くなった?hs.ptr == 0x0だけになった
// TODO:空のテキストを弾く
// 0x0が空とは限らない
// 0.5で0xFFFFFFFFのNULLが無くなった?
#if 01 // 弾かないと例外がめんどい
//if ( hs.id == 0xFFFFFFFF || hs.ptr == 0xFFFFFFFF || hs.ptr == 0x0 ) {
if ( hs.id == 0xFFFFFFFF || hs.ptr == 0xFFFFFFFF ) {
++discardCount;
continue;
}
#endif
chunk.id = bit::swapU32(hs.id);
chunk.offset = bit::swapU32( hs.ptr );
hstr_array.push_back( chunk );
//cout << L"0x" << hex << bit::swapU32(hs.id) << dec << L" (" << bit::swapU32(hs.id) << L") : " << L"0x" << hex << bit::swapU32(hs.ptr) << dec << L" (" << bit::swapU32(hs.ptr) << L")" << endl;
}
cout << "\t\tcount: " << hstr_array.size() << endl;
cout << "\t\tignore null ID or null offset: " << discardCount << endl;
#if 0 // IDソート:v0.5でソート済みになった気がするんだよな
std::sort( hstr_array.begin(), hstr_array.end() );
#endif
// TODO:xml化やフォーマット変更も考慮して直にバッファに入れるのではなくデータ構築のみにする
//cout << "\tdecompressing text." << endl;
cout << "\tdecompressing strings." << endl;
#if 0
std::vector<wchar_t> line_buffer; //omoi
line_buffer.reserve( 4 * 1024 * 1024 ); // 8MB
line_buffer.push_back( 0xFEFF ); // BOM
#endif
std::vector<TLKEntry> entry_array;
entry_array.reserve( hstr_array.size() );
entry_array.resize( hstr_array.size() );
for ( u32 i = 0; i < hstr_array.size(); ++i ) {
HSTRChunk& chunk = hstr_array[ i ];
s32 key = chunk.offset;
TLKEntry& entry = entry_array[ i ];
HNode root = nodes[ nodes.size() - 1 ]; // 逆ならびのハフマンツリー
HNode curNode = root;
// 数字をUTF16に変換
#if 0
line_buffer.push_back( L'{' );
#endif
const int nummax = 128;
wchar_t number[ nummax ];
int stored = swprintf_s( number, nummax - 1, L"%d", chunk.id );
if ( stored < 0 ) {
// error length over
return -6;
}
entry.id = number;
#if 0
for( s32 i = 0; i < stored; ++i ) {
line_buffer.push_back( number[i] );
}
line_buffer.push_back( L'}' );
line_buffer.push_back( L'\r' );
line_buffer.push_back( L'\n' );
#endif
for ( s32 i = key; i < static_cast<s32>(bit_array.size()); ++i ) {
u8 bit = bit_array[ i ];
s32 next = 0;
if ( bit ) {
next = curNode.right;
} else {
next = curNode.left;
}
if( next & 0x80000000 ) {
u32 c = 0xFFFFFFFF - next;
wchar_t wc = c & 0xFFFF;
//u16 wc = c & 0xFFFF;
if ( wc != 0 ) {
// v0.5で改行コードが\r\nになってるなあ
// 制約もあるし別フォーマットでやりたいところだが、xmlは<>が使われているのでエスケープ処理が面倒だ
// 会話も{}で囲ってしまうのがいいかもしらん
// :改行コードの場合はdaotlkeditor用に"\n"に変換する
// ここじゃなくてバッファリング時にやりたいが、文字列操作に弱いのだ
if ( g_usingTroika || g_usingXML ) {
entry.str += wc;
} else {
// 旧textフォーマット時は改行を置換する
if ( wc == 0x000A ) {
entry.str += L"\\n";
} else if ( wc == 0x000D ) {
entry.str += L"\\r";
} else {
entry.str += wc;
}
}
curNode = root;
} else {
key = i + 1;
break;
}
} else {
//assert( (s32)nodes.size() > next );
curNode = nodes[ next ];
}
}
}
if ( bin ) {
delete[] bin;
bin = NULL;
}
// ここでバッファ化
cout << "\tformating text." << endl;
// wstringに入れてみる?
std::wstring output; // bom
output = static_cast<wchar_t>(0xFEFF); // BOM
u32 ignoreCount = 0;
if ( g_usingXML ) {
// xml format
output += xml_head;
output += xml_linefeed;
output += xml_listbeg;
output += xml_linefeed;
for ( u32 i = 0; i < entry_array.size(); ++i ) {
TLKEntry& entry = entry_array[ i ];
if ( g_ignoreEmptyLine && entry.str.size() == 0 ) {
++ignoreCount;
continue;
}
// number
output += xml_whitespace;
output += xml_chunkbeg;
output += xml_linefeed;
output += xml_whitespace;
output += xml_whitespace;
output += xml_idbeg;
output += entry.id;
output += xml_idend;
output += xml_linefeed;
output += xml_whitespace;
output += xml_whitespace;
output += xml_textbeg;
output += entry.str;
output += xml_textend;
output += xml_linefeed;
output += xml_whitespace;
output += xml_chunkend;
output += xml_linefeed;
}
output += xml_listend;
output += xml_linefeed;
} else if ( g_usingTroika ) {
// troika format
for ( u32 i = 0; i < entry_array.size(); ++i ) {
TLKEntry& entry = entry_array[ i ];
if ( g_ignoreEmptyLine && entry.str.size() == 0 ) {
++ignoreCount;
continue;
}
// number
output += L'{';
output += entry.id;
output += L'}';
output += L"\r\n";
output += L'{';
output += entry.str;
output += L'}';
output += L"\r\n";
output += L"\r\n";
}
} else {
// dao format
for ( u32 i = 0; i < entry_array.size(); ++i ) {
TLKEntry& entry = entry_array[ i ];
if ( g_ignoreEmptyLine && entry.str.size() == 0 ) {
++ignoreCount;
continue;
}
// number
output += L'{';
output += entry.id;
output += L'}';
output += L"\r\n";
output += entry.str;
output += L"\r\n";
output += L"\r\n";
}
}
if ( g_ignoreEmptyLine ) {
cout << "\t\tignore IDs: " << ignoreCount << endl;
}
// write
cout << "\twriting output file." << endl;
//bool isWrite = file::write( output_path, reinterpret_cast<const char*>(&line_buffer.front()), line_buffer.size() * sizeof(wchar_t) );
bool isWrite = file::write( output_path, reinterpret_cast<const char*>(output.c_str()), output.length() * sizeof(wchar_t) );
if ( !isWrite ) {
// error
return -4;
}
return 0;
}
int convertTXTintoTLK( const char* input_path, const char* output_path ) {
cout << "---- Converting TXT into TLK. --------------------------------" << endl;
cout << "\treading input file." << endl;
wchar_t* bin = NULL;