-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Binary storage for player data #4126
Draft
WalshyDev
wants to merge
1
commit into
master
Choose a base branch
from
feature/storage-phase-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/binary/BinaryStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package io.github.thebusybiscuit.slimefun4.storage.backend.binary; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import io.github.thebusybiscuit.slimefun4.core.debug.Debug; | ||
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase; | ||
import org.bukkit.Location; | ||
import org.bukkit.NamespacedKey; | ||
|
||
import io.github.bakedlibs.dough.nbt.TagType; | ||
import io.github.bakedlibs.dough.nbt.streams.CompressionType; | ||
import io.github.bakedlibs.dough.nbt.streams.TagInputStream; | ||
import io.github.bakedlibs.dough.nbt.streams.TagOutputStream; | ||
import io.github.bakedlibs.dough.nbt.tags.CompoundTag; | ||
import io.github.bakedlibs.dough.nbt.tags.ListTag; | ||
import io.github.bakedlibs.dough.nbt.tags.StringTag; | ||
import io.github.bakedlibs.dough.nbt.tags.Tag; | ||
import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint; | ||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack; | ||
import io.github.thebusybiscuit.slimefun4.api.researches.Research; | ||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; | ||
import io.github.thebusybiscuit.slimefun4.storage.Storage; | ||
import io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers.LocationSerializer; | ||
import io.github.thebusybiscuit.slimefun4.storage.data.PlayerData; | ||
|
||
public class BinaryStorage implements Storage { | ||
|
||
private static final NamespacedKey PLAYER_DATA = new NamespacedKey(Slimefun.instance(), "player_data"); | ||
private static final NamespacedKey RESEARCHES_KEY = new NamespacedKey(Slimefun.instance(), "researches"); | ||
private static final NamespacedKey BACKPACKS_KEY = new NamespacedKey(Slimefun.instance(), "backpacks"); | ||
private static final NamespacedKey WAYPOINTS_KEY = new NamespacedKey(Slimefun.instance(), "waypoints"); | ||
|
||
// TODO: Move to lz4 or zstd | ||
private static final CompressionType compression = CompressionType.GZIP; | ||
|
||
/* | ||
* Structure as JSON: | ||
* { | ||
* "slimefun:researches": ["slimefun:some_research", "slimefun:some_other_research"], | ||
* "slimefun:backpacks": [ | ||
* { | ||
* "id": x, | ||
* "item": y | ||
* } | ||
* ], | ||
* "slimefun:waypoints": { | ||
* "<ID>": { | ||
* "name": "", | ||
* "location": {} | ||
* } | ||
* } | ||
* } | ||
*/ | ||
|
||
@Override | ||
public PlayerData loadPlayerData(UUID uuid) { | ||
Debug.log(TestCase.PLAYER_PROFILE_DATA, "Loading player data from binary storage for {}", uuid); | ||
File file = new File("data-storage/Slimefun/Players/" + uuid + ".dat"); | ||
|
||
if (!file.exists()) { | ||
return new PlayerData(Set.of(), Map.of(), Set.of()); | ||
} | ||
|
||
CompoundTag root; | ||
try { | ||
try (TagInputStream stream = new TagInputStream(new FileInputStream(file), compression)) { | ||
root = (CompoundTag) stream.readTag(); | ||
} | ||
} catch(IOException e) { | ||
throw new IllegalStateException("Failed to read Player data for " + uuid, e); | ||
} | ||
|
||
// Load researches | ||
ListTag<StringTag> list = root.getList(RESEARCHES_KEY, TagType.STRING); | ||
|
||
Set<Research> researches = new HashSet<>(); | ||
for (Research research : Slimefun.getRegistry().getResearches()) { | ||
for (StringTag tag : list) { | ||
if (tag.getValue().equals(research.getKey().toString())) { | ||
researches.add(research); | ||
} | ||
} | ||
} | ||
|
||
// Load backpacks | ||
Map<Integer, PlayerBackpack> backpacks = Map.of(); | ||
CompoundTag backpackMap = root.getCompound(BACKPACKS_KEY); | ||
// TODO: Item serialization | ||
|
||
// Load waypoints | ||
Set<Waypoint> waypoints = new HashSet<>(); | ||
CompoundTag waypointMap = root.getCompound(WAYPOINTS_KEY); | ||
for (Map.Entry<NamespacedKey, Tag<?>> key : waypointMap) { | ||
CompoundTag waypoint = (CompoundTag) key.getValue(); | ||
|
||
String name = waypoint.getString(CommonKeys.NAME); | ||
Location location = LocationSerializer.INSTANCE.deserialize(waypoint.getCompound(CommonKeys.LOCATION)); | ||
|
||
waypoints.add(new Waypoint(uuid, key.getKey().getKey(), location, name)); | ||
} | ||
|
||
return new PlayerData(researches, backpacks, waypoints); | ||
} | ||
|
||
@Override | ||
public void savePlayerData(UUID uuid, PlayerData data) { | ||
Debug.log(TestCase.PLAYER_PROFILE_DATA, "Saving player data from binary storage for {}", uuid); | ||
File file = new File("data-storage/Slimefun/Players/" + uuid + ".dat"); | ||
|
||
CompoundTag root = new CompoundTag(PLAYER_DATA); | ||
|
||
// Save researches | ||
ListTag<StringTag> list = new ListTag<>(); | ||
for (Research research : data.getResearches()) { | ||
list.add(new StringTag(research.getKey().toString())); | ||
} | ||
|
||
root.putList(RESEARCHES_KEY, list); | ||
|
||
// Save backpacks | ||
CompoundTag backpackMap = new CompoundTag(); | ||
|
||
// Save waypoints | ||
CompoundTag waypointMap = new CompoundTag(); | ||
for (Waypoint waypoint : data.getWaypoints()) { | ||
CompoundTag waypointTag = new CompoundTag(); | ||
|
||
waypointTag.putString(CommonKeys.ID, waypoint.getId()); | ||
waypointTag.putString(CommonKeys.NAME, waypoint.getName()); | ||
waypointTag.putCompound(CommonKeys.LOCATION, LocationSerializer.INSTANCE.serialize(waypoint.getLocation())); | ||
|
||
waypointMap.putCompound(new NamespacedKey(Slimefun.instance(), waypoint.getId()), waypointTag); | ||
} | ||
root.putCompound(WAYPOINTS_KEY, waypointMap); | ||
|
||
try { | ||
try (TagOutputStream stream = new TagOutputStream(new FileOutputStream(file), compression)) { | ||
stream.writeTag(root); | ||
} | ||
} catch(IOException e) { | ||
throw new IllegalStateException("Failed to read Player data for " + uuid, e); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/binary/CommonKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.github.thebusybiscuit.slimefun4.storage.backend.binary; | ||
|
||
import org.bukkit.NamespacedKey; | ||
|
||
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; | ||
|
||
public class CommonKeys { | ||
|
||
public static final NamespacedKey ID = new NamespacedKey(Slimefun.instance(), "id"); | ||
public static final NamespacedKey NAME = new NamespacedKey(Slimefun.instance(), "name"); | ||
// Location stuff | ||
public static final NamespacedKey LOCATION = new NamespacedKey(Slimefun.instance(), "location"); | ||
public static final NamespacedKey WORLD = new NamespacedKey(Slimefun.instance(), "world"); | ||
public static final NamespacedKey POSITION = new NamespacedKey(Slimefun.instance(), "position"); | ||
public static final NamespacedKey PITCH = new NamespacedKey(Slimefun.instance(), "pitch"); | ||
public static final NamespacedKey YAW = new NamespacedKey(Slimefun.instance(), "yaw"); | ||
Comment on lines
+13
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why aren't these in |
||
} |
10 changes: 10 additions & 0 deletions
10
...o/github/thebusybiscuit/slimefun4/storage/backend/binary/serializers/BinarySerialize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers; | ||
|
||
import io.github.bakedlibs.dough.nbt.tags.Tag; | ||
|
||
public interface BinarySerialize<O, T extends Tag<?>> { | ||
|
||
public T serialize(O obj); | ||
|
||
public O deserialize(T obj); | ||
} |
60 changes: 60 additions & 0 deletions
60
...ithub/thebusybiscuit/slimefun4/storage/backend/binary/serializers/LocationSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers; | ||
|
||
import java.util.UUID; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
|
||
import io.github.bakedlibs.dough.blocks.BlockPosition; | ||
import io.github.bakedlibs.dough.nbt.tags.CompoundTag; | ||
import io.github.bakedlibs.dough.nbt.tags.IntArrayTag; | ||
import io.github.thebusybiscuit.slimefun4.storage.backend.binary.CommonKeys; | ||
|
||
public class LocationSerializer implements BinarySerialize<Location, CompoundTag> { | ||
|
||
public static final LocationSerializer INSTANCE = new LocationSerializer(); | ||
|
||
private LocationSerializer() {} | ||
|
||
@Override | ||
public CompoundTag serialize(Location location) { | ||
CompoundTag tag = new CompoundTag(); | ||
|
||
if (location.getWorld() != null) { | ||
tag.put(CommonKeys.WORLD, UUIDSerializer.INSTANCE.serialize(location.getWorld().getUID())); | ||
} | ||
|
||
tag.putDouble(CommonKeys.POSITION, BlockPosition.getAsLong(location)); | ||
|
||
if (location.getPitch() != 0.0F) { | ||
tag.putFloat(CommonKeys.PITCH, location.getPitch()); | ||
} | ||
if (location.getYaw() != 0.0F) { | ||
tag.putFloat(CommonKeys.YAW, location.getYaw()); | ||
} | ||
|
||
return tag; | ||
} | ||
|
||
@Override | ||
public Location deserialize(CompoundTag location) { | ||
UUID worldUuid = null; | ||
IntArrayTag worldTag = (IntArrayTag) location.get(CommonKeys.WORLD); | ||
if (worldTag != null) { | ||
worldUuid = UUIDSerializer.INSTANCE.deserialize(worldTag); | ||
} | ||
|
||
BlockPosition position = new BlockPosition(null, location.getLong(CommonKeys.POSITION).getAsLong()); | ||
|
||
double x = position.getX(); | ||
double y = position.getY(); | ||
double z = position.getZ(); | ||
float pitch = location.getFloat(CommonKeys.PITCH).orElse(0.0f); | ||
float yaw = location.getFloat(CommonKeys.YAW).orElse(0.0f); | ||
|
||
return new Location( | ||
worldUuid != null ? Bukkit.getWorld(worldUuid) : null, | ||
x, y, z, yaw, pitch | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...io/github/thebusybiscuit/slimefun4/storage/backend/binary/serializers/UUIDSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.github.thebusybiscuit.slimefun4.storage.backend.binary.serializers; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.apache.commons.lang.Validate; | ||
|
||
import io.github.bakedlibs.dough.nbt.tags.IntArrayTag; | ||
|
||
public class UUIDSerializer implements BinarySerialize<UUID, IntArrayTag> { | ||
|
||
public static final UUIDSerializer INSTANCE = new UUIDSerializer(); | ||
|
||
private UUIDSerializer() {} | ||
|
||
@Override | ||
public IntArrayTag serialize(@Nonnull UUID uuid) { | ||
Validate.notNull(uuid, "The provided UUID cannot be null"); | ||
|
||
long mostSig = uuid.getMostSignificantBits(); | ||
long leastSig = uuid.getLeastSignificantBits(); | ||
int[] ints = new int[] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NBT has a long array type, I suggest we use that instead of an int array |
||
(int) (mostSig >> 32), | ||
(int) mostSig, | ||
(int) (leastSig >> 32), | ||
(int) leastSig | ||
}; | ||
|
||
return new IntArrayTag(ints); | ||
} | ||
|
||
@Override | ||
public UUID deserialize(@Nonnull IntArrayTag uuid) { | ||
Validate.notNull(uuid, "The provided UUID cannot be null"); | ||
|
||
int[] ints = uuid.getValue(); | ||
return new UUID( | ||
(long) ints[0] << 32L | ints[1] & 0xFFFFFFFFL, | ||
(long) ints[2] << 32L | ints[3] & 0xFFFFFFFFL | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we use a
BinarySerializer
for this?