-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccada10
commit fd8e7df
Showing
4 changed files
with
22 additions
and
21 deletions.
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
27 changes: 15 additions & 12 deletions
27
...vastar/protoweaver/core/util/Furious.java → ...otoweaver/core/util/ObjectSerializer.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 |
---|---|---|
@@ -1,48 +1,51 @@ | ||
package me.mrnavastar.protoweaver.core.util; | ||
|
||
import me.mrnavastar.r.R; | ||
import org.apache.fury.BaseFury; | ||
import org.apache.fury.Fury; | ||
import org.apache.fury.ThreadSafeFury; | ||
import org.apache.fury.exception.InsecureException; | ||
import org.apache.fury.logging.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Furious { | ||
public class ObjectSerializer { | ||
|
||
private final ThreadSafeFury fury = Fury.builder().withJdkClassSerializableCheck(false).buildThreadSafeFury(); | ||
|
||
static { | ||
// Make fury be quiet | ||
LoggerFactory.disableLogging(); | ||
} | ||
|
||
private static void recursiveRegister(BaseFury fury, Class<?> type, List<Class<?>> registered) { | ||
private void recursiveRegister(Class<?> type, List<Class<?>> registered) { | ||
if (type == null || type == Object.class || registered.contains(type)) return; | ||
fury.register(type); | ||
registered.add(type); | ||
|
||
List.of(type.getDeclaredFields()).forEach(field -> recursiveRegister(fury, field.getType(), registered)); | ||
List.of(R.of(type).generics()).forEach(t -> recursiveRegister(fury, t, registered)); | ||
if (!type.isEnum()) recursiveRegister(fury, type.getSuperclass(), registered); | ||
List.of(type.getDeclaredFields()).forEach(field -> recursiveRegister(field.getType(), registered)); | ||
List.of(R.of(type).generics()).forEach(t -> recursiveRegister(t, registered)); | ||
if (!type.isEnum()) recursiveRegister(type.getSuperclass(), registered); | ||
} | ||
|
||
public static void register(BaseFury fury, Class<?> type) { | ||
recursiveRegister(fury, type, new ArrayList<>()); | ||
public void register(Class<?> type) { | ||
recursiveRegister(type, new ArrayList<>()); | ||
} | ||
|
||
public static byte[] serialize(BaseFury fury, Object object) throws InsecureException { | ||
public byte[] serialize(Object object) throws InsecureException { | ||
try { | ||
return fury.serialize(object); | ||
} catch (InsecureException e) { | ||
throw new InsecureException("unregistered packet: " + object.getClass().getName()); | ||
throw new InsecureException("unregistered object: " + object.getClass().getName()); | ||
} | ||
} | ||
|
||
public static Object deserialize(BaseFury fury, byte[] bytes) throws InsecureException { | ||
public Object deserialize(byte[] bytes) throws InsecureException { | ||
try { | ||
return fury.deserialize(bytes); | ||
} catch (InsecureException e) { | ||
String packet = e.getMessage().split(" is not registered")[0].replace("class ", ""); | ||
throw new InsecureException("unregistered packet: " + packet); | ||
throw new InsecureException("unregistered object: " + packet); | ||
} | ||
} | ||
} |
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