Skip to content
This repository was archived by the owner on Oct 5, 2019. It is now read-only.

Commit 1398812

Browse files
author
Zaczero
committed
Merge branch 'master' of https://github.com/Zaczero/SharpLoader
2 parents a9c5734 + fb80a92 commit 1398812

File tree

6 files changed

+1056
-41
lines changed

6 files changed

+1056
-41
lines changed

SharpLoader/MainForm.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Windows.Forms;
34

45
namespace SharpLoader
@@ -9,6 +10,12 @@ public partial class MainForm : Form
910

1011
public MainForm()
1112
{
13+
if (Program.DragDropPaths.Count == 0 &&
14+
!File.Exists(Program.ConfigPath))
15+
{
16+
new SelectForm().ShowDialog();
17+
}
18+
1219
InitializeComponent();
1320

1421
AuthorText.Value2 = Program.Author;
@@ -61,6 +68,7 @@ private void CompileClick(object sender, EventArgs e)
6168

6269
private void CloseClick(object sender, EventArgs e)
6370
{
71+
Program.CleanTemp();
6472
Application.Exit();
6573
}
6674
}

SharpLoader/Program.cs

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public static class Program
4545
private const string ConfigFileName = "SharpLoader.ini";
4646
private static readonly string MyPath = Process.GetCurrentProcess().MainModule.FileName;
4747
private static readonly string MyDirectory = Path.GetDirectoryName(MyPath);
48-
private static readonly string ConfigPath = Path.Combine(MyDirectory, ConfigFileName);
48+
public static string ConfigPath = Path.Combine(MyDirectory, ConfigFileName);
4949

50+
public static List<string> DragDropPaths;
5051
public static int Seed = -1;
5152
public static string Hash;
5253

5354
private static MainForm _form;
54-
private static List<string> _dragDropPaths;
5555
private static bool _outToConsole;
5656

5757
[STAThread]
@@ -62,7 +62,7 @@ public static void Main(string[] args)
6262
// Hide cmd
6363
ShowWindow(GetConsoleWindow(), SW_HIDE);
6464

65-
_dragDropPaths = new List<string>();
65+
DragDropPaths = new List<string>();
6666

6767
var cmdMode = false;
6868

@@ -74,32 +74,20 @@ public static void Main(string[] args)
7474
// Directory
7575
if (File.GetAttributes(args[i]).HasFlag(FileAttributes.Directory))
7676
{
77-
_dragDropPaths.AddRange(GetFilesFromDirectory(args[i]));
77+
DragDropPaths.AddRange(GetFilesFromDirectory(args[i]));
7878
}
7979
// File
8080
else
8181
{
8282
// Zip
8383
if (Path.GetExtension(args[i]) == ".zip")
8484
{
85-
var zipBytes = File.ReadAllBytes(args[i]);
86-
var zipHash = MD5.Create().ComputeHash(zipBytes);
87-
88-
var tempDir = Path.GetTempPath() + ByteArrayToString(zipHash);
89-
90-
if (Directory.Exists(tempDir))
91-
{
92-
Directory.Delete(tempDir, true);
93-
}
94-
95-
ZipFile.ExtractToDirectory(args[i], tempDir);
96-
97-
_dragDropPaths.AddRange(ScanDir(tempDir));
85+
ProcessZip(args[i]);
9886
}
9987
// Normal
10088
else
10189
{
102-
_dragDropPaths.Add(args[i]);
90+
DragDropPaths.Add(args[i]);
10391
}
10492
}
10593
}
@@ -136,25 +124,41 @@ public static void Main(string[] args)
136124
Seed = new Random(Environment.TickCount).Next(0, int.MaxValue);
137125
}
138126

