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