mirror of
https://github.com/ProjectSWGCore/NGECore2.git
synced 2026-09-28 13:12:54 -04:00
Baseline changes, cleaned some things
This commit is contained in:
+121
-124
@@ -22,7 +22,6 @@
|
||||
package resources.objects;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
@@ -30,6 +29,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.UnaryOperator;
|
||||
@@ -40,62 +40,62 @@ import main.NGECore;
|
||||
import org.apache.mina.core.buffer.IoBuffer;
|
||||
import org.python.google.common.collect.Lists;
|
||||
|
||||
import com.sleepycat.persist.model.NotPersistent;
|
||||
import com.sleepycat.persist.model.Persistent;
|
||||
|
||||
import engine.resources.objects.Baseline;
|
||||
import engine.resources.objects.Delta;
|
||||
import engine.resources.objects.IDelta;
|
||||
import engine.resources.objects.SWGObject;
|
||||
|
||||
/* A SWGList element MUST implement IDelta, or it will refuse to work with it */
|
||||
/* A SWGList element should extend Delta or implement IDelta */
|
||||
|
||||
@Persistent
|
||||
public class SWGList<E> implements List<E>, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private List<E> list = new ArrayList<E>();
|
||||
@NotPersistent
|
||||
private transient int updateCounter = 1;
|
||||
|
||||
private List<E> list = new CopyOnWriteArrayList<E>();
|
||||
private transient int updateCounter = 0;
|
||||
private long objectId;
|
||||
private transient ObjectMessageBuilder messageBuilder;
|
||||
private byte viewType;
|
||||
private short updateType;
|
||||
@NotPersistent
|
||||
private boolean addByte;
|
||||
protected transient Object objectMutex = new Object();
|
||||
private transient SWGObject object;
|
||||
|
||||
public SWGList() { }
|
||||
|
||||
public SWGList(long objectId, int viewType, int updateType) {
|
||||
this.objectId = objectId;
|
||||
public SWGList(SWGObject object, int viewType, int updateType, boolean addByte) {
|
||||
this.objectId = object.getObjectID();
|
||||
this.viewType = (byte) viewType;
|
||||
this.updateType = (short) updateType;
|
||||
this.addByte = addByte;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
objectMutex = new Object();
|
||||
updateCounter = 1;
|
||||
updateCounter = 0;
|
||||
object = NGECore.getInstance().objectService.getObject(objectId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
synchronized(objectMutex) {
|
||||
if (list.add(e) && e instanceof IDelta) {
|
||||
queue(item(1, list.lastIndexOf(e), ((IDelta) e).getBytes(), true, true));
|
||||
if (valid(e) && list.add(e)) {
|
||||
queue(item(1, list.lastIndexOf(e), Baseline.toBytes(e), true, true));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(int index, E element) {
|
||||
synchronized(objectMutex) {
|
||||
if (element instanceof IDelta) {
|
||||
if (valid(element)) {
|
||||
list.add(index, element);
|
||||
queue(item(1, index, ((IDelta) element).getBytes(), true, true));
|
||||
queue(item(1, index, Baseline.toBytes(element), true, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
synchronized(objectMutex) {
|
||||
if (!c.isEmpty()) {
|
||||
@@ -103,9 +103,9 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
boolean success = false;
|
||||
|
||||
for (E element : c) {
|
||||
if (element instanceof IDelta) {
|
||||
if (valid(element)) {
|
||||
if (list.add(element)) {
|
||||
buffer.add(item(1, list.lastIndexOf(element), ((IDelta) element).getBytes(), true, true));
|
||||
buffer.add(item(1, list.lastIndexOf(element), Baseline.toBytes(element), true, true));
|
||||
success = true;
|
||||
}
|
||||
} else {
|
||||
@@ -124,16 +124,15 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(int index, Collection<? extends E> c) {
|
||||
synchronized(objectMutex) {
|
||||
if (!c.isEmpty()) {
|
||||
List<byte[]> buffer = new ArrayList<byte[]>();
|
||||
|
||||
for (E element : c) {
|
||||
if (element instanceof IDelta) {
|
||||
if (valid(element)) {
|
||||
list.add(index, element);
|
||||
buffer.add(item(1, index, ((IDelta) element).getBytes(), true, true));
|
||||
buffer.add(item(1, index, Baseline.toBytes(element), true, true));
|
||||
index++;
|
||||
} else {
|
||||
return false;
|
||||
@@ -149,7 +148,6 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
synchronized(objectMutex) {
|
||||
list.clear();
|
||||
@@ -157,21 +155,22 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
synchronized(objectMutex) {
|
||||
return list.contains(o);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
synchronized(objectMutex) {
|
||||
return list.containsAll(c);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super E> action) {
|
||||
list.forEach(action);
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
synchronized(objectMutex) {
|
||||
return list.get(index);
|
||||
@@ -182,49 +181,48 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(Object o) {
|
||||
synchronized(objectMutex) {
|
||||
return list.indexOf(o);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
synchronized(objectMutex) {
|
||||
return list.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
synchronized(objectMutex) {
|
||||
return list.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(Object o) {
|
||||
synchronized(objectMutex) {
|
||||
return list.lastIndexOf(o);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator() {
|
||||
synchronized(objectMutex) {
|
||||
return list.listIterator();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListIterator<E> listIterator(int index) {
|
||||
synchronized(objectMutex) {
|
||||
return listIterator(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<E> parallelStream() {
|
||||
synchronized(objectMutex) {
|
||||
return list.parallelStream();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
synchronized(objectMutex) {
|
||||
int index = list.indexOf(o);
|
||||
@@ -238,7 +236,6 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove(int index) {
|
||||
synchronized(objectMutex) {
|
||||
E element = list.remove(index);
|
||||
@@ -249,7 +246,6 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
synchronized(objectMutex) {
|
||||
if (!c.isEmpty()) {
|
||||
@@ -277,20 +273,34 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAll(UnaryOperator<E> operator) {
|
||||
System.err.println("SWGList::sort: Not currently supported.");
|
||||
}
|
||||
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
synchronized(objectMutex) {
|
||||
return list.retainAll(c);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> reverseGet() {
|
||||
synchronized(objectMutex) {
|
||||
return Lists.reverse(list);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean removeIf(Predicate<? super E> filter) {
|
||||
synchronized(objectMutex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public E set(int index, E element) {
|
||||
synchronized(objectMutex) {
|
||||
if (element instanceof IDelta) {
|
||||
if (valid(element)) {
|
||||
E previousElement = list.set(index, element);
|
||||
|
||||
queue(item(2, index, ((IDelta) element).getBytes(), true, true));
|
||||
queue(item(2, index, Baseline.toBytes(element), true, true));
|
||||
|
||||
return previousElement;
|
||||
}
|
||||
@@ -305,10 +315,10 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
|
||||
if (!list.isEmpty()) {
|
||||
for (E element : list) {
|
||||
if (element instanceof IDelta) {
|
||||
IoBuffer buffer = Delta.createBuffer((newListData.length + ((IDelta) element).getBytes().length));
|
||||
if (valid(element)) {
|
||||
IoBuffer buffer = Delta.createBuffer((newListData.length + Baseline.toBytes(element).length));
|
||||
buffer.put(newListData);
|
||||
buffer.put(((IDelta) element).getBytes());
|
||||
buffer.put(Baseline.toBytes(element));
|
||||
newListData = buffer.array();
|
||||
} else {
|
||||
return false;
|
||||
@@ -327,28 +337,42 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
synchronized(objectMutex) {
|
||||
return list.size();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(Comparator<? super E> c) {
|
||||
synchronized(objectMutex) {
|
||||
System.err.println("SWGList::sort: Not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public Spliterator<E> spliterator() {
|
||||
synchronized(objectMutex) {
|
||||
return list.spliterator();
|
||||
}
|
||||
}
|
||||
|
||||
public Stream<E> stream() {
|
||||
synchronized(objectMutex) {
|
||||
return list.stream();
|
||||
}
|
||||
}
|
||||
|
||||
public List<E> subList(int fromIndex, int toIndex) {
|
||||
synchronized(objectMutex) {
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
synchronized(objectMutex) {
|
||||
return list.toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
synchronized(objectMutex) {
|
||||
return list.toArray(a);
|
||||
@@ -356,25 +380,61 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
|
||||
public int getUpdateCounter() {
|
||||
return updateCounter;
|
||||
synchronized(objectMutex) {
|
||||
return updateCounter;
|
||||
}
|
||||
}
|
||||
|
||||
public Object getMutex() {
|
||||
return objectMutex;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
synchronized(objectMutex) {
|
||||
byte[] objects = { };
|
||||
int size = 0;
|
||||
|
||||
for (Object o : list) {
|
||||
byte[] object = Baseline.toBytes(o);
|
||||
size += object.length;
|
||||
|
||||
IoBuffer buffer = Delta.createBuffer(size);
|
||||
buffer.put(objects);
|
||||
if (addByte) buffer.put((byte) 0);
|
||||
buffer.put(object);
|
||||
buffer.flip();
|
||||
|
||||
objects = buffer.array();
|
||||
}
|
||||
|
||||
IoBuffer buffer = Delta.createBuffer(8 + size);
|
||||
buffer.putInt(list.size());
|
||||
buffer.putInt(updateCounter);
|
||||
buffer.put(objects);
|
||||
buffer.flip();
|
||||
|
||||
return buffer.array();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean valid(Object o) {
|
||||
if (o instanceof String || o instanceof Byte || o instanceof Short ||
|
||||
o instanceof Integer || o instanceof Float || o instanceof Long ||
|
||||
o instanceof IDelta) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] item(int type, int index, byte[] data, boolean useIndex, boolean useData) {
|
||||
int size = 1 + ((useIndex) ? 2 : 0) + ((useData) ? data.length : 0);
|
||||
|
||||
if (messageBuilder == null) {
|
||||
updateCounter++;
|
||||
return new byte[] { };
|
||||
}
|
||||
|
||||
IoBuffer buffer = Delta.createBuffer(size);
|
||||
buffer.put((byte) type);
|
||||
if (useIndex) buffer.putShort((short) index);
|
||||
if (useData) buffer.put(data);
|
||||
buffer.flip();
|
||||
|
||||
updateCounter++;
|
||||
|
||||
@@ -382,22 +442,12 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
}
|
||||
|
||||
private void queue(byte[] data) {
|
||||
if (messageBuilder == null) {
|
||||
try {
|
||||
SWGObject object = NGECore.getInstance().objectService.getObject(objectId);
|
||||
this.messageBuilder = (ObjectMessageBuilder) object.getClass().getMethod("getMessageBuilder", new Class[] {}).invoke(object, new Object[] { });
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
IoBuffer buffer = Delta.createBuffer((data.length + 8));
|
||||
buffer.putInt(1);
|
||||
buffer.putInt(updateCounter);
|
||||
buffer.put(data);
|
||||
messageBuilder.sendListDelta(viewType, updateType, buffer);
|
||||
buffer.flip();
|
||||
object.sendListDelta(viewType, updateType, buffer);
|
||||
}
|
||||
|
||||
private void queue(List<byte[]> data) {
|
||||
@@ -407,66 +457,13 @@ public class SWGList<E> implements List<E>, Serializable {
|
||||
size += queued.length;
|
||||
}
|
||||
|
||||
if (messageBuilder == null) {
|
||||
try {
|
||||
SWGObject object = NGECore.getInstance().objectService.getObject(objectId);
|
||||
this.messageBuilder = (ObjectMessageBuilder) object.getClass().getMethod("getMessageBuilder", new Class[] {}).invoke(object, new Object[] { });
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
IoBuffer buffer = Delta.createBuffer((size + 8));
|
||||
buffer.putInt(data.size());
|
||||
buffer.putInt(updateCounter);
|
||||
for (byte[] queued : data) buffer.put(queued);
|
||||
buffer.flip();
|
||||
|
||||
messageBuilder.sendListDelta(viewType, updateType, buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeIf(Predicate<? super E> filter) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<E> reverseGet()
|
||||
{
|
||||
return Lists.reverse(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<E> stream() {
|
||||
return list.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<E> parallelStream() {
|
||||
return list.parallelStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super E> action) {
|
||||
list.forEach(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAll(UnaryOperator<E> operator) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sort(Comparator<? super E> c) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<E> spliterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
object.sendListDelta(viewType, updateType, buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user