Skip to content

Commit 492563f

Browse files
Joo200mcmonkey4eva
andauthored
Allow plain text files as Content to use. (#1)
* Allow Plain Text * cleanup (see pr comment) * missed format fix --------- Co-authored-by: Alex "mcmonkey" Goodwin <git_commits@alexgoodwin.dev>
1 parent 088857c commit 492563f

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

SharpDenizenTools/MetaHandlers/MetaDocsLoader.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static MetaDocs DownloadAll(string[] sources)
7070
docs.LoadErrors.Add($"Internal exception while reading extra data - {ex.GetType().FullName} ... see bot console for details.");
7171
Console.Error.WriteLine($"Error: {ex}");
7272
}
73-
ConcurrentDictionary<string, ZipArchive> zips = new();
73+
ConcurrentDictionary<string, object> files = new();
7474
List<ManualResetEvent> resets = new();
7575
foreach (string src in sources)
7676
{
@@ -80,13 +80,21 @@ public static MetaDocs DownloadAll(string[] sources)
8080
{
8181
try
8282
{
83-
ZipArchive zip = DownloadZip(webClient, src);
84-
zips[src] = zip;
83+
byte[] data = DownloadData(webClient, src);
84+
// Note: backup check based on PKWARE zip header (PK followed by 2 control bytes, usually 0x03 0x04)
85+
if (src.EndsWith(".zip") || (data[0] == 0x50 && data[1] == 0x4b && data[1] < 0x20 && data[2] < 0x20))
86+
{
87+
files[src] = new ZipArchive(new MemoryStream(data));
88+
}
89+
else
90+
{
91+
files[src] = StringConversionHelper.UTF8Encoding.GetString(data, 0, data.Length);
92+
}
8593
}
8694
catch (Exception ex)
8795
{
88-
Console.Error.WriteLine($"Zip download exception {ex}");
89-
docs.LoadErrors.Add($"Zip download error: {ex.GetType().Name}: {ex.Message}");
96+
Console.Error.WriteLine($"Source download exception {ex}");
97+
docs.LoadErrors.Add($"Source download error: {ex.GetType().Name}: {ex.Message}");
9098
}
9199
evt.Set();
92100
});
@@ -95,11 +103,21 @@ public static MetaDocs DownloadAll(string[] sources)
95103
{
96104
evt.WaitOne();
97105
}
98-
foreach (string src in sources)
106+
foreach ((string src, object file) in files)
99107
{
100108
try
101109
{
102-
(int, string, string)[] fullLines = ReadLines(zips[src]);
110+
(int, string, string)[] fullLines;
111+
if (file is ZipArchive zip)
112+
{
113+
fullLines = ReadLines(zip);
114+
}
115+
else
116+
{
117+
List<(int, string, string)> lines = new();
118+
SeparateDataLines(lines, src, (file as string).Split('\n'));
119+
fullLines = lines.ToArray();
120+
}
103121
LoadDataFromLines(docs, src, fullLines);
104122
}
105123
catch (Exception ex)
@@ -229,7 +247,7 @@ public static void ReadGuides(MetaDocs docs, HttpClient client)
229247
}
230248

231249
/// <summary>Download a zip file from a URL.</summary>
232-
public static ZipArchive DownloadZip(HttpClient webClient, string url)
250+
public static byte[] DownloadData(HttpClient webClient, string url)
233251
{
234252
byte[] zipDataBytes;
235253
if (AlternateZipSourcer != null)
@@ -240,8 +258,7 @@ public static ZipArchive DownloadZip(HttpClient webClient, string url)
240258
{
241259
zipDataBytes = webClient.GetByteArrayAsync(url).Result;
242260
}
243-
MemoryStream zipDataStream = new(zipDataBytes);
244-
return new ZipArchive(zipDataStream);
261+
return zipDataBytes;
245262
}
246263

247264
/// <summary>Read lines of meta docs from Java files in a zip.</summary>

0 commit comments

Comments
 (0)