Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Net.Sockets;
- #endregion Using
- public class ObbDownloader : MonoBehaviour
- {
- #region Static
- public static ObbDownloader Instance
- {
- get
- {
- if (instance == null)
- {
- GameObject go = new GameObject("ObbDownloader");
- instance = go.AddComponent<ObbDownloader>();
- }
- return instance;
- }
- }
- private static ObbDownloader instance;
- #endregion Static
- #region Members
- public float PercentageDone
- { get { return (float)this.downloadedBytes / (float)this.totalBytes; } }
- private string bundleIdentifier = "INSERT YOUR BUNDLE INDENTIFIER HERE"; // com.DefaultCompany.ProductName
- private int bundleVersionCode = 1;
- private string host = "INSERT HOST NAME HERE (without http://)"; // www.mycompany.com or mycompany.com
- private string uri = "INSERT PATH TO FILE ON WEB SERVER HERE"; // /aFolder/com.DefaultCompany.ProductName.obb
- private string diskLocation;
- private string fileName;
- private GameObject requester;
- private NetworkStream networkStream;
- private FileStream fileStream;
- private Socket client;
- private uint totalBytes;
- private int downloadedBytes = 0;
- private int read = 0;
- private bool isDownloading = false;
- #endregion Members
- #region UnityFunctions
- void Update()
- { DownloadPack(); }
- #endregion UnityFunctions
- #region Public
- public bool HasExpansionpack()
- {
- this.fileName = "main." + this.bundleVersionCode.ToString() + "." + this.bundleIdentifier + ".obb";
- this.diskLocation = "/storage/emulated/0/Android/obb/" + this.bundleIdentifier + "/";
- if(!File.Exists(this.diskLocation + this.fileName))
- { return false; }
- UpdatePatchInfo();
- FileInfo fileInfo = new FileInfo(this.diskLocation + this.fileName);
- if (this.totalBytes != fileInfo.Length)
- { return false; }
- return true;
- }
- public void DownloadExpansion(GameObject requester)
- {
- string query = "GET " + this.uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
- "Host: " + this.host + "\r\n" +
- "User-Agent: undefined\r\n" +
- "Connection: close\r\n" +
- "\r\n";
- Debug.Log(query);
- this.client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
- this.client.Connect(this.host, 80);
- this.networkStream = new NetworkStream(this.client);
- byte[] bytes = Encoding.Default.GetBytes(query);
- this.networkStream.Write(bytes, 0, bytes.Length);
- BinaryReader bReader = new BinaryReader(this.networkStream, Encoding.Default);
- string response = "";
- string line;
- char c;
- do
- {
- line = "";
- c = '\u0000';
- while (true)
- {
- c = bReader.ReadChar();
- if (c == '\r')
- break;
- line += c;
- }
- c = bReader.ReadChar();
- response += line + "\r\n";
- }
- while (line.Length > 0);
- Debug.Log(response);
- Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
- this.totalBytes = uint.Parse(reContentLength.Match(response).Value);
- this.fileStream = new FileStream(this.diskLocation + this.fileName, FileMode.Create);
- this.requester = requester;
- this.isDownloading = true;
- }
- #endregion Public
- #region Private
- private void UpdatePatchInfo()
- {
- string query = "GET " + this.uri.Replace(" ", "%20") + " HTTP/1.1\r\n" +
- "Host: " + this.host + "\r\n" +
- "User-Agent: undefined\r\n" +
- "Connection: close\r\n" +
- "\r\n";
- this.client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
- this.client.Connect(this.host, 80);
- this.networkStream = new NetworkStream(this.client);
- byte[] bytes = Encoding.Default.GetBytes(query);
- this.networkStream.Write(bytes, 0, bytes.Length);
- BinaryReader bReader = new BinaryReader(this.networkStream, Encoding.Default);
- string response = "";
- string line;
- char c;
- do
- {
- line = "";
- c = '\u0000';
- while (true)
- {
- c = bReader.ReadChar();
- if (c == '\r')
- break;
- line += c;
- }
- c = bReader.ReadChar();
- response += line + "\r\n";
- }
- while (line.Length > 0);
- Regex reContentLength = new Regex(@"(?<=Content-Length:\s)\d+", RegexOptions.IgnoreCase);
- this.totalBytes = uint.Parse(reContentLength.Match(response).Value);
- this.networkStream.Close();
- this.client.Close();
- }
- private void DownloadPack()
- {
- if (!this.isDownloading)
- { return; }
- byte[] buffer = new byte[4 * 1024];
- if (this.downloadedBytes < this.totalBytes)
- {
- if (this.networkStream.DataAvailable)
- {
- this.read = this.networkStream.Read(buffer, 0, buffer.Length);
- this.downloadedBytes += this.read;
- this.fileStream.Write(buffer, 0, this.read);
- }
- }
- else
- {
- this.fileStream.Flush();
- this.fileStream.Close();
- this.networkStream.Close();
- this.client.Close();
- QuickRestartAndroidApp();
- this.requester.SendMessage("OnExpansionDownloaded", SendMessageOptions.DontRequireReceiver);
- this.isDownloading = false;
- }
- }
- private void QuickRestartAndroidApp()
- {
- using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
- AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");
- int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000;
- intent.Call<AndroidJavaObject>("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION);
- currentActivity.Call("startActivity", intent);
- if (AndroidJNI.ExceptionOccurred() != System.IntPtr.Zero)
- {
- Debug.LogError("Exception occurred while attempting to start activity - is the AndroidManifest.xml incorrect?");
- AndroidJNI.ExceptionDescribe();
- AndroidJNI.ExceptionClear();
- }
- }
- }
- #endregion Private
- }
Add Comment
Please, Sign In to add comment