Guest User

ObbDownloader

a guest
Mar 4th, 2015
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.05 KB | None | 0 0
  1. #region Using
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. using System.IO;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Net.Sockets;
  10.  
  11. #endregion Using
  12.  
  13. public class ObbDownloader : MonoBehaviour
  14. {
  15.     #region Static
  16.  
  17.     public static ObbDownloader Instance
  18.     {
  19.         get
  20.         {
  21.             if (instance == null)
  22.             {
  23.                 GameObject go = new GameObject("ObbDownloader");
  24.                 instance = go.AddComponent<ObbDownloader>();
  25.             }
  26.             return instance;
  27.         }
  28.     }
  29.  
  30.     private static ObbDownloader instance;
  31.  
  32.     #endregion Static
  33.  
  34.     #region Members
  35.  
  36.     public float PercentageDone
  37.     { get { return (float)this.downloadedBytes / (float)this.totalBytes; } }
  38.  
  39.     private string bundleIdentifier = "INSERT YOUR BUNDLE INDENTIFIER HERE"; // com.DefaultCompany.ProductName
  40.     private int bundleVersionCode = 1;
  41.     private string host = "INSERT HOST NAME HERE (without http://)"; // www.mycompany.com or mycompany.com
  42.     private string uri = "INSERT PATH TO FILE ON WEB SERVER HERE"; // /aFolder/com.DefaultCompany.ProductName.obb
  43.  
  44.     private string diskLocation;
  45.     private string fileName;
  46.     private GameObject requester;
  47.  
  48.     private NetworkStream networkStream;
  49.     private FileStream fileStream;
  50.     private Socket client;
  51.  
  52.     private uint totalBytes;
  53.     private int downloadedBytes = 0;
  54.     private int read = 0;
  55.     private bool isDownloading = false;
  56.  
  57.     #endregion Members
  58.  
  59.     #region UnityFunctions
  60.  
  61.     void Update()
  62.     { DownloadPack(); }
  63.  
  64.     #endregion UnityFunctions
  65.  
  66.     #region Public
  67.  
  68.     public bool HasExpansionpack()
  69.     {
  70.         this.fileName = "main." + this.bundleVersionCode.ToString() + "." + this.bundleIdentifier + ".obb";
  71.         this.diskLocation = "/storage/emulated/0/Android/obb/" + this.bundleIdentifier + "/";
  72.  
  73.         if(!File.Exists(this.diskLocation + this.fileName))
  74.         { return false; }
  75.  
  76.         UpdatePatchInfo();
  77.         FileInfo fileInfo = new FileInfo(this.diskLocation + this.fileName);
  78.         if (this.totalBytes != fileInfo.Length)
  79.         { return false; }
  80.         return true;
  81.     }
  82.  
  83.     public void DownloadExpansion(GameObject requester)
  84.     {
  85.         string query = "GET " + this.uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
  86.                         "Host: " + this.host + "\r\n" +
  87.                         "User-Agent: undefined\r\n" +
  88.                         "Connection: close\r\n" +
  89.                         "\r\n";
  90.  
  91.  
  92.         Debug.Log(query);
  93.  
  94.         this.client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  95.         this.client.Connect(this.host, 80);
  96.  
  97.         this.networkStream = new NetworkStream(this.client);
  98.  
  99.         byte[] bytes = Encoding.Default.GetBytes(query);
  100.         this.networkStream.Write(bytes, 0, bytes.Length);
  101.  
  102.         BinaryReader bReader = new BinaryReader(this.networkStream, Encoding.Default);
  103.  
  104.         string response = "";
  105.         string line;
  106.         char c;
  107.  
  108.         do
  109.         {
  110.             line = "";
  111.             c = '\u0000';
  112.             while (true)
  113.             {
  114.                 c = bReader.ReadChar();
  115.                 if (c == '\r')
  116.                     break;
  117.                 line += c;
  118.             }
  119.             c = bReader.ReadChar();
  120.             response += line + "\r\n";
  121.         }
  122.         while (line.Length > 0);
  123.  
  124.         Debug.Log(response);
  125.  
  126.         Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
  127.         this.totalBytes = uint.Parse(reContentLength.Match(response).Value);
  128.  
  129.         this.fileStream = new FileStream(this.diskLocation + this.fileName, FileMode.Create);
  130.  
  131.         this.requester = requester;
  132.         this.isDownloading = true;
  133.     }
  134.  
  135.     #endregion Public
  136.  
  137.     #region Private
  138.  
  139.     private void UpdatePatchInfo()
  140.     {
  141.         string query = "GET " + this.uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
  142.                         "Host: " + this.host + "\r\n" +
  143.                         "User-Agent: undefined\r\n" +
  144.                         "Connection: close\r\n" +
  145.                         "\r\n";
  146.  
  147.  
  148.         this.client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  149.         this.client.Connect(this.host, 80);
  150.  
  151.         this.networkStream = new NetworkStream(this.client);
  152.  
  153.         byte[] bytes = Encoding.Default.GetBytes(query);
  154.         this.networkStream.Write(bytes, 0, bytes.Length);
  155.  
  156.         BinaryReader bReader = new BinaryReader(this.networkStream, Encoding.Default);
  157.  
  158.         string response = "";
  159.         string line;
  160.         char c;
  161.  
  162.         do
  163.         {
  164.             line = "";
  165.             c = '\u0000';
  166.             while (true)
  167.             {
  168.                 c = bReader.ReadChar();
  169.                 if (c == '\r')
  170.                     break;
  171.                 line += c;
  172.             }
  173.             c = bReader.ReadChar();
  174.             response += line + "\r\n";
  175.         }
  176.         while (line.Length > 0);
  177.  
  178.         Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
  179.         this.totalBytes = uint.Parse(reContentLength.Match(response).Value);
  180.  
  181.         this.networkStream.Close();
  182.         this.client.Close();
  183.     }
  184.  
  185.     private void DownloadPack()
  186.     {
  187.         if (!this.isDownloading)
  188.         { return; }
  189.  
  190.         byte[] buffer = new byte[4 * 1024];
  191.  
  192.         if (this.downloadedBytes < this.totalBytes)
  193.         {
  194.             if (this.networkStream.DataAvailable)
  195.             {
  196.                 this.read = this.networkStream.Read(buffer, 0, buffer.Length);
  197.                 this.downloadedBytes += this.read;
  198.                 this.fileStream.Write(buffer, 0, this.read);
  199.             }
  200.         }
  201.         else
  202.         {
  203.             this.fileStream.Flush();
  204.             this.fileStream.Close();
  205.  
  206.             this.networkStream.Close();
  207.             this.client.Close();
  208.  
  209.             QuickRestartAndroidApp();
  210.             this.requester.SendMessage("OnExpansionDownloaded", SendMessageOptions.DontRequireReceiver);
  211.  
  212.             this.isDownloading = false;
  213.         }
  214.     }
  215.  
  216.     private void QuickRestartAndroidApp()
  217.     {
  218.         using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  219.         {
  220.             AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  221.             AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
  222.            
  223.             int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000;
  224.             intent.Call<AndroidJavaObject>("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION);
  225.  
  226.             currentActivity.Call("startActivity", intent);
  227.  
  228.             if (AndroidJNI.ExceptionOccurred() != System.IntPtr.Zero)
  229.             {
  230.                 Debug.LogError("Exception occurred while attempting to start activity - is the AndroidManifest.xml incorrect?");
  231.                 AndroidJNI.ExceptionDescribe();
  232.                 AndroidJNI.ExceptionClear();
  233.             }
  234.         }
  235.     }
  236.  
  237.     #endregion Private
  238. }
Add Comment
Please, Sign In to add comment