Compare commits

...

10 commits

13 changed files with 455 additions and 47 deletions

View file

@ -17,9 +17,11 @@ plugins {
group = "dev.zontreck"
archivesBaseName = "Registry"
java.toolchain.languageVersion = JavaLanguageVersion.of(17)
java {
toolchain.languageVersion = JavaLanguageVersion.of(17)
withSourcesJar()
withJavadocJar()
}
def determinePatchVersion = {
@ -42,40 +44,34 @@ repositories {
// Use Maven Central for resolving dependencies.
// mavenCentral()
maven {
url = "https://maven.zontreck.dev/repository/internal"
url = "https://maven.zontreck.com/repository/internal"
name = "Aria's Creations Caches"
}
maven {
url = "https://maven.zontreck.dev/repository/zontreck"
url = "https://maven.zontreck.com/repository/zontreck"
name = "Aria's Creations"
}
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'
implementation "dev.zontreck:EventsBus:${Bus_API}.${Bus_Patch}"
api "dev.zontreck:EventsBus:${Bus_API}.${Bus_Patch}:sources"
}
def MAVEN_PASSWORD_PROPERTY = "AriasCreationsMavenPassword"
publishing {
publications {
mavenJava(MavenPublication) {
artifact sourcesJar
artifact jar
artifact sourcesJar
artifact javadocJar
}
}
repositories {
maven {
url = "https://maven.zontreck.dev/repository/zontreck"
url = "https://maven.zontreck.com/repository/zontreck"
name = "ariascreations"
if (project.findProperty(MAVEN_PASSWORD_PROPERTY) != null) {
credentials {
@ -87,6 +83,7 @@ publishing {
}
}
compileJava.finalizedBy("sourcesJar")
artifacts {
archives jar

View file

@ -1,3 +1,3 @@
apiVer = 1.0
Bus_API = 1.0
Bus_Patch = 15
apiVer=1.0
Bus_API=1.0
Bus_Patch=32

View file

@ -0,0 +1,15 @@
package dev.zontreck.registry;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public abstract class Header {
public abstract byte Version();
public abstract byte getPatch();
public abstract void Read(DataInputStream dis) throws IOException;
public abstract void Write(DataOutputStream dos) throws IOException;
}

View file

@ -0,0 +1,38 @@
package dev.zontreck.registry;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class HeaderV1 extends Header {
public byte curPatch = 2;
public String Creator = Constants.Creator;
@Override
public void Read(DataInputStream dis) throws IOException {
curPatch = (byte) dis.readInt();
Creator = dis.readUTF();
dis.readNBytes(16);
}
@Override
public void Write(DataOutputStream dos) throws IOException {
HeaderV1 v1 = new HeaderV1();
dos.writeInt(v1.Version());
dos.writeInt(v1.getPatch());
dos.writeUTF(v1.Creator);
dos.write(new byte[16]);
}
@Override
public byte Version() {
return 1;
}
@Override
public byte getPatch() {
return curPatch;
}
}

View file

@ -0,0 +1,49 @@
package dev.zontreck.registry;
import java.io.*;
public class HeaderV2 extends Header {
public byte curPatch = 4;
public String FormatSig = Constants.Extension;
public String CreatorSig = Constants.Creator;
public int LastSavedAt;
@Override
public void Read(DataInputStream dis) throws IOException {
byte[] remain = dis.readNBytes(127);
ByteArrayInputStream bais = new ByteArrayInputStream(remain);
DataInputStream dr = new DataInputStream(bais);
curPatch = dr.readByte();
FormatSig = dr.readUTF();
CreatorSig = dr.readUTF();
LastSavedAt = dr.readInt();
}
@Override
public void Write(DataOutputStream dos) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
DataOutputStream dw = new DataOutputStream(baos);
dw.writeByte(Version());
dw.writeByte(getPatch());
dw.writeUTF(FormatSig);
dw.writeUTF(CreatorSig);
dw.writeInt(LastSavedAt);
dos.write(baos.toByteArray());
}
@Override
public byte Version() {
return 2;
}
@Override
public byte getPatch() {
return curPatch;
}
}

View file

@ -0,0 +1,52 @@
package dev.zontreck.registry;
import java.io.*;
public class HeaderV3 extends Header {
public byte curPatch = 0;
public String FormatSig = Constants.Extension;
public String CreatorSig = Constants.Creator;
public long LastSavedAt;
public int NumOfTags;
@Override
public void Read(DataInputStream dis) throws IOException {
byte[] remain = dis.readNBytes(127);
ByteArrayInputStream bais = new ByteArrayInputStream(remain);
DataInputStream dr = new DataInputStream(bais);
curPatch = dr.readByte();
FormatSig = dr.readUTF();
CreatorSig = dr.readUTF();
LastSavedAt = dr.readLong();
NumOfTags = dr.readInt();
}
@Override
public void Write(DataOutputStream dos) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
DataOutputStream dw = new DataOutputStream(baos);
dw.writeByte(Version());
dw.writeByte(getPatch());
dw.writeUTF(FormatSig);
dw.writeUTF(CreatorSig);
dw.writeLong(LastSavedAt);
dw.writeInt(NumOfTags);
dos.write(baos.toByteArray());
}
@Override
public byte Version() {
return 3;
}
@Override
public byte getPatch() {
return curPatch;
}
}

View file

@ -0,0 +1,35 @@
package dev.zontreck.registry;
import dev.zontreck.registry.v3.Tag;
import java.io.*;
public class NbtIO {
public static void Save(String filename, Tag tag) {
// proceed!
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename));
tag.Write(dos);
tag.WriteValue(dos);
dos.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Tag Load(String file) {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(file));
Tag ret = Tag.Read(dis);
ret.ReadValue(dis);
return ret;
} catch (IOException e) {
throw new RuntimeException();
}
}
}

View file

@ -1,10 +1,26 @@
Registry v3
==========
Registry v3 uses the same byte IDs and naming scheme as NBT. However, Registry includes a file header for some other miscellaneous data.
Registry v3 uses the same byte IDs and naming scheme as NBT. However, Registry includes a file header for some other
miscellaneous data.
Registry does technically include a method for saving as NBT and reading NBT. But, this is not the main priority. Ease of use was the main goal here. If you wish to contribute to this project and add full NBT support, you may do so.
Registry does technically include a method for saving as NBT and reading NBT. But, this is not the main priority. Ease
of use was the main goal here. If you wish to contribute to this project and add full NBT support, you may do so.
NOTE: Registry also introduces other tag types. Any tag type not found in the base NBT specification includes a converter method. Please be sure to use those when utilizing NBT instead of Registry data.
NOTE: Registry also introduces other tag types. Any tag type not found in the base NBT specification includes a
converter method. Please be sure to use those when utilizing NBT instead of Registry data.
Format Specification:
```
{ 'Total Length' 128 bytes
'Version' 1 byte
'Patch' 1 byte
'Format signature' variable bytes - UTF String
'Creator signature' variable bytes - UTF String prefixed with a 32-bit integer
'Timestamp' 8 bytes
'Number of tags' 4 bytes
}
```
https://github.com/zontreck/TP.Java.Registry

View file

@ -0,0 +1,83 @@
package dev.zontreck.registry;
import dev.zontreck.registry.v3.Tag;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class RegistryFile {
public Header RegistryHeader;
public Map<String, Tag> Tags = new HashMap<>();
public void MakeHeader(byte version) {
switch (version) {
case 1: {
RegistryHeader = new HeaderV1();
break;
}
case 2: {
RegistryHeader = new HeaderV2();
break;
}
case 3: {
HeaderV3 v3 = new HeaderV3();
v3.NumOfTags = Tags.size();
RegistryHeader = v3;
break;
}
}
}
public void SaveToFile(String filename) {
try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(RegistryIO.getFileFor(filename)));
RegistryHeader.Write(dos);
for (Map.Entry<String, Tag> entry :
Tags.entrySet()) {
dos.writeUTF(entry.getKey());
entry.getValue().Write(dos);
entry.getValue().WriteValue(dos);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static RegistryFile LoadFromFile(String filename) {
try {
DataInputStream dis = new DataInputStream(new FileInputStream(RegistryIO.getFileFor(filename)));
RegistryFile file = new RegistryFile();
file.RegistryHeader = RegistryIO.ReadHeader(dis);
if (file.RegistryHeader.Version() >= 3) {
// This contains more than one tag!
if (file.RegistryHeader instanceof HeaderV3 v3) {
for (int i = 0; i < v3.NumOfTags; i++) {
String name = dis.readUTF();
Tag tag = Tag.Read(dis);
tag.ReadValue(dis);
file.Tags.put(name, tag);
}
}
} else {
// This contains one tag only, read it, append it to the map with name 'root'
Tag tag = Tag.Read(dis);
tag.ReadValue(dis);
file.Tags.put("root", tag);
}
return file;
} catch (IOException e) {
throw new RuntimeException();
}
}
}

View file

@ -1,47 +1,93 @@
package dev.zontreck.registry;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Instant;
public class RegistryIO
{
public static Path getGlobalPath()
{
public class RegistryIO {
public static Path getGlobalPath() {
Path P = Path.of(System.getProperty("user.home"));
return P.resolve("." + Constants.HSRDGlobalStorage);
}
public static File getFileFor(String name)
{
public static File getFileFor(String name) {
return getGlobalPath().resolve(name + "." + Constants.Extension).toFile();
}
public static File getNBTFor(String name)
{
public static File getNBTFor(String name) {
return getGlobalPath().resolve(name + ".dat").toFile();
}
public static final byte Version = 3;
public static final byte Patch = 0;
public static void writeHeaderV3(DataOutputStream dos) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
DataOutputStream dos2 = new DataOutputStream(baos);
public static void WriteHeader(DataOutputStream dos, int numOfTags) {
switch (Version) {
case 1: {
try {
HeaderV1 v1 = new HeaderV1();
v1.Write(dos);
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
case 2: {
try {
HeaderV2 v2 = new HeaderV2();
v2.LastSavedAt = (int) Instant.now().getEpochSecond();
dos2.writeByte(Version);
dos2.writeByte(Patch);
v2.Write(dos);
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
case 3: {
try {
HeaderV3 v3 = new HeaderV3();
v3.curPatch = Patch;
v3.NumOfTags = numOfTags;
v3.LastSavedAt = Instant.now().getEpochSecond();
dos2.writeUTF(Constants.Extension);
dos2.writeUTF(Constants.Creator);
dos2.writeLong(Instant.now().getEpochSecond());
dos.write(baos.toByteArray());
v3.Write(dos);
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
}
}
}
public static Header ReadHeader(DataInputStream dis) throws IOException {
byte ver = dis.readByte();
switch (ver) {
case 1: {
HeaderV1 v1 = new HeaderV1();
v1.Read(dis);
return v1;
}
case 2: {
HeaderV2 v2 = new HeaderV2();
v2.Read(dis);
return v2;
}
case 3: {
HeaderV3 v3 = new HeaderV3();
v3.Read(dis);
return v3;
}
}
return null;
}
}

View file

@ -1,6 +1,10 @@
package dev.zontreck.registry.v3;
import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public abstract class Tag {
public abstract Type getType();
@ -50,8 +54,7 @@ public abstract class Tag {
public abstract void ReadValue(DataInputStream dis) throws IOException;
public byte[] Serialize()
{
public byte[] Serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
@ -64,8 +67,7 @@ public abstract class Tag {
return baos.toByteArray();
}
public static Tag Deserialize(byte[] array)
{
public static Tag Deserialize(byte[] array) {
ByteArrayInputStream bais = new ByteArrayInputStream(array);
DataInputStream dis = new DataInputStream(bais);
try {
@ -73,8 +75,7 @@ public abstract class Tag {
NTag.ReadValue(dis);
return NTag;
}catch (IOException e)
{
} catch (IOException e) {
throw new RuntimeException();
}
}
@ -170,4 +171,11 @@ public abstract class Tag {
return new LongArrayTag();
}
public UUIDTag asUUID() {
if (this instanceof UUIDTag ut)
return ut;
else
return new UUIDTag();
}
}

View file

@ -14,7 +14,10 @@ public enum Type {
Key(10),
IntArray(11),
LongArray(12),
Boolean(13);
// HERE STARTS THE NON-STANDARD TAG TYPES SPECIFIC TO THIS LIBRARY
Boolean(13),
UUID(14);
byte value;
@ -37,6 +40,7 @@ public enum Type {
TagTypeRegistry.RegisterType(IntArray, IntArrayTag.class);
TagTypeRegistry.RegisterType(LongArray, LongArrayTag.class);
TagTypeRegistry.RegisterType(Boolean, BooleanTag.class);
TagTypeRegistry.RegisterType(UUID, UUIDTag.class);
}
public static Type valueOf(byte b) {

View file

@ -0,0 +1,65 @@
package dev.zontreck.registry.v3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;
public class UUIDTag extends Tag {
long lsb;
long msb;
public UUID value() {
return new UUID(msb, lsb);
}
public UUIDTag() {
UUID id = UUID.randomUUID();
lsb = id.getLeastSignificantBits();
msb = id.getMostSignificantBits();
}
public UUIDTag(UUID ID) {
lsb = ID.getLeastSignificantBits();
msb = ID.getMostSignificantBits();
}
public UUIDTag withLeastSignificantBits(long val) {
lsb = val;
return this;
}
public UUIDTag withMostSignificantBits(long val) {
msb = val;
return this;
}
@Override
public Type getType() {
return Type.UUID;
}
@Override
public String getCanonicalName() {
return "TAG_UUID";
}
@Override
public void WriteValue(DataOutputStream dos) throws IOException {
dos.writeLong(msb);
dos.writeLong(lsb);
}
@Override
public void ReadValue(DataInputStream dis) throws IOException {
msb = dis.readLong();
lsb = dis.readLong();
}
@Override
public String PrettyPrint(int indent, String name) {
String builder = super.PrettyPrint(indent, name);
builder += ": " + new UUID(msb, lsb);
return builder;
}
}