Exemplo n.º 1
0
  private void initManagers() {

    SplashScreen.getInstance().post("Loading modules ...");
    ModuleManager moduleManager =
        CoreRegistry.putPermanently(ModuleManager.class, new ModuleManagerImpl());

    SplashScreen.getInstance().post("Loading reflections ...");
    ReflectFactory reflectFactory =
        CoreRegistry.putPermanently(ReflectFactory.class, new ReflectionReflectFactory());
    CopyStrategyLibrary copyStrategyLibrary =
        CoreRegistry.putPermanently(
            CopyStrategyLibrary.class, new CopyStrategyLibrary(reflectFactory));

    CoreRegistry.putPermanently(
        TypeSerializationLibrary.class,
        new TypeSerializationLibrary(reflectFactory, copyStrategyLibrary));

    SplashScreen.getInstance().post("Loading assets ...");
    AssetManager assetManager =
        CoreRegistry.putPermanently(
            AssetManager.class, new AssetManagerImpl(moduleManager.getEnvironment()));
    assetManager.setEnvironment(moduleManager.getEnvironment());
    CoreRegistry.putPermanently(CollisionGroupManager.class, new CollisionGroupManager());
    CoreRegistry.putPermanently(WorldGeneratorManager.class, new WorldGeneratorManager());
    CoreRegistry.putPermanently(ComponentSystemManager.class, new ComponentSystemManager());
    CoreRegistry.putPermanently(
        ParameterAdapterManager.class, ParameterAdapterManager.createCore());
    CoreRegistry.putPermanently(NetworkSystem.class, new NetworkSystemImpl(time));
    CoreRegistry.putPermanently(Game.class, new Game(this, time));
    assetManager.setEnvironment(moduleManager.getEnvironment());

    AssetType.registerAssetTypes(assetManager);
    ApplyModulesUtil.applyModules();
  }
Exemplo n.º 2
0
 public void applyBinds(InputSystem inputSystem, ModuleManager moduleManager) {
   inputSystem.clearBinds();
   registerButtonBinds(
       inputSystem,
       moduleManager.getEnvironment(),
       moduleManager.getEnvironment().getTypesAnnotatedWith(RegisterBindButton.class));
   registerAxisBinds(
       inputSystem,
       moduleManager.getEnvironment(),
       moduleManager.getEnvironment().getTypesAnnotatedWith(RegisterBindAxis.class));
 }
Exemplo n.º 3
0
  /**
   * Activate all modules. Note: this requires that the {@link ModuleSecurityManager} gives
   * permission to do that
   */
  public void activateAllModules() {
    ModuleManager moduleManager = CoreRegistry.get(ModuleManager.class);
    AssetManager assetManager = CoreRegistry.get(AssetManager.class);

    // activate all modules
    for (Module m : moduleManager.getModules()) {
      moduleManager.enableModule(m);
    }

    moduleManager.applyActiveModules();
    assetManager.clear();
    assetManager.applyOverrides();
  }
Exemplo n.º 4
0
 @Override
 public BlockUri resolveBlockFamilyUri(String uri) {
   List<BlockUri> matches = resolveAllBlockFamilyUri(uri);
   switch (matches.size()) {
     case 0:
       logger.warn("Failed to resolve block family '{}'", uri);
       return null;
     case 1:
       return matches.get(0);
     default:
       Module context = ModuleContext.getContext();
       if (context != null) {
         Set<String> dependencies = moduleManager.getDependencyNamesOf(context);
         Iterator<BlockUri> iterator = matches.iterator();
         while (iterator.hasNext()) {
           BlockUri possibleUri = iterator.next();
           if (context.getId().equals(possibleUri.getNormalisedModuleName())) {
             return possibleUri;
           }
           if (!dependencies.contains(possibleUri.getNormalisedModuleName())) {
             iterator.remove();
           }
         }
         if (matches.size() == 1) {
           return matches.get(0);
         }
       }
       logger.warn(
           "Failed to resolve block family '{}' - too many valid matches {}", uri, matches);
       return null;
   }
 }
