Skip to content

Commit

Permalink
Server: Move tracks to resources, use them instead of local folder
Browse files Browse the repository at this point in the history
With this change, the server no longer needs to be deployed alongside a
tracks directory and the symlink to the tracks directory can also be
removed, making the server work by default on Windows on IDEs.

Tracks are now bundled into the jar file generated by `mvn package`,
however the location where tracks are searched for can still be
overridden with a CLI flag.

This required changing the way FileSystemStatsManager and
FileSystemTrackManager load tracks by making it possible to configure
which kind of file system they should use as jar resources can only be
read as files when using a custom FileSystem.
  • Loading branch information
StenAL committed Oct 14, 2023
1 parent 9e42344 commit 241883f
Show file tree
Hide file tree
Showing 2,086 changed files with 113 additions and 130 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ jobs:
${{ steps.changelog.outputs.changelog }}
draft: true
prerelease: false
- name: Upload Tracks
id: upload-tracks
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: shared/target/tracks.zip
asset_name: tracks.zip
asset_content_type: application/zip
- name: Upload Server Jar
id: upload-server-jar
uses: actions/upload-release-asset@v1
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Run `mvn install` in the root directory. This builds `client`, `server` and `edi
### Running

First, the server application has to be started as it provides resources like sounds, maps and textures which are required for "offline" modes, too.
As I could not manage to include the tracks inside the compiled JAR archive, the `tracks` directory has to be located at the same folder where the `server.jar` is located! There is a symbolic link in the `server/` directory which does not work on Windows systems. Please remove it and copy the directory there instead or launch the server using the `--tracks-dir` option!
By default, the server uses tracks from the project's bundled resources, however if you want to run with a custom set of tracks, launch the server using the `--tracks-dir` option!
Assuming that all 3 tools have compiled successfully (or downloaded them from the [Releases Page](https://github.com/PhilippvK/playforia-minigolf/releases)), you have 3 possible ways for running the server binary:
1. Using the IntelliJ IDE: Use the provides build artifacts or run the server by pressing the play button after compiling
2. Using the Maven tool: Run `mvn compile exec:java` in the `./server`, `./client` or `./editor` directory
Expand All @@ -69,7 +69,7 @@ Running the Editor is quite straightforward as it can be started like expected:
### CLI options
Both client and server include CLI options for hostname (`-ip`), port (`-p`) settings. To learn about all the available setting you can include help with `-h` parameter.

To override the default directory where the server looks for tracks, use the `--tracks-dir` option.
To use custom tracks instead of the default set of bundled tracks, use the `--tracks-dir` option when starting the server and point it to where your tracks are located.

If you want to enable debugging messages, add `--verbose` to the list of arguments.

Expand All @@ -89,7 +89,7 @@ Tested:
1. The code is neither written by me nor my property. I do NOT represent the same values as people who have worked on this code before. (Original Source: https://github.com/WorldStarHipHopX/playforia)
2. I am not responsible for any bug, problems, security flaws,...
3. Also, I do not intent to extend the current codebase very much.
4. The Java code you will find in the repository is pretty bad. Some parts even look like they where generated, for example by an converter tool
4. The Java code you will find in the repository is pretty bad. Some parts even look like they were generated, for example by an converter tool
5. There is actually an aimbot implemented in the client code. Look for `allowCheating` in `GameCanvas.java` for trying it out. Use it wisely.

## Contribution
Expand Down
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand Down
9 changes: 4 additions & 5 deletions server/src/main/java/org/moparforia/server/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.moparforia.shared.ManifestVersionProvider;
import picocli.CommandLine;

import java.util.Optional;
import java.util.concurrent.Callable;

@CommandLine.Command(
Expand All @@ -18,7 +19,6 @@ public class Launcher implements Callable<Integer> {

public static final String DEFAULT_HOST = "0.0.0.0";
public static final String DEFAULT_PORT = "4242";
public static final String DEFAULT_TRACKS_DIRECTORY = "tracks";

@CommandLine.Option(
names = {"--hostname", "-ip"},
Expand All @@ -36,8 +36,7 @@ public class Launcher implements Callable<Integer> {

@CommandLine.Option(
names = {"--tracks-dir", "-t"},
description = "Sets where to look for tracks and track sets",
defaultValue = DEFAULT_TRACKS_DIRECTORY
description = "Sets where to look for tracks and track sets"
)
private String tracksDirectory;

Expand All @@ -50,11 +49,11 @@ public static void main(String... args) {

@Override
public Integer call() {
getServer(host, port, tracksDirectory).start();
getServer(this.host, this.port, this.tracksDirectory).start();
return 0;
}

public Server getServer(String host, int port, String tracksDirectory) {
return new Server(host, port, tracksDirectory);
return new Server(host, port, Optional.ofNullable(tracksDirectory));
}
}
64 changes: 50 additions & 14 deletions server/src/main/java/org/moparforia/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@
import org.moparforia.server.game.Player;
import org.moparforia.server.net.*;
import org.moparforia.shared.tracks.TrackLoadException;
import org.moparforia.shared.tracks.TracksLocation;
import org.moparforia.shared.tracks.filesystem.FileSystemTrackManager;
import org.moparforia.shared.tracks.filesystem.FileSystemStatsManager;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
Expand All @@ -31,15 +39,16 @@
public class Server implements Runnable {

public static final boolean DEBUG = true;
public static final String DEFAULT_TRACKS_DIRECTORY = "tracks";

private HashMap<Integer, Player> players = new HashMap<Integer, Player>();
private HashMap<Integer, Player> players = new HashMap<>();
private ChannelGroup allChannels = new DefaultChannelGroup();
private ConcurrentLinkedQueue<Event> events = new ConcurrentLinkedQueue<Event>();
private HashMap<PacketType, ArrayList<PacketHandler>> packetHandlers = new HashMap<PacketType, ArrayList<PacketHandler>>();
private ConcurrentLinkedQueue<Event> events = new ConcurrentLinkedQueue<>();
private HashMap<PacketType, ArrayList<PacketHandler>> packetHandlers = new HashMap<>();

private String host;
private int port;
private String tracksDirectory;
private Optional<String> tracksDirectory;

private HashMap<LobbyType, Lobby> lobbies = new HashMap<LobbyType, Lobby>();
//private ArrayList<LobbyRef> lobbies = new ArrayList<LobbyRef>();
Expand All @@ -49,7 +58,7 @@ public class Server implements Runnable {
private int gameIdCounter;


public Server(String host, int port, String tracksDirectory) {
public Server(String host, int port, Optional<String> tracksDirectory) {
this.host = host;
this.port = port;
this.tracksDirectory = tracksDirectory;
Expand All @@ -58,12 +67,6 @@ public Server(String host, int port, String tracksDirectory) {
}
}

// public Server() {
// for (LobbyType lt : LobbyType.values()) {
// lobbies.put(lt, new Lobby(lt));
// }
// }

public int getNextPlayerId() {
return playerIdCounter++;
}
Expand Down Expand Up @@ -157,9 +160,10 @@ public void addPlayer(Player p) {

public void start() {
try {
FileSystemTrackManager.getInstance().load(tracksDirectory);
FileSystemStatsManager.getInstance().load(tracksDirectory);
} catch (TrackLoadException | IOException e) {
TracksLocation tracksLocation = this.getTracksLocation();
FileSystemTrackManager.getInstance().load(tracksLocation);
FileSystemStatsManager.getInstance().load(tracksLocation);
} catch (TrackLoadException | IOException | URISyntaxException e) {
System.err.println("Unable to load tracks: " + e.getMessage());
e.printStackTrace();
return;
Expand Down Expand Up @@ -219,4 +223,36 @@ public void run() {
}
}
}

/**
* Determines where to look for tracks.
* In order of priority:
* 1. If user has specified a tracks directory CLI flags, search for it in the default FileSystem
* 2. Else, if running in a jar file, use bundled tracks
* 3. Else, if running in an IDE, use resources folder
* 4. Else, use default filesystem and look in default directory
*/
private TracksLocation getTracksLocation() throws URISyntaxException, IOException {
if (tracksDirectory.isPresent()) {
System.out.println("Using CLI argument for tracks location: " + tracksDirectory.get());
return new TracksLocation(FileSystems.getDefault(), tracksDirectory.get());
}

URL resource = this.getClass().getResource("/tracks");
if (resource != null) {
URI resourceUri = resource.toURI();
if (resourceUri.getScheme().equals("jar")) {
// tracks are bundled in jar
System.out.println("Using bundled jar resources for tracks location");
return new TracksLocation(FileSystems.newFileSystem(resourceUri, Collections.emptyMap()), "/tracks");
}
// running in IDE
String tracksPath = Paths.get(resourceUri).toString();
System.out.println("Using path to resources for tracks location: " + tracksPath);
return new TracksLocation(FileSystems.getDefault(), tracksPath);
}
// running outside of jar, outside of IDE
System.out.println("Using default tracks directory for tracks location");
return new TracksLocation(FileSystems.getDefault(), DEFAULT_TRACKS_DIRECTORY);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 241883f

Please sign in to comment.