Search in sources :

Example 1 with PresenceRenderContext

use of com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext in project Intellij-Discord-Integration by Almighty-Alpaca.

the class RPC method init.

public static synchronized void init(@NotNull DiscordEventHandlers handlers, @NotNull String clientId, @NotNull Supplier<PresenceRenderContext> contextSupplier, @NotNull Function<PresenceRenderContext, DiscordRichPresence> renderer, @NotNull ToLongFunction<PresenceRenderContext> changeCallback) {
    LOG.trace("RPC.init({}, {}, {}, {})", "DiscordEventHandler", clientId, contextSupplier, renderer);
    if (!RPC.initialized) {
        RPC.initialized = true;
        DiscordRPC.INSTANCE.Discord_Initialize(clientId, handlers, false, null);
        RPC.updatePresence(1, TimeUnit.SECONDS);
        if (callbackRunner == null) {
            RPC.callbackRunner = new Thread(() -> {
                while (RPC.initialized) {
                    try {
                        DiscordRPC.INSTANCE.Discord_RunCallbacks();
                        LockSupport.parkNanos(2_000_000);
                    } catch (Exception e) {
                        if (!(e instanceof InterruptedException))
                            LOG.warn("An error occurred in RPC.callbackRunner", e);
                    }
                }
                RPC.callbackRunner = null;
            }, "JetbrainsDiscordIntegration-RPC-Callback-Handler");
            RPC.callbackRunner.start();
        }
        if (RPC.initialized) {
            RPC.delayedPresenceRunner = new Thread(() -> {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        long timeout = RPC.nextPresenceUpdate - System.nanoTime();
                        LOG.trace("RPC.delayedPresenceRunner$run#timeout~1 = {}ns", timeout);
                        if (timeout <= 0) {
                            PresenceRenderContext renderContext = contextSupplier.get();
                            DiscordRichPresence newPresence = renderer.apply(renderContext);
                            if (newPresence == null)
                                newPresence = new DiscordRichPresence();
                            final DiscordRichPresence finalNewPresence = newPresence;
                            LOG.trace("RPC.delayedPresenceRunner$run#newPresence = {}", Logger.LazyString.create(() -> GSON.toJson(finalNewPresence)));
                            if (!Objects.equals(RPC.presence, newPresence)) {
                                DiscordRPC.INSTANCE.Discord_UpdatePresence(newPresence);
                                RPC.presence = newPresence;
                            }
                            timeout = changeCallback.applyAsLong(renderContext);
                        }
                        long newTimeout = RPC.nextPresenceUpdate - System.nanoTime();
                        if (newTimeout < 0)
                            newTimeout = Long.MAX_VALUE;
                        timeout = Long.min(timeout, newTimeout);
                        LOG.trace("RPC.delayedPresenceRunner$run#timeout~2 = {}ns", timeout);
                        if (timeout > 0)
                            LockSupport.parkNanos(timeout);
                        else
                            LockSupport.park();
                    } catch (Exception e) {
                        if (!(e instanceof InterruptedException))
                            LOG.error("An error occurred in RPC.delayedPresenceRunner", e);
                    }
                }
                RPC.delayedPresenceRunner = null;
            }, "JetbrainsDiscordIntegration-RPC-Delayed-Presence-Handler");
            RPC.delayedPresenceRunner.start();
        }
    }
}
Also used : PresenceRenderContext(com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext) DiscordRichPresence(club.minnced.discord.rpc.DiscordRichPresence)

Example 2 with PresenceRenderContext

use of com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext in project Intellij-Discord-Integration by Almighty-Alpaca.

the class DiscordIntegrationApplicationComponent method checkRpcConnection.

private void checkRpcConnection(@Nullable PresenceRenderContext renderContext) {
    synchronized (rpcLock) {
        LOG.trace("DiscordIntegrationApplicationComponent#checkRpcConnection()");
        if (this.data == null || this.instanceInfo == null)
            return;
        Map<String, InstanceInfo> instances = this.data.getInstances();
        boolean rpcConnectionExists;
        // noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (instances) {
            rpcConnectionExists = instances.values().stream().anyMatch(InstanceInfo::isHasRpcConnection);
        }
        LOG.trace("DiscordIntegrationApplicationComponent#checkRpcConnection()#instanceInfo.isHasRpcConnection() = {}", instanceInfo.isHasRpcConnection());
        if (instanceInfo.isHasRpcConnection() && (renderContext == null ? renderContext = new PresenceRenderContext(data) : renderContext).isEmpty()) {
            this.data.instanceSetHasRpcConnection(System.currentTimeMillis(), instanceInfo, false);
            RPC.dispose();
        } else // @formatter:off
        if (!rpcConnectionExists && !instanceInfo.isHasRpcConnection() && this.channel != null && Objects.equals(channel.getView().getCoord(), this.channel.getAddress()) && !(renderContext == null ? new PresenceRenderContext(data) : renderContext).isEmpty()) // @formatter:on
        {
            this.data.instanceSetHasRpcConnection(System.currentTimeMillis(), instanceInfo, true);
            new Thread(() -> {
                DiscordEventHandlers handlers = new DiscordEventHandlers();
                handlers.ready = this::rpcReady;
                handlers.errored = this::rpcError;
                handlers.disconnected = this::rpcDisconnected;
                RPC.init(handlers, CLIENT_ID, () -> new PresenceRenderContext(this.data), new PresenceRenderer(), this::presenceUpdated);
            }, "JetbrainsDiscordIntegration-RPC-Starter").start();
        }
    }
}
Also used : PresenceRenderContext(com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext) PresenceRenderer(com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderer) InstanceInfo(com.almightyalpaca.jetbrains.plugins.discord.data.InstanceInfo) DiscordEventHandlers(club.minnced.discord.rpc.DiscordEventHandlers)

Aggregations

PresenceRenderContext (com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext)2 DiscordEventHandlers (club.minnced.discord.rpc.DiscordEventHandlers)1 DiscordRichPresence (club.minnced.discord.rpc.DiscordRichPresence)1 InstanceInfo (com.almightyalpaca.jetbrains.plugins.discord.data.InstanceInfo)1 PresenceRenderer (com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderer)1