Pro_Unit

RemoteResouceManager

Feb 11th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.64 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Xml.Serialization;
  6.  
  7. using UnityEngine;
  8. using UnityEngine.Networking;
  9.  
  10. public class RemoteResouceManager
  11. {
  12.     public static List<ArVideo> Videos;
  13.     public static float ProgressNormalized { get; private set; }
  14.     public static event Action<float> OnProgressNormalizedChange;
  15.     public static readonly string CachePath = Application.persistentDataPath + @"/";
  16.  
  17.     public static void Temp ()
  18.     {
  19.         UnityWebRequest www = UnityWebRequest.Get (@"http://michaelershov.com/Mila_Sketch_AR/videos_mila.xml");
  20.         var request = www.SendWebRequest ();
  21.         request.completed += (operation) =>
  22.         {
  23.             XmlSerializer serializer = new XmlSerializer (typeof (List<ArVideo>));
  24.             Videos = (List<ArVideo>) serializer.Deserialize (new MemoryStream (www.downloadHandler.data));
  25.             Debug.Log ("Test");
  26.  
  27.         };
  28.     }
  29.  
  30.     public static bool LoadInChache (int Version)
  31.     {
  32.         bool sucsses = false;
  33.         int currentCountFiles = Directory.GetFiles (RemoteResouceManager.CachePath).Length;
  34.  
  35.         if (currentCountFiles == 0)
  36.         {
  37.             sucsses = false;
  38.         }
  39.         else
  40.         {
  41.             Videos = new List<ArVideo> ();
  42.             for (int i = 0, j = 0; i < currentCountFiles; i++, j++)
  43.             {
  44.                 Videos.Add (new ArVideo (Version, i + 1, i + j + 1, i + j + 2));
  45.             }
  46.             sucsses = true;
  47.         }
  48.  
  49.         return sucsses;
  50.     }
  51.  
  52.     public static ArVideo GetByUrl (string url)
  53.     {
  54.         foreach (var arvideo in Videos)
  55.         {
  56.             if (arvideo.Video.Url == url)
  57.             {
  58.                 return arvideo;
  59.             }
  60.         }
  61.         return null;
  62.     }
  63.  
  64.     public static void LoadAll (System.Action onLoad, System.Action<string> onError)
  65.     {
  66.         //TODO: Videos.Xml
  67.         //Example: http://site.ru/videos.xml
  68.         UnityWebRequest www = UnityWebRequest.Get (@"http://michaelershov.com/Mila_Sketch_AR/videos_mila.xml");
  69.         var request = www.SendWebRequest ();
  70.         request.completed += (operation) =>
  71.         {
  72.             if (www.isHttpError || www.isNetworkError)
  73.             {
  74.                 if (onError != null)
  75.                 {
  76.                     onError.Invoke (www.error);
  77.                 }
  78.                 return;
  79.             }
  80.  
  81.             XmlSerializer serializer = new XmlSerializer (typeof (List<ArVideo>));
  82.             Videos = (List<ArVideo>) serializer.Deserialize (new MemoryStream (www.downloadHandler.data));
  83.  
  84.             if (Videos != null)
  85.             {
  86.                 Queue<ArVideo> videos = new Queue<ArVideo> (Videos);
  87.  
  88.                 Action<ArVideo> onLoadVideo = null;
  89.  
  90.                 onLoadVideo = (ar) =>
  91.                 {
  92.                     ProgressNormalized += 1f / Videos.Count;
  93.                     if (OnProgressNormalizedChange != null)
  94.                     {
  95.                         OnProgressNormalizedChange.Invoke (ProgressNormalized);
  96.                     }
  97.  
  98.                     if (videos.Count > 0)
  99.                     {
  100.                         var v = videos.Dequeue ();
  101.                         v.OnLoad += onLoadVideo;
  102.                         v.Load ();
  103.                     }
  104.                     else
  105.                     {
  106.                         if (onLoad != null)
  107.                         {
  108.                             onLoad.Invoke ();
  109.                         }
  110.                     }
  111.                 };
  112.  
  113.                 ArVideo video = videos.Dequeue ();
  114.                 video.OnLoad += onLoadVideo;
  115.                 video.Load ();
  116.  
  117.             }
  118.         };
  119.     }
  120.  
  121. }
  122.  
  123. [System.Serializable]
  124. public class ArVideo
  125. {
  126.     public int Id;
  127.     public int Version;
  128.     public RemoteResouce Video;
  129.     public RemoteResouce Audio;
  130.     public bool IsLoad
  131.     {
  132.         get
  133.         {
  134.             return Video.IsLoad && Audio.IsLoad;
  135.         }
  136.     }
  137.  
  138.     public event Action<ArVideo> OnLoad;
  139.     public ArVideo (int defaultVersion, int ID, int IDVVideo, int IDAudio)
  140.     {
  141.         Version = defaultVersion;
  142.         Id = ID;
  143.         Video = new RemoteResouce (Version, IDVVideo);
  144.         Audio = new RemoteResouce (Version, IDAudio);
  145.     }
  146.     public void Load ()
  147.     {
  148.         Action<RemoteResouce> loadHandler = (ar) =>
  149.         {
  150.             if (IsLoad)
  151.             {
  152.                 if (OnLoad != null)
  153.                 {
  154.                     OnLoad (this);
  155.                 }
  156.             }
  157.         };
  158.  
  159.         Video.OnLoad += loadHandler;
  160.         Audio.OnLoad += loadHandler;
  161.  
  162.         Video.Load (Version);
  163.         Audio.Load (Version);
  164.     }
  165. }
  166.  
  167. [System.Serializable]
  168. public class RemoteResouce
  169. {
  170.     public int Id;
  171.     public string Url;
  172.     public string LocalPath;
  173.  
  174.     public RemoteResouce (int version, int id)
  175.     {
  176.         Id = id;
  177.         Load (version);
  178.     }
  179.  
  180.     public bool IsLoad { get; set; }
  181.  
  182.     public event Action<RemoteResouce> OnLoad;
  183.  
  184.     public void Load (int version)
  185.     {
  186.         foreach (var file in Directory.GetFiles (RemoteResouceManager.CachePath))
  187.         {
  188.             if (file.EndsWith (GetFileName (version)))
  189.             {
  190.                 if (file.Contains (GetFileName (version)))
  191.                 {
  192.                     Debug.Log ("Load from cache" + Url);
  193.                     IsLoad = true;
  194.                     LocalPath = RemoteResouceManager.CachePath + GetFileName (version);
  195.                     if (OnLoad != null)
  196.                     {
  197.                         OnLoad (this);
  198.                     }
  199.                     return;
  200.                 }
  201.                 else
  202.                 {
  203.                     File.Delete (file);
  204.                 }
  205.             }
  206.         }
  207.  
  208.         Debug.Log ("Load from server" + Url);
  209.  
  210.         UnityWebRequest www = UnityWebRequest.Get (Url);
  211.         var request = www.SendWebRequest ();
  212.         if (www.isHttpError || www.isNetworkError)
  213.             return;
  214.         request.completed += (operation) =>
  215.         {
  216.  
  217.             using (var cacheFile = File.Open (RemoteResouceManager.CachePath + GetFileName (version), FileMode.Create))
  218.             {
  219.                 cacheFile.Write (www.downloadHandler.data, 0, www.downloadHandler.data.Length);
  220.             }
  221.  
  222.             IsLoad = true;
  223.             LocalPath = RemoteResouceManager.CachePath + GetFileName (version);
  224.             if (OnLoad != null)
  225.             {
  226.                 OnLoad (this);
  227.             }
  228.         };
  229.     }
  230.  
  231.     private string GetFileName (int version)
  232.     {
  233.         return string.Intern ("resource_" + Id + "_" + version + Path.GetExtension (Url));
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment