Skip to content

Commit b932a06

Browse files
authored
Merge pull request #3053 from ZeroK-RTS/fix_map_synchro
fix map download and scynchronizaiton
2 parents afab839 + d1cbef6 commit b932a06

5 files changed

Lines changed: 273 additions & 273 deletions

File tree

AutoRegistrator/AutoRegistrator.cs

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.Entity;
34
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
@@ -153,31 +154,66 @@ private void OnRapidChanged()
153154

154155
private void SynchronizeMapsFromSpringFiles()
155156
{
156-
if (GlobalConst.Mode == ModeType.Live)
157-
{
158-
var db = new ZkDataContext();
157+
if (GlobalConst.Mode != ModeType.Live) return;
159158

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+
}
162169

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);
164174

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)
166194
{
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;
172198

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+
}
175206

176-
try
207+
if (registered && !hasLocal)
208+
{
209+
try { File.Copy(srcFile, contentFile); }
210+
catch (Exception ex)
177211
{
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+
}
180214
}
215+
216+
try { File.Delete(srcFile); } catch { }
181217
}
182218
}
183219

AutoRegistrator/WebFolderSync.cs

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,62 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5-
using System.Linq;
65
using System.Net;
7-
using System.Text.RegularExpressions;
6+
using Newtonsoft.Json;
7+
using ZkData;
88

99
namespace ZeroKWeb
1010
{
1111
public class WebFolderSyncer
1212
{
13-
private string urlSource;
14-
15-
public WebFolderSyncer(string urlSource = "http://api.springfiles.com/files/maps/")
16-
{
17-
this.urlSource = urlSource;
18-
}
19-
2013
public List<string> GetFileList()
2114
{
15+
var ret = new List<string>();
2216
using (var wc = new WebClient())
2317
{
24-
var str = wc.DownloadString(urlSource);
25-
return (from Match m in Regex.Matches(str, "<a href=\"([^\"]+)\">([^<]+)</a>")
26-
where m.Success && m.Groups[1].Value == m.Groups[2].Value
27-
select m.Groups[1].Value).ToList();
18+
var url = $"{GlobalConst.SpringfilesBaseUrl}json.php?category=map&springname=*&logical=or&nosensitive=on&limit=1000000&offset=0";
19+
var json = wc.DownloadString(url);
20+
var entries = JsonConvert.DeserializeObject<List<SpringfilesEntry>>(json);
21+
if (entries == null) return ret;
22+
foreach (var e in entries)
23+
{
24+
if (e?.path == "maps" && !string.IsNullOrEmpty(e.filename) && e.mirrors != null && e.mirrors.Length > 0)
25+
ret.Add(e.filename);
26+
}
2827
}
28+
return ret;
2929
}
3030

31-
public void DownloadFile(string targetFolder, string file)
31+
public bool DownloadFile(string targetFolder, string file)
3232
{
3333
if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder);
3434
var targetFile = Path.Combine(targetFolder, file);
35-
if (!File.Exists(targetFile))
35+
if (File.Exists(targetFile)) return true;
36+
37+
var tempFile = Path.GetTempFileName();
38+
using (var wc = new WebClient())
3639
{
37-
using (var wc = new WebClient())
40+
try
3841
{
39-
var tempFile = Path.GetTempFileName();
40-
try
41-
{
42-
wc.DownloadFile(urlSource + file, tempFile);
43-
}
44-
catch (Exception ex)
45-
{
46-
Trace.TraceWarning("Download of file {0} failed: {1}", file, ex.Message);
47-
File.Delete(tempFile);
48-
}
49-
try
50-
{
51-
File.Move(tempFile, targetFile);
52-
}
53-
catch { }
42+
wc.DownloadFile($"{GlobalConst.SpringfilesBaseUrl}files/maps/{file}", tempFile);
43+
File.Move(tempFile, targetFile);
44+
return true;
45+
}
46+
catch (Exception ex)
47+
{
48+
Trace.TraceWarning("Springfiles download of {0} failed: {1}", file, ex.Message);
49+
try { File.Delete(tempFile); } catch { }
50+
return false;
5451
}
5552
}
5653
}
5754

58-
59-
public void SynchronizeFolders(string targetFolder)
55+
private class SpringfilesEntry
6056
{
61-
if (!Directory.Exists(targetFolder)) Directory.CreateDirectory(targetFolder);
62-
foreach (var file in GetFileList())
63-
{
64-
DownloadFile(targetFolder, file);
65-
}
57+
public string filename { get; set; }
58+
public string path { get; set; }
59+
public string[] mirrors { get; set; }
6660
}
67-
6861
}
6962
}

Shared/PlasmaShared/GlobalConst.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ static void SetMode(ModeType newMode)
7979
break;
8080
}
8181

82-
if (IsLongAfterSteam) DefaultDownloadMirrors = new[] { BaseSiteUrl +"/content/%t/%f" };
83-
8482
ResourceBaseUrl = string.Format("{0}/Resources", BaseSiteUrl);
8583
BaseImageUrl = string.Format("{0}/img/", BaseSiteUrl);
8684
SelfUpdaterBaseUrl = string.Format("{0}/lobby", BaseSiteUrl);
@@ -98,6 +96,8 @@ static void SetMode(ModeType newMode)
9896
public static string BaseImageUrl;
9997
public static string BaseSiteUrl;
10098

99+
public const string SpringfilesBaseUrl = "https://springfiles.springrts.com/";
100+
101101
public static string DefaultZkTag => Mode == ModeType.Live ? "zk:stable" : "zk:test";
102102
public static string DefaultChobbyTag => Mode == ModeType.Live ? "zkmenu:stable" : "zkmenu:test";
103103

@@ -235,7 +235,6 @@ static void SetMode(ModeType newMode)
235235

236236
public static string ResourceBaseUrl;
237237
public static string SelfUpdaterBaseUrl;
238-
public static string[] DefaultDownloadMirrors = {};
239238
public static string LobbyServerHost;
240239
public static int LobbyServerPort;
241240
public static bool LobbyServerUpdateSpectatorsInstantly = false;

0 commit comments

Comments
 (0)