-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
168 lines (144 loc) · 6.02 KB
/
Copy pathProgram.cs
File metadata and controls
168 lines (144 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
// Silence warnings that annoy me
#pragma warning disable CS8600
#pragma warning disable CS8602
#pragma warning disable CS8604
namespace EtheriumModPackager
{
class Program
{
static void Main(string[] args)
{
string directoryPath;
string mainDllNameArg = null;
// Handle command-line arguments or ask user for path
if (args.Length > 0)
{
directoryPath = args[0];
if (!Directory.Exists(directoryPath))
{
Console.WriteLine($"Directory '{directoryPath}' does not exist.");
return;
}
if (args.Length > 1)
mainDllNameArg = args[1]; // optional main DLL filename
}
else
{
// Prompt user for directory
Console.Write("Enter path to the mod directory: ");
directoryPath = Console.ReadLine();
while (!Directory.Exists(directoryPath))
{
Console.WriteLine("Directory does not exist. Try again.");
directoryPath = Console.ReadLine();
}
}
//Fix a bug where paths such as "./bin/net35/" would cause mods to not have version numbers in the file name
directoryPath = Path.GetFullPath(directoryPath);
// Find all DLLs in the directory
string[] dllFiles = Directory.GetFiles(directoryPath, "*.dll", SearchOption.TopDirectoryOnly);
if (dllFiles.Length == 0)
{
Console.WriteLine("No .dll file found in directory. Cannot create .etheriummod file.");
return;
}
string dllFile;
// If optional DLL argument provided, use it directly
if (!string.IsNullOrEmpty(mainDllNameArg))
{
dllFile = Path.Combine(directoryPath, mainDllNameArg);
if (!File.Exists(dllFile))
{
Console.WriteLine($"Specified DLL '{mainDllNameArg}' does not exist in the directory.");
return;
}
}
else
{
// Find what the main DLL file is
if (dllFiles.Length == 1)
{
dllFile = dllFiles[0];
}
else
{
// Try to find DLL matching the folder name
string folderName = Path.GetFileName(directoryPath);
dllFile = dllFiles.FirstOrDefault(f =>
Path.GetFileNameWithoutExtension(f).Equals(folderName, StringComparison.OrdinalIgnoreCase));
// If no match, prompt the user
if (dllFile == null)
{
Console.WriteLine("Multiple DLLs found, but none match the folder name. Please select the main DLL:");
for (int i = 0; i < dllFiles.Length; i++)
{
Console.WriteLine($"{i + 1}: {Path.GetFileName(dllFiles[i])}");
}
int choice = 0;
while (choice < 1 || choice > dllFiles.Length)
{
Console.Write("Enter the number of the DLL to use: ");
string input = Console.ReadLine();
int.TryParse(input, out choice);
}
dllFile = dllFiles[choice - 1];
}
}
}
string dllName = Path.GetFileNameWithoutExtension(dllFile);
// Attempt to read version, omit if unavailable
string versionString = "";
try
{
Assembly assembly = Assembly.LoadFile(dllFile);
Version ver = assembly.GetName().Version;
if (ver != null)
versionString = $"_{ver.Major}_{ver.Minor}_{ver.Build}";
}
catch
{
// silently ignore if version cannot be read
}
string modFileName = $"{dllName}{versionString}.etheriummod";
string modFilePath = Path.Combine(directoryPath, modFileName);
Console.WriteLine($"Creating mod file: {modFilePath}");
// 6. Compress directory contents into .etheriummod
using (FileStream fsOut = File.Create(modFilePath))
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
zipStream.SetLevel(9); // maximum compression
CompressDirectory(directoryPath, zipStream, directoryPath, modFilePath);
}
Console.WriteLine("Mod file created successfully!");
}
private static void CompressDirectory(string dirPath, ZipOutputStream zipStream, string basePath, string modFilePath)
{
foreach (string filePath in Directory.GetFiles(dirPath))
{
// Skip the mod file itself
if (Path.GetFullPath(filePath) == Path.GetFullPath(modFilePath))
continue;
string entryName = Path.GetRelativePath(basePath, filePath).Replace("\\", "/");
ZipEntry entry = new ZipEntry(entryName)
{
DateTime = DateTime.Now
};
zipStream.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filePath))
{
fs.CopyTo(zipStream);
}
zipStream.CloseEntry();
}
foreach (string subDir in Directory.GetDirectories(dirPath))
{
CompressDirectory(subDir, zipStream, basePath, modFilePath);
}
}
}
}
#pragma warning disable CS8604
#pragma warning disable CS8602
#pragma warning restore CS8600