private static string MakeFullPathFromState(LocalStorageFileState state)
        {
            string temporaryCachePath = BattleNet.Client().GetTemporaryCachePath();
            string str = string.Concat(state.CH.Sha256Digest, ".", state.CH.Usage);

            return(Path.Combine(temporaryCachePath, str));
        }
        public bool GetFile(ContentHandle ch, LocalStorageAPI.DownloadCompletedCallback cb, object userContext = null)
        {
            bool flag;

            try
            {
                LocalStorageFileState localStorageFileState = new LocalStorageFileState(LocalStorageAPI.m_downloadId)
                {
                    CH          = ch,
                    Callback    = cb,
                    UserContext = userContext
                };
                LocalStorageAPI.s_log.LogDebug("Starting GetFile State={0}", new object[] { localStorageFileState });
                if (!this.LoadStateFromDrive(localStorageFileState))
                {
                    LocalStorageAPI.s_log.LogDebug("Unable to load file from disk, starting a download. State={0}", new object[] { localStorageFileState });
                    this.DownloadFromDepot(localStorageFileState);
                }
                return(true);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION (GetFile): {0}", new object[] { exception.Message });
                flag = false;
            }
            return(flag);
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            LocalStorageFileState asyncState = (LocalStorageFileState)ar.AsyncState;

            try
            {
                LocalStorageAPI.s_log.LogDebug("ReceiveCallback called for State={0}", new object[] { asyncState });
                int num = asyncState.Socket.EndReceive(ar);
                if (num <= 0)
                {
                    LocalStorageAPI.CompleteDownload(asyncState);
                }
                else
                {
                    int          num1         = (asyncState.FileData != null ? (int)asyncState.FileData.Length : 0) + num;
                    MemoryStream memoryStream = new MemoryStream(new byte[num1], 0, num1, true, true);
                    if (asyncState.FileData != null)
                    {
                        memoryStream.Write(asyncState.FileData, 0, (int)asyncState.FileData.Length);
                    }
                    memoryStream.Write(asyncState.ReceiveBuffer, 0, num);
                    asyncState.FileData = memoryStream.GetBuffer();
                    asyncState.Socket.BeginReceive(asyncState.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, SocketFlags.None, new AsyncCallback(LocalStorageAPI.ReceiveCallback), asyncState);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[] { exception.Message });
                asyncState.FailureMessage = exception.Message;
                LocalStorageAPI.ExecuteFailedDownload(asyncState);
            }
        }
        private static string MakeFullPathFromState(LocalStorageFileState state)
        {
            string temporaryCachePath = BattleNet.Client().GetTemporaryCachePath();
            string text = state.CH.Sha256Digest + "." + state.CH.Usage;

            return(Path.Combine(temporaryCachePath, text));
        }
 public bool GetFile(ContentHandle ch, LocalStorageAPI.DownloadCompletedCallback cb, object userContext = null)
 {
     try
     {
         LocalStorageFileState localStorageFileState = new LocalStorageFileState(LocalStorageAPI.m_downloadId);
         localStorageFileState.CH          = ch;
         localStorageFileState.Callback    = cb;
         localStorageFileState.UserContext = userContext;
         LocalStorageAPI.s_log.LogDebug("Starting GetFile State={0}", new object[]
         {
             localStorageFileState
         });
         if (!this.LoadStateFromDrive(localStorageFileState))
         {
             LocalStorageAPI.s_log.LogDebug("Unable to load file from disk, starting a download. State={0}", new object[]
             {
                 localStorageFileState
             });
             this.DownloadFromDepot(localStorageFileState);
         }
     }
     catch (Exception ex)
     {
         LocalStorageAPI.s_log.LogWarning("EXCEPTION (GetFile): {0}", new object[]
         {
             ex.get_Message()
         });
         return(false);
     }
     return(true);
 }
        public override void Process()
        {
            List <LocalStorageFileState> completedDownloads = LocalStorageAPI.m_completedDownloads;

            lock (completedDownloads)
            {
                using (List <LocalStorageFileState> .Enumerator enumerator = LocalStorageAPI.m_completedDownloads.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        LocalStorageFileState current = enumerator.get_Current();
                        if (current.FileData != null)
                        {
                            LocalStorageAPI.s_log.LogDebug("Request completed State={0}", new object[]
                            {
                                current
                            });
                        }
                        else
                        {
                            LocalStorageAPI.s_log.LogWarning("Request failed State={0}", new object[]
                            {
                                current
                            });
                        }
                        LocalStorageAPI.s_log.Process();
                        current.Callback(current.FileData, current.UserContext);
                    }
                }
                LocalStorageAPI.m_completedDownloads.Clear();
            }
        }
 private static void StoreStateToDrive(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("StoreState State={0}", new object[] { state });
         string str = LocalStorageAPI.MakeFullPathFromState(state);
         LocalStorageAPI.s_log.LogDebug("Attempting to save {0}", new object[] { str });
         if (!File.Exists(str))
         {
             FileStream fileStream = File.Create(str, (int)state.FileData.Length);
             if (state.CompressedData != null)
             {
                 LocalStorageAPI.s_log.LogDebug("Writing compressed file to disk");
                 fileStream.Write(state.CompressedData, 0, (int)state.CompressedData.Length);
             }
             else
             {
                 LocalStorageAPI.s_log.LogDebug("Writing uncompressed file to disk");
                 fileStream.Write(state.FileData, 0, (int)state.FileData.Length);
             }
             fileStream.Flush();
             fileStream.Close();
             LocalStorageAPI.s_log.LogDebug("Writing completed");
         }
         else
         {
             LocalStorageAPI.s_log.LogDebug("Unable to save the file, it already exists");
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         LocalStorageAPI.s_log.LogWarning("EXCEPTION (StoreStateToDrive): {0}", new object[] { exception.Message });
     }
 }
 private static void ConnectCallback(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("ConnectCallback called for State={0}", new object[]
         {
             state
         });
         state.ReceiveBuffer = new byte[LocalStorageAPI.m_bufferSize];
         state.Socket.BeginReceive(state.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, 0, new AsyncCallback(LocalStorageAPI.ReceiveCallback), state);
         string text = string.Format("GET /{0}.{1} HTTP/1.1\r\n", state.CH.Sha256Digest, state.CH.Usage);
         text  = text + "Host: " + state.Connection.Host + "\r\n";
         text += "User-Agent: HearthStone\r\n";
         text += "Connection: close\r\n";
         text += "\r\n";
         byte[] bytes = Encoding.get_ASCII().GetBytes(text);
         state.Socket.Send(bytes, 0, bytes.Length, 0);
     }
     catch (Exception ex)
     {
         LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[]
         {
             ex.get_Message()
         });
     }
 }
        private static void CompleteDownload(LocalStorageFileState state)
        {
            bool flag = true;

            LocalStorageAPI.s_log.LogDebug("Download completed for State={0}", new object[] { state });
            HTTPHeader hTTPHeader = LocalStorageAPI.ParseHTTPHeader(state.FileData);

            if (hTTPHeader != null)
            {
                byte[] numArray = new byte[hTTPHeader.ContentLength];
                Array.Copy(state.FileData, hTTPHeader.ContentStart, numArray, 0, hTTPHeader.ContentLength);
                if (LocalStorageAPI.ComputeSHA256(numArray) == state.CH.Sha256Digest)
                {
                    flag = false;
                    LocalStorageAPI.DecompressStateIfPossible(state, numArray);
                }
                else
                {
                    LocalStorageAPI.s_log.LogWarning("Integrity check failed for State={0}", new object[] { state });
                }
            }
            else
            {
                LocalStorageAPI.s_log.LogWarning("Parsinig of HTTP header failed for State={0}", new object[] { state });
            }
            if (flag || state.FileData == null)
            {
                LocalStorageAPI.ExecuteFailedDownload(state);
            }
            else
            {
                LocalStorageAPI.ExecuteSucessfulDownload(state);
            }
        }
        private static void FinalizeState(LocalStorageFileState state)
        {
            List <LocalStorageFileState> completedDownloads = LocalStorageAPI.m_completedDownloads;

            lock (completedDownloads)
            {
                LocalStorageAPI.m_completedDownloads.Add(state);
            }
        }
        private void DownloadFromDepot(LocalStorageFileState state)
        {
            string str = string.Format("{0}.depot.battle.net", state.CH.Region);

            LocalStorageAPI.s_log.LogDebug("Starting download from {0}", new object[] { str });
            state.Connection.LogDebug   = new Action <string>(LocalStorageAPI.s_log.LogDebug);
            state.Connection.LogWarning = new Action <string>(LocalStorageAPI.s_log.LogWarning);
            state.Connection.OnFailure  = () => LocalStorageAPI.ExecuteFailedDownload(state);
            state.Connection.OnSuccess  = () => LocalStorageAPI.ConnectCallback(state);
            state.Connection.Connect(str, this.DepotPort);
        }
        private static void FinalizeState(LocalStorageFileState state)
        {
            object mCompletedDownloads = LocalStorageAPI.m_completedDownloads;

            Monitor.Enter(mCompletedDownloads);
            try
            {
                LocalStorageAPI.m_completedDownloads.Add(state);
            }
            finally
            {
                Monitor.Exit(mCompletedDownloads);
            }
        }
        private static void DecompressStateIfPossible(LocalStorageFileState state, byte[] data)
        {
            ulong num;

            if (!LocalStorageAPI.IsCompressedStream(data, out num))
            {
                state.FileData = data;
            }
            else
            {
                state.CompressedData = data;
                MemoryStream memoryStream = new MemoryStream(data, 16, (int)data.Length - 16);
                state.FileData = LocalStorageAPI.Inflate(memoryStream, (int)num);
            }
        }
 private bool LoadStateFromDrive(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("LoadState State={0}", new object[]
         {
             state
         });
         string text = LocalStorageAPI.MakeFullPathFromState(state);
         LocalStorageAPI.s_log.LogDebug("Attempting to load {0}", new object[]
         {
             text
         });
         if (!File.Exists(text))
         {
             LocalStorageAPI.s_log.LogDebug("File does not exist, unable to load from disk.");
             bool result = false;
             return(result);
         }
         FileStream fileStream = File.OpenRead(text);
         byte[]     array      = new byte[fileStream.get_Length()];
         fileStream.Read(array, 0, array.Length);
         fileStream.Close();
         if (LocalStorageAPI.ComputeSHA256(array) != state.CH.Sha256Digest)
         {
             LocalStorageAPI.s_log.LogDebug("File was loaded but integrity check failed, attempting to delete ...");
             File.Delete(text);
             bool result = false;
             return(result);
         }
         LocalStorageAPI.DecompressStateIfPossible(state, array);
         LocalStorageAPI.s_log.LogDebug("Loading completed");
         LocalStorageAPI.FinalizeState(state);
     }
     catch (Exception ex)
     {
         LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[]
         {
             ex.get_Message()
         });
         bool result = false;
         return(result);
     }
     return(true);
 }
        private bool LoadStateFromDrive(LocalStorageFileState state)
        {
            bool flag;

            try
            {
                LocalStorageAPI.s_log.LogDebug("LoadState State={0}", new object[] { state });
                string str = LocalStorageAPI.MakeFullPathFromState(state);
                LocalStorageAPI.s_log.LogDebug("Attempting to load {0}", new object[] { str });
                if (File.Exists(str))
                {
                    FileStream fileStream = File.OpenRead(str);
                    byte[]     numArray   = new byte[checked ((IntPtr)fileStream.Length)];
                    fileStream.Read(numArray, 0, (int)numArray.Length);
                    fileStream.Close();
                    if (LocalStorageAPI.ComputeSHA256(numArray) == state.CH.Sha256Digest)
                    {
                        LocalStorageAPI.DecompressStateIfPossible(state, numArray);
                        LocalStorageAPI.s_log.LogDebug("Loading completed");
                        LocalStorageAPI.FinalizeState(state);
                        return(true);
                    }
                    else
                    {
                        LocalStorageAPI.s_log.LogDebug("File was loaded but integrity check failed, attempting to delete ...");
                        File.Delete(str);
                        flag = false;
                    }
                }
                else
                {
                    LocalStorageAPI.s_log.LogDebug("File does not exist, unable to load from disk.");
                    flag = false;
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[] { exception.Message });
                flag = false;
            }
            return(flag);
        }
 private static void ConnectCallback(LocalStorageFileState state)
 {
     try
     {
         LocalStorageAPI.s_log.LogDebug("ConnectCallback called for State={0}", new object[] { state });
         state.ReceiveBuffer = new byte[LocalStorageAPI.m_bufferSize];
         state.Socket.BeginReceive(state.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, SocketFlags.None, new AsyncCallback(LocalStorageAPI.ReceiveCallback), state);
         string str = string.Format("GET /{0}.{1} HTTP/1.1\r\n", state.CH.Sha256Digest, state.CH.Usage);
         str = string.Concat(str, "Host: ", state.Connection.Host, "\r\n");
         str = string.Concat(str, "User-Agent: HearthStone\r\n");
         str = string.Concat(str, "Connection: close\r\n");
         str = string.Concat(str, "\r\n");
         byte[] bytes = Encoding.ASCII.GetBytes(str);
         state.Socket.Send(bytes, 0, (int)bytes.Length, SocketFlags.None);
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[] { exception.Message });
     }
 }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            LocalStorageFileState localStorageFileState = (LocalStorageFileState)ar.get_AsyncState();

            try
            {
                LocalStorageAPI.s_log.LogDebug("ReceiveCallback called for State={0}", new object[]
                {
                    localStorageFileState
                });
                int num = localStorageFileState.Socket.EndReceive(ar);
                if (num > 0)
                {
                    int          num2         = (localStorageFileState.FileData != null) ? localStorageFileState.FileData.Length : 0;
                    int          num3         = num2 + num;
                    MemoryStream memoryStream = new MemoryStream(new byte[num3], 0, num3, true, true);
                    if (localStorageFileState.FileData != null)
                    {
                        memoryStream.Write(localStorageFileState.FileData, 0, localStorageFileState.FileData.Length);
                    }
                    memoryStream.Write(localStorageFileState.ReceiveBuffer, 0, num);
                    localStorageFileState.FileData = memoryStream.GetBuffer();
                    localStorageFileState.Socket.BeginReceive(localStorageFileState.ReceiveBuffer, 0, LocalStorageAPI.m_bufferSize, 0, new AsyncCallback(LocalStorageAPI.ReceiveCallback), localStorageFileState);
                }
                else
                {
                    LocalStorageAPI.CompleteDownload(localStorageFileState);
                }
            }
            catch (Exception ex)
            {
                LocalStorageAPI.s_log.LogWarning("EXCEPTION: {0}", new object[]
                {
                    ex.get_Message()
                });
                localStorageFileState.FailureMessage = ex.get_Message();
                LocalStorageAPI.ExecuteFailedDownload(localStorageFileState);
            }
        }
 private static void ExecuteFailedDownload(LocalStorageFileState state)
 {
     state.FileData = null;
     LocalStorageAPI.FinalizeDownload(state);
 }
 private static void ExecuteSucessfulDownload(LocalStorageFileState state)
 {
     LocalStorageAPI.StoreStateToDrive(state);
     LocalStorageAPI.FinalizeDownload(state);
 }
 private static void FinalizeDownload(LocalStorageFileState state)
 {
     state.Connection.Disconnect();
     state.ReceiveBuffer = null;
     LocalStorageAPI.FinalizeState(state);
 }