Pro_Unit

RemoteResouceManager_SO

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