mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2024-09-04 03:10:27 +02:00
Let APIUtils.needsSynchronization include NET, use a HashMap, add tests.
This commit is contained in:
parent
81d10a314c
commit
7ab21d9e72
|
@ -3,6 +3,7 @@ package fr.neatmonster.nocheatplus.hooks;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -22,7 +23,11 @@ public class APIUtils {
|
|||
/** Check including children, for convenient iteration. */
|
||||
private static final Map<CheckType, CheckType[]> withChildrenMap = new HashMap<CheckType, CheckType[]>();
|
||||
|
||||
/** Checks/groups that might be run off the primary thread. */
|
||||
private static final Set<CheckType> needSync = new HashSet<CheckType>();
|
||||
|
||||
static {
|
||||
// Parent/children relations.
|
||||
final Map<CheckType, Set<CheckType>> map = new HashMap<CheckType, Set<CheckType>>();
|
||||
for (final CheckType type : CheckType.values())
|
||||
map.put(type, new LinkedHashSet<CheckType>());
|
||||
|
@ -41,6 +46,21 @@ public class APIUtils {
|
|||
aw[set.size()] = parent;
|
||||
withChildrenMap.put(parent, aw);
|
||||
}
|
||||
// needSync: Note that tests use the same definitions.
|
||||
for (final CheckType checkType : new CheckType[]{CheckType.CHAT, CheckType.NET}) {
|
||||
needSync.add(checkType);
|
||||
}
|
||||
boolean added = true;
|
||||
while (added) { // Just in case.
|
||||
added = false;
|
||||
for (final CheckType checkType: CheckType.values()) {
|
||||
// Fill in needSync.
|
||||
if (checkType.getParent() != null && !needSync.contains(checkType) && needSync.contains(checkType.getParent())) {
|
||||
needSync.add(checkType);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,14 +98,20 @@ public class APIUtils {
|
|||
* @return true, if is parent
|
||||
*/
|
||||
public static final boolean isParent(final CheckType supposedParent, final CheckType supposedChild) {
|
||||
if (supposedParent == supposedChild) return false;
|
||||
else if (supposedParent == CheckType.ALL) return true;
|
||||
if (supposedParent == supposedChild) {
|
||||
return false;
|
||||
}
|
||||
else if (supposedParent == CheckType.ALL) {
|
||||
return true;
|
||||
}
|
||||
CheckType parent = supposedChild.getParent();
|
||||
while (parent != null)
|
||||
if (parent == supposedParent)
|
||||
if (parent == supposedParent) {
|
||||
return true;
|
||||
else
|
||||
}
|
||||
else {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -99,7 +125,7 @@ public class APIUtils {
|
|||
* @return true, if successful
|
||||
*/
|
||||
public static final boolean needsSynchronization(final CheckType type) {
|
||||
return type == CheckType.CHAT || isParent(CheckType.CHAT, type);
|
||||
return needSync.contains(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package fr.neatmonster.nocheatplus;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.neatmonster.nocheatplus.checks.CheckType;
|
||||
import fr.neatmonster.nocheatplus.hooks.APIUtils;
|
||||
|
||||
public class TestCheckType {
|
||||
|
||||
@Test
|
||||
public void testIsParent() {
|
||||
for (CheckType parent : CheckType.values()) {
|
||||
if (parent != CheckType.ALL && !APIUtils.isParent(CheckType.ALL, parent)) {
|
||||
fail("Expect ALL to be parent of " + parent + " .");
|
||||
}
|
||||
// Rough simplified check by naming.
|
||||
String parentName = parent.getName();
|
||||
for (final CheckType child : CheckType.values()) {
|
||||
if (child == parent) {
|
||||
if (APIUtils.isParent(parent, child)) {
|
||||
fail("Check can't be parent of itself: " + parent);
|
||||
}
|
||||
// Ignore otherwise.
|
||||
continue;
|
||||
}
|
||||
String childName = child.getName();
|
||||
if (childName.startsWith(parentName) && childName.charAt(parentName.length()) == '.' && !APIUtils.isParent(parent, child)) {
|
||||
fail("Expect " + parentName + " to be parent of " + childName + ".");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNeedsSynchronization() {
|
||||
for (CheckType parent : new CheckType[]{CheckType.CHAT, CheckType.NET}) {
|
||||
for (CheckType type : CheckType.values()) {
|
||||
if ((parent == type || APIUtils.isParent(parent, type)) && !APIUtils.needsSynchronization(type)) {
|
||||
fail("Expect " + type + " to need synchronization, as it is child of " + parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user