Skip to content

Commit

Permalink
feat: Add new and modify existing dungeon entities (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morazzer authored Sep 14, 2024
1 parent 6efb891 commit 6b8d2e4
Show file tree
Hide file tree
Showing 30 changed files with 640 additions and 61 deletions.
Empty file.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id("java")
id("maven-publish")
`cookies-entities-changelog`
`cookies-get-version`
}

group = "dev.morazzer.cookies"
Expand All @@ -13,6 +15,7 @@ repositories {
dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("org.jetbrains:annotations:24.1.0")
}

tasks.test {
Expand Down
11 changes: 11 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.eclipse.jgit:org.eclipse.jgit:6.10.0.202406032230-r")
}
42 changes: 42 additions & 0 deletions buildSrc/src/main/kotlin/cookies-entities-changelog.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import org.eclipse.jgit.api.Git
import kotlin.io.path.readText

tasks {
val changelogTask = register("createChangelog")
changelogTask.configure {
group = "cookies"
enabled = true
doLast {
val git = Git.open(rootDir)

val currentTag = git.tagList().call().first()
val previousTag = git.tagList().call().let {
if (it.size > 1) {
it[1].objectId
} else {
git.log().call().first().toObjectId()
}
}

val gitHistory = git.log().addRange(previousTag, currentTag.objectId).call()
val changeLogBuilder = StringBuilder()
val changeLogHeader = rootDir.toPath().resolve("gradle/CHANGELOG_HEADER.md").readText(Charsets.UTF_8)

changeLogBuilder.append(applyPlaceholders(changeLogHeader))

gitHistory.forEach {
if (it.fullMessage.contains("[SKIP]")) {
return@forEach
}
changeLogBuilder.append("- ").append(it.shortMessage).append(" - @").append(it.committerIdent.name).append("\n")
}

rootDir.resolve("CHANGELOG.md").writeText(changeLogBuilder.toString())
}
}
}

fun applyPlaceholders(text: String): String {
return text.replace("\$VERSION$", "${getRootProject().version}")
.replace("\$projectName$", getRootProject().name)
}
10 changes: 10 additions & 0 deletions buildSrc/src/main/kotlin/cookies-get-version.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tasks {
val register = register("version")
register.configure {
group = "cookies"
enabled = true
doLast {
rootDir.resolve("version.txt").writeText("${rootProject.version}")
}
}
}
Empty file added gradle/CHANGELOG_HEADER.md
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.morazzer.cookies.entities.misc;

