Skip to content

Commit

Permalink
Add JSON support and Java Modules: v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxSamu committed Nov 17, 2023
1 parent 993fd79 commit e79bb35
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 6 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
This is a very simple Java tool for games and other apps to manage resources.

# Installation
The current version is `1.0`. This version is compatible with Java 17 and above.
The current version is `1.1`. This version is compatible with Java 17 and above.

The artifact can be installed from my [Maven repository](https://maven.shadew.net/).

## Gradle
```kotlin
// Kotlin DSL:

repositories {
// Add my repository
maven { url = uri("https://maven.shadew.net/") }
}

dependencies {
// Add the artifact
implementation("dev.runefox:rms:1.1")
}
```

```groovy
// Groovy DSL:
repositories {
// Add my repository
maven { url "https://maven.shadew.net/" }
}
dependencies {
// Add the artifact
implementation "dev.runefox:rms:1.0"
implementation "dev.runefox:rms:1.1"
}
```

Expand All @@ -34,7 +50,7 @@ dependencies {
<dependency>
<groupId>dev.runefox</groupId>
<artifactId>rms</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
</dependencies>
```
Expand Down Expand Up @@ -166,7 +182,7 @@ ContentResource myContent = mgr.get(ContentResourceType.INSTANCE, "my_content");
```


## 5. Extra: Using handles
## Extra: Using handles

In some occasion, you may want to reload all the resources in your application. This means all the `Resource` instances change. You will have to re-obtain all the resources yourself, which can be quite annoying to do. That is, unless you use `Handle`s. A `Handle` is a reference to a resource. It will store the resource for quick access but it's controlled by the resource manager and reset upon `dispose()`. Instead of calling `ResourceManager.get`, you can call `ResourceManager.handle` in exactly the same way to get a handle instead. You can then store this handle anywhere in your app without ever having to think about reloading it again. Another advantage of using handles is that they're lazily loaded: it will load the resource only when you request it using `Handle.get`. A handle is simply just a glorified `Supplier`.
```java
Expand All @@ -177,6 +193,14 @@ ContentResource instance = myContent.get();

Now you can call `dispose` at any time to simply unload all resources. RMS will invalidate all handles and the next time you call `get` on any of them, it will reload the resource. In fact, you can call `dispose` and give a new root path, and the resource will load as usual from the new path.

# Libraries for certain formats

Below is a list of libraries that load files in certain formats, with an artifact that belongs to it. All artifacts have the same version as the main project and all artifacts depend on the main project with the same version (in the list referred to with `[rms-version]`).

- [JSON](https://github.com/FoxSamu/json/): use `dev.runefox:rms-json:[rms-version]`.

In the future I may add more supported formats through my libraries or third-party libraries.

# License
Licensed under the LGPLv3 license. For the full LGPL+GPLv3 license, see `LICENSE`.

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/

plugins {
id("java")
id("java-library")
id("maven-publish")
}

group = "dev.runefox"
version = "1.0"
version = "1.1"

repositories {
mavenCentral()
Expand Down
78 changes: 78 additions & 0 deletions rms-json/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2023 Samū
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


plugins {
id("java")
id("maven-publish")
}

group = rootProject.group
version = rootProject.version

repositories {
mavenCentral()
maven {
url = uri("https://maven.shadew.net/")
}
}

dependencies {
implementation("dev.runefox:json:0.7.2")
implementation(rootProject)

testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
useJUnitPlatform()
}

java {
withSourcesJar()
withJavadocJar()
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = "${project.group}"
artifactId = project.name
version = "${project.version}"

from(components["java"])
}
}
repositories {
maven {
// You should have received these credentials when given permission
// to the Runefox Maven repository
val rfxMavenHost: String by properties
val rfxMavenUser: String by properties
val rfxMavenPass: String by properties

name = "RfxMaven"
url = uri(rfxMavenHost)
credentials {
username = rfxMavenUser
password = rfxMavenPass
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2023 Samū
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.runefox.rms.json;

import dev.runefox.json.Json;
import dev.runefox.json.codec.JsonCodec;
import dev.runefox.rms.Resource;
import dev.runefox.rms.ResourceManager;

import java.util.function.Supplier;

/**
* A {@link Json5ResourceType} that decodes the JSON tree using a {@link JsonCodec}. You don't need to implement this
* interface if you just want the default features, you can use {@link #create} to get an instance with basic
* functionality.
*
* @author Samū
* @since 1.1
*/
public interface Json5CodecResourceType<R extends Resource> extends JsonCodecResourceType<R>, Json5ResourceType<R> {
// I don't know which of the interfaces takes priority if we don't
// override these methods, so I'll override them just to be sure
// (plus it's probably clearer what's happening).

@Override
default Json json() {
return Json.json5();
}

@Override
default String extension() {
return "json5";
}

/**
* Creates a basic {@link Json5CodecResourceType} with a fixed codec and a supplied fallback instance. This is a
* convenience method to quickly have a resource type to load JSON resources.
*
* @param codec The codec.
* @param fallback The fallback supplier.
* @return A new {@link JsonCodecResourceType}.
*
* @since 1.1
*/
static <R extends Resource> Json5CodecResourceType<R> create(JsonCodec<R> codec, Supplier<R> fallback) {
return new Json5CodecResourceType<>() {
@Override
public JsonCodec<R> codec(ResourceManager res, String ns, String name) {
return codec;
}

@Override
public R createFallback(ResourceManager res, String ns, String name) {
return fallback.get();
}
};
}
}
41 changes: 41 additions & 0 deletions rms-json/src/main/java/dev/runefox/rms/json/Json5ResourceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 Samū
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.runefox.rms.json;

import dev.runefox.json.Json;
import dev.runefox.rms.Resource;

/**
* A {@link JsonResourceType} that uses {@linkplain Json#json5() JSON 5} to parse a resource.
*
* @author Samū
* @see JsonResourceType
* @see Json5CodecResourceType
* @since 1.1
*/
public interface Json5ResourceType<R extends Resource> extends JsonResourceType<R> {
@Override
default Json json() {
return Json.json5();
}

@Override
default String extension() {
return "json5";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2023 Samū
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.runefox.rms.json;

import dev.runefox.json.JsonNode;
import dev.runefox.json.codec.JsonCodec;
import dev.runefox.rms.Resource;
import dev.runefox.rms.ResourceManager;

import java.util.function.Supplier;

/**
* A {@link JsonResourceType} that decodes the JSON tree using a {@link JsonCodec}. You don't need to implement this
* interface if you just want the default features, you can use {@link #create} to get an instance with basic
* functionality.
*
* @author Samū
* @since 1.1
*/
public interface JsonCodecResourceType<R extends Resource> extends JsonResourceType<R> {
@Override
default R load(ResourceManager res, String ns, String name, JsonNode in) {
return codec(res, ns, name).decode(in);
}

/**
* Returns the {@link JsonCodec} to be used to decode resource instances.
*
* @param res The resource manager.
* @param ns The namespace.
* @param name The resource's name.
* @return The {@link JsonCodec} to decode with.
*
* @since 1.1
*/
JsonCodec<R> codec(ResourceManager res, String ns, String name);

/**
* Creates a basic {@link JsonCodecResourceType} with a fixed codec and a supplied fallback instance. This is a
* convenience method to quickly have a resource type to load JSON resources.
*
* @param codec The codec.
* @param fallback The fallback supplier.
* @return A new {@link JsonCodecResourceType}.
*
* @since 1.1
*/
static <R extends Resource> JsonCodecResourceType<R> create(JsonCodec<R> codec, Supplier<R> fallback) {
return new JsonCodecResourceType<>() {
@Override
public JsonCodec<R> codec(ResourceManager res, String ns, String name) {
return codec;
}

@Override
public R createFallback(ResourceManager res, String ns, String name) {
return fallback.get();
}
};
}
}
Loading

0 comments on commit e79bb35

Please sign in to comment.