Handle drawn maps from net qmlfrontend
authorunc0rr
Fri, 01 Jan 2016 19:14:59 +0300
branchqmlfrontend
changeset 11485 b0c34402038c
parent 11467 33a0e3a14ddc
child 11486 caa1e84c3ac2
Handle drawn maps from net
hedgewars/uFLDrawnMap.pas
hedgewars/uFLGameConfig.pas
--- a/hedgewars/uFLDrawnMap.pas	Sat Dec 26 22:29:57 2015 +0300
+++ b/hedgewars/uFLDrawnMap.pas	Fri Jan 01 19:14:59 2016 +0300
@@ -1,13 +1,58 @@
 unit uFLDrawnMap;
 interface
+uses SDLh;
 
-function decodeDrawnMap(data: ansistring): ansistring;
+procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
 
 implementation
-uses uUtils;
+uses uUtils, zlib;
+
+procedure decodeDrawnMap(data: ansistring; dataSize: Longword; var mapData: PByteArray; var size: Longword);
+var i, cl: Longword;
+    ul: uLong;
+    s: shortstring;
+    r: LongInt;
+    compressedBuf, uncompressedData: PByteArray;
+begin
+    if dataSize = 0 then
+    begin
+        mapData:= nil;
+        size:= 0;
+        exit;
+    end;
+
+    compressedBuf:= GetMem(dataSize * 3 div 4);
+    cl:= 0;
+    i:= 1;
 
-function decodeDrawnMap(data: ansistring): ansistring;
-begin
+    while i < dataSize do
+    begin
+        if dataSize - i > 240 then
+            s:= copy(data, i, 240)
+        else
+            s:= copy(data, i, dataSize - i + 1);
+
+        s:= DecodeBase64(s);
+        Move(s[1], compressedBuf^[cl], byte(s[0]));
+        inc(i, 240);
+        inc(cl, byte(s[0]));
+    end;
+
+    ul:= SDLNet_Read32(compressedBuf);
+    uncompressedData:= GetMem(ul);
+    r:= uncompress(pBytef(uncompressedData), @ul, @(compressedBuf^[4]), cl - 4);
+    FreeMem(compressedBuf, dataSize * 3 div 4);
+
+    if r = Z_OK then
+    begin
+        mapData:= uncompressedData;
+        size:= ul
+    end else
+    begin
+        FreeMem(uncompressedData, ul);
+        mapData:= nil;
+        size:= 0
+    end;
 end;
 
 end.
--- a/hedgewars/uFLGameConfig.pas	Sat Dec 26 22:29:57 2015 +0300
+++ b/hedgewars/uFLGameConfig.pas	Fri Jan 01 19:14:59 2016 +0300
@@ -42,7 +42,8 @@
 
 implementation
 uses uFLIPC, uFLUtils, uFLTeams, uFLThemes, uFLSChemes, uFLAmmo
-    , uFLUICallback, uFLRunQueue, uFLNet, uUtils;
+    , uFLUICallback, uFLRunQueue, uFLNet, uUtils, uFLDrawnMap
+    , SDLh;
 
 var
     currentConfig: TGameConfig;
@@ -55,20 +56,32 @@
 
 procedure sendDrawnMap(config: PGameConfig);
 var i: Longword;
+    data: PByteArray;
+    dataLen: Longword;
     s: shortstring;
 begin
-    i:= 1;
+    decodeDrawnMap(config^.drawnData, config^.drawnDataSize, data, dataLen);
 
-    while i < config^.drawnDataSize do
+    i:= 0;
+
+    s[0]:= #240;
+    while i < dataLen do
     begin
-        if config^.drawnDataSize - i > 240 then
-            s:= copy(config^.drawnData, i, 240)
-        else
-            s:= copy(config^.drawnData, i, config^.drawnDataSize - i);
-        system.writeln('[DRAWN MAP] ', s);
-        ipcToEngine('edraw ' + DecodeBase64(s));
+        if dataLen - i > 240 then
+        begin
+            Move(data^[i], s[1], 240)
+        end else
+        begin
+            Move(data^[i], s[1], dataLen - i);
+            s[0]:= char(dataLen - i)
+        end;
+
+        ipcToEngine('edraw ' + s);
         inc(i, 240)
     end;
+
+    if dataLen > 0 then
+        FreeMem(data, dataLen);
 end;
 
 procedure sendConfig(config: PGameConfig);