Skip to content

Commit

Permalink
Merge pull request #34 from TeamKun/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
PeyaPeyaPeyang authored Feb 27, 2021
2 parents 1d83d12 + 69dedd4 commit 58cfc0f
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.kunmc.lab</groupId>
<artifactId>TeamKunPluginManager</artifactId>
<version>2.0</version>
<version>2.1</version>
<packaging>jar</packaging>

<name>TeamKunPluginManager</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,19 @@ public static InstallResult install(CommandSender sender, String url, boolean ig
catch (FileNotFoundException e)
{
finalSender.sendMessage(ChatColor.RED + "E: ファイルが見つかりませんでした。");
if(!withoutRemove)
delete(finalSender, new File("plugins/" + downloadResult.getValue()));

finalSender.sendMessage(Messages.getStatusMessage(add, remove, modify));

return new InstallResult(add, remove, modify, false);
}
catch (IOException | InvalidDescriptionException e)
{
finalSender.sendMessage(ChatColor.RED + "E: 情報を読み込めませんでした。");
if(!withoutRemove)
delete(finalSender, new File("plugins/" + downloadResult.getValue()));

finalSender.sendMessage(Messages.getStatusMessage(add, remove, modify));
return new InstallResult(add, remove, modify, false);
}
Expand All @@ -198,10 +205,32 @@ public static InstallResult install(CommandSender sender, String url, boolean ig
{
sender.sendMessage(ChatColor.RED + "E: このプラグインは保護されています。");
add--;
if(!withoutRemove)
delete(finalSender, new File("plugins/" + downloadResult.getValue()));
finalSender.sendMessage(Messages.getStatusMessage(add, remove, modify));
return new InstallResult(add, remove, modify, false);
}


if (atomicURL.get().startsWith("https://apple.api.spiget.org"))
{
try
{
String fileName = description.getName().replace(" ", "") +
"-" +
description.getVersion() +
".jar";
FileUtils.moveFile(
new File("plugins/" + downloadResult.getValue()),
new File("plugins/" + fileName));
downloadResult = new Pair<>(false, fileName);
}
catch (IOException e)
{
e.printStackTrace();
}
}

DependencyTree.Info info = DependencyTree.getInfo(description.getName(), false);

Plugin plugin = Bukkit.getPluginManager().getPlugin(description.getName());
Expand All @@ -222,14 +251,10 @@ public static InstallResult install(CommandSender sender, String url, boolean ig
@Override
public void run()
{
try
{
PluginUtil.getFile(plugin).delete();
}
catch (Exception e)
{
finalSender.sendMessage(ChatColor.RED + "E: ファイルの削除に失敗しました: " + downloadResult.getValue());
}

if(!withoutRemove)
delete(finalSender, PluginUtil.getFile(plugin));


}
}.runTaskLater(TeamKunPluginManager.plugin, 10L);
Expand All @@ -241,16 +266,7 @@ else if (PluginUtil.isPluginLoaded(description.getName()))
finalSender.sendMessage(ChatColor.YELLOW + "W: 既に同じプラグインが存在します。");
if (!withoutRemove && new File("plugins/" + downloadResult.getValue()).exists())
{
try
{
File f = new File("plugins/" + downloadResult.getValue());
f.setWritable(true);
f.delete();
}
catch (Exception e)
{
finalSender.sendMessage(ChatColor.RED + "E: ファイルの削除に失敗しました: " + downloadResult.getValue());
}
delete(finalSender, new File("plugins/" + downloadResult.getValue()));
}
finalSender.sendMessage(Messages.getStatusMessage(add, remove, modify));
finalSender.sendMessage(ChatColor.GREEN + "S: " + description.getFullName() + " を正常にインストールしました。");
Expand Down Expand Up @@ -325,18 +341,9 @@ else if (PluginUtil.isPluginLoaded(description.getName()))
if (PluginUtil.isPluginLoaded(description.getName()))
{
finalSender.sendMessage(ChatColor.RED + "E: Bukkitのインジェクションに失敗しました。");
try
{
if (!withoutRemove)
{
new File("plugins/" + f.fileName).setWritable(true);
new File("plugins/" + f.fileName).delete();
}
}
catch (Exception ex)
{
finalSender.sendMessage(ChatColor.RED + "E: ファイルの削除に失敗しました: " + downloadResult.getValue());
}

if(!withoutRemove)
delete(finalSender, new File("plugins/" + f.fileName));

PluginUtil.unload(plugin);

Expand All @@ -357,20 +364,8 @@ public void run()
}
catch (Exception e)
{
try
{
if (!withoutRemove)
{
new File("plugins/" + f.fileName).setWritable(true);
new File("plugins/" + f.fileName).delete();
}


}
catch (Exception ex)
{
finalSender.sendMessage(ChatColor.RED + "E: ファイルの削除に失敗しました: " + downloadResult.getValue());
}
if (!withoutRemove)
delete(finalSender, new File("plugins/" + f.fileName));
e.printStackTrace();
success.set(false);
}
Expand Down Expand Up @@ -411,6 +406,19 @@ public static String[] getRemovableDataDirs()
}
}

public static void delete(CommandSender sender, File f)
{
try
{
f.delete();
}
catch (Exception e)
{
sender.sendMessage(ChatColor.RED + "E: ファイルの削除に失敗しました: " + f.getName());
}

}

