98 lines
3.5 KiB
Java
98 lines
3.5 KiB
Java
/*
|
|
* Copyright (C) 2025 Minecon724
|
|
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
|
|
* in the project root for the full license text.
|
|
*/
|
|
|
|
package eu.m724.tweaks.module.updater;
|
|
|
|
import eu.m724.tweaks.DebugLogger;
|
|
import eu.m724.tweaks.module.TweaksModule;
|
|
import eu.m724.tweaks.module.updater.backend.UpdateChecker;
|
|
import eu.m724.tweaks.module.updater.backend.VersionCache;
|
|
import eu.m724.tweaks.module.updater.object.ResourceVersion;
|
|
import eu.m724.tweaks.module.updater.object.SpigotResource;
|
|
import eu.m724.tweaks.module.updater.object.VersionedResource;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class UpdaterModule extends TweaksModule {
|
|
@Override
|
|
protected void onInit() {
|
|
var resources = loadInstalledPlugins();
|
|
|
|
// load installed versions from cache
|
|
var cacheFile = new File(getPlugin().getDataFolder(), "storage/cache/updater");
|
|
|
|
final var installedVersions = loadInstalledVersionCache(cacheFile);
|
|
|
|
var versionedResources = resources.stream()
|
|
.map(res -> new VersionedResource(
|
|
res, installedVersions.stream().filter(iv -> iv.resourceId() == res.resourceId()).findFirst().orElse(null), null
|
|
))
|
|
.collect(Collectors.toSet());
|
|
|
|
|
|
var updateChecker = new UpdateChecker(getPlugin().getLogger(), cacheFile, versionedResources);
|
|
updateChecker.runTaskTimerAsynchronously(getPlugin(), 600, 12 * 3600 * 20); // 12 hours
|
|
|
|
registerCommand("updates", new UpdaterCommands(updateChecker));
|
|
}
|
|
|
|
private Set<ResourceVersion> loadInstalledVersionCache(File cacheFile) {
|
|
try (FileInputStream inputStream = new FileInputStream(cacheFile)) {
|
|
return VersionCache.loadAll(inputStream);
|
|
} catch (FileNotFoundException ignored) {
|
|
} catch (IOException e) {
|
|
DebugLogger.warning("Error loading installed version cache, starting fresh. " + e.getMessage());
|
|
}
|
|
|
|
return new HashSet<>();
|
|
}
|
|
|
|
private Set<SpigotResource> loadInstalledPlugins() {
|
|
File installedPluginsYml = new File(getPlugin().getDataFolder(), "installed_plugins.yml");
|
|
|
|
if (!installedPluginsYml.exists()) {
|
|
getPlugin().saveResource("installed_plugins.yml", false);
|
|
}
|
|
|
|
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(installedPluginsYml);
|
|
|
|
Plugin[] plugins = getPlugin().getServer().getPluginManager().getPlugins();
|
|
Set<SpigotResource> spigotResources = new HashSet<>();
|
|
|
|
for (Plugin plugin : plugins) {
|
|
String pluginName = plugin.getName();
|
|
|
|
if (!configuration.isSet(pluginName)) {
|
|
configuration.set(pluginName, -1);
|
|
continue;
|
|
}
|
|
|
|
int pluginId = configuration.getInt(pluginName);
|
|
if (pluginId > 0) {
|
|
spigotResources.add(
|
|
new SpigotResource(plugin, pluginId, pluginName)
|
|
);
|
|
}
|
|
}
|
|
|
|
try {
|
|
configuration.save(installedPluginsYml);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Failed to update installed_plugins.yml", e);
|
|
}
|
|
|
|
return spigotResources;
|
|
|
|
}
|
|
}
|