82
82
# 46 SaveAndSubmitBlock
83
83
84
84
package StarsBlock ;
85
- use TotalHost; # eval'd at compile time
85
+ # 220824 Don't think this is ever called from StarsBlock. Fix for required SSL from SMTP library for block applications
86
+ # use TotalHost; # eval'd at compile time
86
87
use StarStat; # eval'd at compile time
87
88
do ' config.pl' ;
88
89
@@ -93,7 +94,7 @@ our @ISA = qw(Exporter);
93
94
our @EXPORT = qw(
94
95
StarsPWD
95
96
nextRandom StarsRandom
96
- initDecryption getFileHeaderBlock getFileFooterBlock getFileFooter
97
+ initDecryption getFileHeaderBlock getFileFooterBlock getFileFooter
97
98
encryptBytes decryptBytes
98
99
read8 read16 read32 readN write16 parseBlock
99
100
dec2bin bin2dec
@@ -129,6 +130,7 @@ our @EXPORT = qw(
129
130
readList writeList printList updateList
130
131
cleanFiles
131
132
adjustFleetCargo tallyFleet
133
+ decryptMessages
132
134
) ;
133
135
134
136
my $debug = 1;
@@ -3956,6 +3958,101 @@ sub decryptFix {
3956
3958
return \@outBytes , $needsFixing , \%warning , \%fleetList , \%queueList , \%designList , \%waypointList , $lastPlayer ;
3957
3959
}
3958
3960
3961
+ sub decryptMessages {
3962
+ my (@fileBytes ) = @_ ;
3963
+ my @block ;
3964
+ my @data ;
3965
+ my ($decryptedData , $encryptedBlock , $padding );
3966
+ my @decryptedData ;
3967
+ my @encryptedBlock ;
3968
+ my @outBytes ;
3969
+ my ($binSeed , $fShareware , $Player , $turn , $lidGame , $Magic , $fMulti );
3970
+ my ($random , $seedA , $seedB , $seedX , $seedY );
3971
+ my ( $FileValues , $typeId , $size );
3972
+ my $currentTurn ;
3973
+ my $offset = 0; # Start at the beginning of the file
3974
+ my @messages ;
3975
+ while ($offset < @fileBytes ) {
3976
+ # Get block info and data
3977
+ $FileValues = $fileBytes [$offset + 1] . $fileBytes [$offset ];
3978
+ ( $typeId , $size ) = &parseBlock($FileValues , $offset );
3979
+ @data = @fileBytes [$offset +2 .. $offset +(2+$size )-1]; # The non-header portion of the block
3980
+ @block = @fileBytes [$offset .. $offset +(2+$size )-1]; # The entire block in question
3981
+
3982
+ if ($typeId == 8 ) { # File Header Block, never encrypted
3983
+ # We always have this data before getting to block 6, because block 8 is first
3984
+ # If there are two (or more) block 8s, the seeds reset for each block 8
3985
+ ($binSeed , $fShareware , $Player , $turn , $lidGame , $Magic , $fMulti ) = &getFileHeaderBlock(\@block );
3986
+ if ($fMulti ) {
3987
+ my @footer = ( $fileBytes [-2], $fileBytes [-1] );
3988
+ $currentTurn = &getFileFooterBlock(\@footer , 2) + 2400;
3989
+ push @messages , " Current Year: $currentTurn \n " ;
3990
+ }
3991
+ ($seedA , $seedB ) = &initDecryption ($binSeed , $fShareware , $Player , $turn , $lidGame );
3992
+ } elsif ($typeId == 0) { # FileFooterBlock, not encrypted
3993
+ } else {
3994
+ # Everything else needs to be decrypted
3995
+ ($decryptedData , $seedA , $seedB , $padding ) = &decryptBytes(\@data , $seedA , $seedB );
3996
+ @decryptedData = @{ $decryptedData };
3997
+ # WHERE THE MAGIC HAPPENS
3998
+ # Display the messages in the file
3999
+ my $message ;
4000
+ # We need the names to display
4001
+ # Check the Player Block so we can get the race names
4002
+ # although there are no names in .x files
4003
+ if ($typeId == 6) { # Player Block
4004
+ my $playerId = $decryptedData [0] & 0xFF;
4005
+ my $fullDataFlag = ($decryptedData [6] & 0x04);
4006
+ my $index = 8;
4007
+ if ($fullDataFlag ) {
4008
+ # The player names are at the end which is not a fixed length
4009
+ $index = 112;
4010
+ my $playerRelationsLength = $decryptedData [112];
4011
+ $index = $index + $playerRelationsLength + 1;
4012
+ }
4013
+ my $singularNameLength = $decryptedData [$index ] & 0xFF;
4014
+ my $singularMessageEnd = $index + $singularNameLength ;
4015
+ # changed this 210516
4016
+ # my $pluralNameLength = $decryptedData[$index+2] & 0xFF;
4017
+ my $pluralNameLength = $decryptedData [$index +$singularNameLength +1] & 0xFF;
4018
+ if ($pluralNameLength == 0) { $pluralNameLength = 1; } # Because there's a 0 byte after it
4019
+ $playerId ++; # As 0 is "Everyone" need to use representative IDs
4020
+ $singularRaceName [$playerId ] = &decodeBytesForStarsString(@decryptedData [$index ..$singularMessageEnd ]);
4021
+ $pluralRaceName [$playerId ] = &decodeBytesForStarsString(@decryptedData [$singularMessageEnd +1..$size -1]);
4022
+ $singularRaceName [0] = ' Everyone' ;
4023
+ } elsif ($typeId == 40) { # check the Message block
4024
+ my $byte0 = &read16(\@decryptedData , 0); # unknown
4025
+ my $byte2 = &read16(\@decryptedData , 2); # unknown
4026
+ my $senderId = &read16(\@decryptedData , 4);
4027
+ my $recipientId = &read16(\@decryptedData , 6);
4028
+ my $byte8 = &read16(\@decryptedData , 8); # unknown
4029
+ my $messageBytes = &read16(\@decryptedData , 10);
4030
+ my $messageLength = $size -1;
4031
+ $message = &decodeBytesForStarsString(@decryptedData [11..$messageLength ]);
4032
+ # print "From: $senderId, To: $recipientId, \"$message\"\n";
4033
+ if ($message ) {
4034
+ my $recipient ;
4035
+ if ($ext =~ / [xX]/ ) {
4036
+ # Different for x files, as we don't have player names in it.
4037
+ # Player ID #s are a bit weird, as 0 in this case is "everyone", not Player 1 (ID:0)
4038
+ if ( $recipientId == 0 ) { $recipient = ' Everyone' ; } else { $recipient = ' Player ' . $recipientId ; }
4039
+ push @messages , " Message Year:" . ($turn +2400) . " , From: Me, To: $recipient , \" $message \"\n " ;
4040
+ } else {
4041
+ # We don't have player names for races undiscovered either
4042
+ # push @messages, "\tMessage Year:" . ($turn+2400) . ", From: $singularRaceName[$senderId+1], To: $singularRaceName[$recipientId], \"$message\"\n";
4043
+ push @messages , " Message Year:" . ($turn +2400) . " , From: $singularRaceName [$senderId +1], To: $singularRaceName [$recipientId ], \" $message \"\n " ;
4044
+ }
4045
+ }
4046
+ }
4047
+ # return @decryptedData;
4048
+ # END OF MAGIC
4049
+ }
4050
+ $offset = $offset + (2 + $size );
4051
+ }
4052
+ return \@messages ;
4053
+ }
4054
+
4055
+
3959
4056
3960
4057
# CSV position 18 is fuel
3961
4058
# 1,1,"Stargate 100/250",1,0,0,5,5,0,0,0,400,100,40,40,144,100,250,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
0 commit comments