AssetStoreMonitorHandler class

Пакет: com.hypixel.hytale.server.core.asset

Файл: com/hypixel/hytale/server/core/asset/HytaleAssetStore.java

implements: AssetMonitorHandler

Поля (12)

МодификаторыТипИмя
break
break
private final String packKey
IEventDispatcher var15
long var16
Set var17
AssetLoadResult var18
ObjectArrayList var2
ObjectArrayList var3
ObjectArrayList var4
ObjectArrayList var5
Path var8

Методы (3)

МодификаторыВозвратСигнатура
public void acceptpublic void accept(Map<Path, EventKind> var1)
public Object getKeypublic Object getKey()
public boolean testpublic boolean test(Path var1, EventKind var2)

Исходный код

Показать/скрыть
class="kw">package com.hypixel.hytale.server.core.asset;

class="kw">import com.hypixel.hytale.assetstore.AssetLoadResult;
class="kw">import com.hypixel.hytale.assetstore.AssetMap;
class="kw">import com.hypixel.hytale.assetstore.AssetStore;
class="kw">import com.hypixel.hytale.assetstore.AssetUpdateQuery;
class="kw">import com.hypixel.hytale.assetstore.event.AssetStoreMonitorEvent;
class="kw">import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;
class="kw">import com.hypixel.hytale.common.util.FormatUtil;
class="kw">import com.hypixel.hytale.common.util.PathUtil;
class="kw">import com.hypixel.hytale.event.EventBus;
class="kw">import com.hypixel.hytale.event.IEventDispatcher;
class="kw">import com.hypixel.hytale.logger.HytaleLogger;
class="kw">import com.hypixel.hytale.protocol.ItemWithAllMetadata;
class="kw">import com.hypixel.hytale.protocol.ToClientPacket;
class="kw">import com.hypixel.hytale.protocol.packets.interface_.NotificationStyle;
class="kw">import com.hypixel.hytale.server.core.HytaleServer;
class="kw">import com.hypixel.hytale.server.core.Message;
class="kw">import com.hypixel.hytale.server.core.Options;
class="kw">import com.hypixel.hytale.server.core.asset.monitor.AssetMonitor;
class="kw">import com.hypixel.hytale.server.core.asset.monitor.AssetMonitorHandler;
class="kw">import com.hypixel.hytale.server.core.asset.monitor.EventKind;
class="kw">import com.hypixel.hytale.server.core.asset.packet.AssetPacketGenerator;
class="kw">import com.hypixel.hytale.server.core.universe.Universe;
class="kw">import com.hypixel.hytale.server.core.util.NotificationUtil;
class="kw">import com.hypixel.hytale.server.core.util.io.FileUtil;
class="kw">import it.unimi.dsi.fastutil.objects.ObjectArrayList;
class="kw">import java.io.IOException;
class="kw">import java.lang.ref.SoftReference;
class="kw">import java.nio.file.Files;
class="kw">import java.nio.file.Path;
class="kw">import java.util.Map;
class="kw">import java.util.Objects;
class="kw">import java.util.Queue;
class="kw">import java.util.Set;
class="kw">import java.util.Map.Entry;
class="kw">import java.util.concurrent.ConcurrentLinkedQueue;
class="kw">import java.util.function.Consumer;
class="kw">import java.util.function.Function;
class="kw">import java.util.logging.Level;
class="kw">import java.util.stream.Stream;
class="kw">import javax.annotation.Nonnull;
class="kw">import javax.annotation.Nullable;

class="kw">public class HytaleAssetStore<K, T class="kw">extends JsonAssetWithMap<K, M>, M class="kw">extends AssetMap<K, T>> class="kw">extends AssetStore<K, T, M> {
   class="kw">public class="kw">static class="kw">final Queue<Consumer<ToClientPacket>> SETUP_PACKET_CONSUMERS = new ConcurrentLinkedQueue<>();
   class="kw">protected class="kw">final AssetPacketGenerator<K, T, M> packetGenerator;
   class="kw">protected class="kw">final Function<K, ItemWithAllMetadata> notificationItemFunction;
   @Nullable
   class="kw">protected SoftReference<ToClientPacket[]> cachedInitPackets;

