Skip to content

Commit

Permalink
Tidy some things up, do more bad things
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Oct 19, 2020
1 parent d800b71 commit 07093b1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.asset.Asset;
import org.spongepowered.api.asset.AssetId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.inject.ProvisionException;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.config.ConfigRoot;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
Expand All @@ -44,6 +45,8 @@
import org.spongepowered.plugin.PluginContainer;

import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
Expand Down Expand Up @@ -92,6 +95,34 @@ protected void configure() {
this.bind(COMMENTED_CONFIGURATION_NODE_REFERENCE)
.annotatedWith(DefaultConfigAnnotation.NON_SHARED)
.toProvider(PrivateCommentedConfigurationNodeReference.class);

this.requestStaticInjection(IHateGuiceInjectorProvider.class);
}

/**
* So... we need a way to get the final Injector used to create the plugin
* instance, but Guice doesn't want to give us that in the provider for the
* TypeSerializerCollection.
*
* Instead, we request static injection of this class, which collects the
* necessary information right as the Injector is created, but before it
* has been passed to the plugin loader to create the plugin instance.
*
* Please, please try to find something nicer. I give up.
*/
static final class IHateGuiceInjectorProvider {

private static final Map<PluginContainer, Injector> injectors = new ConcurrentHashMap<>();

@Inject
static void acceptRegistration(final PluginContainer container, final Injector injector) {
IHateGuiceInjectorProvider.injectors.put(container, injector);
}

public static @Nullable Injector get(final PluginContainer container) {
return IHateGuiceInjectorProvider.injectors.get(container);
}

}

/**
Expand Down Expand Up @@ -289,32 +320,29 @@ public ConfigurationReference<CommentedConfigurationNode> get() {

static final class TypeSerializers implements Provider<TypeSerializerCollection> {

private final Provider<Injector> provider;
private final PluginContainer container;
private final PluginConfigManager mgr;

@Inject
TypeSerializers(final Provider<Injector> provider, final PluginConfigManager mgr) {
this.provider = provider;
TypeSerializers(final PluginContainer container, final PluginConfigManager mgr) {
this.container = container;
this.mgr = mgr;
}

@Override
public TypeSerializerCollection get() {
System.out.println(this.getClass());
System.out.println(this.getClass().getClassLoader());
System.out.println(Injector.class.getClassLoader());
System.out.println(this.provider.getClass().getClassLoader());
System.out.println(this.provider.get());
System.out.println(this.provider.get().getClass());
// This is weird... we need to get access to the injector before its state is fully initialized
// So we have to work on it here
/*return this.mgr.getSerializers().childBuilder()
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
.addDiscoverer(GuiceObjectMapperProvider.injectedObjectDiscoverer(this.provider.get()))
.build())
.build();*/
return this.mgr.getSerializers();
final @Nullable Injector injector = IHateGuiceInjectorProvider.get(this.container);
if (injector == null) {
return this.mgr.getSerializers();
} else {
return this.mgr.getSerializers().childBuilder()
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
.addDiscoverer(GuiceObjectMapperProvider.injectedObjectDiscoverer(injector))
.build())
.build();
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,47 @@
package org.spongepowered.test.configtest;

import com.google.inject.Inject;
import com.google.inject.Injector;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.ConstructPluginEvent;
import org.spongepowered.api.event.network.ServerSideConnectionEvent;
import org.spongepowered.configurate.reference.ConfigurationReference;
import org.spongepowered.configurate.reference.ValueReference;
import org.spongepowered.plugin.jvm.Plugin;
import org.spongepowered.test.LoadableModule;

@Plugin("configtest")
public class ConfigTest implements LoadableModule {

private final Logger logger;
private final ConfigurationLoader<CommentedConfigurationNode> loader;
private ConfigurationNode node;
private ExampleConfiguration config;
private final ConfigurationReference<CommentedConfigurationNode> reference;
private ValueReference<ExampleConfiguration, CommentedConfigurationNode> config;

@Inject
ConfigTest(final Logger logger, final @DefaultConfig(sharedRoot = true) ConfigurationLoader<CommentedConfigurationNode> loader,
final Injector injector) {
ConfigTest(final Logger logger, final @DefaultConfig(sharedRoot = true) ConfigurationReference<CommentedConfigurationNode> reference) {
this.logger = logger;
this.loader = loader;
this.logger.info("Injector: {} of class {}", injector, injector.getClass());
this.reference = reference;
}

@Listener
public void onConstruction(final ConstructPluginEvent event) {
try {
this.node = this.loader.load();
this.config = this.node.get(ExampleConfiguration.class, new ExampleConfiguration());
this.node.set(this.config);
this.loader.save(this.node);
this.config = this.reference.referenceTo(ExampleConfiguration.class);
this.reference.save();
} catch (final ConfigurateException ex) {
this.logger.error("Unable to load test configuration", ex);
}
}

@Listener
public void clientConnected(final ServerSideConnectionEvent.Join event) {
final Component motd = this.config.getMotd();
final Component motd = this.config.get().getMotd();
if (motd == null || motd == Component.empty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public abstract class AbstractVanillaLaunchHandler implements ILaunchHandlerServ
"org.spongepowered.configurate."
);

/**
* Exceptions to the exclusions, because we've reached that point in life.
*/
protected static final List<String> EXCLUDED_EXCEPTIONS = Arrays.asList(
"org.spongepowered.configurate.objectmapping.guice."
);

@Override
public void configureTransformationClassLoader(final ITransformingClassLoaderBuilder builder) {
builder.setClassBytesLocator(this.getResourceLocator());
Expand All @@ -87,8 +94,15 @@ public Callable<Void> launchService(final String[] arguments, final ITransformin
this.logger.info("Transitioning to Sponge launcher, please wait...");

launchClassLoader.addTargetPackageFilter(klass -> {
for (final String pkg : EXCLUDED_PACKAGES) {
if (klass.startsWith(pkg)) return false;
exclusions: for (final String pkg : EXCLUDED_PACKAGES) {
if (klass.startsWith(pkg)) {
for (final String bypass : EXCLUDED_EXCEPTIONS) {
if (klass.startsWith(bypass)) {
continue exclusions;
}
}
return false;
}
}
return true;
});
Expand Down

0 comments on commit 07093b1

Please sign in to comment.