hedgewars/uFLDrawnMap.pas
author unc0rr
Tue, 15 Mar 2016 22:29:40 +0300
branchqmlfrontend
changeset 11607 f0dcdbb9b2fe
parent 11480 b0c34402038c
permissions -rw-r--r--
logging via physfs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11462
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     1
unit uFLDrawnMap;
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     2
interface
11480
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
     3
uses SDLh;
11462
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     4
11480
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
     5
procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
11462
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     6
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     7
implementation
11480
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
     8
uses uUtils, zlib;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
     9
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    10
procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    11
var i, cl: Longword;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    12
    ul: uLong;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    13
    s: shortstring;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    14
    r: LongInt;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    15
    compressedBuf, uncompressedData: PByteArray;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    16
begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    17
    if dataSize = 0 then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    18
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    19
        mapData:= nil;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    20
        size:= 0;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    21
        exit;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    22
    end;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    23
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    24
    compressedBuf:= GetMem(dataSize * 3 div 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    25
    cl:= 0;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    26
    i:= 1;
11462
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
    27
11480
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    28
    while i < dataSize do
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    29
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    30
        if dataSize - i > 240 then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    31
            s:= copy(data, i, 240)
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    32
        else
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    33
            s:= copy(data, i, dataSize - i + 1);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    34
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    35
        s:= DecodeBase64(s);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    36
        Move(s[1], compressedBuf^[cl], byte(s[0]));
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    37
        inc(i, 240);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    38
        inc(cl, byte(s[0]));
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    39
    end;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    40
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    41
    ul:= SDLNet_Read32(compressedBuf);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    42
    uncompressedData:= GetMem(ul);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    43
    r:= uncompress(pBytef(uncompressedData), @ul, @(compressedBuf^[4]), cl - 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    44
    FreeMem(compressedBuf, dataSize * 3 div 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    45
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    46
    if r = Z_OK then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    47
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    48
        mapData:= uncompressedData;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    49
        size:= ul
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    50
    end else
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    51
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    52
        FreeMem(uncompressedData, ul);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    53
        mapData:= nil;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    54
        size:= 0
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11462
diff changeset
    55
    end;
11462
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
    56
end;
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
    57
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
    58
end.