   class="kw">public HytaleAssetStore(@Nonnull HytaleAssetStore.Builder<K, T, M> var1) {
      super(var1);
      this.packetGenerator = var1.packetGenerator;
      this.notificationItemFunction = var1.notificationItemFunction;
   }

   class="kw">public AssetPacketGenerator<K, T, M> getPacketGenerator() {
      class="kw">return this.packetGenerator;
   }

   class="kw">public Function<K, ItemWithAllMetadata> getNotificationItemFunction() {
      class="kw">return this.notificationItemFunction;
   }

   @Nonnull
   class="kw">protected EventBus getEventBus() {
      class="kw">return HytaleServer.get().getEventBus();
   }

   @Override
   class="kw">public void addFileMonitor(@Nonnull String var1, @Nonnull Path var2) {
      AssetMonitor var3 = AssetModule.get().getAssetMonitor();
      if (var3 != null) {
         var3.monitorDirectoryFiles(var2, new HytaleAssetStore.AssetStoreMonitorHandler(var1));
      }
   }

   @Override
   class="kw">public void removeFileMonitor(@Nonnull Path var1) {
      AssetMonitor var2 = AssetModule.get().getAssetMonitor();
      if (var2 != null) {
         var2.removeMonitorDirectoryFiles(var1, this);
      }
   }

   @Override
   class="kw">protected void handleRemoveOrUpdate(@Nullable Set<K> var1, @Nullable Map<K, T> var2, @Nonnull AssetUpdateQuery var3) {
      if (this.packetGenerator != null) {
         this.cachedInitPackets = null;
         Universe var4 = Universe.get();
         if (var4.getPlayerCount() != 0 || !SETUP_PACKET_CONSUMERS.isEmpty()) {
            if (var1 != null && !var1.isEmpty()) {
               ToClientPacket var5 = this.packetGenerator.generateRemovePacket(this.assetMap, var1, var3);
               var4.broadcastPacketNoCache(var5);

               for (Consumer var7 : SETUP_PACKET_CONSUMERS) {
                  var7.accept(var5);
               }
            }

            if (var2 != null && !var2.isEmpty()) {
               ToClientPacket var8 = this.packetGenerator.generateUpdatePacket(this.assetMap, var2, var3);
               var4.broadcastPacketNoCache(var8);

               for (Consumer var10 : SETUP_PACKET_CONSUMERS) {
                  var10.accept(var8);
               }
            }
         }
      }
   }

   class="kw">public void sendAssets(@Nonnull Consumer<ToClientPacket[]> var1) {
      if (this.packetGenerator != null) {
         ToClientPacket[] var2 = this.cachedInitPackets == null ? null : this.cachedInitPackets.get();
         if (var2 != null) {
            var1.accept(var2);
         } else {
            Map var3 = this.assetMap.getAssetMap();
            ToClientPacket var4 = this.packetGenerator.generateInitPacket(this.assetMap, var3);
            this.cachedInitPackets = new SoftReference<>(var2 = new ToClientPacket[]{var4});
            var1.accept(var2);
         }
      }
   }

   class="kw">protected void sendReloadedNotification(@Nonnull AssetLoadResult<K, T> var1) {
      this.sendNotificationKeys(
         Message.translation("server.general.assetstore.reloadAssets").param("class", this.tClass.getSimpleName()).color("#A7AfA7"),
         "Icons/AssetNotifications/AssetReloaded.png",
         var1.getLoadedAssets().keySet()
      );
      this.sendNotificationKeys(
         Message.translation("server.general.assetstore.loadFailed").param("class", this.tClass.getSimpleName()), null, var1.getFailedToLoadKeys()
      );
      this.sendNotificationPaths(
         Message.translation("server.general.assetstore.loadFailed").param("class", this.tClass.getSimpleName()), var1.getFailedToLoadPaths()
      );
   }

