Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Initialization #37

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.sourcewriters.minecraft.vcompat.provider.VersionControl;
import net.sourcewriters.minecraft.vcompat.provider.lookup.ClassLookupProvider;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.version.Versions;

public abstract class VersionCompatProvider {

Expand All @@ -15,11 +16,21 @@ public static VersionCompatProvider get() {
if (PROVIDER.isPresent()) {
return PROVIDER.get();
}
Object object = ClassLookup.of(IMPLEMENTATION_PATH).init();
if (object == null || !(object instanceof VersionCompatProvider)) {
throw new IllegalStateException("Can't initialize VersionCompatProvider!");
}
return PROVIDER.replace((VersionCompatProvider) object).lock().get();
System.out.println(
"Initializing vCompat on with '" + Versions.getServerAsString() + "' core on '" + Versions.getMinecraftAsString() + "'!");
Object object = ClassLookup.of(IMPLEMENTATION_PATH).init();
if (object == null || !(object instanceof VersionCompatProvider)) {
throw new IllegalStateException("Can't initialize VersionCompatProvider!");
}
VersionCompatProvider provider = PROVIDER.replace((VersionCompatProvider) object).lock().get();
try {
provider.init();
} catch (Exception exception) {
throw new IllegalStateException("Failed to initialize vCompat implementation", exception);
}
Runtime.getRuntime().addShutdownHook(new Thread(provider::shutdown));
System.out.println("Initialization of vCompat complete!");
return provider;
}

protected final ClassLookupProvider lookupProvider = new ClassLookupProvider();
Expand All @@ -28,6 +39,10 @@ public static VersionCompatProvider get() {
* Impl
*/

protected abstract void init() throws Exception;

protected abstract void shutdown();

public abstract VersionControl getControl();

public final ClassLookupProvider getLookupProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Object initialize(Constructor<?> constructor, Object... arguments) {
}
try {
if (arguments.length == 0) {
return lookup().unreflectConstructor(constructor).invokeWithArguments();
return lookup().unreflectConstructor(constructor).invoke();
}
return lookup().unreflectConstructor(constructor).invokeWithArguments(arguments);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@

import net.sourcewriters.minecraft.vcompat.provider.VersionControl;
import net.sourcewriters.minecraft.vcompat.provider.lookup.handle.ClassLookup;
import net.sourcewriters.minecraft.vcompat.shaded.syntaxapi.utils.java.tools.Container;
import net.sourcewriters.minecraft.vcompat.version.Versions;

public final class VersionCompat extends VersionCompatProvider {

private static final String VERSION_PATH = String.format("%s.provider.impl.%s.VersionControl%s", VersionCompat.class.getPackageName(), Versions.getServerAsString(), Versions.getServerAsString().substring(1));

private final VersionControl control;
private static final String VERSION_PATH = String.format("%s.provider.impl.%s.VersionControl%s", VersionCompat.class.getPackageName(),
Versions.getServerAsString(), Versions.getServerAsString().substring(1));

public VersionCompat() {
this.control = initControl();
Runtime.getRuntime().addShutdownHook(new Thread(control::shutdown));
private final Container<VersionControl> control = Container.of();
private boolean init = false;

@Override
protected final void init() throws Exception {
if (init) {
return;
}
init = true;
control.replace(initControl()).lock();
}

@Override
protected final void shutdown() {
if(!init) {
return;
}
control.ifPresent(VersionControl::shutdown);
}

private final VersionControl initControl() {
Expand All @@ -25,7 +40,7 @@ private final VersionControl initControl() {

@Override
public final VersionControl getControl() {
return control;
return control.get();
}

}