Skip to content

Commit 6a2e055

Browse files
committed
Added HTML page
1 parent 16271b0 commit 6a2e055

File tree

3 files changed

+161
-26
lines changed

3 files changed

+161
-26
lines changed

SolderPackager/Program.cs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
using System;
22
using System.IO;
33
using System.IO.Compression;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
46

57
namespace SolderPackager
68
{
79
class Program
810
{
911
static void Main(string[] args)
1012
{
11-
1213
WriteCenter("Solder Packager app");
1314
WriteCenter("by StormMC team");
1415
WriteCenter("It is recommended that you maximize your console window.", 1);
1516
WriteCenter("Please, specify the path to your mods folder.", 2);
1617
Console.Write("\n Path to mods folder> ");
17-
var path = Console.ReadLine();
18-
18+
var path = Console.ReadLine();
19+
1920
try
20-
{
21-
DirectoryInfo d = new DirectoryInfo(path);
21+
{
22+
DirectoryInfo d = new DirectoryInfo(path);
2223
if (d.GetFiles("*.jar").Length == 0)
23-
{
24-
WriteCenter("There are no jar files in that directory!!");
25-
}
24+
WriteCenter("There are no jar files in that directory!!");
25+
2626
else
2727
{
2828
WriteCenter($"All jar files in directory {path}: ", 1);
2929

3030
foreach (var item in d.GetFiles("*.jar"))
31-
{
3231
WriteCenter($"{Path.GetFileNameWithoutExtension(item.Name)} - {item.CreationTime.ToShortDateString()} {item.CreationTime.ToShortTimeString()}");
33-
}
3432

3533
Console.BackgroundColor = ConsoleColor.Red;
3634
Console.ForegroundColor = ConsoleColor.Black;
@@ -48,32 +46,64 @@ static void Main(string[] args)
4846
Console.BackgroundColor = ConsoleColor.Yellow;
4947
Console.ForegroundColor = ConsoleColor.Black;
5048
WriteCenter("OK, working on that...", 2);
49+
5150
Console.ResetColor();
5251
var zipPath = path + "/solderzips/";
5352
Directory.CreateDirectory(zipPath);
54-
foreach (var item in d.GetFiles("*.jar"))
53+
54+
var basehtml = "<!DOCTYPE html><html><head> <title>Solder Packager Information</title> <meta charset=\"utf-8\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"> <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script> <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script></head><body> <div class=\"container\"> <div class=\"row text-center\" style=\"\"> <h1>Solder Packager report<p>&nbsp; </p></h1> <table id=\"tablePreview\" class=\"table\"> <thead> <tr> <th>No</th> <th style=\"text-align: center;\">Mod Name</th> <th style=\"text-align: center;\">Mod Version</th> </tr></thead> <tbody>";
55+
var endhtml = @"</tr></tbody><!--Table body--></table><!--Table--></div></div></body></html>";
56+
57+
int modNumber = 0;
58+
59+
var files = d.GetFiles("*.jar").OrderBy(fi => fi.Name).ToArray();
60+
foreach (var item in files)
5561
{
56-
var ms = new MemoryStream();
57-
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
58-
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
59-
ms.Seek(0, SeekOrigin.Begin);
60-
File.WriteAllBytes($"{zipPath}{Path.GetFileNameWithoutExtension(item.Name)}.zip", ms.ToArray());
61-
}
62+
var content = Path.GetFileNameWithoutExtension(item.Name);
63+
var replaced = (content.Replace('+', '-')).Replace('_', '-').Replace("'", "");
64+
Regex regexDash = new Regex(@"(?<=-).*$");
65+
Match matchDash = regexDash.Match(replaced);
66+
modNumber++;
67+
try
68+
{
69+
var foldername = replaced.Trim(new char[] { '+', '-', '_', '\'' }).Replace(matchDash.Value, "").Trim(new char[] { '+', '-', '_', '\'' });
70+
Directory.CreateDirectory(zipPath + foldername.ToLower());
71+
var ms = new MemoryStream();
72+
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
73+
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
74+
ms.Seek(0, SeekOrigin.Begin);
75+
File.WriteAllBytes($"{zipPath}{foldername.ToLower()}/{replaced.ToLower()}.zip", ms.ToArray());
76+
basehtml += $"<tr> <th scope=\"row\">{modNumber}</th> <td>{foldername}</td><td>{matchDash.Value}</td></tr>";
77+
}
78+
catch
79+
{
80+
basehtml += $"<tr> <th scope=\"row\">{modNumber}</th> <td>{Path.GetFileNameWithoutExtension(item.Name)}</td><td>(no more info available)</td></tr>";
81+
WriteCenter($"Couldn't figure out a folder to the mod {item.Name}", 2);
82+
WriteCenter("so it's in the root folder of your solderzips.");
83+
var ms = new MemoryStream();
84+
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
85+
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
86+
ms.Seek(0, SeekOrigin.Begin);
87+
File.WriteAllBytes($"{zipPath}{Path.GetFileNameWithoutExtension(item.Name).ToLower()}.zip", ms.ToArray());
88+
}
89+
}
6290

91+
basehtml += endhtml;
92+
File.WriteAllText(zipPath + "report.html", basehtml);
6393
Console.BackgroundColor = ConsoleColor.DarkGreen;
6494
Console.ForegroundColor = ConsoleColor.Black;
6595
WriteCenter("Success!", 2);
6696
Console.BackgroundColor = ConsoleColor.Green;
67-
WriteCenter("Keep in mind that file names are the same from the");
68-
WriteCenter("jar files, but with the zip extension. You might");
97+
WriteCenter("Keep in mind that file names are the same from the", 1);
98+
WriteCenter("jar files, but with the zip extension. You might");
6999
WriteCenter("need to change the file names.");
70-
}
71-
}
100+
WriteCenter("A HTML file has been created with information.", 1);
101+
}
102+
}
72103
catch
73-
{
74-
WriteCenter("There is no directory by that path!");
75-
}
76-
104+
{
105+
WriteCenter("There is no directory by that path!");
106+
}
77107
}
78108

