summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/Enigma.java
diff options
context:
space:
mode:
authorGravatar Runemoro2020-06-03 13:39:42 -0400
committerGravatar GitHub2020-06-03 18:39:42 +0100
commit0f47403d0220757fed189b76e2071e25b1025cb8 (patch)
tree879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/Enigma.java
parentFix search dialog hanging for a short time sometimes (#250) (diff)
downloadenigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz
enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz
enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip
Split GUI code to separate module (#242)
* Split into modules * Post merge compile fixes Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/Enigma.java')
-rw-r--r--src/main/java/cuchaz/enigma/Enigma.java121
1 files changed, 0 insertions, 121 deletions
diff --git a/src/main/java/cuchaz/enigma/Enigma.java b/src/main/java/cuchaz/enigma/Enigma.java
deleted file mode 100644
index f5f0649..0000000
--- a/src/main/java/cuchaz/enigma/Enigma.java
+++ /dev/null
@@ -1,121 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma;
13
14import com.google.common.base.Preconditions;
15import com.google.common.collect.ImmutableListMultimap;
16import cuchaz.enigma.analysis.ClassCache;
17import cuchaz.enigma.analysis.index.JarIndex;
18import cuchaz.enigma.api.EnigmaPlugin;
19import cuchaz.enigma.api.EnigmaPluginContext;
20import cuchaz.enigma.api.service.EnigmaService;
21import cuchaz.enigma.api.service.EnigmaServiceFactory;
22import cuchaz.enigma.api.service.EnigmaServiceType;
23import cuchaz.enigma.api.service.JarIndexerService;
24import cuchaz.enigma.utils.Utils;
25
26import java.io.IOException;
27import java.nio.file.Path;
28import java.util.List;
29import java.util.ServiceLoader;
30
31public class Enigma {
32 private final EnigmaProfile profile;
33 private final EnigmaServices services;
34
35 private Enigma(EnigmaProfile profile, EnigmaServices services) {
36 this.profile = profile;
37 this.services = services;
38 }
39
40 public static Enigma create() {
41 return new Builder().build();
42 }
43
44 public static Builder builder() {
45 return new Builder();
46 }
47
48 public EnigmaProject openJar(Path path, ProgressListener progress) throws IOException {
49 ClassCache classCache = ClassCache.of(path);
50 JarIndex jarIndex = classCache.index(progress);
51
52 services.get(JarIndexerService.TYPE).forEach(indexer -> indexer.acceptJar(classCache, jarIndex));
53
54 return new EnigmaProject(this, classCache, jarIndex, Utils.zipSha1(path));
55 }
56
57 public EnigmaProfile getProfile() {
58 return profile;
59 }
60
61 public EnigmaServices getServices() {
62 return services;
63 }
64
65 public static class Builder {
66 private EnigmaProfile profile = EnigmaProfile.EMPTY;
67 private Iterable<EnigmaPlugin> plugins = ServiceLoader.load(EnigmaPlugin.class);
68
69 private Builder() {
70 }
71
72 public Builder setProfile(EnigmaProfile profile) {
73 Preconditions.checkNotNull(profile, "profile cannot be null");
74 this.profile = profile;
75 return this;
76 }
77
78 public Builder setPlugins(Iterable<EnigmaPlugin> plugins) {
79 Preconditions.checkNotNull(plugins, "plugins cannot be null");
80 this.plugins = plugins;
81 return this;
82 }
83
84 public Enigma build() {
85 PluginContext pluginContext = new PluginContext(profile);
86 for (EnigmaPlugin plugin : plugins) {
87 plugin.init(pluginContext);
88 }
89
90 EnigmaServices services = pluginContext.buildServices();
91 return new Enigma(profile, services);
92 }
93 }
94
95 private static class PluginContext implements EnigmaPluginContext {
96 private final EnigmaProfile profile;
97
98 private final ImmutableListMultimap.Builder<EnigmaServiceType<?>, EnigmaService> services = ImmutableListMultimap.builder();
99
100 PluginContext(EnigmaProfile profile) {
101 this.profile = profile;
102 }
103
104 @Override
105 public <T extends EnigmaService> void registerService(String id, EnigmaServiceType<T> serviceType, EnigmaServiceFactory<T> factory) {
106 List<EnigmaProfile.Service> serviceProfiles = profile.getServiceProfiles(serviceType);
107
108 for (EnigmaProfile.Service serviceProfile : serviceProfiles) {
109 if (serviceProfile.matches(id)) {
110 T service = factory.create(serviceProfile::getArgument);
111 services.put(serviceType, service);
112 break;
113 }
114 }
115 }
116
117 EnigmaServices buildServices() {
118 return new EnigmaServices(services.build());
119 }
120 }
121}