Skip to content

Commit

Permalink
add dummy game locator
Browse files Browse the repository at this point in the history
  • Loading branch information
vectrixdevelops committed Jan 6, 2024
1 parent 4f48dc0 commit 2347cfe
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -112,6 +113,7 @@ private void run(final String@NotNull [] args) {
final Optional<GameLocatorService> gameLocatorProvider = requiredGameLocator.map(locatorIdentifier -> IgniteCollections.stream(gameLocatorLoader)
.filter(locator -> locator.id().equalsIgnoreCase(locatorIdentifier))
.findFirst()).orElseGet(() -> IgniteCollections.stream(gameLocatorLoader)
.sorted(Comparator.comparingInt(GameLocatorService::priority))
.filter(GameLocatorService::shouldApply)
.findFirst()
);
Expand Down
101 changes: 101 additions & 0 deletions launcher/src/main/java/space/vectrix/ignite/game/DummyGameLocator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* This file is part of Ignite, licensed under the MIT License (MIT).
*
* Copyright (c) vectrix.space <https://vectrix.space/>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package space.vectrix.ignite.game;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;
import space.vectrix.ignite.Blackboard;
import space.vectrix.ignite.IgniteBootstrap;

/**
* Provides a general game locator.
*
* @author vectrix
* @since 1.0.0
*/
public final class DummyGameLocator implements GameLocatorService {
private DummyGameProvider provider;

@Override
public @NotNull String id() {
return "dummy";
}

@Override
public @NotNull String name() {
return "Dummy";
}

@Override
public int priority() {
return Integer.MAX_VALUE;
}

@Override
public boolean shouldApply() {
return true;
}

@Override
public void apply(final @NotNull IgniteBootstrap bootstrap) throws Throwable {
Logger.warn("Using the dummy game provider means that all the jars found in the game libraries directory");
Logger.warn("will be loaded into the classpath. If this causes an unexpected problem, please delete the");
Logger.warn("libraries directory and try launch again.");

if(this.provider == null) {
this.provider = new DummyGameProvider();
}
}

@Override
public @NotNull GameProvider locate() {
return this.provider;
}

/* package */ static final class DummyGameProvider implements GameProvider {
/* package */ DummyGameProvider() {
}

@Override
public @NotNull Stream<Path> gameLibraries() {
final Path libraryPath = Blackboard.raw(Blackboard.GAME_LIBRARIES);
try(final Stream<Path> stream = Files.walk(libraryPath)) {
return stream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().endsWith(".jar"));
} catch(final Throwable throwable) {
return Stream.empty();
}
}

@Override
public @NotNull Path gamePath() {
return Blackboard.raw(Blackboard.GAME_JAR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public interface GameLocatorService {
*/
@NotNull String name();

/**
* The order to try select this locator.
*
* @return the priority
* @since 1.0.0
*/
int priority();

/**
* Returns {@code true} if this locator should be used, otherwise returns
* {@code false}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public final class LegacyPaperGameLocator implements GameLocatorService {
return "Legacy Paper";
}

@Override
public int priority() {
return 100;
}

@Override
public boolean shouldApply() {
final Path path = Blackboard.raw(LegacyPaperGameLocator.PAPER_JAR);
Expand Down Expand Up @@ -125,7 +130,9 @@ public void apply(final @NotNull IgniteBootstrap bootstrap) throws Throwable {
System.setSecurityManager(original);

// Create the game provider.
this.provider = this.createProvider();
if(this.provider == null) {
this.provider = this.createProvider();
}

// Locate the game jar.
if(!Blackboard.get(Blackboard.GAME_JAR).isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public final class PaperGameLocator implements GameLocatorService {
return "Paper";
}

@Override
public int priority() {
return 50;
}

@Override
public boolean shouldApply() {
final Path path = Blackboard.raw(PaperGameLocator.PAPER_JAR);
Expand Down Expand Up @@ -111,7 +116,9 @@ public void apply(final @NotNull IgniteBootstrap bootstrap) throws Throwable {
}

// Create the game provider.
this.provider = this.createProvider();
if(this.provider == null) {
this.provider = this.createProvider();
}

// Locate the game jar.
if(!Blackboard.get(Blackboard.GAME_JAR).isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public final class SpigotGameLocator implements GameLocatorService {
return "Spigot";
}

@Override
public int priority() {
return 50;
}

@Override
public boolean shouldApply() {
final Path path = Blackboard.raw(SpigotGameLocator.SPIGOT_JAR);
Expand Down Expand Up @@ -123,7 +128,9 @@ public void apply(final @NotNull IgniteBootstrap bootstrap) throws Throwable {
}

// Create the game provider.
this.provider = this.createProvider();
if(this.provider == null) {
this.provider = this.createProvider();
}

// Locate the game jar.
if(!Blackboard.get(Blackboard.GAME_JAR).isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
space.vectrix.ignite.game.SpigotGameLocator
space.vectrix.ignite.game.PaperGameLocator
space.vectrix.ignite.game.LegacyPaperGameLocator
space.vectrix.ignite.game.DummyGameLocator

0 comments on commit 2347cfe

Please sign in to comment.