hedgewars/uFLDrawnMap.pas
author sheepluva
Sun, 01 May 2016 21:09:45 +0200
branchqmlfrontend
changeset 11792 bcba7938ccb5
parent 11485 b0c34402038c
permissions -rw-r--r--
qmlfrontend/map-preview: add hog count display thingy (only displays "?" for now)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11467
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
11485
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
     3
uses SDLh;
11467
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
     4
11485
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
     5
procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
11467
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
11485
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
     8
uses uUtils, zlib;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
     9
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    10
procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    11
var i, cl: Longword;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    12
    ul: uLong;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    13
    s: shortstring;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    14
    r: LongInt;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    15
    compressedBuf, uncompressedData: PByteArray;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    16
begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    17
    if dataSize = 0 then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    18
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    19
        mapData:= nil;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    20
        size:= 0;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    21
        exit;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    22
    end;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    23
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    24
    compressedBuf:= GetMem(dataSize * 3 div 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    25
    cl:= 0;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    26
    i:= 1;
11467
33a0e3a14ddc - Fix passing of ansistrings via ipc
unc0rr
parents:
diff changeset
    27
11485
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    28
    while i < dataSize do
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    29
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    30
        if dataSize - i > 240 then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    31
            s:= copy(data, i, 240)
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    32
        else
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    33
            s:= copy(data, i, dataSize - i + 1);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    34
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    35
        s:= DecodeBase64(s);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    36
        Move(s[1], compressedBuf^[cl], byte(s[0]));
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    37
        inc(i, 240);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    38
        inc(cl, byte(s[0]));
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    39
    end;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    40
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    41
    ul:= SDLNet_Read32(compressedBuf);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    42
    uncompressedData:= GetMem(ul);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    43
    r:= uncompress(pBytef(uncompressedData), @ul, @(compressedBuf^[4]), cl - 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    44
    FreeMem(compressedBuf, dataSize * 3 div 4);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    45
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    46
    if r = Z_OK then
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    47
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    48
        mapData:= uncompressedData;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    49
        size:= ul
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    50
    end else
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    51
    begin
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    52
        FreeMem(uncompressedData, ul);
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    53
        mapData:= nil;
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    54
        size:= 0
b0c34402038c Handle drawn maps from net
unc0rr
parents: 11467
diff changeset
    55
    end;
11467
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.