| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using System.Linq; |
| | 6 | | using MappingPair = DCL.ContentServerUtils.MappingPair; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class ContentProvider |
| | 11 | | { |
| | 12 | | public static bool VERBOSE = false; |
| | 13 | |
|
| | 14 | | public string baseUrl; |
| | 15 | | public string baseUrlBundles; |
| | 16 | | public string assetBundlesBaseUrl; |
| | 17 | | public string assetBundlesVersion; |
| | 18 | | public string sceneCid; |
| | 19 | |
|
| 826 | 20 | | public List<MappingPair> contents = new (); |
| 826 | 21 | | public Dictionary<string, string> fileToHash = new (); |
| 826 | 22 | | public HashSet<string> assetBundles = new (); |
| | 23 | | public bool assetBundlesFetched; |
| | 24 | |
|
| | 25 | | public override string ToString() |
| | 26 | | { |
| 0 | 27 | | string result = $"baseUrl: {baseUrl}\n"; |
| | 28 | |
|
| 0 | 29 | | foreach (var pair in contents) { result += $"file: {pair.file} ... hash: {pair.hash}\n"; } |
| | 30 | |
|
| 0 | 31 | | return result; |
| | 32 | | } |
| | 33 | |
|
| 1652 | 34 | | public ContentProvider() { } |
| | 35 | |
|
| 0 | 36 | | public ContentProvider(ContentProvider toCopy) |
| | 37 | | { |
| 0 | 38 | | baseUrl = toCopy.baseUrl; |
| 0 | 39 | | contents = new List<MappingPair>(toCopy.contents); |
| 0 | 40 | | fileToHash = new Dictionary<string, string>(toCopy.fileToHash); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public MappingPair GetMappingForHash(string hash) |
| | 44 | | { |
| 0 | 45 | | if (contents == null) { return null; } |
| | 46 | |
|
| 0 | 47 | | return contents.FirstOrDefault((x) => x.hash == hash); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | //todo: thread this |
| | 51 | | public void BakeHashes() |
| | 52 | | { |
| 850 | 53 | | if (contents == null) { return; } |
| | 54 | |
|
| 52 | 55 | | if (VERBOSE) { Debug.Log("Baking hashes..."); } |
| | 56 | |
|
| 52 | 57 | | if (fileToHash == null) { fileToHash = new Dictionary<string, string>(contents.Count); } |
| | 58 | |
|
| 380 | 59 | | for (int i = 0; i < contents.Count; i++) |
| | 60 | | { |
| 138 | 61 | | MappingPair m = contents[i]; |
| 138 | 62 | | var key = m.file.ToLower(); |
| | 63 | |
|
| 138 | 64 | | if (fileToHash.ContainsKey(key)) |
| | 65 | | { |
| 2 | 66 | | Debug.Log($"Hash key: {key} already exists in the map"); |
| 2 | 67 | | continue; |
| | 68 | | } |
| | 69 | |
|
| 136 | 70 | | fileToHash.Add(key, m.hash); |
| | 71 | |
|
| 136 | 72 | | if (VERBOSE) { Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}"); |
| | 73 | | } |
| 52 | 74 | | } |
| | 75 | |
|
| | 76 | | public virtual bool HasContentsUrl(string url) |
| | 77 | | { |
| 52 | 78 | | if (string.IsNullOrEmpty(url)) { return false; } |
| | 79 | |
|
| 52 | 80 | | url = url.ToLower(); |
| | 81 | |
|
| | 82 | | #if UNITY_EDITOR |
| 104 | 83 | | if (HasTestSchema(url)) { return true; } |
| | 84 | | #endif |
| 0 | 85 | | if (fileToHash == null) { return false; } |
| | 86 | |
|
| 0 | 87 | | return fileToHash.ContainsKey(url); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public virtual string GetContentsUrl(string url) |
| | 91 | | { |
| 40 | 92 | | string result = ""; |
| 40 | 93 | | url = url.ToLower(); |
| | 94 | |
|
| 80 | 95 | | if (TryGetContentsUrl(url, out result)) { return result; } |
| | 96 | |
|
| 0 | 97 | | return null; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | public virtual bool TryGetContentsUrl_Raw(string url, out string result) |
| | 101 | | { |
| 198 | 102 | | url = url.ToLower(); |
| 198 | 103 | | result = url; |
| | 104 | |
|
| | 105 | | #if UNITY_EDITOR |
| 318 | 106 | | if (HasTestSchema(url)) { return true; } |
| | 107 | | #endif |
| 78 | 108 | | if (fileToHash != null) |
| | 109 | | { |
| 78 | 110 | | if (!fileToHash.ContainsKey(url)) |
| | 111 | | { |
| 2 | 112 | | Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!"); |
| 2 | 113 | | return false; |
| | 114 | | } |
| | 115 | |
|
| 76 | 116 | | result = fileToHash[url]; |
| | 117 | | } |
| 0 | 118 | | else { result = url; } |
| | 119 | |
|
| 76 | 120 | | return true; |
| | 121 | | } |
| | 122 | |
|
| | 123 | | public virtual bool TryGetContentsUrl(string url, out string result) |
| | 124 | | { |
| 110 | 125 | | url = url.ToLower(); |
| 110 | 126 | | result = url; |
| | 127 | |
|
| 166 | 128 | | if (HasTestSchema(url)) { return true; } |
| | 129 | |
|
| 54 | 130 | | if (fileToHash != null) |
| | 131 | | { |
| 54 | 132 | | if (!fileToHash.ContainsKey(url)) |
| | 133 | | { |
| 39 | 134 | | if (VERBOSE) { Debug.LogError($"GetContentsUrl >>> File {url} not found!!!"); } |
| | 135 | |
|
| 39 | 136 | | return false; |
| | 137 | | } |
| | 138 | |
|
| 15 | 139 | | result = baseUrl + fileToHash[url]; |
| | 140 | | } |
| 0 | 141 | | else { result = baseUrl + url; } |
| | 142 | |
|
| 15 | 143 | | if (VERBOSE) { Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}"); } |
| | 144 | |
|
| 15 | 145 | | return true; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public bool HasTestSchema(string url) |
| | 149 | | { |
| 360 | 150 | | url = url.ToLower(); |
| | 151 | |
|
| | 152 | | #if UNITY_EDITOR |
| 588 | 153 | | if (url.StartsWith("file://")) { return true; } |
| | 154 | |
|
| 132 | 155 | | if (url.StartsWith(TestAssetsUtils.GetPath())) { return true; } |
| | 156 | | #endif |
| 132 | 157 | | return false; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public bool TryGetContentHash(string file, out string result) |
| | 161 | | { |
| 0 | 162 | | file = file.ToLower(); |
| 0 | 163 | | result = string.Empty; |
| | 164 | |
|
| 0 | 165 | | if (fileToHash == null) |
| | 166 | | { |
| 0 | 167 | | result = file; |
| 0 | 168 | | return true; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | if (fileToHash.TryGetValue(file.ToLower(), out result)) { return true; } |
| | 172 | |
|
| 0 | 173 | | return false; |
| | 174 | | } |
| | 175 | | } |
| | 176 | | } |