-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
1945 lines (1719 loc) · 47.3 KB
/
main.go
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
package main
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"math/big"
"net/http"
"net/url"
"os"
"path"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
)
func latLonToTileTMS(zoom int, lat, lon float64) (x, y int) {
n := float64(pow2(zoom))
x = int(n * ((lon + 180) / 360))
latRad := lat / 180 * math.Pi
y = int((math.Log(math.Tan(latRad*0.5+math.Pi/4))*1/(2*math.Pi) + 0.5) * n)
return
}
func pow2(y int) int {
return 1 << y
}
type AltitudeManifest struct {
XMLName xml.Name `xml:"manifest"`
Triggers []Trigger `xml:"triggers>trigger"`
}
type Trigger struct {
XMLName xml.Name `xml:"trigger"`
Name string `xml:"name,attr"`
LatRad float64 `xml:"latitude,attr"`
LonRad float64 `xml:"longitude,attr"`
Radius float64 `xml:"radius,attr"`
Region int `xml:"region,attr"`
Version int `xml:"version,attr"`
Lat float64 // converted from LatRad
Lon float64 // converted from LonRad
}
type C3MM struct {
Header C3MM_Header
FileIndex *FileIndex
RootIndex *RootIndex
DataSection DataSection
}
type C3MM_Header struct {
Unkn6 int
FileType uint8
Mult1 float32
Mult2 float32
CompressedSize int
UncompressedSize int
}
type FileIndex struct {
Entries []int
}
type RootIndex struct {
SmallestZ int
Entries []Root
}
type Root struct {
Tile C3MM_Tile
Offset int
StructureType int
}
type DataSection struct {
Raw []byte
}
type Octant struct {
Bits int16
AltitudeHigh float32
AltitudeLow float32
Next int
}
type Context struct {
AuthContext
ResourceManifest
}
type AuthContext struct {
Session
ResourceManifest
TokenP1
}
type TokenP1 string
type Session struct {
ID string
}
type ResourceManifest struct {
StyleConfig []*ResourceManifest_StyleConfig `protobuf:"bytes,2,rep,name=style_config,json=styleConfig,proto3" json:"style_config,omitempty"`
TokenP2 string `protobuf:"bytes,30,opt,name=token_p2,json=tokenP2,proto3" json:"token_p2,omitempty"`
CacheBaseUrl string `protobuf:"bytes,31,opt,name=cache_base_url,json=cacheBaseUrl,proto3" json:"cache_base_url,omitempty"`
CacheFile []*ResourceManifest_CacheFile `protobuf:"bytes,72,rep,name=cache_file,json=cacheFile,proto3" json:"cache_file,omitempty"`
CacheFile_2 []string `protobuf:"bytes,9,rep,name=cache_file_2,json=cacheFile2,proto3" json:"cache_file_2,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
type ResourceManifest_StyleConfig struct {
UrlPrefix_1 string `protobuf:"bytes,1,opt,name=url_prefix_1,json=urlPrefix1,proto3" json:"url_prefix_1,omitempty"`
UrlPrefix_2 string `protobuf:"bytes,9,opt,name=url_prefix_2,json=urlPrefix2,proto3" json:"url_prefix_2,omitempty"`
StyleId int32 `protobuf:"varint,3,opt,name=style_id,json=styleId,proto3,enum=mps.ResourceManifest_StyleConfig_StyleID" json:"style_id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
type ResourceManifest_CacheFile struct {
FileName string `protobuf:"bytes,2,opt,name=file_name,json=fileName,proto3" json:"file_name,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
type writer struct {
file *os.File
writer *bufio.Writer
}
type Export interface {
Next(c3m C3M, subPfx string) error
Close() error
}
type C3M struct {
Header Header
Materials []Material
Meshes []Mesh
}
type Header struct {
Translation [3]float64
Rotation [9]float64
}
type Material struct {
JPEG []byte
}
type Mesh struct {
Vertices []Vertex
Groups map[int]Group
}
type Vertex struct {
X, Y, Z, U, V float32
}
type Group struct {
Material int
Faces []Face
}
type Face struct {
A, B, C int32
}
type OBJExport struct {
dir string
fnPfx string
vtxCount int
objWriter writer
mtlWriter writer
}
func findPlace(lat, long float64) Trigger {
minDist, minPlace := math.Inf(1), Trigger{}
for _, v := range am.Triggers {
dist := math.Sqrt(math.Pow(lat-v.Lat, 2) + math.Pow(long-v.Lon, 2))
if dist <= v.Radius && dist < minDist {
minDist, minPlace = dist, v
}
}
return minPlace
}
func (e *OBJExport) Close() (err error) {
if err = e.objWriter.done(); err != nil {
return
}
if err = e.mtlWriter.done(); err != nil {
return
}
return
}
func (e *OBJExport) Next(c3m C3M, subPfx string) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(fmt.Sprintln(e, string(debug.Stack())))
}
}()
dir, fnPfx := e.dir, e.fnPfx
for i, material := range c3m.Materials {
ioutil.WriteFile(path.Join(dir, fmt.Sprintf("%s%s_%d.jpg", fnPfx, subPfx, i)), material.JPEG, 0655)
nxt := fmt.Sprintf(`
newmtl mtl_%s_%d
Kd 1.000 1.000 1.000
d 1.0
illum 0
map_Kd %s%s_%d.jpg
`, subPfx, i, fnPfx, subPfx, i)
e.mtlWriter.write(nxt)
}
for i, mesh := range c3m.Meshes {
e.objWriter.write(fmt.Sprintf("mtllib %smodel.mtl\n", fnPfx))
e.objWriter.write(fmt.Sprintf("o test_%s_%d\n", subPfx, i))
for _, vtx := range mesh.Vertices {
x, y, z := float64(vtx.X), float64(vtx.Y), float64(vtx.Z)
if transform {
x, y, z =
c3m.Header.Rotation[0]*x+c3m.Header.Rotation[1]*y+c3m.Header.Rotation[2]*z,
c3m.Header.Rotation[3]*x+c3m.Header.Rotation[4]*y+c3m.Header.Rotation[5]*z,
c3m.Header.Rotation[6]*x+c3m.Header.Rotation[7]*y+c3m.Header.Rotation[8]*z
x += c3m.Header.Translation[0]
y += c3m.Header.Translation[1]
z += c3m.Header.Translation[2]
}
e.objWriter.write(fmt.Sprintln("v", x, y, z))
e.objWriter.write(fmt.Sprintln("vt", vtx.U, vtx.V))
}
for i, group := range mesh.Groups {
e.objWriter.write(fmt.Sprintf("g g_%s_%d\n", subPfx, i))
e.objWriter.write(fmt.Sprintf("usemtl mtl_%s_%d\n", subPfx, i))
for _, face := range group.Faces {
a, b, c := int(face.A)+1+e.vtxCount, int(face.B)+1+e.vtxCount, int(face.C)+1+e.vtxCount
e.objWriter.write(fmt.Sprintf("f %d/%d %d/%d %d/%d\n", a, a, b, b, c, c))
}
}
e.vtxCount += len(mesh.Vertices)
}
return
}
func newWriter(fn string) (writer, error) {
f, err := create(fn)
if err != nil {
return writer{}, err
}
w := bufio.NewWriter(f)
return writer{file: f, writer: w}, nil
}
func create(fn string) (*os.File, error) {
perm := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
return os.OpenFile(fn, perm, 0655)
}
func (w *writer) write(txt string) {
w.writer.WriteString(txt)
}
func (w writer) done() (err error) {
if err = w.writer.Flush(); err != nil {
return
}
if err = w.file.Close(); err != nil {
return
}
return
}
func newExporter(dir, fnPfx string) Export {
objWriter, _ := newWriter(path.Join(dir, fmt.Sprintf("%smodel.obj", fnPfx)))
mtlWriter, _ := newWriter(path.Join(dir, fmt.Sprintf("%smodel.mtl", fnPfx)))
return &OBJExport{
dir: dir,
fnPfx: fnPfx,
vtxCount: 0,
objWriter: objWriter,
mtlWriter: mtlWriter,
}
}
func tileCountPerAxis(zoom int) int {
return pow2(zoom)
}
func quaternionToMatrix(qx, qy, qz, qw float64) (m [9]float64) {
m[0] = 1 - 2*qy*qy - 2*qz*qz
m[1] = 2*qx*qy - 2*qw*qz
m[2] = 2*qx*qz + 2*qw*qy
m[3] = 2*qx*qy + 2*qw*qz
m[4] = 1 - 2*qx*qx - 2*qz*qz
m[5] = 2*qy*qz - 2*qw*qx
m[6] = 2*qx*qz - 2*qw*qy
m[7] = 2*qy*qz + 2*qw*qx
m[8] = 1 - 2*qx*qx - 2*qy*qy
return
}
func parseHeader(data []byte, offset *int) Header {
qx := ReadFloat64(data, *offset+9)
qy := ReadFloat64(data, *offset+17)
qz := ReadFloat64(data, *offset+25)
qw := ReadFloat64(data, *offset+33)
x := ReadFloat64(data, *offset+41)
y := ReadFloat64(data, *offset+49)
z := ReadFloat64(data, *offset+57)
m := quaternionToMatrix(qx, qy, qz, qw)
*offset += 113
return Header{[3]float64{x, y, z}, m}
}
func parseMaterial(data []byte, offset *int) []Material {
*offset += 5
numberOfItems := int(ReadInt32(data, *offset+0))
*offset += 4
materials := make([]Material, numberOfItems)
for processedItems := 0; processedItems < numberOfItems; processedItems++ {
materialType := data[*offset]
switch materialType {
case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:
textureFormat := data[*offset+3]
textureOffset := ReadInt32(data, *offset+4)
textureLength := ReadInt32(data, *offset+8)
textureLength2 := ReadInt32(data, *offset+12)
_ = textureLength
switch textureFormat {
case 0:
materials[processedItems].JPEG = data[textureOffset : textureOffset+textureLength2]
*offset += 16
default:
panic(fmt.Sprintf("Unsupported textureFormat %d", textureFormat))
}
default:
panic(fmt.Sprintf("materialType %d not implemented", materialType))
}
}
return materials
}
func (hp HuffmanParams) createTable() HuffmanTable {
type tree struct {
index int16
unknown4 int32
unknown8 int32
child1 *tree
child2 *tree
unknown40 int8
}
type tree2 struct {
xUnknown8 int32
xUnknown40 int8
}
bufs := make([]*tree, hp.p3)
if hp.p3 == 0 {
panic("not implemented")
}
for i, hp1 := 0, hp.p1; i < hp.p3; i, hp1 = i+1, hp1+hp.p0 {
var buf tree
buf.index = int16(i)
buf.unknown4 = int32(0xFFFFFFFF / (hp.p2 + hp1*i))
bufs[i] = &buf
}
for hp3 := hp.p3; hp3 > 1; hp3-- {
b1 := bufs[hp3-1]
b2 := bufs[hp3-2]
var buf1 tree
buf1.index = int16(-1)
buf1.unknown4 = int32(b1.unknown4 + b2.unknown4)
buf1.child1 = b1
buf1.child2 = b2
for _hp3 := hp3; ; {
b := bufs[_hp3-2]
if buf1.unknown4 <= b.unknown4 {
bufs[_hp3-1] = &buf1
break
}
bufs[_hp3-1] = b
_hp3--
if _hp3-2 == -1 {
bufs[0] = &buf1
break
}
}
}
buf3 := make([]*tree, 20)
buf2 := make([]*tree2, hp.p3)
buf3[0] = bufs[0]
for counter := 1; counter != 0; {
tree := buf3[counter-1]
if tree.index < 0 {
buf3[counter-1] = tree.child1
buf3[counter-0] = tree.child2
counter++
} else {
var t2 tree2
t2.xUnknown8 = tree.unknown8
t2.xUnknown40 = tree.unknown40
buf2[tree.index] = &t2
counter--
}
if tree.child1 != nil {
tree.child1.unknown8 = 2 * tree.unknown8
tree.child1.unknown40 = tree.unknown40 + 1
}
if tree.child2 != nil {
tree.child2.unknown8 = 2*tree.unknown8 + 1
tree.child2.unknown40 = tree.unknown40 + 1
}
}
buf4 := make([]int8, 0x10001)
buf5 := make([]int16, 0x20000/2)
var counter int16 = 1
for buf2idx, hp3 := 0, hp.p3; hp3 != 0; buf2idx, hp3 = buf2idx+1, hp3-1 {
b := buf2[buf2idx]
xu40 := b.xUnknown40
if xu40 >= 17 {
xu40m16 := xu40 - 16
idx5 := b.xUnknown8 >> uint(xu40m16)
idx4 := buf5[idx5]
if idx4 == 0 {
buf5[idx5] = counter
idx4 = counter
counter++
}
if xu40m16 > buf4[idx4] {
buf4[idx4] = xu40m16
}
}
}
buf4[0] = 16
buf6 := make([][]byte, counter)
for i, j := 16, 0; j < int(counter); i, j = int(buf4[j+1]), j+1 {
count := 1 << uint(i)
buf6SubBuf := make([]byte, 8*count)
buf6[j] = buf6SubBuf
for ctr := 0; ctr < count; ctr++ {
WriteUInt16(buf6SubBuf, 8*ctr, 0xFFFF)
}
}
for buf2idx := 0; buf2idx < hp.p3; buf2idx++ {
b2xu4 := buf2[buf2idx].xUnknown40
if b2xu4 > 16 {
b2xu8mod := buf2[buf2idx].xUnknown8 >> uint(b2xu4-16)
b5val := buf5[b2xu8mod]
WriteInt32(buf6[0], 8*int(b2xu8mod), int32(b5val))
WriteInt32(buf6[0], 8*int(b2xu8mod)+4, int32(-buf4[b5val]))
b4val := buf4[b5val]
b2xu4m16 := b2xu4 - 16
lob := 0xff & buf2[buf2idx].xUnknown8
subbufPtr := int8(int8(lob&((1<<uint(b2xu4m16))-1)) << uint(b4val-b2xu4m16))
WriteInt32(buf6[b5val], 8*int(subbufPtr), int32(buf2idx))
WriteInt32(buf6[b5val], 8*int(subbufPtr)+4, int32(buf2[buf2idx].xUnknown40))
} else {
subbufPtr := buf2[buf2idx].xUnknown8 << uint(16-b2xu4)
WriteInt32(buf6[0], 8*int(subbufPtr), int32(buf2idx))
WriteInt32(buf6[0], 8*int(subbufPtr)+4, int32(b2xu4))
}
}
for i, j := 16, 0; j < int(counter); i, j = int(buf4[j+1]), j+1 {
if i != 0 {
buf6SubBuf := buf6[j]
buf6SubBufVal := ReadUInt64(buf6SubBuf, 0)
count := 1 << uint(i)
for ctr := 1; ctr < count; ctr++ {
if ReadUInt16(buf6SubBuf[ctr*8:], 0) == 0xFFFF {
WriteUInt64(buf6SubBuf, ctr*8, buf6SubBufVal)
} else {
buf6SubBufVal = ReadUInt64(buf6SubBuf[ctr*8:], 0)
}
}
}
}
return HuffmanTable{buf6}
}
func readHuffmanParams(data []byte, offset int) HuffmanParams {
return HuffmanParams{
int(ReadInt32(data, offset+0)),
int(ReadInt32(data, offset+4)),
int(ReadInt32(data, offset+8)),
int(ReadInt16(data, offset+12))}
}
type HuffmanTable struct {
data [][]byte
}
func (hp HuffmanTable) Length() int {
return len(hp.data)
}
type HuffmanParams struct {
p0 int
p1 int
p2 int
p3 int
}
func parseMesh(data []byte, offset *int) []Mesh {
*offset += 5
numberOfItems := int(ReadInt32(data, *offset+0))
*offset += 4
meshes := make([]Mesh, numberOfItems)
for currentItem := 0; currentItem < numberOfItems; currentItem++ {
meshType := ReadInt8(data, *offset+0)
unknown_1_2 := int(ReadInt8(data, *offset+1)) + int(ReadInt8(data, *offset+2))<<8
switch meshType {
case 2:
offset3 := *offset + 3
unknownA8 := ReadInt8(data, offset3+0)
hpa := readHuffmanParams(data, offset3+1)
ebta := hpa.createTable()
hpb := readHuffmanParams(data, offset3+15)
ebtb := hpb.createTable()
gUvCount := ReadInt32(data, offset3+29+0)
gFacesCount := ReadInt32(data, offset3+29+4)
groupCount := ReadInt32(data, offset3+29+8)
dataOffset := int(ReadInt32(data, offset3+29+12))
if groupCount == 0 && unknownA8 == 6 {
panic("??? 1")
}
if unknownA8 == 8 {
panic("??? 2")
}
rmd := Decompress(data, dataOffset, ebta, ebtb)
if rmd.UVCount != gUvCount || rmd.FacesCount != gFacesCount {
panic("decompressed mesh counts != header counts")
}
tmpBufFst := make([]int32, rmd.UVCount)
for ctr := 0; ctr < 3*int(rmd.FacesCount); ctr++ {
tmpBufFst[rmd.Res5[ctr]] = rmd.Faces[ctr]
}
tmpBufSnd := make([]int32, rmd.UVCount)
preIdx := 0
off := 0
uvCount1 := int(rmd.UVCount)
uvCount2 := int(rmd.UVCount)
vertices := make([]Vertex, uvCount2)
for {
tmpBufFstItm := tmpBufFst[off]
uvCountMin1 := uvCount1 - 1
if rmd.Res8[tmpBufFstItm] != 0 {
uvCount1 = uvCountMin1
} else {
uvCountMin1 = preIdx
preIdx++
}
tmpBufSnd[off] = int32(uvCountMin1)
idx := uvCountMin1
vertices[idx].X = rmd.Vertices[3*tmpBufFstItm+0]
vertices[idx].Y = rmd.Vertices[3*tmpBufFstItm+1]
vertices[idx].Z = rmd.Vertices[3*tmpBufFstItm+2]
vertices[idx].U = rmd.UV[off*2+0]
vertices[idx].V = rmd.UV[off*2+1]
off++
uvCount2--
if uvCount2 == 0 {
break
}
}
for ctr := 0; ctr < 3*int(rmd.FacesCount); ctr++ {
rmd.Res5[ctr] = tmpBufSnd[rmd.Res5[ctr]]
}
gm := make(map[int]int)
for i := 0; i < len(rmd.Res6); i++ {
gm[int(rmd.Res6[i])]++
}
groups := make(map[int]Group)
for i := 0; i < len(rmd.Res6); i++ {
e := int(rmd.Res6[i])
if gm[e] > 0 {
group := groups[e]
if group.Faces == nil {
group.Material = e
group.Faces = make([]Face, gm[e])
groups[e] = group
}
face := &group.Faces[len(group.Faces)-gm[e]]
face.A, face.B, face.C = rmd.Res5[i*3], rmd.Res5[i*3+1], rmd.Res5[i*3+2]
_ = face
gm[e]--
}
}
meshes[currentItem].Groups = groups
meshes[currentItem].Vertices = vertices
*offset += unknown_1_2
default:
panic(fmt.Sprintf("Unsupported meshType %d", meshType))
}
}
return meshes
}
type RawMeshData struct {
Vertices []float32
VerticesCount int32
UV []float32
UVCount int32
Faces []int32
Res5 []int32
Res6 []int32
Res7 []int32
Res8 []int32
FacesCount int32
}
func decompressList(outBuf []int32, length int, inBuf []byte, outNum *int, sh int) {
readShift := 0
inBufOff := 0
outBufOff := 0
var result uint64
if length > 0 {
var input uint64
for {
if readShift < sh {
input |= uint64(ReadUInt32BE(inBuf, inBufOff)) << uint(32-readShift)
readShift += 32
inBufOff += 4
}
result = input >> uint(64-sh)
readShift -= sh
input <<= uint(sh)
outBuf[outBufOff] = int32(result)
outBufOff++
length--
if length == 0 {
break
}
}
}
*outNum += 8*inBufOff - readShift
//l.Println("decompressList result", result)
_ = result
}
func read10MeshBufs(data []byte, dataOffset int, ebta HuffmanTable, ebtb HuffmanTable) (bufs [10][]byte) {
off := 120
for i := 0; i < 10; i++ {
len1 := int(ReadUInt32(data, dataOffset+12*i))
len2 := int(ReadUInt32(data, dataOffset+12*i+4))
val := ReadUInt8(data, dataOffset+12*i+8)
desc := "?"
switch i {
case 0:
desc = "header"
case 2:
desc = "eb clers"
case 5:
desc = "eb other"
case 6:
desc = "uv"
case 7:
desc = "vtx"
}
_ = desc
outBuf := make([]byte, len1+3)
switch val {
case 0:
buf := data[dataOffset+off : dataOffset+off+int(len2)]
copy(outBuf, buf)
case 3:
buf := data[dataOffset+off : dataOffset+off+int(len2)]
hp, s := ebta, "a"
if i == 7 {
hp, s = ebtb, "b"
}
_ = s
hp.Decode(buf, len1, len2, &outBuf)
case 1:
panic("Unsupported type: 1")
default:
panic(fmt.Sprintf("Unknown type: %d", val))
}
bufs[i] = outBuf
off += len2
}
return
}
func (table HuffmanTable) Decode(data []byte, len1 int, len2 int, writeBuf *[]byte) {
readBuf := make([]byte, len2+3)
copy(readBuf, data)
if len1 < 2 {
return
}
len2mul8 := 8 * len2
len1div2 := len1 / 2
tblFst := table.data[0]
readShift1 := 0
var input1 uint64
readBufOff := 0
writeBufOff := 0
for {
if readShift1 <= 0 {
input1 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(32-readShift1)
readShift1 += 32
readBufOff += 4
}
negToggle := input1 >> 63
readShift2 := readShift1 - 1
input2 := 2 * input1
shiftTest := len2mul8 - (8*readBufOff - (readShift1 - 1))
var tblFstIdx uint64
if shiftTest > 15 {
if readShift1 <= 16 {
input2 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(33-readShift1)
readBufOff += 4
readShift2 = readShift1 + 31
}
tblFstIdx = input2 >> 48
} else {
if readShift1 <= int(shiftTest) {
input2 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(33-readShift1)
readBufOff += 4
readShift2 = readShift1 + 31
}
tblFstIdx = uint64(input2 >> uint(64-uint8(shiftTest)) << uint(16-shiftTest))
}
tblFstVal := int(ReadInt8(tblFst, 8*int(tblFstIdx)+4))
if tblFstVal <= 0 {
tblFstValNeg := -tblFstVal
tblIdx := ReadInt32(tblFst, 8*int(tblFstIdx))
if readShift2 <= 15 {
input2 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(32-readShift2)
readShift2 += 32
readBufOff += 4
}
readShift3 := readShift2 - 16
input3 := input2 << 16
if readShift2-16 < tblFstValNeg {
input3 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(48-readShift2)
readBufOff += 4
readShift3 = readShift2 + 16
}
tblOthIdx := uint(input3 >> uint(64-tblFstValNeg))
tblOth := table.data[tblIdx]
tblOthValNeg := int(tblOth[8*tblOthIdx+4]) - 16
if readShift3 < tblOthValNeg {
input3 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(32-readShift3)
readShift3 += 32
readBufOff += 4
}
readShift1 = readShift3 - tblOthValNeg
input1 = input3 << uint(tblOthValNeg)
outVal := -ReadInt32(tblOth, 8*int(tblOthIdx))
if int32(negToggle) == 0 {
outVal = ReadInt32(tblOth, 8*int(tblOthIdx))
}
WriteInt16(*writeBuf, writeBufOff, int16(outVal))
} else {
if readShift2 < tblFstVal {
input2 |= uint64(ReadUInt32BE(readBuf, readBufOff)) << uint(32-readShift2)
readShift2 += 32
readBufOff += 4
}
input1 = input2 << uint(tblFstVal)
outVal := -ReadInt32(tblFst, 8*int(tblFstIdx))
if int32(negToggle) == 0 {
outVal = ReadInt32(tblFst, 8*int(tblFstIdx))
}
WriteInt16(*writeBuf, writeBufOff, int16(outVal))
readShift1 = readShift2 - tblFstVal
}
writeBufOff += 2
len1div2--
// end
if len1div2 == 0 {
break
}
}
// todo check unvisited branches
}
// WriteUInt64 writes a uint64 value to data at offset
func WriteUInt64(data []byte, offset int, value uint64) {
binary.LittleEndian.PutUint64(data[offset:], value)
}
// WriteInt64 writes an int64 value to data at offset
func WriteInt64(data []byte, offset int, value int64) {
binary.LittleEndian.PutUint64(data[offset:], uint64(value))
}
// WriteInt32 writes an int32 value to data at offset
func WriteInt32(data []byte, offset int, value int32) {
binary.LittleEndian.PutUint32(data[offset:], uint32(value))
}
// WriteUInt32 writes a uint32 value to data at offset
func WriteUInt32(data []byte, offset int, value uint32) {
binary.LittleEndian.PutUint32(data[offset:], value)
}
// WriteInt16 writes an int16 value to data at offset
func WriteInt16(data []byte, offset int, value int16) {
binary.LittleEndian.PutUint16(data[offset:], uint16(value))
}
// WriteUInt16 writes a uint16 value to data at offset
func WriteUInt16(data []byte, offset int, value uint16) {
binary.LittleEndian.PutUint16(data[offset:], value)
}
/*
* binary readers
*/
// ReadFloat32 reads a float32 value from data at offset
func ReadFloat32(data []byte, offset int) float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(data[offset:]))
}
// ReadFloat64 reads a float64 value from data at offset
func ReadFloat64(data []byte, offset int) float64 {
return math.Float64frombits(binary.LittleEndian.Uint64(data[offset:]))
}
// ReadUInt8 reads a uint8 value from data at offset
func ReadUInt8(data []byte, offset int) uint8 {
return uint8(data[offset])
}
// ReadInt8 reads an int8 value from data at offset
func ReadInt8(data []byte, offset int) int8 {
return int8(data[offset])
}
// ReadInt16 reads an int16 value from data at offset
func ReadInt16(data []byte, offset int) int16 {
return int16(binary.LittleEndian.Uint16(data[offset:]))
}
// ReadUInt16 reads a uint16 value from data at offset
func ReadUInt16(data []byte, offset int) uint16 {
return binary.LittleEndian.Uint16(data[offset:])
}
// ReadInt32 reads an int32 value from data at offset
func ReadInt32(data []byte, offset int) int32 {
return int32(binary.LittleEndian.Uint32(data[offset:]))
}
// ReadUInt32 reads a uint32 value from data at offset
func ReadUInt32(data []byte, offset int) uint32 {
return binary.LittleEndian.Uint32(data[offset:])
}
// ReadInt32BE reads a big endian int32 value from data at offset
func ReadInt32BE(data []byte, offset int) int32 {
return int32(binary.BigEndian.Uint32(data[offset:]))
}
// ReadUInt32BE reads a big endian uint32 value from data at offset
func ReadUInt32BE(data []byte, offset int) uint32 {
return binary.BigEndian.Uint32(data[offset:])
}
// ReadInt64 reads an int64 value from data at offset
func ReadInt64(data []byte, offset int) int64 {
return int64(binary.LittleEndian.Uint64(data[offset:]))
}
// ReadUInt64 reads a uint64 value from data at offset
func ReadUInt64(data []byte, offset int) uint64 {
return binary.LittleEndian.Uint64(data[offset:])
}
// ByteSwapUInt64 returns a uint64 with its bytes swapped
func ByteSwapUInt64(value uint64) uint64 {
return value>>56&0xff | value>>48&0xff<<8 | value>>40&0xff<<16 | value>>32&0xff<<24 |
value>>24&0xff<<32 | value>>16&0xff<<40 | value>>8&0xff<<48 | value&0xff<<56
}
// ByteSwapUInt32 returns a uint32 with its bytes swapped
func ByteSwapUInt32(value uint32) uint32 {
return value>>24&0xff | value>>16&0xff<<8 | value>>8&0xff<<16 | value&0xff<<24
}
func Decompress(data []byte, dataOffset int, ebta HuffmanTable, ebtb HuffmanTable) RawMeshData {
bufs := read10MeshBufs(data, dataOffset, ebta, ebtb)
b0 := bufs[0]
i32_0 := ReadInt32(b0, 0)
f64_0 := ReadFloat64(b0, 4)
f64_1 := ReadFloat64(b0, 12)
f64_2 := ReadFloat64(b0, 20)
f32_0 := ReadFloat32(b0, 28)
f32_1 := ReadFloat32(b0, 32)
f32_2 := ReadFloat32(b0, 36)
i8_0 := ReadUInt8(b0, 40)
res3 := ReadInt32(b0, 41)
i32_1 := ReadInt32(b0, 45)
i32_2 := ReadInt32(b0, 49)
i8_1 := ReadUInt8(b0, 53)
i32_3 := ReadInt32(b0, 54)
i32_4 := ReadUInt32(b0, 58)
if i32_0 < 0 || i8_0 == 0 || (i32_1|i32_2) < 0 || i8_1 == 0 || (i32_4&0x80000000) != 0 {
panic("incorrect values in buf 0")
}
b5 := bufs[5]
res9 := ReadInt32(b5, 0)
if res9 < 0 {
panic("incorrect values in buf 5 #1")
}
i32_0min32 := i32_0 - 32
fst := i32_0min32
snd := 32
buf_res9vmul3mul4_a := make([]int32, res9*3)
for i := 0; i < len(buf_res9vmul3mul4_a); i++ {
buf_res9vmul3mul4_a[i] = -1
}
buf_res9vmul3mul4_b := make([]int32, res9*3)
for i := 0; i < len(buf_res9vmul3mul4_b); i++ {
buf_res9vmul3mul4_b[i] = -1
}
if i32_0min32 >= 128 {