-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTDXeditProcs.bas
1526 lines (1364 loc) · 56.6 KB
/
TTDXeditProcs.bas
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
Attribute VB_Name = "TTDXeditProcs"
Option Explicit
Option Compare Text
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal sz As Long)
Private Declare Function GetUNIXTime Lib "TTDXHelp.dll" () As Long
Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Boolean
Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal NPos As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal NPos As Long) As Long
Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ShellExecuteEx Lib "Shell32.dll" Alias "ShellExecuteExA" (lpSEI As SHELLEXECUTEINFO) As Long
Declare Function ShellExecuteElevated Lib "elevate.dll" Alias "ShellExecuteElevatedA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Declare Function ShellExecuteExElevated Lib "elevate.dll" Alias "ShellExecuteExElevatedA" (lpSEI As SHELLEXECUTEINFO) As Long
Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function IsUserAnAdmin Lib "TTDXHelp.dll" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetTempFileNameAPI Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Global Const MF_BYPOSITION = &H400&
Global Const BCM_FIRST = &H1600&
Global Const BCM_SETSHIELD = (BCM_FIRST + &HC&)
Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hWnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long 'Optional
lpClass As String 'Optional
hkeyClass As Long 'Optional
dwHotKey As Long 'Optional
hIcon As Long 'Optional
hProcess As Long 'Optional
End Type
Global Const SEE_MASK_NOCLOSEPROCESS = &H40&
Global Const INFINITE = -1&
Private Const Poff As Long = &H52A62
Public Type TTDXgeneral
GameName As String
ClimType As Byte
ClimName As String
CityNameSet As Byte
CityNames As String
VehSize As Byte
End Type
Public Type TTDXlandscape
X As Integer
Y As Integer
Owner As Byte
Object As Byte
Height As Byte
L1 As Byte
L2 As Byte
L3 As Long
L4 As Byte
L5 As Byte
End Type
Public Type FinanceInfo
Construction As Long
NewVehicles As Long
TrainRunningCosts As Long
RoadVehRunningCosts As Long
AircraftRunningCosts As Long
ShipRunningCosts As Long
PropertyMaintenance As Long
TrainIncome As Long
RoadVehIncome As Long
AircraftIncome As Long
ShipIncome As Long
LoanInterest As Long
Other As Long
End Type
Public Type TTDXplayer
Number As Integer
Id As Integer
Money As Long
Debt As Long
HQx As Integer
HQy As Integer
Offset As Long
FinancesThisYear As FinanceInfo
FinancesLastYear As FinanceInfo
FinancesTwoYearsAgo As FinanceInfo
End Type
Public Type TTDXIndInfo
Number As Integer
X As Byte
Y As Byte
W As Byte
H As Byte
Type As Byte
HomeTown As Byte
Prod(1) As Byte
ProR(1) As Byte
del(2) As Byte
Name As String
Offset As Long
End Type
Public Type TTDXCitInfo
Number As Integer
X As Byte
Y As Byte
Population As Long
CRate(7) As Long
CRateE(7) As Boolean
Name As String
Offset As Long
End Type
Public Type TTDXStation
Number As Integer
BaseX As Byte
BaseY As Byte
Parts As Byte
BusX As Byte
BusY As Byte
BusStatus As Byte
TruckX As Byte
TruckY As Byte
TruckStatus As Byte
RailX As Byte
RailY As Byte
RailDir As Boolean
RailTracks As Byte
RailTrackLen As Byte
AirX As Byte
AirY As Byte
AirportType As Byte
DockX As Byte
DockY As Byte
Cargo(11) As Integer
CRate(11) As Byte
CAcc(11) As Byte
CEnrout(11) As Byte
CEnroutOrg(11) As Byte
Cgood2(11) As Byte
Cgood5(11) As Byte
Cgood6(11) As Byte
Cgood7(11) As Byte
HomeTown As Byte
Owner As Byte
Name As String
Offset As Long
End Type
Public Type TTDXVehicle
Number As Long
Class As Byte
Subclass As Byte
Owner As Byte
CargoT As Byte
CargoMax As Long
CargoCur As Long
SpeedMax As Long
Value As Long
Age As Long
AgeMax As Long
Rel As Byte
RelDropRate As Long
Type As Byte
Next As Long
Name As String
Offset As Long
End Type
Public CurFile As String, FileChanged As Boolean
Public CargoTypes(25) As String, CargoTypesAccel(25) As String, IndustryTypes(50) As String
Public Cities(70) As String, Stations(250) As String
Private F As New FileSystemObject
'
' The importaint internal values
'
Public wData() As Byte, wHeadData(46) As Byte
Private wTTDPext As Integer, wClimate As Integer
Private wError As Integer, wExtraChunks
Global CurrencyMultiplier As Double
Global CurrencyLabel As String
Global CurrencySeparator As String
Global CurrencySymbolBefore As Boolean
Function GetTempFileName()
On Error GoTo Error
Dim TmpPath As String * 250
Dim TmpFN As String * 260
GetTempPath 250, TmpPath
If GetTempFileNameAPI(TmpPath, "TDX", 0, TmpFN) = 0 Then
MsgBox "Unable to create temporary filename.", vbCritical, "Error"
Exit Function
Else
GetTempFileName = APITrim(TmpFN)
End If
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.GetTempFileName")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Function APITrim(Buf As String) As String
On Error GoTo Error
Dim Tmp As String
Dim NullPos As Integer
Buf = Trim$(Buf)
NullPos = InStr(1, Buf, Chr$(0), 0)
If NullPos <> 0 Then
Tmp = Left$(Buf, NullPos - 1)
Else
Tmp = Buf
End If
If Right$(Tmp, 1) = Chr$(0) Then
Tmp = Left$(Tmp, Len(Tmp) - 1)
End If
APITrim = Tmp
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.APITrim(""" & Buf & """)")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Function IsElevated() As Boolean
If RunningWin9x() = True Then
IsElevated = True
Else
If IsUserAnAdmin() = 1 Then
IsElevated = True
Else
IsElevated = False
End If
End If
End Function
Sub MarkGame(ByVal Value As Integer)
If GetLong(&H44CB4) = &H70445454 Then
wData(&H44BBD) = wData(&H44BBD) Or Value
Else
wData(&H24CCE) = wData(&H24CCE) Or Value
End If
End Sub
Public Sub RegisterSGMPlugin(ByVal MajorVer As Double, ByVal DllPath As String, ByVal SGMPath As String)
Dim Wa As Long, Wb As Double
Wb = Shell("Regsvr32 /s " + Chr(34) + DllPath + Chr(34))
If MajorVer < 2.3 Then
Wa = fWriteValue(F.BuildPath(SGMPath, "plugins2.ini"), "TTDXEdit", "Class", "S", "SGMTTDXEdit")
Wa = fWriteValue(F.BuildPath(SGMPath, "plugins2.ini"), "TTDXEdit", "Enabled", "S", 1)
Wa = fWriteValue(F.BuildPath(SGMPath, "plugins2.ini"), "TTDXEdit", "Filename", "S", DllPath)
Wa = fWriteValue(F.BuildPath(SGMPath, "plugins2.ini"), "Plugins", "TTDXEdit", "S", "TTDXEdit")
Else
Wa = fWriteValue("HKLM", "Software\Owen Rudge\Transport Tycoon Saved Game Manager\Plugins\TTDXEdit", "Class", "S", "SGMTTDXEdit")
Wa = fWriteValue("HKLM", "Software\Owen Rudge\Transport Tycoon Saved Game Manager\Plugins\TTDXEdit", "Filename", "S", DllPath)
Wa = fWriteValue("HKLM", "Software\Owen Rudge\Transport Tycoon Saved Game Manager\Plugins\TTDXEdit", "Type", "S", "COM")
End If
End Sub
Public Sub RegisterSGMPluginStartup()
Dim Wsa As String, wFl As Boolean, SGMVersion As String, Pos As Integer
Dim MajorVer As Double, DllPath As String, SGMPath As String
SGMPath = fReadValue("HKLM", "Software\Owen Rudge\InstalledSoftware\TTSGM", "Path", "S", "")
DllPath = F.BuildPath(App.Path, "SGMPlugIn\TTDXEdit.dll")
If F.FileExists(DllPath) = False Then
Exit Sub
End If
SGMVersion = fReadValue("HKLM", "Software\Owen Rudge\InstalledSoftware\TTSGM", "Version", "S", "")
On Error Resume Next
Pos = InStr(3, SGMVersion, ".")
MajorVer = CDbl(Left(SGMVersion, Pos - 1))
On Error GoTo 0
If MajorVer <= 0 Then
Exit Sub
End If
RegisterSGMPlugin MajorVer, DllPath, SGMPath
End Sub
Public Function StartElevated(ByVal hWnd As Long, ByVal AppName As String, ByVal Params As String, ByVal WorkingDir As String, ByVal Show As Integer, ByVal Message As String) As Boolean
On Error GoTo Error
Dim sei As SHELLEXECUTEINFO
Dim osinfo As OSVERSIONINFO
Dim retvalue As Integer
Dim RetVal As Long
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(osinfo)
sei.cbSize = Len(sei)
sei.fMask = SEE_MASK_NOCLOSEPROCESS
sei.hWnd = hWnd
sei.lpVerb = "open"
sei.lpFile = AppName
sei.lpParameters = Params
sei.lpDirectory = WorkingDir
sei.nShow = Show
If osinfo.dwPlatformId = 2 Then
RetVal = IsUserAnAdmin()
If RetVal = 0 Then
If MsgBox(Message, vbExclamation Or vbYesNo, "TTDX Editor") = vbNo Then
StartElevated = False
Exit Function
End If
If osinfo.dwMajorVersion >= 6 Then
RetVal = ShellExecuteExElevated(sei)
Else
sei.lpVerb = "runas"
RetVal = ShellExecuteEx(sei)
End If
GoTo WaitForTermination
End If
End If
RetVal = ShellExecuteEx(sei)
WaitForTermination:
If RetVal = 1 Then
WaitForSingleObject sei.hProcess, INFINITE
StartElevated = True
End If
StartElevated = False
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.StartElevated(" & hWnd & ", """ & AppName & """, """ & Params & """, """ & WorkingDir & """, " & Show & ")")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Public Function RunningWin9x() As Boolean
On Error GoTo Error
Dim osinfo As OSVERSIONINFO
Dim retvalue As Integer
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(osinfo)
If osinfo.dwPlatformId = 1 Then
RunningWin9x = True
Else
RunningWin9x = False
End If
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.RunningWin9x()")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Function MakePath(Path As String) As String
On Error Resume Next
If Right$(Path, 1) = "\" Then
MakePath = Path
Else
MakePath = Path & "\"
End If
End Function
Function FlipSign(ByVal Value As Long) As Long
FlipSign = -Value
'If Value > 0 Then
'FlipSign = 0 - Value
'Else
'FlipSign = 0 - ((2 * Value))
'End If
End Function
Function FormatMoney(ByVal Money As Long) As String
On Error GoTo Error
Dim MoneyStr As String
Dim NewMoneyStr As String
Dim SetOf3 As Byte
Dim i As Integer
Dim Negative As Byte
MoneyStr = CStr(CCur(Money * CurrencyMultiplier))
If Left$(MoneyStr, 1) = "-" Then
MoneyStr = Right$(MoneyStr, Len(MoneyStr) - 1)
Negative = True
Else
Negative = False
End If
NewMoneyStr = ""
SetOf3 = 0
For i = Len(MoneyStr) To 1 Step -1
SetOf3 = SetOf3 + 1
NewMoneyStr = MID$(MoneyStr, i, 1) & NewMoneyStr
If SetOf3 = 3 And i <> 1 Then
NewMoneyStr = CurrencySeparator & NewMoneyStr
SetOf3 = 0
End If
Next i
If CurrencySymbolBefore = True Then
FormatMoney = CurrencyLabel & NewMoneyStr
' FormatMoney = CurrencyLabel & Format(Money * CurrencyMultiplier, "###" & CurrencySeparator & "###" & CurrencySeparator & "###" & CurrencySeparator & "###" & CurrencySeparator & "###")
Else
FormatMoney = NewMoneyStr & CurrencyLabel
'FormatMoney = Format(Money * CurrencyMultiplier, "###,###,###,###,###") & CurrencyLabel
End If
If Negative = True Then
FormatMoney = "-" & FormatMoney
End If
Exit Function
Error:
Select Case ErrorProc(Err, "Function: TTDXeditProcs.FormatMoney(" & Money & ")")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Function ErrorProc(Code As Long, Optional Extra)
Dim ErrDesc As String
ErrDesc = Err.Description
On Error GoTo Error
Dim mb As Integer
Dim ExtraText As String
If IsMissing(Extra) = False Then
ExtraText = vbCrLf & vbCrLf & Extra
Else
ExtraText = ""
End If
mb = MsgBox("The following error occurred:" & vbCrLf & vbCrLf & ErrDesc & vbCrLf & "Error Number " & CStr(Code) & ExtraText, vbCritical Or vbAbortRetryIgnore, "TTDX Editor")
If mb = vbAbort Then
ErrorProc = 3
ElseIf mb = vbRetry Then
ErrorProc = 1
ElseIf mb = vbIgnore Then
ErrorProc = 2
End If
' ErrorProc = mb
Exit Function
Error:
Select Case ErrorProc(Err, "WARNING! This error occurred during the error procedure. Something is very wrong!")
Case 3:
End
Case 2:
Resume Next
Case 1:
Resume
End Select
End Function
Public Function TTDXLoadFile(ByVal vPath As String) As Integer
Dim Wa As Long, Wb As Long, Wc As Integer, Wd As Integer, wWork() As Byte
Dim Wsa As String, NoMore As Boolean, Tmpstr As String, TmpPos As Long
Dim j As Integer
Dim hbf As New HugeBinaryFile
TTDXLoadFile = 0: Wa = 0: Wb = 0
If Not F.FileExists(vPath) Then TTDXLoadFile = 1: Exit Function
If (vPath Like "*.s??hdr") Or (vPath Like "*.s??dta") Then
'
' Loading an uncompressed file
'
Wsa = Left(vPath, Len(vPath) - 3)
If F.FileExists(Wsa + "hdr") And F.FileExists(Wsa + "dta") Then
Erase wData: CurFile = "": wTTDPext = 0
Wb = F.GetFile(Wsa + "dta").Size
End If
If Wb < 49 Then TTDXLoadFile = 1: Exit Function
If (Wb Mod 108800) <> 74873 Then TTDXLoadFile = 2: Exit Function
'
' Reserve Mem and Load Data
'
ReDim wData(Wb - 1)
hbf.OpenFile Wsa + "hdr", False
hbf.ReadBytes wHeadData()
hbf.CloseFile
hbf.OpenFile Wsa + "dta", False
hbf.ReadBytes wData()
hbf.CloseFile
vPath = Wsa
ElseIf InStr(".sv0.sv1.sv2.ss0.ss1.", "." + F.GetExtensionName(vPath) + ".") > 0 Then
'
' Load a normal savegame
'
Erase wData: CurFile = "": wTTDPext = 0
Wa = F.GetFile(vPath).Size
'
' Load The Data
'
ReDim wWork(Wa - 1)
hbf.OpenFile vPath, False
hbf.ReadBytes wWork()
hbf.CloseFile
'
' Copy Headerinformation
'
For Wa = 0 To UBound(wHeadData)
wHeadData(Wa) = wWork(Wa)
Next Wa
'
' Decompression
'
ReDim wData(618873 + 256)
Wa = 49: Wb = 0
While Wa < UBound(wWork) - 4
'
' Increase dataarea if needed
'
If Wb > UBound(wData) - 256 Then ReDim Preserve wData(UBound(wData) + 108800)
Wc = wWork(Wa)
If Wc > 127 Then
Wc = Abs(Wc - 256): Wa = Wa + 1
For Wd = 0 To Wc
wData(Wb) = wWork(Wa): Wb = Wb + 1
Next Wd
Wa = Wa + 1
Else
Wa = Wa + 1
For Wd = 0 To Wc
wData(Wb) = wWork(Wa): Wb = Wb + 1: Wa = Wa + 1
Next Wd
End If
If Not fFastMode Then DoEvents
Wend
'
' Check result and reduce memory array size to minimum
'
If Wa <> UBound(wWork) - 3 Then TTDXLoadFile = 4: Exit Function
'OCR'If (Wb Mod 108800) <> 74873 Then TTDXLoadFile = 2: Exit Function
ReDim Preserve wData(Wb - 1)
Else
TTDXLoadFile = 5: Exit Function
End If
'
' Check filesize, check for extended vehicles
'
If Wb < 618873 Then
'
' Not enough data
'
TTDXLoadFile = 2
ElseIf Wb = 618873 Then
'
' No extended vehicles
'
CurFile = vPath: wTTDPext = 1
wClimate = wData(&H77131)
Else
If GetLong(&H44CB4) = &H70445454 Then
wExtraChunks = wData(&H44CB8)
Else
wExtraChunks = wData(&H24CBC)
End If
Dim ExtraChunkPos As Long, i As Integer, ExtraChunkLength As Long
Dim ExtraChunkSingleLength As Long
Dim MoreVehicles As Long
Dim Chunk6Broken As Boolean
MoreVehicles = wData(&H24CBA)
If MoreVehicles = 0 Then
MoreVehicles = 1
ElseIf MoreVehicles = 1 Then
MoreVehicles = 2
End If
'ExtraChunkPos = ((wData(&H24CBA) - 1) * &H1A900) + &H97179
ExtraChunkPos = ((MoreVehicles - 1) * &H1A900) + &H97179
'If wData(&H24CBA) = 0 Then
'ExtraChunkPos = &H97179
'End If
For i = 1 To wExtraChunks
If wData(ExtraChunkPos) = 1 And wData(ExtraChunkPos + 1) = 128 Then
If GetLong(ExtraChunkPos + 6) And 1073741824 Then '31 Then ' and 2147483648
Chunk6Broken = False
Else
Chunk6Broken = True
End If
End If
ExtraChunkSingleLength = GetLong(ExtraChunkPos + 2)
ExtraChunkLength = ExtraChunkLength + ExtraChunkSingleLength + 6
ExtraChunkPos = ExtraChunkPos + ExtraChunkSingleLength + 6
If Chunk6Broken = True Then
ExtraChunkPos = ExtraChunkPos + 4
ExtraChunkLength = ExtraChunkLength + 4
Chunk6Broken = False
End If
Next i
' If Wb = (618873 + ExtraChunkLength) + ((wData(&H24CBA) - 1) * 108800) Then
If Wb = (618873 + ExtraChunkLength) + ((MoreVehicles - 1) * 108800) Then
'wTTDPext = wData(&H24CBA)
wTTDPext = MoreVehicles
CurFile = vPath
wClimate = wData(&H77131 + (wTTDPext - 1) * 108800)
Else
If Wb = (618873 + ExtraChunkLength) Then
wTTDPext = 1
CurFile = vPath
wClimate = wData(&H77131)
Else
TTDXLoadFile = 3
End If
End If
End If
#If MarkGame = 1 Then
'If wData(&H24CCB) >= 1 Then
'MsgBox "This game has been modified by TTDX Editor " & wData(&H24CCB) & _
'"." & wData(&H24CCC) & "." & wData(&H24CCD) & "!", vbExclamation, "TTDX Editor"
'End If
If GetLong(&H44CB4) = &H70445454 Then
wData(&H44BBA) = App.Major
wData(&H44BBB) = App.Minor
wData(&H44BBC) = App.Revision
Else
wData(&H24CCB) = App.Major
wData(&H24CCC) = App.Minor
wData(&H24CCD) = App.Revision
End If
#End If
#If MarkGameOldCode = 1 Then
wExtraChunks = wData(&H24CBC)
NoMore = False
ExtraChunkPos = ((wData(&H24CBA) - 1) * &H1A900) + &H97179
If wData(&H24CBA) > 0 Then
For i = 1 To wExtraChunks
If wData(ExtraChunkPos) = 99 Then
Tmpstr = ""
TmpPos = 16
For j = 1 To wData(ExtraChunkPos + 6)
Tmpstr = Tmpstr & vbCrLf
Tmpstr = Tmpstr & _
"v" & wData(ExtraChunkPos + TmpPos) & "." _
& wData(ExtraChunkPos + TmpPos + 2) & "." _
& wData(ExtraChunkPos + TmpPos + 4)
Tmpstr = Tmpstr & vbTab & GetLong(ExtraChunkPos + TmpPos + 6)
TmpPos = TmpPos + 16
Next j
MsgBox "This game has been edited using TTDX Editor as follows:" & Tmpstr, vbInformation, "TTDX Editor"
NoMore = True
End If
ExtraChunkSingleLength = GetLong(ExtraChunkPos + 2)
ExtraChunkLength = ExtraChunkLength + ExtraChunkSingleLength + 6
ExtraChunkPos = ExtraChunkPos + ExtraChunkSingleLength + 6
Next i
If NoMore = False Then
wData(&H24CBC) = wData(&H24CBC) + 1
ReDim Preserve wData(UBound(wData) + 32)
wData(ExtraChunkPos) = 99
PutLong wData, ExtraChunkPos + 2, 26
wData(ExtraChunkPos + 6) = 1
' padding space (8 bytes)
wData(ExtraChunkPos + 16) = App.Major
wData(ExtraChunkPos + 18) = App.Minor
wData(ExtraChunkPos + 20) = App.Revision
PutLong wData, ExtraChunkPos + 22, GetUNIXTime()
' padding space -> 32 (6 bytes)
Else
ReDim Preserve wData(UBound(wData) + 16)
'ExtraChunkPos = (ExtraChunkPos - ExtraChunkSingleLength) - 6
PutLong wData, ExtraChunkPos + 2, GetLong(ExtraChunkPos + 2) + 16
wData(ExtraChunkPos + 6) = wData(ExtraChunkPos + 6) + 1
wData((ExtraChunkPos + 6) - 1 * 16) = App.Major
wData(((ExtraChunkPos + 6) - 1 * 16) + 2) = App.Minor
wData(((ExtraChunkPos + 6) - 1 * 16) + 4) = App.Revision
PutLong wData, ((ExtraChunkPos + 6) - 1 * 16) + 4, GetUNIXTime()
End If
End If
#End If
SetVars
End Function
Public Function TTDXLoadError(vNo As Integer) As String
If vNo = 0 Then TTDXLoadError = "No Error."
If vNo = 1 Then TTDXLoadError = "File Not Found."
If vNo = 2 Then TTDXLoadError = "Unexpected Filesize."
If vNo = 3 Then TTDXLoadError = "Unexpected Extended Vehicle Array Size."
If vNo = 4 Then TTDXLoadError = "Decompression Error, Unexpected EOF."
If vNo = 5 Then TTDXLoadError = "Unknown fileformat."
End Function
Public Function TTDXSaveFile(ByVal vPath As String) As Integer
Dim Wa As Long, Wb As Long, Wc As Long, Wd As Byte, We As Byte, wWork() As Byte
Dim lUncompressedDataSize As Long
Dim bTempByte As Byte
Dim wHeadCheck(1) As Byte
TTDXSaveFile = 0
On Error GoTo TTDXsaveFileErr
On Error GoTo 0
If vPath > " " Then
If F.FileExists(vPath) Then F.DeleteFile vPath
'
' Reserve workarea, calculate header checksum and copy the header
'
ReDim wWork(UBound(wData))
Wc = TTDXCalcHdCheck(wHeadData)
wWork(47) = Wc Mod 256
wWork(48) = Fix(Wc / 256)
For Wa = 0 To 46: wWork(Wa) = wHeadData(Wa): Next Wa
Wc = 49: Wb = 0
'
' Compress into RLE data
'
' Old code
' While Wb <= UBound(wData)
' If wData(Wb) = wData(Wb + 1) Then
' We = 124
' For Wd = 2 To 124
' If Wb + Wd > UBound(wData) Then We = Wd - 1: Exit For
' If wData(Wb) <> wData(Wb + Wd) Then We = Wd - 1: Exit For
' Next Wd
' wWork(Wc) = 256 - We: wWork(Wc + 1) = wData(Wb): Wc = Wc + 2: Wb = Wb + We + 1
' Else
' We = 124
' For Wd = 0 To 124
' If Wb + Wd > UBound(wData) Then We = Wd - 1: Exit For
' If wData(Wb + Wd) = wData(Wb + Wd + 1) Then We = Wd - 1: Exit For
' Next Wd
' wWork(Wc) = We: Wc = Wc + 1
' For Wd = 0 To We: wWork(Wc) = wData(Wb): Wc = Wc + 1: Wb = Wb + 1: Next Wd
' End If
' If Not fFastMode Then DoEvents ' Give some room to other programs running
' Wend
'
' New Code based on old code
' Wc current byte in outputstream
' Wb current byte in inputstream
lUncompressedDataSize = UBound(wData)
While Wb <= lUncompressedDataSize
If (wData(Wb) = wData(Wb + 1)) Then
bTempByte = wData(Wb)
If (Wb + 124 >= lUncompressedDataSize) Then
We = lUncompressedDataSize - Wb
For Wd = 2 To We
If bTempByte <> wData(Wb + Wd) Then We = Wd - 1: Exit For
Next Wd
Else
We = 124
For Wd = 2 To 124
If bTempByte <> wData(Wb + Wd) Then We = Wd - 1: Exit For
Next Wd
End If
' We = 124
' For Wd = 2 To 124
' If Wb + Wd > lUncompressedDataSize Then We = Wd - 1: Exit For
' If wData(Wb) <> wData(Wb + Wd) Then We = Wd - 1: Exit For
' Next Wd
wWork(Wc) = 256 - We ' write down how many bytes are same
wWork(Wc + 1) = wData(Wb) ' the actual byte data
Wc = Wc + 2
Wb = Wb + We + 1
Else
' Different Bytes
If (Wb + 124 >= lUncompressedDataSize) Then
We = lUncompressedDataSize - Wb
For Wd = 0 To We - 1
If wData(Wb + Wd) = wData(Wb + Wd + 1) Then We = Wd - 1: Exit For
Next Wd
Else
We = 124
For Wd = 0 To 124
If wData(Wb + Wd) = wData(Wb + Wd + 1) Then We = Wd - 1: Exit For
Next Wd
End If
' We = 124
' For Wd = 0 To 124
' If Wb + Wd > lUncompressedDataSize Then We = Wd - 1: Exit For
' // new crash fix:
' If Wb + Wd = lUncompressedDataSize Then We = Wd: Exit For
' //
' If wData(Wb + Wd) = wData(Wb + Wd + 1) Then We = Wd - 1: Exit For
' Next Wd
wWork(Wc) = We ' write down how many different bytes we have
Wc = Wc + 1
For Wd = 0 To We ' write the actuall different bytes
wWork(Wc) = wData(Wb)
Wc = Wc + 1:
Wb = Wb + 1:
Next Wd
End If
If Not fFastMode Then DoEvents ' Give some room to other programs running
Wend
'
' Set proper size and calculate checksum
'
ReDim Preserve wWork(Wc - 1)
Wc = 0
For Wa = 0 To UBound(wWork)
Wc = (Wc And &HFFFFFF00) Or ((Wc And &HFF) + wWork(Wa) And &HFF)
If Wc And &H10000000 Then
Wc = (((Wc And &HFFFFFFF) * 8) Or ((Wc And &HE0000000) / 2 ^ 29) And &H7) Or &H80000000
Else
Wc = ((Wc And &HFFFFFFF) * 8) Or ((Wc And &HE0000000) / 2 ^ 29) And &H7
End If
Next Wa
'
' Puttin´ on the disc
'
Dim hbf As New HugeBinaryFile
Dim LongVar(0 To 3) As Byte
Dim LongVal As Long
LongVal = CLng(Wc + 201100)
CopyMemory VarPtr(LongVar(0)), VarPtr(LongVal), 4
hbf.OpenFile vPath, False
hbf.WriteBytes wWork()
hbf.WriteBytes LongVar()
hbf.CloseFile
FileChanged = False
Else
TTDXSaveFile = 1
End If
Exit Function
TTDXsaveFileErr:
' would be good to look what error was here...
TTDXSaveFile = 1
End Function
Public Function TTDXCalcHdCheck(vHd() As Byte) As Long
Dim Wc As Long, Wa As Integer
Wc = 0
For Wa = 0 To 46
Wc = (Wc + vHd(Wa)) * 2
If Wc And &H10000 Then Wc = (Wc Or 1) And &HFFFF&
Next Wa
TTDXCalcHdCheck = (Wc Xor &HAAAA&)
End Function
Public Function TTDXSaveUncom(ByVal vPath As String) As Integer
Dim Wsa As String, Wsb As String, Wc As Long
TTDXSaveUncom = 0
If vPath > " " Then
Wsa = F.GetExtensionName(vPath)
If InStr(".sv0.sv1.sv2.ss0.ss1.", "." + Wsa + ".") Then
Else
vPath = vPath + ".sv1"
End If
If F.FileExists(vPath + "hdr") Then F.DeleteFile (vPath + "hdr")
Wc = TTDXCalcHdCheck(wHeadData)
Dim hbf As New HugeBinaryFile
Dim ByteData(1 To 1) As Byte
hbf.OpenFile vPath & "hdr", True
hbf.WriteBytes wHeadData()
ByteData(1) = CByte(Wc Mod 256)
hbf.WriteBytes ByteData()
ByteData(1) = CByte(Fix(Wc / 256))
hbf.WriteBytes ByteData()
hbf.CloseFile
If F.FileExists(vPath + "dta") Then F.DeleteFile (vPath + "dta")
hbf.OpenFile vPath & "dta", True
hbf.WriteBytes wData()
hbf.CloseFile
FileChanged = False
End If
Exit Function
TTDXsaveFileErr:
TTDXSaveUncom = 1
End Function
'****************************************************************************************************
'**** Landscape Procedures ****
'****************************************************************************************************
Public Function TTDXgetLandscape(Wx As Integer, Wy As Integer) As TTDXlandscape
Dim Wa As Integer, Woff As Long
If Wx < 0 Or Wy < 0 Then TTDXgetLandscape.Object = 7: Exit Function
If Wx > 255 Or Wy > 255 Then TTDXgetLandscape.Object = 7: Exit Function
Woff = Wx + 256& * Wy
If CurFile > " " Then
With TTDXgetLandscape
.X = Wx
.Y = Wy
.Owner = wData(&H4CBA + Woff)
.Object = (wData(&H77179 + (wTTDPext - 1) * 108800 + Woff) And &HF0) / 16