/**
* Information about the current backend version.
*/
public class BackendVersion {

public static final int CURRENT_API_VERSION = 1;
public static final int CURRENT_PACKET_VERSION = 2;

public static final String CURRENT_VERSION_STRING = "v" + CURRENT_API_VERSION;

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package dev.morazzer.cookies.entities.request;

/**
* Entity to initialize authentication.
* @param sharedSecret The secret created by the client.
* @param username The username of the client.
*/
public record AuthRequest(String sharedSecret, String username) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.morazzer.cookies.entities.response;

public record AuthResponse(String token) {

}
/**
* The auth response from the server.
*
* @param token The token of the client.
*/
public record AuthResponse(String token) {}

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/java/dev/morazzer/cookies/entities/websocket/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@
import java.util.List;
import java.util.function.Consumer;

/**
* A packet that can be sent between server and client.
* @param <T> The type of the packet.
*/
public interface Packet<T extends Packet<T>> {
List<PacketConsumer<?>> consumers = new ArrayList<>();

/**
* Serializes the packet into a byte stream.
* @param serializer The serializer.
*/
void serialize(PacketSerializer serializer);

/**
* Registers a packet listener.
* @param clazz The packet to listen to.
* @param consumer The listener.
* @param <T> The type of the packet.
*/
static <T extends Packet<T>> void onReceive(Class<T> clazz, Consumer<T> consumer) {
consumers.add(new PacketConsumer<>(clazz, consumer));
}

/**
* Called when a packet is received.
* @param receive The packet that was received.
* @param <T> The type of the packet.
*/
static <T extends Packet<T>> void receive(Packet<T> receive) {
for (PacketConsumer<?> consumer : consumers) {
if (consumer.clazz().isInstance(receive)) {
Expand All @@ -20,6 +40,12 @@ static <T extends Packet<T>> void receive(Packet<T> receive) {
}
}

/**
* Invokes the packet listener for the packet.
* @param consumer The listener.
* @param packet The packet.
* @param <T> The type of the packet.
*/
private static <T extends Packet<T>> void invoke(Consumer<T> consumer, Packet<?> packet) {
//noinspection unchecked
consumer.accept((T) packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@

import java.util.function.Consumer;

/**
* A consumer with additional information about the packet.
* @param clazz The class of the packet.
* @param consumer The consumer.
* @param <T> The type of the packet.
*/
public record PacketConsumer<T extends Packet<T>>(Class<T> clazz, Consumer<T> consumer) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
* Helper used for serialization/deserialization of packets.
*/
public class PacketSerializer {

private ByteArrayInputStream input;
private ByteArrayOutputStream output;

/**
* Creates a write only packet serializer.
*/
public PacketSerializer() {
this.output = new ByteArrayOutputStream();
}

/**
* Creates a read only packet serializer.
*
* @param bytes The bytes to be read.
*/
public PacketSerializer(byte[] bytes) {
this.input = new ByteArrayInputStream(bytes);
}

/**
* Writes the int to the output.
*/
public void writeInt(int number) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (number & 0xFF);
Expand All @@ -28,62 +42,154 @@ public void writeInt(int number) {
this.output.writeBytes(bytes);
}

/**
* Reads an int from the input.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#read(byte[])}.
*/
public int readInt() throws IOException {
byte[] bytes = new byte[4];
this.input.read(bytes);
byte[] bytes = this.input.readNBytes(4);
return bytes[0] & 0xFF | (bytes[1] & 0xFF) << 8 | (bytes[2] & 0xFF) << 16 | (bytes[3] & 0xFF) << 24;
}

/**
* Writes the string to the output in form of a byte[] and prefixes said array with its length.
*
* @param string The string to write.
*/
public void writeString(String string) {
this.writeByteArray(string.getBytes(StandardCharsets.UTF_8));
}

/**
* Reads a string from the input stream.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#read(byte[])}.
*/
public String readString() throws IOException {
return new String(this.readByteArray(), StandardCharsets.UTF_8);
}

/**
* @return The output as a byte array.
*/
public byte[] toByteArray() {
return this.output.toByteArray();
}

/**
* Writes the long as two integers to the output.
*
* @param number The long to write.
*/
public void writeLong(long number) {
this.writeInt((int) (number));
this.writeInt((int) (number >>> 32));
}

/**
* Reads a long from the input stream.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#read(byte[])}.
*/
public long readLong() throws IOException {
return ((long) this.readInt() & 0xFFFFFFFFL) | ((long) this.readInt()) << 32;
}

/**
* Writes an uuid as two longs to the output.
*
* @param uuid The uuid to write.
*/
public void writeUUID(UUID uuid) {
this.writeLong(uuid.getMostSignificantBits());
this.writeLong(uuid.getLeastSignificantBits());
}

/**
* Reads an uuid from the input stream.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#read(byte[])}.
*/
public UUID readUUID() throws IOException {
long mostSignificantBits = this.readLong();
long leastSignificantBits = this.readLong();
return new UUID(mostSignificantBits, leastSignificantBits);
}

/**
* Writes a byte array to the output stream and prefixes said array with its length.
*
* @param bytes The byte array to write.
*/
public void writeByteArray(byte[] bytes) {
this.writeInt(bytes.length);
this.output.writeBytes(bytes);
}

/**
* Writes a byte array to the output stream without prefixing it with its length.
*
* @param bytes The byte array to write.
*/
public void writeByteArrayWithoutLength(byte[] bytes) {
this.output.writeBytes(bytes);
}

/**
* Reads a byte array that was prefixed with its own length.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#read(byte[])}.
*/
public byte[] readByteArray() throws IOException {
final int length = this.readInt();
final byte[] bytes = new byte[length];
this.input.read(bytes);
return bytes;
}

/**
* Ensures that the input stream has no remaining bytes, if any are found an exception is thrown.
*/
public void ensureEmpty() {
if (this.input.available() > 0) {
throw new IllegalStateException("Buffer not empty after packet deserialization");
}
}

/**
* Writes a single byte to the output stream.
*
* @param b The byte to write.
*/
public void writeByte(byte b) {
this.output.write(b);
}

/**
* Reads a single byte from the input stream.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#readNBytes(int)}.
*/
public byte readByte() throws IOException {
return this.input.readNBytes(1)[0];
}

/**
* Writes a boolean in form of a byte to the output stream.
*
* @param bool The boolean to write.
*/
public void writeBoolean(boolean bool) {
this.writeByte((byte) (bool ? 1 : 0));
}

/**
* Reads a boolean from the input stream.
*
* @throws IOException May be thrown, for more information see {@link ByteArrayInputStream#readNBytes(int)}.
*/
public boolean readBoolean() throws IOException {
return readByte() == 1;
}
}
Loading

0 comments on commit 6b8d2e4

Please sign in to comment.