From 5c618099af4b06a9ee22a2dd29c0646686f49db9 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sun, 26 Sep 2021 14:50:14 +0300 Subject: [PATCH] Added web-dev related settings - Customized_files.Path can now be used to choose where Html Customized files are placed (Change this to `"/Plan/common/src/main/resources/assets/plan/web"` for easy time developing) - Customized_files.Enable_web_dev_mode can be used to enable modifications of all files as well as bypass resource caching for instant updates on browser refresh. Adding new files still needs recompiling the plugin, but this should speed up modifications to existing parts of the website considerably. - #2098 --- .../plan/delivery/web/ResourceSvc.java | 4 +-- .../plan/settings/config/PlanConfig.java | 4 +-- .../settings/config/ResourceSettings.java | 18 +++++++++- .../config/paths/CustomizedFileSettings.java | 26 ++++++++++++++ .../plan/storage/file/PlanFiles.java | 36 ++++++++++++++----- .../resources/assets/plan/bungeeconfig.yml | 4 +++ .../src/main/resources/assets/plan/config.yml | 4 +++ .../plan/storage/file/SpongePlanFiles.java | 7 ++-- 8 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/settings/config/paths/CustomizedFileSettings.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/web/ResourceSvc.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/web/ResourceSvc.java index 2af14d94c..54fb14e0c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/web/ResourceSvc.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/web/ResourceSvc.java @@ -146,7 +146,7 @@ public class ResourceSvc implements ResourceService { } } catch (IOException e) { errorLogger.warn(e, ErrorContext.builder() - .whatToDo("Report this or provide " + fileName + " in " + files.getCustomizationDirectory()) + .whatToDo("Report this or provide " + fileName + " in " + resourceSettings.getCustomizationDirectory()) .related("Fetching resource", "Of: " + pluginName, fileName).build()); } // Return original by default @@ -174,7 +174,7 @@ public class ResourceSvc implements ResourceService { WebResource original = source.get(); byte[] bytes = original.asBytes(); OpenOption[] overwrite = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE}; - Path to = files.getCustomizationDirectory().resolve(fileName); + Path to = resourceSettings.getCustomizationDirectory().resolve(fileName); Path dir = to.getParent(); if (!Files.isSymbolicLink(dir)) Files.createDirectories(dir); Files.write(to, bytes, overwrite); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/config/PlanConfig.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/PlanConfig.java index fb4dc6f86..88ea69c7a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/settings/config/PlanConfig.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/PlanConfig.java @@ -67,7 +67,7 @@ public class PlanConfig extends Config { this.files = files; this.extensionSettings = new ExtensionSettings(this); - this.resourceSettings = new ResourceSettings(this); + this.resourceSettings = new ResourceSettings(files, this); this.worldAliasSettings = worldAliasSettings; this.logger = logger; } @@ -123,7 +123,7 @@ public class PlanConfig extends Config { public Path getPageExportPath() { Path exportDirectory = Paths.get(get(ExportSettings.HTML_EXPORT_PATH)); - Path customizationDirectory = files.getCustomizationDirectory(); + Path customizationDirectory = resourceSettings.getCustomizationDirectory(); if (exportDirectory.toAbsolutePath().equals(customizationDirectory.toAbsolutePath())) { logger.warn("'" + ExportSettings.HTML_EXPORT_PATH.getPath() + "' can not be '/Plan/web/' directory, using '/Plan/Analysis Results' as fallback."); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/config/ResourceSettings.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/ResourceSettings.java index 8fdab79f7..a7b6ea246 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/settings/config/ResourceSettings.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/ResourceSettings.java @@ -16,23 +16,33 @@ */ package com.djrapitops.plan.settings.config; +import com.djrapitops.plan.settings.config.paths.CustomizedFileSettings; +import com.djrapitops.plan.storage.file.PlanFiles; import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; public class ResourceSettings { + private final PlanFiles files; private final PlanConfig config; public ResourceSettings( - PlanConfig config + PlanFiles files, PlanConfig config ) { + this.files = files; this.config = config; } public boolean shouldBeCustomized(String plugin, String fileName) { + if (config.isTrue(CustomizedFileSettings.WEB_DEV_MODE)) { + return true; + } + ConfigNode fileCustomization = getCustomizationConfigNode(); fileCustomization.setComment(Collections.singletonList("The files are placed in /Plan/web/ if the setting is 'true' when accessed.")); @@ -57,4 +67,10 @@ public class ResourceSettings { return config.getNode("Customized_files").orElseGet(() -> config.addNode("Customized_files")); } + public Path getCustomizationDirectory() { + Path exportDirectory = Paths.get(config.get(CustomizedFileSettings.PATH)); + return exportDirectory.isAbsolute() + ? exportDirectory + : files.getDataDirectory().resolve(exportDirectory); + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/settings/config/paths/CustomizedFileSettings.java b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/paths/CustomizedFileSettings.java new file mode 100644 index 000000000..047a45e0e --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/settings/config/paths/CustomizedFileSettings.java @@ -0,0 +1,26 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.settings.config.paths; + +import com.djrapitops.plan.settings.config.paths.key.BooleanSetting; +import com.djrapitops.plan.settings.config.paths.key.Setting; +import com.djrapitops.plan.settings.config.paths.key.StringSetting; + +public class CustomizedFileSettings { + public static final Setting WEB_DEV_MODE = new BooleanSetting("Customized_files.Enable_web_dev_mode"); + public static final Setting PATH = new StringSetting("Customized_files.Path"); +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java index e4fa1b0c2..69614266a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/file/PlanFiles.java @@ -18,6 +18,9 @@ package com.djrapitops.plan.storage.file; import com.djrapitops.plan.SubSystem; import com.djrapitops.plan.exceptions.EnableException; +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plan.settings.config.paths.CustomizedFileSettings; +import dagger.Lazy; import javax.inject.Inject; import javax.inject.Named; @@ -42,13 +45,17 @@ public class PlanFiles implements SubSystem { private final File dataFolder; private final File configFile; + private final Lazy config; + @Inject public PlanFiles( @Named("dataFolder") File dataFolder, - JarResource.StreamFunction getResourceStream + JarResource.StreamFunction getResourceStream, + Lazy config ) { this.dataFolder = dataFolder; this.getResourceStream = getResourceStream; + this.config = config; this.configFile = getFileFromPluginFolder("config.yml"); } @@ -60,8 +67,9 @@ public class PlanFiles implements SubSystem { return dataFolder.toPath(); } + @Deprecated public Path getCustomizationDirectory() { - return getDataDirectory().resolve("web"); + return config.get().getResourceSettings().getCustomizationDirectory(); } public File getLogsFolder() { @@ -129,16 +137,28 @@ public class PlanFiles implements SubSystem { return new FileResource(resourceName, getFileFromPluginFolder(resourceName)); } + // TODO Customized file logic should be moved to another class so the circular dependency on config can be removed. public Optional getCustomizableResource(String resourceName) { - return Optional.ofNullable(ResourceCache.getOrCache(resourceName, - () -> attemptToFind(resourceName) - .map(found -> new FileResource(resourceName, found)) - .orElse(null) - )); + return Optional.ofNullable(findCustomized(resourceName)); + } + + private Resource findCustomized(String resourceName) { + if (config.get().isTrue(CustomizedFileSettings.WEB_DEV_MODE)) { + // Bypass cache in web developer mode. + return getFileResource(resourceName); + } else { + return ResourceCache.getOrCache(resourceName, () -> getFileResource(resourceName)); + } + } + + private FileResource getFileResource(String resourceName) { + return attemptToFind(resourceName) + .map(found -> new FileResource(resourceName, found)) + .orElse(null); } public Optional attemptToFind(String resourceName) { - Path dir = getCustomizationDirectory(); + Path dir = config.get().getResourceSettings().getCustomizationDirectory(); if (dir.toFile().exists() && dir.toFile().isDirectory()) { Path asPath = dir.resolve(resourceName); File found = asPath.toFile(); diff --git a/Plan/common/src/main/resources/assets/plan/bungeeconfig.yml b/Plan/common/src/main/resources/assets/plan/bungeeconfig.yml index 6c2b77156..cb69c5440 100644 --- a/Plan/common/src/main/resources/assets/plan/bungeeconfig.yml +++ b/Plan/common/src/main/resources/assets/plan/bungeeconfig.yml @@ -216,3 +216,7 @@ Plugins: Buycraft: # https://docs.tebex.io/store/faq#how-can-i-find-my-secret-key Secret: "-" +Customized_files: + Path: "web" + # Web dev mode enables all customized files and disables webserver resource cache for instant changes on browser refresh. + Enable_web_dev_mode: false \ No newline at end of file diff --git a/Plan/common/src/main/resources/assets/plan/config.yml b/Plan/common/src/main/resources/assets/plan/config.yml index 70016783e..7c1adc984 100644 --- a/Plan/common/src/main/resources/assets/plan/config.yml +++ b/Plan/common/src/main/resources/assets/plan/config.yml @@ -228,3 +228,7 @@ Plugins: Towny: HideTowns: - ExampleTown +Customized_files: + Path: "web" + # Web dev mode enables all customized files and disables webserver resource cache for instant changes on browser refresh. + Enable_web_dev_mode: false \ No newline at end of file diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/storage/file/SpongePlanFiles.java b/Plan/sponge/src/main/java/com/djrapitops/plan/storage/file/SpongePlanFiles.java index f4c65ba44..ce201018d 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/storage/file/SpongePlanFiles.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/storage/file/SpongePlanFiles.java @@ -17,6 +17,8 @@ package com.djrapitops.plan.storage.file; import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.settings.config.PlanConfig; +import dagger.Lazy; import org.spongepowered.api.Sponge; import javax.inject.Inject; @@ -38,9 +40,10 @@ public class SpongePlanFiles extends PlanFiles { public SpongePlanFiles( @Named("dataFolder") File dataFolder, JarResource.StreamFunction getResourceStream, - PlanPlugin plugin + PlanPlugin plugin, + Lazy config ) { - super(dataFolder, getResourceStream); + super(dataFolder, getResourceStream, config); this.plugin = plugin; }