   class="kw">protected void sendRemovedNotification(@Nonnull Set<K> var1) {
      this.sendNotificationKeys(
         Message.translation("server.general.assetstore.removedAssets").param("class", this.tClass.getSimpleName()).color("#FF3874"),
         "Icons/AssetNotifications/Trash.png",
         var1
      );
   }

   class="kw">protected void sendNotificationKeys(Message var1, @Nullable String var2, @Nonnull Set<K> var3) {
      if (!var3.isEmpty()) {
         if (this.notificationItemFunction != null && var3.size() < 5) {
            for (Object var5 : var3) {
               ItemWithAllMetadata var6 = this.notificationItemFunction.apply(var5);
               if (var6 != null) {
                  NotificationUtil.sendNotificationToUniverse(var1, Message.raw(var5.toString()), var6, NotificationStyle.Default);
               } else {
                  NotificationUtil.sendNotificationToUniverse(var1, Message.raw(var5.toString()), var2, null, NotificationStyle.Default);
               }
            }
         } else {
            Message var4 = Message.translation("server.general.assetstore.removedAssetsSecondaryGeneric").param("count", var3.size());
            NotificationUtil.sendNotificationToUniverse(var1, var4, var2, NotificationStyle.Default);
         }
      }
   }

   class="kw">protected void sendNotificationPaths(Message var1, @Nonnull Set<Path> var2) {
      if (!var2.isEmpty()) {
         NotificationUtil.sendNotificationToUniverse(var1, Message.raw(var2.toString()), NotificationStyle.Warning);
      }
   }

   @Nonnull
   class="kw">public class="kw">static <T class="kw">extends JsonAssetWithMap<String, M>, M class="kw">extends AssetMap<String, T>> HytaleAssetStore.Builder<String, T, M> builder(Class<T> var0, M var1) {
      class="kw">return new HytaleAssetStore.Builder<>(String.class, var0, var1);
   }

   @Nonnull
   class="kw">public class="kw">static <K, T class="kw">extends JsonAssetWithMap<K, M>, M class="kw">extends AssetMap<K, T>> HytaleAssetStore.Builder<K, T, M> builder(Class<K> var0, Class<T> var1, M var2) {
      class="kw">return new HytaleAssetStore.Builder<>(var0, var1, var2);
   }

   class="kw">static {
      AssetStore.DISABLE_ASSET_COMPARE = Options.getOptionSet().has(Options.DISABLE_ASSET_COMPARE);
   }

   class="kw">private class AssetStoreMonitorHandler class="kw">implements AssetMonitorHandler {
      class="kw">private class="kw">final String packKey;

      class="kw">public AssetStoreMonitorHandler(String nullx) {
         this.packKey = nullx;
      }

      @Override
      class="kw">public Object getKey() {
         class="kw">return HytaleAssetStore.this;
      }

      class="kw">public boolean test(Path var1, EventKind var2) {
         class="kw">return !Files.isRegularFile(var1) || var1.getFileName().toString().endsWith(HytaleAssetStore.this.extension);
      }

      class="kw">public void accept(Map<Path, EventKind> var1) {
         ObjectArrayList var2 = new ObjectArrayList();
         ObjectArrayList var3 = new ObjectArrayList();
         ObjectArrayList var4 = new ObjectArrayList();
         ObjectArrayList var5 = new ObjectArrayList();

