|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Data.Entity; |
3 | 4 | using System.Diagnostics; |
4 | 5 | using System.IO; |
5 | 6 | using System.Linq; |
@@ -153,31 +154,66 @@ private void OnRapidChanged() |
153 | 154 |
|
154 | 155 | private void SynchronizeMapsFromSpringFiles() |
155 | 156 | { |
156 | | - if (GlobalConst.Mode == ModeType.Live) |
157 | | - { |
158 | | - var db = new ZkDataContext(); |
| 157 | + if (GlobalConst.Mode != ModeType.Live) return; |
159 | 158 |
|
160 | | - var processedFiles = db.ResourceContentFiles.Where(x => x.Resource.TypeID == ResourceType.Map).Select(x => x.FileName.ToLower()).Distinct().ToLookup(x => x); |
161 | | - var triedFiles = db.SpringFilesUnitsyncAttempts.Select(x => x.FileName.ToLower()).Distinct().ToLookup(x => x); |
| 159 | + HashSet<string> processedFiles, triedFiles; |
| 160 | + using (var db = new ZkDataContext()) |
| 161 | + { |
| 162 | + processedFiles = new HashSet<string>( |
| 163 | + db.ResourceContentFiles.AsNoTracking() |
| 164 | + .Where(x => x.Resource.TypeID == ResourceType.Map) |
| 165 | + .Select(x => x.FileName.ToLower())); |
| 166 | + triedFiles = new HashSet<string>( |
| 167 | + db.SpringFilesUnitsyncAttempts.AsNoTracking().Select(x => x.FileName.ToLower())); |
| 168 | + } |
162 | 169 |
|
163 | | - var webSyncer = new WebFolderSyncer(); |
| 170 | + var webSyncer = new WebFolderSyncer(); |
| 171 | + var autoregMaps = Path.Combine(Paths.WritableDirectory, "maps"); |
| 172 | + var contentMaps = Path.Combine(sitePath, "content", "maps"); |
| 173 | + if (!Directory.Exists(contentMaps)) Directory.CreateDirectory(contentMaps); |
164 | 174 |
|
165 | | - foreach (var file in webSyncer.GetFileList()) |
| 175 | + foreach (var file in webSyncer.GetFileList()) |
| 176 | + { |
| 177 | + var fileLc = file.ToLower(); |
| 178 | + var alreadyRegistered = processedFiles.Contains(fileLc); |
| 179 | + var alreadyAttempted = triedFiles.Contains(fileLc); |
| 180 | + var contentFile = Path.Combine(contentMaps, file); |
| 181 | + var hasLocal = File.Exists(contentFile); |
| 182 | + |
| 183 | + // skip cases: |
| 184 | + // - registered AND local copy present → nothing to do |
| 185 | + // - previously attempted but never made it into DB → known failure, don't retry |
| 186 | + if (alreadyRegistered && hasLocal) continue; |
| 187 | + if (alreadyAttempted && !alreadyRegistered) continue; |
| 188 | + |
| 189 | + if (!webSyncer.DownloadFile(autoregMaps, file)) continue; |
| 190 | + var srcFile = Path.Combine(autoregMaps, file); |
| 191 | + |
| 192 | + var registered = alreadyRegistered; |
| 193 | + if (!alreadyRegistered) |
166 | 194 | { |
167 | | - if (processedFiles.Contains(file.ToLower()) || triedFiles.Contains(file.ToLower())) continue; |
168 | | - |
169 | | - webSyncer.DownloadFile(Path.Combine(Paths.WritableDirectory,"maps"), file); |
170 | | - |
171 | | - UnitSyncer.Scan(); |
| 195 | + var results = UnitSyncer.Scan(); |
| 196 | + var ourResult = results?.FirstOrDefault(r => string.Equals(r.ResourceInfo?.ArchiveName, file, StringComparison.OrdinalIgnoreCase)); |
| 197 | + registered = ourResult != null && ourResult.Status != UnitSyncer.ResourceFileStatus.RegistrationError; |
172 | 198 |
|
173 | | - db.SpringFilesUnitsyncAttempts.Add(new SpringFilesUnitsyncAttempt() { FileName = file }); |
174 | | - db.SaveChanges(); |
| 199 | + using (var db = new ZkDataContext()) |
| 200 | + { |
| 201 | + db.SpringFilesUnitsyncAttempts.Add(new SpringFilesUnitsyncAttempt() { FileName = file }); |
| 202 | + db.SaveChanges(); |
| 203 | + } |
| 204 | + triedFiles.Add(fileLc); |
| 205 | + } |
175 | 206 |
|
176 | | - try |
| 207 | + if (registered && !hasLocal) |
| 208 | + { |
| 209 | + try { File.Copy(srcFile, contentFile); } |
| 210 | + catch (Exception ex) |
177 | 211 | { |
178 | | - File.Delete(Path.Combine(Paths.WritableDirectory, "maps", file)); |
179 | | - } catch (Exception ex) { } |
| 212 | + Trace.TraceWarning("Copy {0} to content/maps failed: {1}", file, ex.Message); |
| 213 | + } |
180 | 214 | } |
| 215 | + |
| 216 | + try { File.Delete(srcFile); } catch { } |
181 | 217 | } |
182 | 218 | } |
183 | 219 |
|
|
0 commit comments