127+
// Show UI
139128
if (!cmdMode)
140129
{
141130
Application.EnableVisualStyles();
142131
Application.SetCompatibleTextRenderingDefault(false);
143132
Application.Run(_form = new MainForm());
144-
145-
return;
146133
}
147-
148134
// Show cmd
149-
ShowWindow(GetConsoleWindow(), SW_SHOW);
150-
_outToConsole = true;
151-
152-
var compileResult = Compile();
153-
if (compileResult != 0)
135+
else
154136
{
155-
Console.ReadKey();
137+
// Data file not found
138+
if (!File.Exists(ConfigPath))
139+
{
140+
WinApi.WritePrivateProfileString("SharpLoader", "References", "", ConfigPath);
141+
WinApi.WritePrivateProfileString("SharpLoader", "Directory", "", ConfigPath);
142+
WinApi.WritePrivateProfileString("SharpLoader", "Sources", "", ConfigPath);
143+
WinApi.WritePrivateProfileString("SharpLoader", "Output", "SharpLoader", ConfigPath);
144+
WinApi.WritePrivateProfileString("SharpLoader", "Arguments", "/platform:anycpu32bitpreferred", ConfigPath);
145+
WinApi.WritePrivateProfileString("SharpLoader", "AutoRun", "false", ConfigPath);
146+
147+
Environment.Exit(1);
148+
}
149+
150+
ShowWindow(GetConsoleWindow(), SW_SHOW);
151+
_outToConsole = true;
152+
153+
var compileResult = Compile();
154+
if (compileResult != 0)
155+
{
156+
Console.ReadKey();
157+
}
158+
159+
CleanTemp();
160+
Environment.Exit(compileResult);
156161
}
157-
Environment.Exit(compileResult);
158162
}
159163

160164
public static int Compile()
@@ -175,21 +179,11 @@ public static int Compile()
175179
Out("");
176180

177181
// Data file not found
178-
if (!File.Exists(ConfigFileName))
182+
if (!File.Exists(ConfigPath))
179183
{
180184
Console.ForegroundColor = ConsoleColor.Red;
181185
Out($"-=: Config file not found ({ConfigFileName})");
182186

183-
WinApi.WritePrivateProfileString("SharpLoader", "References", "", ConfigPath);
184-
WinApi.WritePrivateProfileString("SharpLoader", "Directory", "", ConfigPath);
185-
WinApi.WritePrivateProfileString("SharpLoader", "Sources", "", ConfigPath);
186-
WinApi.WritePrivateProfileString("SharpLoader", "Output", "SharpLoader", ConfigPath);
187-
WinApi.WritePrivateProfileString("SharpLoader", "Arguments", "/platform:anycpu32bitpreferred", ConfigPath);
188-
WinApi.WritePrivateProfileString("SharpLoader", "AutoRun", "false", ConfigPath);
189-
190-
Console.ForegroundColor = ConsoleColor.Red;
191-
Out($"-=: Default config file generated");
192-
193187
return 1;
194188
}
195189

@@ -218,9 +212,9 @@ public static int Compile()
218212
var autoRun = autoRunReadSb.ToString().Equals("true", StringComparison.OrdinalIgnoreCase);
219213

220214
// Drag n Drop
221-
if (_dragDropPaths.Count != 0)
215+
if (DragDropPaths.Count != 0)
222216
{
223-
userSourcePaths = _dragDropPaths.ToArray();
217+
userSourcePaths = DragDropPaths.ToArray();
224218
}
225219
// Read from config
226220
else
@@ -450,6 +444,44 @@ public static void Out(object obj)
450444
}
451445
}
452446

447+
public static void ProcessZip(string path)
448+
{
449+
var zipBytes = File.ReadAllBytes(path);
450+
var zipHash = MD5.Create().ComputeHash(zipBytes);
451+
452+
var tempDir = Path.GetTempPath() + @"SharpLoader\" + ByteArrayToString(zipHash);
453+
454+
if (Directory.Exists(tempDir))
455+
{
456+
Directory.Delete(tempDir, true);
457+
}
458+
459+
ZipFile.ExtractToDirectory(path, tempDir);
460+
461+
var unzipFiles = ScanDir(tempDir);
462+
463+
foreach (var unzipFile in unzipFiles)
464+
{
465+
if (unzipFile.EndsWith(ConfigFileName))
466+
{
467+
ConfigPath = unzipFile;
468+
}
469+
else
470+
{
471+
DragDropPaths.Add(unzipFile);
472+
}
473+
}
474+
}
475+
476+
public static void CleanTemp()
477+
{
478+
var path = Path.GetTempPath() + "SharpLoader";
479+
if (Directory.Exists(path))
480+
{
481+
Directory.Delete(path, true);
482+
}
483+
}
484+
453485
private static IEnumerable<string> GetFilesFromDirectory(string directory)
454486
{
455487
var dir = new DirectoryInfo(directory);

SharpLoader/SelectForm.Designer.cs

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)