Usuário:SandroHcBot/Código Fonte/HelpTask

De RuneScape Wiki
Ir para: navegação, pesquisa

Dá uma ajuda em que tarefas estão disponíveis.

/*--------------------------------------------------------
 * AmauriceBot - RuneScape Wikia update task robot
 * Copyright (c) 2009-2012  Maurice Abraham.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *   None
 *------------------------------------------------------*/

package amauricebot;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

@Description(summary = "Dá uma ajuda em que tarefas estão disponíveis.")
public class HelpTask implements BotTask {
	public HelpTask(WikiSession wiki) {
		// wiki session not actually needed for this task
	}

	public void perform() throws IOException {
		ArrayList<String> taskList = getTaskList("amauricebot");
		Collections.sort(taskList);

		System.err.println("As Tarefas podem ser:");
		for (String taskName : taskList) {
			String summary = "???";

			// get task class
			String className = "amauricebot."
					+ taskName.substring(0, 1).toUpperCase()
					+ taskName.substring(1).toLowerCase() + "Task";
			try {
				Class<?> taskClass = Class.forName(className);
				Description desc = taskClass.getAnnotation(Description.class);
				if (desc != null)
					summary = desc.summary();
			} catch (ClassNotFoundException ex) {
				// ignore exception
			}

			System.err
					.println(String.format("  %-10s = %s", taskName, summary));
		}
		System.err.println("");
	}

	private ArrayList<String> getTaskList(String packageName)
			throws IOException {
		ArrayList<String> taskList = new ArrayList<String>();
		ClassLoader classLoader = Thread.currentThread()
				.getContextClassLoader();
		if (classLoader == null)
			return taskList;

		Enumeration<URL> resources = classLoader.getResources(packageName);
		while (resources.hasMoreElements()) {
			URL resource = resources.nextElement();

			try {
				// if classes are in a directory
				if (resource.getProtocol().equals("file")) {
					File directory = new File(resource.toURI());
					if (!directory.isDirectory())
						continue;

					File[] files = directory.listFiles();
					for (File file : files) {
						String task = getTaskName(file.getName());
						if (task != null)
							taskList.add(task);
					}
					continue;
				}

				// if classes are in a directory
				if (resource.getProtocol().equals("jar")) {
					String jarPath = resource.toString().substring(4);
					jarPath = jarPath.replaceAll("!.*", "");
					String pkgPath = packageName.replaceAll("\\.", "/") + "/";

					JarFile jarFile = new JarFile(new File(new URI(jarPath)));
					Enumeration<JarEntry> entries = jarFile.entries();
					while (entries.hasMoreElements()) {
						JarEntry entry = entries.nextElement();
						String entryName = entry.getName();
						if (!entryName.startsWith(pkgPath))
							continue;
						entryName = entryName.substring(pkgPath.length());
						String task = getTaskName(entryName);
						if (task != null)
							taskList.add(task);
					}
					jarFile.close();
				}
			} catch (Exception ex) {
				System.err.println(ex.toString());
			}
		}

		return taskList;
	}

	private String getTaskName(String fileName) {
		if (!fileName.endsWith("Task.class"))
			return null;
		if (fileName.contains("$"))
			return null;
		if (fileName.equals("BotTask.class"))
			return null;
		if (fileName.startsWith("Dev"))
			return null;

		return fileName.replaceAll("Task\\.class", "").toLowerCase();
	}
}