         for (Entry var7 : var1.entrySet()) {
            Path var8 = var7.getKey();
            class="kw">switch ((EventKind)var7.getValue()) {
               case ENTRY_CREATE:
                  if (Files.isDirectory(var8)) {
                     HytaleAssetStore.this.logger.at(Level.FINEST).log("Directory Created: %s", var8);

                     try (Stream var19 = Files.walk(var8, FileUtil.DEFAULT_WALK_TREE_OPTIONS_ARRAY)) {
                        var19.forEach(var4x -> {
                           if (Files.isDirectory(var4x)) {
                              var4.add(var8);
                           }

                           if (Files.isRegularFile(var4x) && var4x.getFileName().toString().endsWith(HytaleAssetStore.this.extension)) {
                              var2.add(var4x);
                           }
                        });
                     } catch (IOException var14) {
                        ((HytaleLogger.Api)HytaleAssetStore.this.logger.at(Level.SEVERE).withCause(var14))
                           .log("Failed to reload assets in directory: %s", var8);
                     }
                  } else {
                     HytaleAssetStore.this.logger.at(Level.FINEST).log("File Created: %s", var8);
                     var2.add(var8);
                     var2.add(var8);
                  }
                  break;
               case ENTRY_DELETE:
                  var5.add(var8);

                  for (Path var10 : HytaleAssetStore.this.getAssetMap().getPathMap(this.packKey).values()) {
                     if (PathUtil.isChildOf(var8, var10)) {
                        HytaleAssetStore.this.logger.at(Level.FINEST).log("Deleted: %s", var10);
                        var3.add(var10);
                     }
                  }
                  break;
               case ENTRY_MODIFY:
                  if (Files.isDirectory(var8)) {
                     HytaleAssetStore.this.logger.at(Level.FINEST).log("Directory Modified: %s", var8);
                     var4.add(var8);
                  } else {
                     HytaleAssetStore.this.logger.at(Level.FINEST).log("File Modified: %s", var8);
                     var2.add(var8);
                  }
            }
         }

         if (!var5.isEmpty() || !var2.isEmpty() || !var4.isEmpty()) {
            IEventDispatcher var15 = HytaleAssetStore.this.getEventBus().dispatchFor(AssetStoreMonitorEvent.class);
            if (var15.hasListener()) {
               var15.dispatch(new AssetStoreMonitorEvent(this.packKey, HytaleAssetStore.this, var2, var3, var4, var5));
            }
         }

         if (!var2.isEmpty()) {
            HytaleAssetStore.this.logger.at(Level.INFO).log("Reloading assets: %s", var2);
            long var16 = System.nanoTime();
            AssetLoadResult var18 = HytaleAssetStore.this.loadAssetsFromPaths(this.packKey, var2, AssetUpdateQuery.DEFAULT, true);
            HytaleAssetStore.this.logger.at(Level.INFO).log("Took %s to reload assets: %s", FormatUtil.nanosToString(System.nanoTime() - var16), var2);
            HytaleAssetStore.this.sendReloadedNotification(var18);
         }

         if (!var3.isEmpty()) {
            HytaleAssetStore.this.logger.at(Level.INFO).log("Removing deleted assets: %s", var3);
            Set var17 = HytaleAssetStore.this.removeAssetWithPaths(this.packKey, var3);
            HytaleAssetStore.this.sendRemovedNotification(var17);
         }
      }
   }

   class="kw">public class="kw">static class Builder<K, T class="kw">extends JsonAssetWithMap<K, M>, M class="kw">extends AssetMap<K, T>>
      class="kw">extends AssetStore.Builder<K, T, M, HytaleAssetStore.Builder<K, T, M>> {
      class="kw">protected AssetPacketGenerator<K, T, M> packetGenerator;
      class="kw">protected Function<K, ItemWithAllMetadata> notificationItemFunction;

      class="kw">public Builder(Class<K> var1, Class<T> var2, M var3) {
         super(var1, var2, var3);
      }

      @Nonnull
      class="kw">public HytaleAssetStore.Builder<K, T, M> setPacketGenerator(AssetPacketGenerator<K, T, M> var1) {
         this.packetGenerator = Objects.requireNonNull(var1, "packetGenerator can't be null!");
         class="kw">return this;
      }

      @Nonnull
      class="kw">public HytaleAssetStore.Builder<K, T, M> setNotificationItemFunction(Function<K, ItemWithAllMetadata> var1) {
         this.notificationItemFunction = Objects.requireNonNull(var1, "notificationItemFunction can't be null!");
         class="kw">return this;
      }

      @Nonnull
      class="kw">public HytaleAssetStore<K, T, M> build() {
         class="kw">return new HytaleAssetStore<>(this);
      }
   }
}