79109
internal static void WriteCenter(string value, int skipline = 0)

SolderPackager/ProgressBar.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading;
4+
5+
/// <summary>
6+
/// An ASCII progress bar
7+
/// </summary>
8+
public class ProgressBar : IDisposable, IProgress<double>
9+
{
10+
private const int blockCount = 10;
11+
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
12+
private const string animation = @"|/-\";
13+
14+
private readonly Timer timer;
15+
16+
private double currentProgress = 0;
17+
private string currentText = string.Empty;
18+
private bool disposed = false;
19+
private int animationIndex = 0;
20+
21+
public ProgressBar()
22+
{
23+
timer = new Timer(TimerHandler);
24+
25+
// A progress bar is only for temporary display in a console window.
26+
// If the console output is redirected to a file, draw nothing.
27+
// Otherwise, we'll end up with a lot of garbage in the target file.
28+
if (!Console.IsOutputRedirected)
29+
{
30+
ResetTimer();
31+
}
32+
}
33+
34+
public void Report(double value)
35+
{
36+
// Make sure value is in [0..1] range
37+
value = Math.Max(0, Math.Min(1, value));
38+
Interlocked.Exchange(ref currentProgress, value);
39+
}
40+
41+
private void TimerHandler(object state)
42+
{
43+
lock (timer)
44+
{
45+
if (disposed) return;
46+
47+
int progressBlockCount = (int)(currentProgress * blockCount);
48+
int percent = (int)(currentProgress * 100);
49+
string text = string.Format("[{0}{1}] {2,3}% {3}",
50+
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
51+
percent,
52+
animation[animationIndex++ % animation.Length]);
53+
UpdateText(text);
54+
55+
ResetTimer();
56+
}
57+
}
58+
59+
private void UpdateText(string text)
60+
{
61+
// Get length of common portion
62+
int commonPrefixLength = 0;
63+
int commonLength = Math.Min(currentText.Length, text.Length);
64+
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
65+
{
66+
commonPrefixLength++;
67+
}
68+
69+
// Backtrack to the first differing character
70+
StringBuilder outputBuilder = new StringBuilder();
71+
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);
72+
73+
// Output new suffix
74+
outputBuilder.Append(text.Substring(commonPrefixLength));
75+
76+
// If the new text is shorter than the old one: delete overlapping characters
77+
int overlapCount = currentText.Length - text.Length;
78+
if (overlapCount > 0)
79+
{
80+
outputBuilder.Append(' ', overlapCount);
81+
outputBuilder.Append('\b', overlapCount);
82+
}
83+
84+
Console.Write(outputBuilder);
85+
currentText = text;
86+
}
87+
88+
private void ResetTimer()
89+
{
90+
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
91+
}
92+
93+
public void Dispose()
94+
{
95+
lock (timer)
96+
{
97+
disposed = true;
98+
UpdateText(string.Empty);
99+
}
100+
}
101+
102+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<RuntimeIdentifiers>win10-x64;osx.10.10-x64
55
</RuntimeIdentifiers>
66
<OutputType>Exe</OutputType>
77
<TargetFramework>netcoreapp2.0</TargetFramework>
88
</PropertyGroup>
9+
<ItemGroup>
10+
<Compile Remove="ProgressBar.cs" />
11+
</ItemGroup>
912
</Project>

0 commit comments

Comments
 (0)