/**
* プラグインデータフォルダを削除
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import net.kunmc.lab.teamkunpluginmanager.TeamKunPluginManager;
import net.kunmc.lab.teamkunpluginmanager.plugin.KnownPlugins;
import net.kunmc.lab.teamkunpluginmanager.utils.rdmarker.DevBukkit;
import net.kunmc.lab.teamkunpluginmanager.utils.rdmarker.Spigotmc;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.UrlValidator;

import java.util.List;

public class PluginResolver
{

/**
* プラグインのURLを名前解決します。
*
Expand All @@ -18,7 +21,13 @@ public class PluginResolver
public static String asUrl(String query)
{
if (UrlValidator.getInstance().isValid(query))
{
if (DevBukkit.isMatch(query))
return DevBukkit.toDownloadUrl(query);
else if (Spigotmc.isMatch(query))
return Spigotmc.toDownloadUrl(query);
return GitHubURLBuilder.urlValidate(query);
}

if (KnownPlugins.isKnown(query))
return GitHubURLBuilder.urlValidate(KnownPlugins.getKnown(query).url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ public static String getAsString(String urlString)
*/
public static Pair<Boolean, String> downloadFile(String url)
{
String fileName = url.substring(url.lastIndexOf("/") + 1);
return downloadFile(url, url.substring(url.lastIndexOf("/") + 1));
}

/**
* ファイルをだうんろーど!
*
* @param url URL
* @param fileName ファイル名
* @return ローカルのパス
*/
public static Pair<Boolean, String> downloadFile(String url, String fileName)
{
boolean duplicateFile = false;

if (fileName.equals(""))
Expand All @@ -70,13 +80,34 @@ public static Pair<Boolean, String> downloadFile(String url)
{
URL urlObj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod("GET");
if (urlObj.getHost().equals("api.github.com"))
connection.setRequestProperty("Authorization", "token " + TeamKunPluginManager.vault.getToken());
connection.setRequestProperty("User-Agent", "Mozilla/8.10; Safari/Chrome/Opera/Edge/KungleBot-Peyang; Mobile-Desktop");
connection.connect();
if (connection.getResponseCode() != 200)
return new Pair<>(false, "");
boolean redir = false;
do
{
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
if (urlObj.getHost().equals("api.github.com"))
connection.setRequestProperty("Authorization", "token " + TeamKunPluginManager.vault.getToken());
connection.setRequestProperty("User-Agent", "Mozilla/8.10; Safari/Chrome/Opera/Edge/KungleBot-Peyang; Mobile-Desktop");
connection.connect();

redir = false;
if (String.valueOf(connection.getResponseCode()).startsWith("3"))
{
URL base = connection.getURL();
String locationStr = connection.getHeaderField("Location");
if (locationStr != null)
base = new URL(base, locationStr);

connection.disconnect();
if (base != null)
{
redir = true;
connection = (HttpURLConnection) base.openConnection();
}
}

}
while(redir);

FileUtils.copyInputStreamToFile(connection.getInputStream(), new File("plugins/" + fileName));
return new Pair<>(duplicateFile, fileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package net.kunmc.lab.teamkunpluginmanager.utils.rdmarker;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.kunmc.lab.teamkunpluginmanager.utils.URLUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;

import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DevBukkit
{

private static final Pattern PATTERN = Pattern.compile("^/projects/(?<slug>\\w+)(/?$|/files(/(?<id>\\d+)((/files/(?<version>\\d+))?/download)?)?)/?$");

public static boolean isMatch(String urlName)
{
URL url;
try
{
url = new URL(urlName);
}
catch (Exception e)
{
return false;
}

if (!url.getHost().equals("dev.bukkit.org"))
return false;

Matcher matcher = PATTERN.matcher(url.getPath());

while(matcher.find())
if (matcher.group("slug") != null && !matcher.group("slug").equals(""))
return true;
return false;
}

public static String toDownloadUrl(String urlName)
{
URL url;
try
{
url = new URL(urlName);
}
catch (Exception e)
{
return urlName;
}

if (!url.getHost().equals("dev.bukkit.org"))
return urlName;

Matcher matcher = PATTERN.matcher(url.getPath());

String slug = null;
String version = null;

while(matcher.find())
{
if (matcher.group("slug") != null && !matcher.group("slug").equals(""))
slug = matcher.group("slug");
if (matcher.group("version") != null && !matcher.group("version").equals(""))
version = matcher.group("version");
}

if (slug == null)
return urlName;
JsonArray projectSearchResult = new Gson().fromJson(
URLUtils.getAsString("https://servermods.forgesvc.net/servermods/projects?search=" + slug),
JsonArray.class);

String id = null;

for(JsonElement elm: projectSearchResult)
{
JsonObject obj = (JsonObject) elm;

if (obj.get("slug").getAsString().equalsIgnoreCase(slug))
{
id = obj.get("id").getAsString();
break;
}
}


if (id == null)
return urlName;

JsonArray files = new Gson().fromJson(
URLUtils.getAsString("https://servermods.forgesvc.net/servermods/files?projectIds=" + id),
JsonArray.class);

String apiVersion = StringUtils.split(Bukkit.getVersion(), "-")[0];

String downloadUrl = null;

for(JsonElement elm: files)
{
JsonObject obj = (JsonObject) elm;

if (version != null)
{
if (obj.get("fileUrl").getAsString().endsWith(version))
{
downloadUrl = obj.get("downloadUrl").getAsString();
break;
}
}
else
{
if (apiVersion.contains(obj.get("gameVersion").getAsString()))
{
downloadUrl = obj.get("downloadUrl").getAsString();
break;
}
}
}

if (downloadUrl == null && files.size() > 0)
downloadUrl = ((JsonObject) files.get(files.size() - 1)).get("downloadUrl").getAsString();

if (downloadUrl == null)
return urlName;

return downloadUrl;
}
}
Loading

0 comments on commit 58cfc0f

Please sign in to comment.