mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Sponge: replace MemorySubjectData with own implementation
This commit is contained in:
parent
2893d4aebd
commit
793a84edfb
@ -30,7 +30,7 @@ import me.lucko.luckperms.bukkit.calculators.ChildProcessor;
|
||||
import me.lucko.luckperms.bukkit.calculators.DefaultsProcessor;
|
||||
import me.lucko.luckperms.bukkit.inject.Injector;
|
||||
import me.lucko.luckperms.bukkit.model.LPPermissible;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.AbstractCalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.common.calculators.PermissionProcessor;
|
||||
import me.lucko.luckperms.common.calculators.processors.MapProcessor;
|
||||
@ -41,7 +41,7 @@ import me.lucko.luckperms.common.core.model.User;
|
||||
import java.util.UUID;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BukkitCalculatorFactory implements CalculatorFactory {
|
||||
public class BukkitCalculatorFactory extends AbstractCalculatorFactory {
|
||||
private final LPBukkitPlugin plugin;
|
||||
|
||||
@Override
|
||||
@ -63,6 +63,6 @@ public class BukkitCalculatorFactory implements CalculatorFactory {
|
||||
}
|
||||
processors.add(new DefaultsProcessor(contexts.isOp(), plugin.getDefaultsProvider()));
|
||||
|
||||
return new PermissionCalculator(plugin, user.getName(), processors.build());
|
||||
return registerCalculator(new PermissionCalculator(plugin, user.getName(), processors.build()));
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ package me.lucko.luckperms.bungee;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.AbstractCalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.common.calculators.PermissionProcessor;
|
||||
import me.lucko.luckperms.common.calculators.processors.MapProcessor;
|
||||
@ -34,7 +34,7 @@ import me.lucko.luckperms.common.calculators.processors.WildcardProcessor;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class BungeeCalculatorFactory implements CalculatorFactory {
|
||||
public class BungeeCalculatorFactory extends AbstractCalculatorFactory {
|
||||
private final LPBungeePlugin plugin;
|
||||
|
||||
@Override
|
||||
@ -48,6 +48,6 @@ public class BungeeCalculatorFactory implements CalculatorFactory {
|
||||
processors.add(new RegexProcessor());
|
||||
}
|
||||
|
||||
return new PermissionCalculator(plugin, user.getName(), processors.build());
|
||||
return registerCalculator(new PermissionCalculator(plugin, user.getName(), processors.build()));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.common.calculators;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class AbstractCalculatorFactory implements CalculatorFactory {
|
||||
private final Set<PermissionCalculator> calculators = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
|
||||
|
||||
protected PermissionCalculator registerCalculator(PermissionCalculator calculator) {
|
||||
calculators.add(calculator);
|
||||
return calculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateAll() {
|
||||
for (PermissionCalculator calculator : calculators) {
|
||||
calculator.invalidateCache();
|
||||
}
|
||||
}
|
||||
}
|
@ -30,6 +30,17 @@ import me.lucko.luckperms.common.core.model.User;
|
||||
*/
|
||||
public interface CalculatorFactory {
|
||||
|
||||
/**
|
||||
* Builds a PermissionCalculator for the user in the given context
|
||||
* @param contexts the contexts to build the calculator in
|
||||
* @param user the user to build for
|
||||
* @return a permission calculator instance
|
||||
*/
|
||||
PermissionCalculator build(Contexts contexts, User user);
|
||||
|
||||
/**
|
||||
* Invalidates all calculators build by this factory
|
||||
*/
|
||||
void invalidateAll();
|
||||
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ package me.lucko.luckperms.sponge;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.AbstractCalculatorFactory;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.common.calculators.PermissionProcessor;
|
||||
import me.lucko.luckperms.common.calculators.processors.MapProcessor;
|
||||
@ -37,7 +37,7 @@ import me.lucko.luckperms.sponge.calculators.SpongeWildcardProcessor;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class SpongeCalculatorFactory implements CalculatorFactory {
|
||||
public class SpongeCalculatorFactory extends AbstractCalculatorFactory {
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
@Override
|
||||
@ -53,6 +53,6 @@ public class SpongeCalculatorFactory implements CalculatorFactory {
|
||||
}
|
||||
processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContexts())));
|
||||
|
||||
return new PermissionCalculator(plugin, user.getName(), processors.build());
|
||||
return registerCalculator(new PermissionCalculator(plugin, user.getName(), processors.build()));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,350 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.sponge.service.data;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.lucko.luckperms.common.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.common.calculators.PermissionProcessor;
|
||||
import me.lucko.luckperms.common.calculators.processors.MapProcessor;
|
||||
import me.lucko.luckperms.common.utils.ImmutableCollectors;
|
||||
import me.lucko.luckperms.sponge.calculators.SpongeWildcardProcessor;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class CalculatedSubjectData implements SubjectData {
|
||||
private static final ContextComparator CONTEXT_COMPARATOR = new ContextComparator();
|
||||
|
||||
private final LuckPermsService service;
|
||||
private final String calculatorDisplayName;
|
||||
|
||||
private final LoadingCache<Set<Context>, CalculatorHolder> permissionCache = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<Set<Context>, CalculatorHolder>() {
|
||||
@Override
|
||||
public CalculatorHolder load(Set<Context> contexts) {
|
||||
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
|
||||
processors.add(new MapProcessor());
|
||||
processors.add(new SpongeWildcardProcessor());
|
||||
|
||||
CalculatorHolder holder = new CalculatorHolder(new PermissionCalculator(service.getPlugin(), calculatorDisplayName, processors.build()));
|
||||
holder.setPermissions(flattenMap(contexts, permissions));
|
||||
|
||||
return holder;
|
||||
}
|
||||
});
|
||||
|
||||
private final LoadingCache<Set<Context>, Map<String, String>> optionCache = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<Set<Context>, Map<String, String>>() {
|
||||
@Override
|
||||
public Map<String, String> load(Set<Context> contexts) {
|
||||
return flattenMap(contexts, options);
|
||||
}
|
||||
});
|
||||
|
||||
private final Map<Set<Context>, Map<String, Boolean>> permissions = new ConcurrentHashMap<>();
|
||||
private final Map<Set<Context>, Set<SubjectReference>> parents = new ConcurrentHashMap<>();
|
||||
private final Map<Set<Context>, Map<String, String>> options = new ConcurrentHashMap<>();
|
||||
|
||||
public Tristate getPermissionValue(Set<Context> contexts, String permission) {
|
||||
return LuckPermsService.convertTristate(permissionCache.getUnchecked(contexts).getCalculator().getPermissionValue(permission));
|
||||
}
|
||||
|
||||
public Map<Set<Context>, Set<SubjectReference>> getParents() {
|
||||
ImmutableMap.Builder<Set<Context>, Set<SubjectReference>> map = ImmutableMap.builder();
|
||||
for (Map.Entry<Set<Context>, Set<SubjectReference>> e : parents.entrySet()) {
|
||||
map.put(ImmutableSet.copyOf(e.getKey()), ImmutableSet.copyOf(e.getValue()));
|
||||
}
|
||||
return map.build();
|
||||
}
|
||||
|
||||
public void replacePermissions(Map<Set<Context>, Map<String, Boolean>> map) {
|
||||
permissions.clear();
|
||||
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : map.entrySet()) {
|
||||
permissions.put(ImmutableSet.copyOf(e.getKey()), new ConcurrentHashMap<>(e.getValue()));
|
||||
}
|
||||
permissionCache.invalidateAll();
|
||||
}
|
||||
|
||||
public void replaceParents(Map<Set<Context>, Set<SubjectReference>> map) {
|
||||
parents.clear();
|
||||
for (Map.Entry<Set<Context>, Set<SubjectReference>> e : map.entrySet()) {
|
||||
Set<SubjectReference> set = ConcurrentHashMap.newKeySet();
|
||||
set.addAll(e.getValue());
|
||||
parents.put(ImmutableSet.copyOf(e.getKey()), set);
|
||||
}
|
||||
}
|
||||
|
||||
public void replaceOptions(Map<Set<Context>, Map<String, String>> map) {
|
||||
options.clear();
|
||||
for (Map.Entry<Set<Context>, Map<String, String>> e : map.entrySet()) {
|
||||
options.put(ImmutableSet.copyOf(e.getKey()), new ConcurrentHashMap<>(e.getValue()));
|
||||
}
|
||||
optionCache.invalidateAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Set<Context>, Map<String, Boolean>> getAllPermissions() {
|
||||
ImmutableMap.Builder<Set<Context>, Map<String, Boolean>> map = ImmutableMap.builder();
|
||||
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : permissions.entrySet()) {
|
||||
map.put(ImmutableSet.copyOf(e.getKey()), ImmutableMap.copyOf(e.getValue()));
|
||||
}
|
||||
return map.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Boolean> getPermissions(Set<Context> contexts) {
|
||||
return ImmutableMap.copyOf(permissions.getOrDefault(contexts, ImmutableMap.of()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setPermission(Set<Context> contexts, String permission, Tristate value) {
|
||||
boolean b;
|
||||
if (value == Tristate.UNDEFINED) {
|
||||
Map<String, Boolean> perms = permissions.get(contexts);
|
||||
b = perms != null && perms.remove(permission.toLowerCase()) != null;
|
||||
} else {
|
||||
Map<String, Boolean> perms = permissions.computeIfAbsent(ImmutableSet.copyOf(contexts), c -> new ConcurrentHashMap<>());
|
||||
b = !Objects.equals(perms.put(permission.toLowerCase(), value.asBoolean()), value.asBoolean());
|
||||
}
|
||||
if (b) {
|
||||
permissionCache.invalidateAll();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearPermissions() {
|
||||
if (permissions.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
permissions.clear();
|
||||
permissionCache.invalidateAll();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearPermissions(Set<Context> contexts) {
|
||||
Map<String, Boolean> perms = permissions.get(contexts);
|
||||
if (perms == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
permissions.remove(contexts);
|
||||
if (!perms.isEmpty()) {
|
||||
permissionCache.invalidateAll();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Set<Context>, List<Subject>> getAllParents() {
|
||||
ImmutableMap.Builder<Set<Context>, List<Subject>> map = ImmutableMap.builder();
|
||||
for (Map.Entry<Set<Context>, Set<SubjectReference>> e : parents.entrySet()) {
|
||||
map.put(
|
||||
ImmutableSet.copyOf(e.getKey()),
|
||||
e.getValue().stream().map(s -> s.resolve(service)).collect(ImmutableCollectors.toImmutableList())
|
||||
);
|
||||
}
|
||||
return map.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Subject> getParents(Set<Context> contexts) {
|
||||
return parents.getOrDefault(contexts, ImmutableSet.of()).stream()
|
||||
.map(s -> s.resolve(service))
|
||||
.collect(ImmutableCollectors.toImmutableList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addParent(Set<Context> contexts, Subject parent) {
|
||||
Set<SubjectReference> set = parents.computeIfAbsent(ImmutableSet.copyOf(contexts), c -> ConcurrentHashMap.newKeySet());
|
||||
return set.add(SubjectReference.of(parent));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeParent(Set<Context> contexts, Subject parent) {
|
||||
Set<SubjectReference> set = parents.get(contexts);
|
||||
return set != null && set.remove(SubjectReference.of(parent));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearParents() {
|
||||
if (parents.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
parents.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearParents(Set<Context> contexts) {
|
||||
Set<SubjectReference> set = parents.get(contexts);
|
||||
if (set == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
parents.remove(contexts);
|
||||
return !set.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Set<Context>, Map<String, String>> getAllOptions() {
|
||||
ImmutableMap.Builder<Set<Context>, Map<String, String>> map = ImmutableMap.builder();
|
||||
for (Map.Entry<Set<Context>, Map<String, String>> e : options.entrySet()) {
|
||||
map.put(ImmutableSet.copyOf(e.getKey()), ImmutableMap.copyOf(e.getValue()));
|
||||
}
|
||||
return map.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getOptions(Set<Context> contexts) {
|
||||
return ImmutableMap.copyOf(options.getOrDefault(contexts, ImmutableMap.of()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setOption(Set<Context> contexts, String key, @Nullable String value) {
|
||||
boolean b;
|
||||
if (value == null) {
|
||||
Map<String, String> options = this.options.get(contexts);
|
||||
b = options != null && options.remove(key.toLowerCase()) != null;
|
||||
} else {
|
||||
Map<String, String> options = this.options.computeIfAbsent(ImmutableSet.copyOf(contexts), c -> new ConcurrentHashMap<>());
|
||||
b = !stringEquals(options.put(key.toLowerCase(), value), value);
|
||||
}
|
||||
if (b) {
|
||||
optionCache.invalidateAll();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearOptions() {
|
||||
if (options.isEmpty()) {
|
||||
return false;
|
||||
} else {
|
||||
options.clear();
|
||||
optionCache.invalidateAll();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clearOptions(Set<Context> contexts) {
|
||||
Map<String, String> map = options.get(contexts);
|
||||
if (map == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
options.remove(contexts);
|
||||
if (!map.isEmpty()) {
|
||||
optionCache.invalidateAll();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static <V> Map<String, V> flattenMap(Set<Context> contexts, Map<Set<Context>, Map<String, V>> source) {
|
||||
Map<String, V> map = new HashMap<>();
|
||||
|
||||
SortedMap<Set<Context>, Map<String, V>> ret = getRelevantEntries(contexts, source);
|
||||
for (Map<String, V> m : ret.values()) {
|
||||
for (Map.Entry<String, V> e : m.entrySet()) {
|
||||
if (!map.containsKey(e.getKey())) {
|
||||
map.put(e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ImmutableMap.copyOf(map);
|
||||
}
|
||||
|
||||
private static <K, V> SortedMap<Set<Context>, Map<K, V>> getRelevantEntries(Set<Context> set, Map<Set<Context>, Map<K, V>> map) {
|
||||
ImmutableSortedMap.Builder<Set<Context>, Map<K, V>> perms = ImmutableSortedMap.orderedBy(CONTEXT_COMPARATOR);
|
||||
|
||||
loop:
|
||||
for (Map.Entry<Set<Context>, Map<K, V>> e : map.entrySet()) {
|
||||
for (Context c : e.getKey()) {
|
||||
if (!set.contains(c)) {
|
||||
continue loop;
|
||||
}
|
||||
}
|
||||
|
||||
perms.put(ImmutableSet.copyOf(e.getKey()), ImmutableMap.copyOf(e.getValue()));
|
||||
}
|
||||
|
||||
return perms.build();
|
||||
}
|
||||
|
||||
private static boolean stringEquals(String a, String b) {
|
||||
return a == null && b == null || a != null && b != null && a.equalsIgnoreCase(b);
|
||||
}
|
||||
|
||||
private static class ContextComparator implements Comparator<Set<Context>> {
|
||||
|
||||
@Override
|
||||
public int compare(Set<Context> o1, Set<Context> o2) {
|
||||
int i = Integer.compare(o1.size(), o2.size());
|
||||
return i == 0 ? 1 : i;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CalculatorHolder {
|
||||
|
||||
@Getter
|
||||
private final PermissionCalculator calculator;
|
||||
|
||||
@Getter
|
||||
private final Map<String, Boolean> permissions;
|
||||
|
||||
public CalculatorHolder(PermissionCalculator calculator) {
|
||||
this.calculator = calculator;
|
||||
this.permissions = new ConcurrentHashMap<>();
|
||||
this.calculator.updateBacking(permissions);
|
||||
}
|
||||
|
||||
public void setPermissions(Map<String, Boolean> permissions) {
|
||||
this.permissions.clear();
|
||||
this.permissions.putAll(permissions);
|
||||
calculator.updateBacking(this.permissions);
|
||||
calculator.invalidateCache();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.sponge.service.data;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
import org.spongepowered.api.service.permission.PermissionService;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
@AllArgsConstructor(staticName = "of")
|
||||
public class SubjectReference {
|
||||
public static SubjectReference deserialize(String s) {
|
||||
List<String> parts = Splitter.on('/').limit(2).splitToList(s);
|
||||
return of(parts.get(0), parts.get(1));
|
||||
}
|
||||
|
||||
public static SubjectReference of(Subject subject) {
|
||||
return of(subject.getContainingCollection().getIdentifier(), subject.getIdentifier());
|
||||
}
|
||||
|
||||
private final String collection;
|
||||
private final String identifier;
|
||||
|
||||
public Subject resolve(PermissionService service) {
|
||||
return service.getSubjects(collection).get(identifier);
|
||||
}
|
||||
|
||||
public String serialize() {
|
||||
return collection + "/" + identifier;
|
||||
}
|
||||
|
||||
}
|
@ -29,10 +29,10 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import me.lucko.luckperms.sponge.service.data.CalculatedSubjectData;
|
||||
import me.lucko.luckperms.sponge.timings.LPTiming;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
@ -52,7 +52,7 @@ public class PersistedSubject implements Subject {
|
||||
private final LuckPermsService service;
|
||||
private final PersistedCollection containingCollection;
|
||||
private final PersistedSubjectData subjectData;
|
||||
private final MemorySubjectData transientSubjectData;
|
||||
private final CalculatedSubjectData transientSubjectData;
|
||||
private final BufferedRequest<Void> saveBuffer = new BufferedRequest<Void>(1000L, r -> PersistedSubject.this.service.getPlugin().doAsync(r)) {
|
||||
@Override
|
||||
protected Void perform() {
|
||||
@ -71,13 +71,13 @@ public class PersistedSubject implements Subject {
|
||||
this.identifier = identifier;
|
||||
this.service = service;
|
||||
this.containingCollection = containingCollection;
|
||||
this.subjectData = new PersistedSubjectData(service, this);
|
||||
this.transientSubjectData = new MemorySubjectData(service);
|
||||
this.subjectData = new PersistedSubjectData(service, "local:" + containingCollection.getIdentifier() + "/" + identifier + "(p)", this);
|
||||
this.transientSubjectData = new CalculatedSubjectData(service, "local:" + containingCollection.getIdentifier() + "/" + identifier + "(t)");
|
||||
}
|
||||
|
||||
public void loadData(SubjectDataHolder dataHolder) {
|
||||
subjectData.setSave(false);
|
||||
dataHolder.copyTo(subjectData, service);
|
||||
dataHolder.copyTo(subjectData);
|
||||
subjectData.setSave(true);
|
||||
}
|
||||
|
||||
@ -98,12 +98,12 @@ public class PersistedSubject implements Subject {
|
||||
@Override
|
||||
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String node) {
|
||||
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.PERSISTED_SUBJECT_GET_PERMISSION_VALUE)) {
|
||||
Tristate res = subjectData.getNodeTree(contexts).get(node);
|
||||
Tristate res = subjectData.getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = transientSubjectData.getNodeTree(contexts).get(node);
|
||||
res = transientSubjectData.getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ package me.lucko.luckperms.sponge.service.persisted;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import me.lucko.luckperms.sponge.service.data.CalculatedSubjectData;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.PermissionService;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
@ -36,15 +36,15 @@ import java.util.Set;
|
||||
/**
|
||||
* Extension of MemorySubjectData which persists data when modified
|
||||
*/
|
||||
public class PersistedSubjectData extends MemorySubjectData {
|
||||
public class PersistedSubjectData extends CalculatedSubjectData {
|
||||
private final PersistedSubject subject;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean save = true;
|
||||
|
||||
public PersistedSubjectData(PermissionService service, PersistedSubject subject) {
|
||||
super(service);
|
||||
public PersistedSubjectData(LuckPermsService service, String calculatorDisplayName, PersistedSubject subject) {
|
||||
super(service, calculatorDisplayName);
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
|
@ -22,15 +22,16 @@
|
||||
|
||||
package me.lucko.luckperms.sponge.service.persisted;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import lombok.ToString;
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.sponge.service.data.CalculatedSubjectData;
|
||||
import me.lucko.luckperms.sponge.service.data.SubjectReference;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.PermissionService;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static me.lucko.luckperms.sponge.service.LuckPermsService.convertContexts;
|
||||
@ -44,7 +45,7 @@ public class SubjectDataHolder {
|
||||
private final Map<Map<String, String>, Map<String, String>> options;
|
||||
private final Map<Map<String, String>, List<String>> parents;
|
||||
|
||||
public SubjectDataHolder(Map<Set<Context>, Map<String, String>> options, Map<Set<Context>, Map<String, Boolean>> permissions, Map<Set<Context>, List<Map.Entry<String, String>>> parents) {
|
||||
public SubjectDataHolder(Map<Set<Context>, Map<String, String>> options, Map<Set<Context>, Map<String, Boolean>> permissions, Map<Set<Context>, Set<SubjectReference>> parents) {
|
||||
this.options = new HashMap<>();
|
||||
for (Map.Entry<Set<Context>, Map<String, String>> e : options.entrySet()) {
|
||||
this.options.put(convertContexts(e.getKey()).toMap(), new HashMap<>(e.getValue()));
|
||||
@ -56,50 +57,35 @@ public class SubjectDataHolder {
|
||||
}
|
||||
|
||||
this.parents = new HashMap<>();
|
||||
for (Map.Entry<Set<Context>, List<Map.Entry<String, String>>> e : parents.entrySet()) {
|
||||
this.parents.put(convertContexts(e.getKey()).toMap(), e.getValue().stream().map(p -> p.getKey() + "/" + p.getValue()).collect(Collectors.toList()));
|
||||
for (Map.Entry<Set<Context>, Set<SubjectReference>> e : parents.entrySet()) {
|
||||
this.parents.put(convertContexts(e.getKey()).toMap(), e.getValue().stream().map(SubjectReference::serialize).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
public SubjectDataHolder(MemorySubjectData data) {
|
||||
this(
|
||||
data.getAllOptions(),
|
||||
data.getAllPermissions(),
|
||||
data.getAllParents().entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
e -> e.getValue().stream()
|
||||
.map(s -> new AbstractMap.SimpleEntry<>(
|
||||
s.getContainingCollection().getIdentifier(),
|
||||
s.getIdentifier())
|
||||
)
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
)
|
||||
public SubjectDataHolder(CalculatedSubjectData data) {
|
||||
this(data.getAllOptions(), data.getAllPermissions(), data.getParents());
|
||||
}
|
||||
|
||||
public void copyTo(CalculatedSubjectData subjectData) {
|
||||
subjectData.replacePermissions(permissions.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
k -> convertContexts(ContextSet.fromMap(k.getKey())),
|
||||
Map.Entry::getValue
|
||||
))
|
||||
);
|
||||
|
||||
subjectData.replaceOptions(options.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
k -> convertContexts(ContextSet.fromMap(k.getKey())),
|
||||
Map.Entry::getValue
|
||||
))
|
||||
);
|
||||
|
||||
subjectData.replaceParents(parents.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
k -> convertContexts(ContextSet.fromMap(k.getKey())),
|
||||
v -> v.getValue().stream().map(SubjectReference::deserialize).collect(Collectors.toSet())
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
public void copyTo(MemorySubjectData subjectData, PermissionService service) {
|
||||
for (Map.Entry<Map<String, String>, Map<String, Boolean>> e : permissions.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (Map.Entry<String, Boolean> perm : e.getValue().entrySet()) {
|
||||
subjectData.setPermission(contexts, perm.getKey(), Tristate.fromBoolean(perm.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Map<String, String>, Map<String, String>> e : options.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (Map.Entry<String, String> option : e.getValue().entrySet()) {
|
||||
subjectData.setOption(contexts, option.getKey(), option.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Map<String, String>, List<String>> e : parents.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (String parent : e.getValue()) {
|
||||
List<String> parts = Splitter.on('/').limit(2).splitToList(parent);
|
||||
subjectData.addParent(contexts, service.getSubjects(parts.get(0)).get(parts.get(1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,12 +28,11 @@ import com.google.common.collect.ImmutableSet;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import me.lucko.luckperms.sponge.service.data.CalculatedSubjectData;
|
||||
import me.lucko.luckperms.sponge.timings.LPTiming;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -50,13 +49,15 @@ public class SimpleSubject implements Subject {
|
||||
|
||||
private final LuckPermsService service;
|
||||
private final SimpleCollection containingCollection;
|
||||
private final MemorySubjectData subjectData;
|
||||
private final CalculatedSubjectData subjectData;
|
||||
private final CalculatedSubjectData transientSubjectData;
|
||||
|
||||
public SimpleSubject(String identifier, LuckPermsService service, SimpleCollection containingCollection) {
|
||||
this.identifier = identifier;
|
||||
this.service = service;
|
||||
this.containingCollection = containingCollection;
|
||||
this.subjectData = new MemorySubjectData(service);
|
||||
this.subjectData = new CalculatedSubjectData(service, "local:" + containingCollection.getIdentifier() + "/" + identifier + "(p)");
|
||||
this.transientSubjectData = new CalculatedSubjectData(service, "local:" + containingCollection.getIdentifier() + "/" + identifier + "(t)");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,11 +65,6 @@ public class SimpleSubject implements Subject {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SubjectData getTransientSubjectData() {
|
||||
return getSubjectData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NonNull Set<Context> contexts, @NonNull String node) {
|
||||
return getPermissionValue(contexts, node).asBoolean();
|
||||
@ -77,15 +73,20 @@ public class SimpleSubject implements Subject {
|
||||
@Override
|
||||
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String node) {
|
||||
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.SIMPLE_SUBJECT_GET_PERMISSION_VALUE)) {
|
||||
Tristate res = subjectData.getNodeTree(contexts).get(node);
|
||||
Tristate res = transientSubjectData.getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = subjectData.getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
|
||||
for (Subject parent : getParents(contexts)) {
|
||||
Tristate tempRes = parent.getPermissionValue(contexts, node);
|
||||
if (tempRes != Tristate.UNDEFINED) {
|
||||
return tempRes;
|
||||
res = parent.getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +94,7 @@ public class SimpleSubject implements Subject {
|
||||
return Tristate.UNDEFINED;
|
||||
}
|
||||
|
||||
res = service.getGroupSubjects().getDefaults().getPermissionValue(contexts, node);
|
||||
res = getContainingCollection().getDefaults().getPermissionValue(contexts, node);
|
||||
if (res != Tristate.UNDEFINED) {
|
||||
return res;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user