| | 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; |
| 759 | 15 | | public List<MappingPair> contents = new List<MappingPair>(); |
| 759 | 16 | | public Dictionary<string, string> fileToHash = new Dictionary<string, string>(); |
| | 17 | |
|
| | 18 | | public override string ToString() |
| | 19 | | { |
| 0 | 20 | | string result = $"baseUrl: {baseUrl}\n"; |
| | 21 | |
|
| 0 | 22 | | foreach (var pair in contents) |
| | 23 | | { |
| 0 | 24 | | result += $"file: {pair.file} ... hash: {pair.hash}\n"; |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | return result; |
| | 28 | | } |
| | 29 | |
|
| 1518 | 30 | | public ContentProvider() { } |
| | 31 | |
|
| 0 | 32 | | public ContentProvider(ContentProvider toCopy) |
| | 33 | | { |
| 0 | 34 | | baseUrl = toCopy.baseUrl; |
| 0 | 35 | | contents = new List<MappingPair>(toCopy.contents); |
| 0 | 36 | | fileToHash = new Dictionary<string, string>(toCopy.fileToHash); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public MappingPair GetMappingForHash(string hash) |
| | 40 | | { |
| 0 | 41 | | if (contents == null) |
| | 42 | | { |
| 0 | 43 | | return null; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | return contents.FirstOrDefault((x) => x.hash == hash); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | //todo: thread this |
| | 50 | | public void BakeHashes() |
| | 51 | | { |
| 594 | 52 | | if (contents == null) |
| | 53 | | { |
| 506 | 54 | | return; |
| | 55 | | } |
| | 56 | |
|
| 88 | 57 | | if (VERBOSE) |
| | 58 | | { |
| 0 | 59 | | Debug.Log("Baking hashes..."); |
| | 60 | | } |
| | 61 | |
|
| 88 | 62 | | if (fileToHash == null) |
| | 63 | | { |
| 0 | 64 | | fileToHash = new Dictionary<string, string>(contents.Count); |
| | 65 | | } |
| | 66 | |
|
| 690 | 67 | | for (int i = 0; i < contents.Count; i++) |
| | 68 | | { |
| 257 | 69 | | MappingPair m = contents[i]; |
| 257 | 70 | | var key = m.file.ToLower(); |
| 257 | 71 | | if (fileToHash.ContainsKey(key)) |
| | 72 | | { |
| 2 | 73 | | Debug.Log($"Hash key: {key} already exists in the map"); |
| 2 | 74 | | continue; |
| | 75 | | } |
| 255 | 76 | | fileToHash.Add(key, m.hash); |
| | 77 | |
|
| 255 | 78 | | if (VERBOSE) |
| | 79 | | { |
| 0 | 80 | | Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}"); |
| | 81 | | } |
| | 82 | | } |
| 88 | 83 | | } |
| | 84 | |
|
| | 85 | | public virtual bool HasContentsUrl(string url) |
| | 86 | | { |
| 89 | 87 | | if (string.IsNullOrEmpty(url)) |
| | 88 | | { |
| 0 | 89 | | return false; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | #if UNITY_EDITOR |
| 89 | 93 | | if (HasTestSchema(url)) |
| | 94 | | { |
| 74 | 95 | | return true; |
| | 96 | | } |
| | 97 | | #endif |
| 15 | 98 | | if (fileToHash == null) |
| | 99 | | { |
| 0 | 100 | | return false; |
| | 101 | | } |
| | 102 | |
|
| 15 | 103 | | return fileToHash.ContainsKey(url.ToLower()); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public virtual string GetContentsUrl(string url) |
| | 107 | | { |
| 39 | 108 | | string result = ""; |
| | 109 | |
|
| 39 | 110 | | if (TryGetContentsUrl(url, out result)) |
| | 111 | | { |
| 39 | 112 | | return result; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | return null; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public virtual bool TryGetContentsUrl_Raw(string url, out string result) |
| | 119 | | { |
| 117 | 120 | | url = url.ToLower(); |
| 117 | 121 | | result = url; |
| | 122 | |
|
| | 123 | | #if UNITY_EDITOR |
| 117 | 124 | | if (HasTestSchema(url)) |
| | 125 | | { |
| 82 | 126 | | return true; |
| | 127 | | } |
| | 128 | | #endif |
| 35 | 129 | | if (fileToHash != null) |
| | 130 | | { |
| 35 | 131 | | if (!fileToHash.ContainsKey(url)) |
| | 132 | | { |
| 0 | 133 | | Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!"); |
| 0 | 134 | | return false; |
| | 135 | | } |
| | 136 | |
|
| 35 | 137 | | result = fileToHash[url]; |
| 35 | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 0 | 141 | | result = url; |
| | 142 | | } |
| | 143 | |
|
| 35 | 144 | | return true; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | public virtual bool TryGetContentsUrl(string url, out string result) |
| | 148 | | { |
| 54 | 149 | | url = url.ToLower(); |
| 54 | 150 | | result = url; |
| | 151 | |
|
| 54 | 152 | | if (HasTestSchema(url)) |
| | 153 | | { |
| 54 | 154 | | return true; |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | if (fileToHash != null) |
| | 158 | | { |
| 0 | 159 | | if (!fileToHash.ContainsKey(url)) |
| | 160 | | { |
| 0 | 161 | | Debug.LogError($"GetContentsUrl >>> File {url} not found!!!"); |
| 0 | 162 | | return false; |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | result = baseUrl + fileToHash[url]; |
| 0 | 166 | | } |
| | 167 | | else |
| | 168 | | { |
| 0 | 169 | | result = baseUrl + url; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | if (VERBOSE) |
| | 173 | | { |
| 0 | 174 | | Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}"); |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | return true; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | public bool HasTestSchema(string url) |
| | 181 | | { |
| | 182 | | #if UNITY_EDITOR |
| 260 | 183 | | if (url.StartsWith("file://")) |
| | 184 | | { |
| 210 | 185 | | return true; |
| | 186 | | } |
| | 187 | |
|
| 50 | 188 | | if (url.StartsWith(TestAssetsUtils.GetPath())) |
| | 189 | | { |
| 0 | 190 | | return true; |
| | 191 | | } |
| | 192 | | #endif |
| 50 | 193 | | return false; |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public bool TryGetContentHash(string file, out string result) |
| | 197 | | { |
| 305 | 198 | | result = string.Empty; |
| | 199 | |
|
| 305 | 200 | | if (fileToHash == null) |
| | 201 | | { |
| 0 | 202 | | result = file; |
| 0 | 203 | | return true; |
| | 204 | | } |
| | 205 | |
|
| 305 | 206 | | if (fileToHash.TryGetValue(file.ToLower(), out result)) |
| | 207 | | { |
| 64 | 208 | | return true; |
| | 209 | | } |
| | 210 | |
|
| 241 | 211 | | return false; |
| | 212 | | } |
| | 213 | | } |
| | 214 | | } |