Skip to content

Commit

Permalink
feat(SkinOverlay): Fixed (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed Apr 9, 2023
1 parent eb96495 commit 3ed625a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/src/main/java/com/georgev22/skinoverlay/SkinOverlay.java
Expand Up @@ -5,6 +5,8 @@
import com.georgev22.library.database.DatabaseWrapper;
import com.georgev22.library.maps.HashObjectMap;
import com.georgev22.library.maps.ObjectMap;
import com.georgev22.library.maps.ObjectMap.Pair;
import com.georgev22.library.maps.ObjectMap.PairDocument;
import com.georgev22.library.maps.ObservableObjectMap;
import com.georgev22.library.scheduler.SchedulerManager;
import com.georgev22.library.utilities.UserManager;
Expand All @@ -19,8 +21,10 @@
import com.georgev22.skinoverlay.utilities.OptionsUtil;
import com.georgev22.skinoverlay.utilities.PluginMessageUtils;
import com.georgev22.skinoverlay.utilities.Updater;
import com.georgev22.skinoverlay.utilities.gson.ObjectMapSPropertyTypeAdapter;
import com.georgev22.skinoverlay.utilities.interfaces.SkinOverlayImpl;
import com.georgev22.skinoverlay.utilities.player.PlayerObject;
import com.google.gson.reflect.TypeToken;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;
Expand Down Expand Up @@ -212,9 +216,9 @@ public List<String> getOverlayList() {
* @throws ClassNotFoundException When class is not found
*/
private void setupDatabase() throws Exception {
ObjectMap<String, ObjectMap.Pair<String, String>> map = new HashObjectMap<String, ObjectMap.Pair<String, String>>()
.append("user_id", ObjectMap.Pair.create("VARCHAR(38)", "NULL"))
.append("user_json", ObjectMap.Pair.create("LONGTEXT", "NULL"));
ObjectMap<String, Pair<String, String>> map = new HashObjectMap<String, Pair<String, String>>()
.append("user_id", Pair.create("VARCHAR(38)", "NULL"))
.append("user_json", Pair.create("LONGTEXT", "NULL"));
switch (OptionsUtil.DATABASE_TYPE.getStringValue()) {
case "MySQL" -> {
if (connection == null || connection.isClosed()) {
Expand Down Expand Up @@ -270,13 +274,14 @@ private void setupDatabase() throws Exception {
databaseWrapper = null;
mongoClient = null;
mongoDatabase = null;
this.userManager = new UserManager(UserManager.Type.JSON, new File(this.getDataFolder(), "userdata"), null).initializeGsonBuilder();
this.userManager = new UserManager(UserManager.Type.JSON, new File(this.getDataFolder(), "userdata"), null);
getLogger().log(Level.INFO, "[" + getDescription().name() + "] [" + getDescription().version() + "] Database: File");
}
}

//TODO TYPE ADAPTERS
this.userManager.initializeGsonBuilder();
this.userManager.registerTypeAdaptersByTypeToken(new PairDocument<>(
new Pair<>(new TypeToken<ObjectMap<String, Object>>() {
}, new ObjectMapSPropertyTypeAdapter())));

//userManager.loadAll();

Expand Down
@@ -0,0 +1,70 @@
package com.georgev22.skinoverlay.utilities.gson;

import com.georgev22.library.maps.HashObjectMap;
import com.georgev22.library.maps.ObjectMap;
import com.georgev22.skinoverlay.SkinOverlay;
import com.georgev22.skinoverlay.handler.SProperty;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.Map;

/**
* A Gson TypeAdapter for serializing and deserializing an {@link ObjectMap} with
* support for custom {@link SProperty} objects.
*/
public class ObjectMapSPropertyTypeAdapter extends TypeAdapter<ObjectMap<String, Object>> {

private final SkinOverlay skinOverlay = SkinOverlay.getInstance();

/**
* Writes the specified {@link ObjectMap} to the specified {@link JsonWriter}.
*
* @param out the {@link JsonWriter} to write to
* @param map the {@link ObjectMap} to write
* @throws IOException if an I/O error occurs
*/
@Override
public void write(JsonWriter out, ObjectMap<String, Object> map) throws IOException {
out.beginObject();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof SProperty) {
out.name("sProperty_" + key);
skinOverlay.getUserManager().getGson().toJson(value, SProperty.class, out);
} else {
out.name(key);
skinOverlay.getUserManager().getGson().toJson(value, Object.class, out);
}
}
out.endObject();
}

/**
* Reads an {@link ObjectMap} from the specified {@link JsonReader}.
*
* @param in the {@link JsonReader} to read from
* @return the deserialized {@link ObjectMap}
* @throws IOException if an I/O error occurs
*/
@Override
public ObjectMap<String, Object> read(JsonReader in) throws IOException {
ObjectMap<String, Object> map = new HashObjectMap<>();
in.beginObject();
while (in.hasNext()) {
String key = in.nextName();
if (key.startsWith("sProperty")) {
String sPropertyKey = key.substring("sProperty".length() + 1);
map.put(sPropertyKey, skinOverlay.getUserManager().getGson().fromJson(in, SProperty.class));
} else {
Object value = skinOverlay.getUserManager().getGson().fromJson(in, Object.class);
map.put(key, value);
}
}
in.endObject();
return map;
}
}

0 comments on commit 3ed625a

Please sign in to comment.