Skip to content

Commit

Permalink
Merge pull request #37 from SourceWriters/development
Browse files Browse the repository at this point in the history
Fixed Initialization
  • Loading branch information
Lauriichan authored Jan 23, 2022
2 parents 891248c + c2ea562 commit e4295e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
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();
}

}

0 comments on commit e4295e8

Please sign in to comment.