3
3
using System . Collections . Generic ;
4
4
using System . Net ;
5
5
using System . Text ;
6
+ #if ! UNITY_WSA && ! UNITY_WP8
7
+ using System . IO ;
8
+ using Ionic . Zlib ;
9
+ #endif
6
10
7
11
namespace PlayFab . Internal
8
12
{
@@ -28,20 +32,40 @@ private IEnumerator MakeRequestViaUnity(CallRequestContainer requestContainer)
28
32
{
29
33
_pendingWwwMessages += 1 ;
30
34
string fullUrl = PlayFabSettings . GetFullUrl ( requestContainer . UrlPath ) ;
31
- byte [ ] bData = Encoding . UTF8 . GetBytes ( requestContainer . Data ) ;
35
+ byte [ ] payload = Encoding . UTF8 . GetBytes ( requestContainer . Data ) ;
32
36
33
37
#if UNITY_4_4 || UNITY_4_3 || UNITY_4_2 || UNITY_4_2 || UNITY_4_0 || UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5
34
38
// Using hashtable for compatibility with Unity < 4.5
35
39
Hashtable headers = new Hashtable ( ) ;
36
40
#else
37
41
Dictionary < string , string > headers = new Dictionary < string , string > ( ) ;
38
42
#endif
43
+
44
+ #if ! UNITY_WSA && ! UNITY_WP8
45
+ if ( PlayFabSettings . CompressApiData )
46
+ {
47
+ headers . Add ( "Content-Encoding" , "GZIP" ) ;
48
+ headers . Add ( "Accept-Encoding" , "GZIP" ) ;
49
+
50
+ using ( var stream = new MemoryStream ( ) )
51
+ {
52
+ using (
53
+ GZipStream zipstream = new GZipStream ( stream , CompressionMode . Compress ,
54
+ CompressionLevel . BestCompression ) )
55
+ {
56
+ zipstream . Write ( payload , 0 , payload . Length ) ;
57
+ }
58
+ payload = stream . ToArray ( ) ;
59
+ }
60
+ }
61
+ #endif
62
+
39
63
headers . Add ( "Content-Type" , "application/json" ) ;
40
64
if ( requestContainer . AuthType != null )
41
65
headers . Add ( requestContainer . AuthType , requestContainer . AuthKey ) ;
42
66
headers . Add ( "X-ReportErrorAsSuccess" , "true" ) ;
43
67
headers . Add ( "X-PlayFabSDK" , PlayFabSettings . VersionString ) ;
44
- WWW www = new WWW ( fullUrl , bData , headers ) ;
68
+ WWW www = new WWW ( fullUrl , payload , headers ) ;
45
69
46
70
PlayFabSettings . InvokeRequest ( requestContainer . UrlPath , requestContainer . CallId , requestContainer . Request , requestContainer . CustomData ) ;
47
71
@@ -50,9 +74,52 @@ private IEnumerator MakeRequestViaUnity(CallRequestContainer requestContainer)
50
74
requestContainer . ResultStr = null ;
51
75
requestContainer . Error = null ;
52
76
if ( ! string . IsNullOrEmpty ( www . error ) )
77
+ {
53
78
requestContainer . Error = PlayFabHttp . GeneratePfError ( HttpStatusCode . ServiceUnavailable , PlayFabErrorCode . ServiceUnavailable , www . error ) ;
79
+ }
54
80
else
55
- requestContainer . ResultStr = www . text ;
81
+ {
82
+ string finalWwwText = "" ;
83
+
84
+ #if ! UNITY_WSA && ! UNITY_WP8
85
+ if ( PlayFabSettings . CompressApiData )
86
+ {
87
+ try
88
+ {
89
+ var stream = new MemoryStream ( www . bytes ) ;
90
+ using ( var gZipStream = new GZipStream ( stream , CompressionMode . Decompress , false ) )
91
+ {
92
+ var buffer = new byte [ 4096 ] ;
93
+ using ( var output = new MemoryStream ( ) )
94
+ {
95
+ var read = 0 ;
96
+ while ( ( read = gZipStream . Read ( buffer , 0 , buffer . Length ) ) > 0 )
97
+ {
98
+ output . Write ( buffer , 0 , read ) ;
99
+ }
100
+ output . Seek ( 0 , SeekOrigin . Begin ) ;
101
+ var streamReader = new System . IO . StreamReader ( output ) ;
102
+ finalWwwText = streamReader . ReadToEnd ( ) ;
103
+ }
104
+ }
105
+ }
106
+ catch
107
+ {
108
+ // if this was not a valid GZip response, then send the message back as text to the call back.
109
+ requestContainer . Error = PlayFabHttp . GeneratePfError ( HttpStatusCode . PreconditionFailed , PlayFabErrorCode . Unknown , www . text ) ;
110
+ finalWwwText = www . text ;
111
+ }
112
+ }
113
+ else
114
+ {
115
+ #endif
116
+ finalWwwText = www . text ;
117
+ #if ! UNITY_WSA && ! UNITY_WP8
118
+ }
119
+ #endif
120
+
121
+ requestContainer . ResultStr = finalWwwText ;
122
+ }
56
123
57
124
requestContainer . InvokeCallback ( ) ;
58
125
0 commit comments