Skip to content

Commit 052b3b8

Browse files
committed
Add --level option to special logging level. And more attempts to fix corrupt jar creation.
1 parent be70073 commit 052b3b8

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/main/java/de/oceanlabs/mcp/mcinjector/MCInjector.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.file.FileSystem;
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
10+
import java.util.Locale;
1011
import java.util.Optional;
1112
import java.util.logging.FileHandler;
1213
import java.util.logging.Handler;
@@ -142,6 +143,23 @@ public String valuePattern()
142143
return null;
143144
}
144145
};
146+
private static ValueConverter<Level> LEVEL_ARG = new ValueConverter<Level>()
147+
{
148+
public Level convert( String value )
149+
{
150+
return Level.parse(value.toUpperCase(Locale.ENGLISH));
151+
}
152+
153+
public Class<Level> valueType()
154+
{
155+
return Level.class;
156+
}
157+
158+
public String valuePattern()
159+
{
160+
return null;
161+
}
162+
};
145163

146164
public static void main(String[] args) throws Exception
147165
{
@@ -157,6 +175,7 @@ public static void main(String[] args) throws Exception
157175
OptionSpec<Path> accOut = parser.accepts("accOut").withRequiredArg().withValuesConvertedBy(PATH_ARG);
158176
OptionSpec<Path> ctr = parser.accepts("ctr") .withRequiredArg().withValuesConvertedBy(PATH_ARG);
159177
OptionSpec<Path> ctrOut = parser.accepts("ctrOut").withRequiredArg().withValuesConvertedBy(PATH_ARG);
178+
OptionSpec<Level> logLvl = parser.accepts("level") .withRequiredArg().withValuesConvertedBy(LEVEL_ARG).defaultsTo(Level.INFO);
160179
OptionSpec<LVTNaming> lvt = parser.accepts("lvt").withRequiredArg().ofType(LVTNaming.class).defaultsTo(LVTNaming.STRIP);
161180

162181
try
@@ -175,7 +194,7 @@ else if (o.has(ver))
175194
}
176195

177196
MCInjector.LOG.setUseParentHandlers(false);
178-
MCInjector.LOG.setLevel(Level.ALL);
197+
MCInjector.LOG.setLevel(o.valueOf(logLvl));
179198

180199
LOG.info(MCInjector.VERSION);
181200
LOG.info("Input: " + o.valueOf(in));

src/main/java/de/oceanlabs/mcp/mcinjector/MCInjectorImpl.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Set;
15+
import java.util.jar.JarEntry;
1516
import java.util.logging.Level;
1617
import java.util.stream.Collectors;
1718
import java.util.zip.ZipEntry;
@@ -83,13 +84,15 @@ private void processJar(Path inFile, Path outFile) throws IOException
8384
Set<String> entries = new HashSet<>();
8485
try (ZipInputStream inJar = new ZipInputStream(Files.newInputStream(inFile)))
8586
{
86-
try (ZipOutputStream outJar = new ZipOutputStream(outFile == null ? new ByteArrayOutputStream() : Files.newOutputStream(outFile, StandardOpenOption.CREATE)))
87+
try (ZipOutputStream outJar = new ZipOutputStream(outFile == null ? new ByteArrayOutputStream() : Files.newOutputStream(outFile)))
8788
{
8889
for (ZipEntry entry = inJar.getNextEntry(); entry != null; entry = inJar.getNextEntry())
8990
{
91+
String entryName = entry.getName();
92+
9093
if (entry.isDirectory())
9194
{
92-
outJar.putNextEntry(entry);
95+
outJar.putNextEntry(new JarEntry(entryName));
9396
outJar.closeEntry();
9497
continue;
9598
}
@@ -109,8 +112,6 @@ private void processJar(Path inFile, Path outFile) throws IOException
109112

110113
byte[] entryData = entryBuffer.toByteArray();
111114

112-
String entryName = entry.getName();
113-
114115
boolean mojang = entryName.startsWith("net/minecraft/") || entryName.startsWith("com/mojang/");
115116

116117
if (entryName.endsWith(".class") && mojang) //TODO: Remove this hardcoding? SRG input? process all?
@@ -136,9 +137,9 @@ private void processJar(Path inFile, Path outFile) throws IOException
136137

137138
if (!abstractParameters.isEmpty() && !entries.contains("fernflower_abstract_parameter_names.txt"))
138139
{
139-
ZipEntry entry = new ZipEntry("fernflower_abstract_parameter_names.txt");
140-
entry.setTime(0); //Stabilize time.
141-
outJar.putNextEntry(entry);
140+
ZipEntry newEntry = new ZipEntry("fernflower_abstract_parameter_names.txt");
141+
newEntry.setTime(0); //Stabilize time.
142+
outJar.putNextEntry(newEntry);
142143
for (String key : abstractParameters.keySet().stream().sorted().collect(Collectors.toList()))
143144
{
144145
outJar.write(key.getBytes(StandardCharsets.UTF_8));//class method desc
@@ -148,8 +149,6 @@ private void processJar(Path inFile, Path outFile) throws IOException
148149
}
149150
outJar.closeEntry();
150151
}
151-
152-
outJar.flush();
153152
}
154153
}
155154
}

0 commit comments

Comments
 (0)