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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* <p>Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.gui;
import java.awt.Desktop;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import cuchaz.enigma.Enigma;
import cuchaz.enigma.EnigmaProject;
import cuchaz.enigma.analysis.ClassImplementationsTreeNode;
import cuchaz.enigma.analysis.ClassInheritanceTreeNode;
import cuchaz.enigma.analysis.ClassReferenceTreeNode;
import cuchaz.enigma.analysis.EntryReference;
import cuchaz.enigma.analysis.FieldReferenceTreeNode;
import cuchaz.enigma.analysis.IndexTreeBuilder;
import cuchaz.enigma.analysis.MethodImplementationsTreeNode;
import cuchaz.enigma.analysis.MethodInheritanceTreeNode;
import cuchaz.enigma.analysis.MethodReferenceTreeNode;
import cuchaz.enigma.analysis.StructureTreeNode;
import cuchaz.enigma.analysis.StructureTreeOptions;
import cuchaz.enigma.api.DataInvalidationEvent;
import cuchaz.enigma.api.DataInvalidationListener;
import cuchaz.enigma.api.service.ObfuscationTestService;
import cuchaz.enigma.api.service.ProjectService;
import cuchaz.enigma.api.view.GuiView;
import cuchaz.enigma.api.view.entry.EntryReferenceView;
import cuchaz.enigma.classhandle.ClassHandle;
import cuchaz.enigma.classhandle.ClassHandleProvider;
import cuchaz.enigma.classprovider.ClasspathClassProvider;
import cuchaz.enigma.gui.config.NetConfig;
import cuchaz.enigma.gui.config.UiConfig;
import cuchaz.enigma.gui.dialog.ProgressDialog;
import cuchaz.enigma.gui.newabstraction.EntryValidation;
import cuchaz.enigma.gui.stats.StatsGenerator;
import cuchaz.enigma.gui.stats.StatsMember;
import cuchaz.enigma.gui.util.History;
import cuchaz.enigma.network.ClientPacketHandler;
import cuchaz.enigma.network.EnigmaClient;
import cuchaz.enigma.network.EnigmaServer;
import cuchaz.enigma.network.IntegratedEnigmaServer;
import cuchaz.enigma.network.Message;
import cuchaz.enigma.network.ServerPacketHandler;
import cuchaz.enigma.network.packet.EntryChangeC2SPacket;
import cuchaz.enigma.network.packet.LoginC2SPacket;
import cuchaz.enigma.network.packet.Packet;
import cuchaz.enigma.source.DecompiledClassSource;
import cuchaz.enigma.source.DecompilerService;
import cuchaz.enigma.source.SourceIndex;
import cuchaz.enigma.source.Token;
import cuchaz.enigma.translation.TranslateResult;
import cuchaz.enigma.translation.Translator;
import cuchaz.enigma.translation.mapping.EntryChange;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.EntryRemapper;
import cuchaz.enigma.translation.mapping.EntryUtil;
import cuchaz.enigma.translation.mapping.MappingDelta;
import cuchaz.enigma.translation.mapping.ResolutionStrategy;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingParseException;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.HashEntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.Entry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.utils.Utils;
import cuchaz.enigma.utils.validation.PrintValidatable;
import cuchaz.enigma.utils.validation.ValidationContext;
public class GuiController implements ClientPacketHandler, GuiView, DataInvalidationListener {
private final Gui gui;
public final Enigma enigma;
public EnigmaProject project;
private IndexTreeBuilder indexTreeBuilder;
private Path loadedMappingPath;
private MappingFormat loadedMappingFormat = MappingFormat.ENIGMA_DIRECTORY;
private ClassHandleProvider chp;
private ClassHandle tokenHandle;
private EnigmaClient client;
private EnigmaServer server;
private History<EntryReference<Entry<?>, Entry<?>>> referenceHistory;
public GuiController(Gui gui, Enigma enigma) {
this.gui = gui;
this.enigma = enigma;
}
@Override
public EnigmaProject getProject() {
return project;
}
@Override
public JFrame getFrame() {
return gui.getFrame();
}
public boolean isDirty() {
return project != null && project.getMapper().isDirty();
}
public CompletableFuture<Void> openJar(final List<Path> jarPaths) {
this.gui.onStartOpenJar();
return ProgressDialog.runOffThread(gui.getFrame(), progress -> {
project = enigma.openJars(jarPaths, new ClasspathClassProvider(), progress, false);
project.addDataInvalidationListener(this);
indexTreeBuilder = new IndexTreeBuilder(project.getJarIndex());
chp = new ClassHandleProvider(project, UiConfig.getDecompiler().service);
SwingUtilities.invokeLater(() -> {
for (ProjectService projectService : enigma.getServices().get(ProjectService.TYPE)) {
projectService.onProjectOpen(project);
}
gui.onFinishOpenJar(getFileNames(jarPaths));
refreshClasses();
});
});
}
private static String getFileNames(List<Path> jarPaths) {
return jarPaths.stream()
.map(Path::getFileName)
.map(Object::toString)
.collect(Collectors.joining(", "));
}
public void closeJar() {
for (ProjectService projectService : enigma.getServices().get(ProjectService.TYPE)) {
projectService.onProjectClose(project);
}
this.chp.destroy();
this.chp = null;
this.project = null;
this.gui.onCloseJar();
}
@ApiStatus.Internal
public CompletableFuture<Void> openMappings(MappingFormat format, Path path, boolean useMappingIo) {
System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false");
return openMappings(format, path);
}
public CompletableFuture<Void> openMappings(MappingFormat format, Path path) {
if (project == null) {
return CompletableFuture.completedFuture(null);
}
gui.setMappingsFile(path);
return ProgressDialog.runOffThread(gui.getFrame(), progress -> {
try {
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
project.setMappings(format.read(path, progress, saveParameters, project.getJarIndex()));
loadedMappingFormat = format;
loadedMappingPath = path;
refreshClasses();
project.invalidateData(DataInvalidationEvent.InvalidationType.JAVADOC);
} catch (MappingParseException e) {
JOptionPane.showMessageDialog(gui.getFrame(), e.getMessage());
}
});
}
@Override
public void openMappings(EntryTree<EntryMapping> mappings) {
if (project == null) {
return;
}
project.setMappings(mappings);
refreshClasses();
project.invalidateData(DataInvalidationEvent.InvalidationType.JAVADOC);
}
public MappingFormat getLoadedMappingFormat() {
return loadedMappingFormat;
}
public CompletableFuture<Void> saveMappings(Path path) {
return saveMappings(path, loadedMappingFormat);
}
@ApiStatus.Internal
public CompletableFuture<Void> saveMappings(Path path, MappingFormat format, boolean useMappingIo) {
System.getProperties().setProperty("enigma.use_mappingio", useMappingIo ? "true" : "false");
return saveMappings(path, format);
}
/**
* Saves the mappings, with a dialog popping up, showing the progress.
*
* <p>Notice the returned completable future has to be completed by
* {@link SwingUtilities#invokeLater(Runnable)}. Hence, do not try to
* join on the future in gui, but rather call {@code thenXxx} methods.
*
* @param path the path of the save
* @param format the format of the save
* @return the future of saving
*/
public CompletableFuture<Void> saveMappings(Path path, MappingFormat format) {
if (project == null) {
return CompletableFuture.completedFuture(null);
}
return ProgressDialog.runOffThread(this.gui.getFrame(), progress -> {
EntryRemapper mapper = project.getMapper();
MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters();
MappingDelta<EntryMapping> delta = mapper.takeMappingDelta();
boolean saveAll = !path.equals(loadedMappingPath);
loadedMappingFormat = format;
loadedMappingPath = path;
if (saveAll) {
format.write(mapper.getObfToDeobf(), path, progress, saveParameters);
} else {
format.write(mapper.getObfToDeobf(), delta, path, progress, saveParameters);
}
});
}
public void closeMappings() {
if (project == null) {
return;
}
project.setMappings(null);
this.gui.setMappingsFile(null);
refreshClasses();
project.invalidateData(DataInvalidationEvent.InvalidationType.JAVADOC);
}
public void reloadAll() {
List<Path> jarPaths = this.project.getJarPaths();
MappingFormat loadedMappingFormat = this.loadedMappingFormat;
Path loadedMappingPath = this.loadedMappingPath;
this.closeJar();
CompletableFuture<Void> f = this.openJar(jarPaths);
if (loadedMappingFormat != null && loadedMappingPath != null) {
f.whenComplete((v, t) -> this.openMappings(loadedMappingFormat, loadedMappingPath));
}
}
public void reloadMappings() {
MappingFormat loadedMappingFormat = this.loadedMappingFormat;
Path loadedMappingPath = this.loadedMappingPath;
if (loadedMappingFormat != null && loadedMappingPath != null) {
this.closeMappings();
this.openMappings(loadedMappingFormat, loadedMappingPath);
}
}
public CompletableFuture<Void> dropMappings() {
if (project == null) {
return CompletableFuture.completedFuture(null);
}
return ProgressDialog.runOffThread(this.gui.getFrame(), progress -> project.dropMappings(progress));
}
public CompletableFuture<Void> exportSource(final Path path) {
if (project == null) {
return CompletableFuture.completedFuture(null);
}
return ProgressDialog.runOffThread(this.gui.getFrame(), progress -> {
EnigmaProject.JarExport jar = project.exportRemappedJar(progress);
jar.decompileStream(progress, chp.getDecompilerService(), EnigmaProject.DecompileErrorStrategy.TRACE_AS_SOURCE).forEach(source -> {
try {
source.writeTo(source.resolvePath(path));
} catch (IOException e) {
e.printStackTrace();
}
});
});
}
public CompletableFuture<Void> exportJar(final Path path) {
if (project == null) {
return CompletableFuture.completedFuture(null);
}
return ProgressDialog.runOffThread(this.gui.getFrame(), progress -> {
EnigmaProject.JarExport jar = project.exportRemappedJar(progress);
jar.write(path, progress);
});
}
public void setTokenHandle(ClassHandle handle) {
if (tokenHandle != null) {
tokenHandle.close();
}
tokenHandle = handle;
}
public ClassHandle getTokenHandle() {
return tokenHandle;
}
public ReadableToken getReadableToken(Token token) {
if (tokenHandle == null) {
return null;
}
try {
return tokenHandle.getSource().get().map(DecompiledClassSource::getIndex).map(index -> new ReadableToken(index.getLineNumber(token.start), index.getColumnNumber(token.start), index.getColumnNumber(token.end))).unwrapOr(null);
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
@Override
@Nullable
public EntryReferenceView getCursorReference() {
return gui.getCursorReference();
}
/**
* Navigates to the declaration with respect to navigation history.
*
* @param entry the entry whose declaration will be navigated to
*/
public void openDeclaration(Entry<?> entry) {
if (entry == null) {
throw new IllegalArgumentException("Entry cannot be null!");
}
openReference(EntryReference.declaration(entry, entry.getName()));
}
/**
* Navigates to the reference with respect to navigation history.
*
* @param reference the reference
*/
public void openReference(EntryReference<Entry<?>, Entry<?>> reference) {
if (reference == null) {
throw new IllegalArgumentException("Reference cannot be null!");
}
if (this.referenceHistory == null) {
this.referenceHistory = new History<>(reference);
} else {
if (!reference.equals(this.referenceHistory.getCurrent())) {
this.referenceHistory.push(reference);
}
}
this.gui.showReference(reference);
}
public List<Token> getTokensForReference(DecompiledClassSource source, EntryReference<Entry<?>, Entry<?>> reference) {
EntryRemapper mapper = this.project.getMapper();
SourceIndex index = source.getIndex();
return mapper.getObfResolver().resolveReference(reference, ResolutionStrategy.RESOLVE_CLOSEST).stream().flatMap(r -> index.getReferenceTokens(r).stream()).sorted().toList();
}
public void openPreviousReference() {
if (hasPreviousReference()) {
this.gui.showReference(referenceHistory.goBack());
}
}
public boolean hasPreviousReference() {
return referenceHistory != null && referenceHistory.canGoBack();
}
public void openNextReference() {
if (hasNextReference()) {
this.gui.showReference(referenceHistory.goForward());
}
}
public boolean hasNextReference() {
return referenceHistory != null && referenceHistory.canGoForward();
}
public void navigateTo(Entry<?> entry) {
if (!project.isRenamable(entry)) {
// entry is not in the jar. Ignore it
return;
}
openDeclaration(entry);
}
public void navigateTo(EntryReference<Entry<?>, Entry<?>> reference) {
if (!project.isRenamable(reference.getLocationClassEntry())) {
return;
}
openReference(reference);
}
public void refreshClasses() {
if (project == null) {
return;
}
List<ClassEntry> obfClasses = new ArrayList<>();
List<ClassEntry> deobfClasses = new ArrayList<>();
this.addSeparatedClasses(obfClasses, deobfClasses);
this.gui.setObfClasses(obfClasses);
this.gui.setDeobfClasses(deobfClasses);
}
public void addSeparatedClasses(List<ClassEntry> obfClasses, List<ClassEntry> deobfClasses) {
EntryRemapper mapper = project.getMapper();
Collection<ClassEntry> classes = project.getJarIndex().getEntryIndex().getClasses();
Stream<ClassEntry> visibleClasses = classes.stream().filter(entry -> !entry.isInnerClass());
visibleClasses.forEach(entry -> {
if (gui.isSingleClassTree()) {
deobfClasses.add(entry);
return;
}
TranslateResult<ClassEntry> result = mapper.extendedDeobfuscate(entry);
ClassEntry deobfEntry = result.getValue();
List<ObfuscationTestService> obfService = enigma.getServices().get(ObfuscationTestService.TYPE);
boolean obfuscated = result.isObfuscated() && deobfEntry.equals(entry);
if (obfuscated && !obfService.isEmpty()) {
if (obfService.stream().anyMatch(service -> service.testDeobfuscated(entry))) {
obfuscated = false;
}
}
if (obfuscated) {
obfClasses.add(entry);
} else {
deobfClasses.add(entry);
}
});
}
public StructureTreeNode getClassStructure(ClassEntry entry, StructureTreeOptions options) {
StructureTreeNode rootNode = new StructureTreeNode(this.project, entry, entry);
rootNode.load(this.project, options);
return rootNode;
}
public ClassInheritanceTreeNode getClassInheritance(ClassEntry entry) {
Translator translator = project.getMapper().getDeobfuscator();
ClassInheritanceTreeNode rootNode = indexTreeBuilder.buildClassInheritance(translator, entry);
return ClassInheritanceTreeNode.findNode(rootNode, entry);
}
public ClassImplementationsTreeNode getClassImplementations(ClassEntry entry) {
Translator translator = project.getMapper().getDeobfuscator();
return this.indexTreeBuilder.buildClassImplementations(translator, entry);
}
public MethodInheritanceTreeNode getMethodInheritance(MethodEntry entry) {
Translator translator = project.getMapper().getDeobfuscator();
MethodInheritanceTreeNode rootNode = indexTreeBuilder.buildMethodInheritance(translator, entry);
return MethodInheritanceTreeNode.findNode(rootNode, entry);
}
public MethodImplementationsTreeNode getMethodImplementations(MethodEntry entry) {
Translator translator = project.getMapper().getDeobfuscator();
List<MethodImplementationsTreeNode> rootNodes = indexTreeBuilder.buildMethodImplementations(translator, entry);
if (rootNodes.isEmpty()) {
return null;
}
if (rootNodes.size() > 1) {
System.err.println("WARNING: Method " + entry + " implements multiple interfaces. Only showing first one.");
}
return MethodImplementationsTreeNode.findNode(rootNodes.get(0), entry);
}
public ClassReferenceTreeNode getClassReferences(ClassEntry entry) {
Translator deobfuscator = project.getMapper().getDeobfuscator();
ClassReferenceTreeNode rootNode = new ClassReferenceTreeNode(deobfuscator, entry);
rootNode.load(project.getJarIndex(), true);
return rootNode;
}
public FieldReferenceTreeNode getFieldReferences(FieldEntry entry) {
Translator translator = project.getMapper().getDeobfuscator();
FieldReferenceTreeNode rootNode = new FieldReferenceTreeNode(translator, entry);
rootNode.load(project.getJarIndex(), true);
return rootNode;
}
public MethodReferenceTreeNode getMethodReferences(MethodEntry entry, boolean recursive) {
Translator translator = project.getMapper().getDeobfuscator();
MethodReferenceTreeNode rootNode = new MethodReferenceTreeNode(translator, entry);
rootNode.load(project.getJarIndex(), true, recursive);
return rootNode;
}
@Override
public boolean applyChangeFromServer(EntryChange<?> change) {
ValidationContext vc = new ValidationContext();
vc.setActiveElement(PrintValidatable.INSTANCE);
this.applyChange0(vc, change);
gui.showStructure(gui.getActiveEditor());
return vc.canProceed();
}
public void validateChange(ValidationContext vc, EntryChange<?> change) {
if (change.getDeobfName().isSet()) {
EntryValidation.validateRename(vc, this.project, change.getTarget(), change.getDeobfName().getNewValue());
}
if (change.getJavadoc().isSet()) {
EntryValidation.validateJavadoc(vc, change.getJavadoc().getNewValue());
}
}
public void applyChange(ValidationContext vc, EntryChange<?> change) {
this.applyChange0(vc, change);
gui.showStructure(gui.getActiveEditor());
if (!vc.canProceed()) {
return;
}
this.sendPacket(new EntryChangeC2SPacket(change));
}
private void applyChange0(ValidationContext vc, EntryChange<?> change) {
validateChange(vc, change);
if (!vc.canProceed()) {
return;
}
Entry<?> target = change.getTarget();
EntryMapping prev = this.project.getMapper().getDeobfMapping(target);
EntryMapping mapping = EntryUtil.applyChange(vc, this.project.getMapper(), change);
boolean renamed = !change.getDeobfName().isUnchanged();
if (renamed && target instanceof ClassEntry && !((ClassEntry) target).isInnerClass()) {
this.gui.moveClassTree(target, prev.targetName() == null, mapping.targetName() == null);
}
if (!Objects.equals(prev.javadoc(), mapping.javadoc())) {
project.invalidateData(target.getTopLevelClass().getFullName(), DataInvalidationEvent.InvalidationType.JAVADOC);
// invalidateJavadoc implies invalidateMapped, so no need to check for that too
} else if (!Objects.equals(prev.targetName(), mapping.targetName())) {
project.invalidateData(DataInvalidationEvent.InvalidationType.MAPPINGS);
}
gui.showStructure(gui.getActiveEditor());
}
public void openStats(Set<StatsMember> includedMembers, String topLevelPackage, boolean includeSynthetic) {
ProgressDialog.runOffThread(gui.getFrame(), progress -> {
String data = new StatsGenerator(project).generate(progress, includedMembers, topLevelPackage, includeSynthetic).getTreeJson();
try {
File statsFile = File.createTempFile("stats", ".html");
try (FileWriter w = new FileWriter(statsFile, StandardCharsets.UTF_8)) {
w.write(Utils.readResourceToString("/stats.html").replace("/*data*/", data));
}
Desktop.getDesktop().open(statsFile);
} catch (IOException e) {
throw new Error(e);
}
});
}
public void setDecompiler(DecompilerService service) {
if (chp != null) {
chp.setDecompilerService(service);
}
}
public ClassHandleProvider getClassHandleProvider() {
return chp;
}
public EnigmaClient getClient() {
return client;
}
public EnigmaServer getServer() {
return server;
}
public void createClient(String username, String ip, int port, char[] password) throws IOException {
client = new EnigmaClient(this, ip, port);
client.connect();
client.sendPacket(new LoginC2SPacket(project.getJarChecksum(), password, username));
gui.setConnectionState(ConnectionState.CONNECTED);
}
public void createServer(int port, char[] password) throws IOException {
server = new IntegratedEnigmaServer(project.getJarChecksum(), password, EntryRemapper.mapped(project.getJarIndex(), new HashEntryTree<>(project.getMapper().getObfToDeobf())), port);
server.start();
client = new EnigmaClient(this, "127.0.0.1", port);
client.connect();
client.sendPacket(new LoginC2SPacket(project.getJarChecksum(), password, NetConfig.getUsername()));
gui.setConnectionState(ConnectionState.HOSTING);
}
@Override
public synchronized void disconnectIfConnected(String reason) {
if (client == null && server == null) {
return;
}
if (client != null) {
client.disconnect();
}
if (server != null) {
server.stop();
}
client = null;
server = null;
SwingUtilities.invokeLater(() -> {
if (reason != null) {
JOptionPane.showMessageDialog(gui.getFrame(), I18n.translate(reason), I18n.translate("disconnect.disconnected"), JOptionPane.INFORMATION_MESSAGE);
}
gui.setConnectionState(ConnectionState.NOT_CONNECTED);
});
}
@Override
public void sendPacket(Packet<ServerPacketHandler> packet) {
if (client != null) {
client.sendPacket(packet);
}
}
@Override
public void addMessage(Message message) {
gui.addMessage(message);
}
@Override
public void updateUserList(List<String> users) {
gui.setUserList(users);
}
@Override
public void onDataInvalidated(DataInvalidationEvent event) {
Objects.requireNonNull(project, "Invalidating data when no project is open");
if (event.getClasses() == null) {
switch (event.getType()) {
case MAPPINGS -> chp.invalidateMapped();
case JAVADOC -> chp.invalidateJavadoc();
case DECOMPILE -> chp.invalidate();
}
} else {
switch (event.getType()) {
case MAPPINGS -> {
for (String clazz : event.getClasses()) {
chp.invalidateMapped(new ClassEntry(clazz));
}
}
case JAVADOC -> {
for (String clazz : event.getClasses()) {
chp.invalidateJavadoc(new ClassEntry(clazz));
}
}
case DECOMPILE -> {
for (String clazz : event.getClasses()) {
chp.invalidate(new ClassEntry(clazz));
}
}
}
}
}
}
|