Skip to content

Commit

Permalink
feat: Shit is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Mar 6, 2024
1 parent 9652b95 commit 1932295
Show file tree
Hide file tree
Showing 24 changed files with 430 additions and 27 deletions.
2 changes: 2 additions & 0 deletions distributor-build-logic/src/main/kotlin/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ open class DistributorModuleExtension(project: Project) {
val display = project.objects.property(String::class.java)
val main = project.objects.property(String::class.java)
val description = project.objects.property(String::class.java)

// TODO Does not handle transitive dependencies, FIX IT
val dependencies = project.objects.setProperty(Project::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.xpdustry.distributor.command.cloud.specifier.AllTeams;
import com.xpdustry.distributor.core.DistributorProvider;
import com.xpdustry.distributor.core.command.CommandSender;
import com.xpdustry.distributor.core.permission.PermissionManager;
import com.xpdustry.distributor.core.plugin.MindustryPlugin;
import com.xpdustry.distributor.core.plugin.PluginAware;
import io.leangen.geantyref.TypeToken;
Expand Down Expand Up @@ -113,8 +112,7 @@ public boolean hasPermission(final @NonNull C sender, final String permission) {
final var reversed = senderMapper().reverse(sender);
return reversed.isServer()
|| DistributorProvider.get()
.getService(PermissionManager.class)
.orElseThrow()
.getPermissionManager()
.getPermission(reversed.getPlayer(), permission)
.asBoolean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/
package com.xpdustry.distributor.core;

import java.util.Optional;
import com.xpdustry.distributor.core.permission.PermissionManager;
import com.xpdustry.distributor.core.service.ServiceManager;

public interface Distributor {

<S> Optional<S> getService(final Class<S> clazz);
ServiceManager getServiceManager();

PermissionManager getPermissionManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@
*/
package com.xpdustry.distributor.core;

import com.xpdustry.distributor.core.permission.PermissionManager;
import com.xpdustry.distributor.core.plugin.AbstractMindustryPlugin;
import com.xpdustry.distributor.core.service.ServiceManager;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

public final class DistributorCorePlugin extends AbstractMindustryPlugin {}
public final class DistributorCorePlugin extends AbstractMindustryPlugin implements Distributor {

private final ServiceManager services = ServiceManager.simple();
private @Nullable PermissionManager permissions = null;

@Override
public ServiceManager getServiceManager() {
return this.services;
}

@Override
public PermissionManager getPermissionManager() {
return Objects.requireNonNull(permissions, notInitialized("permission"));
}

@Override
public void onInit() {
DistributorProvider.set(this);
}

@Override
public void onLoad() {
this.permissions = services.provide(PermissionManager.class);
}

private String notInitialized(final String subsystem) {
return String.format("The \"%s\" subsystem is not initialized yet.", subsystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jspecify.annotations.Nullable;

/**
* A holder for the global {@link Distributor} instance.
* A holder for the global {@link Distributor} factory.
*/
public final class DistributorProvider {

Expand All @@ -30,7 +30,7 @@ public final class DistributorProvider {
private DistributorProvider() {}

/**
* Returns the global {@link Distributor} instance.
* Returns the global {@link Distributor} factory.
* @throws DistributorInitializationException if the API hasn't been initialized yet
*/
public static Distributor get() {
Expand All @@ -41,7 +41,7 @@ public static Distributor get() {
}

/**
* Sets the global {@link Distributor} instance.
* Sets the global {@link Distributor} factory.
* @throws DistributorInitializationException if the API has already been initialized
*/
public static void set(final Distributor distributor) {
Expand All @@ -52,7 +52,7 @@ public static void set(final Distributor distributor) {
}

/**
* Clears the global {@link Distributor} instance.
* Clears the global {@link Distributor} factory.
* @throws DistributorInitializationException if the API hasn't been initialized yet
*/
public static void clear() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.core.permission;

import java.util.Collections;
import java.util.Map;

final class EmptyPermissionTree implements PermissionTree {

static EmptyPermissionTree INSTANCE = new EmptyPermissionTree();

private EmptyPermissionTree() {}

@Override
public TriState getPermission(final String permission) {
return TriState.UNDEFINED;
}

@Override
public void setPermission(final String permission, final TriState state) {
throw new UnsupportedOperationException();
}

@Override
public Map<String, Boolean> getPermissions() {
return Collections.emptyMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ static PermissionTree simple() {
return new SimplePermissionTree();
}

static PermissionTree empty() {
return EmptyPermissionTree.INSTANCE;
}

static PermissionTree immutable(final PermissionTree tree) {
return (tree instanceof ImmutablePermissionTree) ? tree : new ImmutablePermissionTree(tree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private SimplePermissionTree(final @Nullable SimplePermissionTree parent) {
this.parent = parent;
}

@Override
public TriState getPermission(final String permission) {
if (!PermissionManager.PERMISSION_PATTERN.matcher(permission).matches()) {
throw new IllegalArgumentException("The permission doesn't match the regex: " + permission);
Expand All @@ -57,6 +58,7 @@ public TriState getPermission(final String permission) {
return state;
}

@Override
public void setPermission(final String permission, final TriState state) {
if (!PermissionManager.PERMISSION_PATTERN.matcher(permission).matches()) {
throw new IllegalArgumentException("The permission doesn't match the regex: " + permission);
Expand Down Expand Up @@ -85,6 +87,7 @@ public void setPermission(final String permission, final TriState state) {
}
}

@Override
public Map<String, Boolean> getPermissions() {
final Map<String, Boolean> permissions = new HashMap<>();
for (final var child : this.children.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static MindustryPlugin wrap(final Plugin plugin) {
}

/**
* Called after the plugin instance creation.
* Called after the plugin factory creation.
* Initialize your plugin here.
* <p>
* <strong>Warning: </strong> Only call other plugins in this method if they use Distributor too.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.core.service;

import com.xpdustry.distributor.core.internal.GeneratedDataClass;
import com.xpdustry.distributor.core.plugin.MindustryPlugin;
import com.xpdustry.distributor.core.util.Priority;
import java.util.List;
import java.util.function.Supplier;
import org.immutables.value.Value;

public interface ServiceManager {

static ServiceManager simple() {
return new SimpleServiceManager();
}

<T> void register(
final MindustryPlugin plugin, final Class<T> clazz, final Priority priority, final Supplier<T> factory);

<T> T provide(final Class<T> clazz);

<T> List<Provider<T>> getProviders(final Class<T> clazz);

@GeneratedDataClass
@Value.Immutable
sealed interface Provider<T> permits ImmutableProvider {

static <T> Provider<T> of(
final MindustryPlugin plugin,
final Class<T> clazz,
final Priority priority,
final Supplier<T> factory) {
return ImmutableProvider.of(plugin, clazz, priority, factory);
}

MindustryPlugin getPlugin();

Class<T> getClazz();

Priority getPriority();

Supplier<T> getFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.core.service;

import com.xpdustry.distributor.core.plugin.MindustryPlugin;
import com.xpdustry.distributor.core.util.Priority;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.function.Supplier;

final class SimpleServiceManager implements ServiceManager {

private final Map<Class<?>, Queue<Provider<?>>> services = new HashMap<>();
private final Object lock = new Object();

@Override
public <T> void register(
final MindustryPlugin plugin, final Class<T> clazz, final Priority priority, final Supplier<T> instance) {
synchronized (this.lock) {
this.services
.computeIfAbsent(
clazz, k -> new PriorityQueue<Provider<?>>(Comparator.comparing(Provider::getPriority)))
.add(Provider.of(plugin, clazz, priority, instance));
}
}

@Override
public <T> T provide(final Class<T> clazz) {
final var providers = this.getProviders(clazz);
if (providers.isEmpty()) {
throw new IllegalStateException("Expected provider for " + clazz.getCanonicalName() + ", got nothing.");
}
return providers.get(0).getFactory().get();
}

@SuppressWarnings("unchecked")
@Override
public <T> List<Provider<T>> getProviders(final Class<T> clazz) {
synchronized (this.lock) {
final Object implementation = this.services.get(clazz);
return implementation != null
? List.copyOf((Collection<? extends Provider<T>>) implementation)
: Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.permission;
package com.xpdustry.distributor.core.util;

import com.xpdustry.distributor.core.plugin.AbstractMindustryPlugin;

public final class DistributorPermissionRankPlugin extends AbstractMindustryPlugin {}
public enum Priority {
HIGHEST,
HIGH,
NORMAL,
LOW,
LOWEST
}
7 changes: 7 additions & 0 deletions distributor-permission-rank/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ module {
display = "DistributorPermissionRank"
main = "com.xpdustry.distributor.permission.rank.DistributorPermissionRankPlugin"
description = "Simple permission system based on ranks."
dependencies = setOf(project(":distributor-common"))
}

dependencies {
compileOnly(project(":distributor-common"))
implementation(libs.configurate.core)
implementation(libs.configurate.yaml)
}

tasks.runMindustryServer {
mods.from(project(":distributor-logging-simple").tasks.shadowJar)
}
Loading

0 comments on commit 1932295

Please sign in to comment.