Exemplo n.º 5
0
 /**
  * Updates a config with any binds that it may be missing, through reflection over
  * RegisterBindButton annotations
  */
 public void updateForChangedMods(Context context) {
   ModuleManager moduleManager = context.get(ModuleManager.class);
   DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
   for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
     if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) {
       ResolutionResult result = resolver.resolve(moduleId);
       if (result.isSuccess()) {
         try (ModuleEnvironment environment =
             moduleManager.loadEnvironment(result.getModules(), false)) {
           updateInputsFor(
               moduleId,
               environment.getTypesAnnotatedWith(
                   RegisterBindButton.class, new FromModule(environment, moduleId)));
         }
       }
     }
   }
 }
Exemplo n.º 6
0
 /**
  * @return A new BindsConfig, with inputs set from the DefaultBinding annotations on bind classes
  */
 public static BindsConfig createDefault(Context context) {
   ModuleManager moduleManager = context.get(ModuleManager.class);
   BindsConfig config = new BindsConfig();
   DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
   for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
     if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) {
       ResolutionResult result = resolver.resolve(moduleId);
       if (result.isSuccess()) {
         try (ModuleEnvironment environment =
             moduleManager.loadEnvironment(result.getModules(), false)) {
           config.addDefaultsFor(
               moduleId,
               environment.getTypesAnnotatedWith(
                   RegisterBindButton.class, new FromModule(environment, moduleId)));
         }
       }
     }
   }
   return config;
 }
Exemplo n.º 7
0
 /**
  * Updates a config with any binds that it may be missing, through reflection over
  * RegisterBindButton annotations
  */
 public void updateForChangedMods(Context context) {
   ModuleManager moduleManager = context.get(ModuleManager.class);
   DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
   for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
     if (moduleManager.getRegistry().getLatestModuleVersion(moduleId).isCodeModule()) {
       ResolutionResult result = resolver.resolve(moduleId);
       if (result.isSuccess()) {
         try (ModuleEnvironment environment =
             moduleManager.loadEnvironment(result.getModules(), false)) {
           FromModule filter = new FromModule(environment, moduleId);
           Iterable<Class<?>> buttons =
               environment.getTypesAnnotatedWith(RegisterBindButton.class, filter);
           Iterable<Class<?>> axes =
               environment.getTypesAnnotatedWith(RegisterRealBindAxis.class, filter);
           updateButtonInputsFor(moduleId, buttons);
           updateAxisInputsFor(moduleId, axes);
         }
       }
     }
   }
 }
  private void sendModules(List<NetData.ModuleRequest> moduleRequestList) {
    for (NetData.ModuleRequest request : moduleRequestList) {
      NetData.ModuleDataHeader.Builder result = NetData.ModuleDataHeader.newBuilder();
      result.setId(request.getModuleId());
      Module module = moduleManager.getEnvironment().get(new Name(request.getModuleId()));
      if (module.isOnClasspath()
          || module.getLocations().size() != 1
          || !Files.isReadable(module.getLocations().get(0))) {
        result.setError("Module not available for download");
      } else {
        Path location = module.getLocations().get(0);
        try {
          result.setVersion(module.getVersion().toString());
          result.setSize(Files.size(location));
          channelHandlerContext
              .getChannel()
              .write(NetData.NetMessage.newBuilder().setModuleDataHeader(result).build());
        } catch (IOException e) {
          logger.error("Error sending module data header", e);
          channelHandlerContext.getChannel().close();
          break;
        }

        try (InputStream stream = new BufferedInputStream(Files.newInputStream(location))) {

          long remainingData = Files.size(location);
          byte[] data = new byte[1024];
          while (remainingData > 0) {
            int nextBlock = (int) Math.min(remainingData, 1024);
            ByteStreams.read(stream, data, 0, nextBlock);
            channelHandlerContext
                .getChannel()
                .write(
                    NetData.NetMessage.newBuilder()
                        .setModuleData(
                            NetData.ModuleData.newBuilder()
                                .setModule(ByteString.copyFrom(data, 0, nextBlock)))
                        .build());
            remainingData -= nextBlock;
          }
        } catch (IOException e) {
          logger.error("Error sending module", e);
          channelHandlerContext.getChannel().close();
          break;
        }
      }
    }
  }