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