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