Skip to content

Commit

Permalink
Added HTML page
Browse files Browse the repository at this point in the history
  • Loading branch information
Animadoria committed Aug 6, 2018
1 parent 16271b0 commit 6a2e055
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 26 deletions.
80 changes: 55 additions & 25 deletions SolderPackager/Program.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;

namespace SolderPackager
{
class Program
{
static void Main(string[] args)
{

WriteCenter("Solder Packager app");
WriteCenter("by StormMC team");
WriteCenter("It is recommended that you maximize your console window.", 1);
WriteCenter("Please, specify the path to your mods folder.", 2);
Console.Write("\n Path to mods folder> ");
var path = Console.ReadLine();

var path = Console.ReadLine();

try
{
DirectoryInfo d = new DirectoryInfo(path);
{
DirectoryInfo d = new DirectoryInfo(path);
if (d.GetFiles("*.jar").Length == 0)
{
WriteCenter("There are no jar files in that directory!!");
}
WriteCenter("There are no jar files in that directory!!");

else
{
WriteCenter($"All jar files in directory {path}: ", 1);

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

Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Expand All @@ -48,32 +46,64 @@ static void Main(string[] args)
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Black;
WriteCenter("OK, working on that...", 2);

Console.ResetColor();
var zipPath = path + "/solderzips/";
Directory.CreateDirectory(zipPath);
foreach (var item in d.GetFiles("*.jar"))

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>";
var endhtml = @"</tr></tbody><!--Table body--></table><!--Table--></div></div></body></html>";

int modNumber = 0;

var files = d.GetFiles("*.jar").OrderBy(fi => fi.Name).ToArray();
foreach (var item in files)
{
var ms = new MemoryStream();
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
ms.Seek(0, SeekOrigin.Begin);
File.WriteAllBytes($"{zipPath}{Path.GetFileNameWithoutExtension(item.Name)}.zip", ms.ToArray());
}
var content = Path.GetFileNameWithoutExtension(item.Name);
var replaced = (content.Replace('+', '-')).Replace('_', '-').Replace("'", "");
Regex regexDash = new Regex(@"(?<=-).*$");
Match matchDash = regexDash.Match(replaced);
modNumber++;
try
{
var foldername = replaced.Trim(new char[] { '+', '-', '_', '\'' }).Replace(matchDash.Value, "").Trim(new char[] { '+', '-', '_', '\'' });
Directory.CreateDirectory(zipPath + foldername.ToLower());
var ms = new MemoryStream();
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
ms.Seek(0, SeekOrigin.Begin);
File.WriteAllBytes($"{zipPath}{foldername.ToLower()}/{replaced.ToLower()}.zip", ms.ToArray());
basehtml += $"<tr> <th scope=\"row\">{modNumber}</th> <td>{foldername}</td><td>{matchDash.Value}</td></tr>";
}
catch
{
basehtml += $"<tr> <th scope=\"row\">{modNumber}</th> <td>{Path.GetFileNameWithoutExtension(item.Name)}</td><td>(no more info available)</td></tr>";
WriteCenter($"Couldn't figure out a folder to the mod {item.Name}", 2);
WriteCenter("so it's in the root folder of your solderzips.");
var ms = new MemoryStream();
var archive = new ZipArchive(ms, ZipArchiveMode.Create, true);
archive.CreateEntryFromFile(item.FullName, $"mods/{item.Name}");
ms.Seek(0, SeekOrigin.Begin);
File.WriteAllBytes($"{zipPath}{Path.GetFileNameWithoutExtension(item.Name).ToLower()}.zip", ms.ToArray());
}
}

basehtml += endhtml;
File.WriteAllText(zipPath + "report.html", basehtml);
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ForegroundColor = ConsoleColor.Black;
WriteCenter("Success!", 2);
Console.BackgroundColor = ConsoleColor.Green;
WriteCenter("Keep in mind that file names are the same from the");
WriteCenter("jar files, but with the zip extension. You might");
WriteCenter("Keep in mind that file names are the same from the", 1);
WriteCenter("jar files, but with the zip extension. You might");
WriteCenter("need to change the file names.");
}
}
WriteCenter("A HTML file has been created with information.", 1);
}
}
catch
{
WriteCenter("There is no directory by that path!");
}

{
WriteCenter("There is no directory by that path!");
}
}

internal static void WriteCenter(string value, int skipline = 0)
Expand Down
102 changes: 102 additions & 0 deletions SolderPackager/ProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Text;
using System.Threading;

/// <summary>
/// An ASCII progress bar
/// </summary>
public class ProgressBar : IDisposable, IProgress<double>
{
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";

private readonly Timer timer;

private double currentProgress = 0;
private string currentText = string.Empty;
private bool disposed = false;
private int animationIndex = 0;

public ProgressBar()
{
timer = new Timer(TimerHandler);

// A progress bar is only for temporary display in a console window.
// If the console output is redirected to a file, draw nothing.
// Otherwise, we'll end up with a lot of garbage in the target file.
if (!Console.IsOutputRedirected)
{
ResetTimer();
}
}

public void Report(double value)
{
// Make sure value is in [0..1] range
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}

private void TimerHandler(object state)
{
lock (timer)
{
if (disposed) return;

int progressBlockCount = (int)(currentProgress * blockCount);
int percent = (int)(currentProgress * 100);
string text = string.Format("[{0}{1}] {2,3}% {3}",
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
percent,
animation[animationIndex++ % animation.Length]);
UpdateText(text);

ResetTimer();
}
}

private void UpdateText(string text)
{
// Get length of common portion
int commonPrefixLength = 0;
int commonLength = Math.Min(currentText.Length, text.Length);
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
{
commonPrefixLength++;
}

// Backtrack to the first differing character
StringBuilder outputBuilder = new StringBuilder();
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);

// Output new suffix
outputBuilder.Append(text.Substring(commonPrefixLength));

// If the new text is shorter than the old one: delete overlapping characters
int overlapCount = currentText.Length - text.Length;
if (overlapCount > 0)
{
outputBuilder.Append(' ', overlapCount);
outputBuilder.Append('\b', overlapCount);
}

Console.Write(outputBuilder);
currentText = text;
}

private void ResetTimer()
{
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
}

public void Dispose()
{
lock (timer)
{
disposed = true;
UpdateText(string.Empty);
}
}

}
5 changes: 4 additions & 1 deletion SolderPackager/SolderPackager.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RuntimeIdentifiers>win10-x64;osx.10.10-x64
</RuntimeIdentifiers>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="ProgressBar.cs" />
</ItemGroup>
</Project>

0 comments on commit 6a2e055

Please sign in to comment.