Skip to content

Commit

Permalink
Merge dev/dev - 1.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
GliczDev committed Nov 11, 2023
1 parent b461143 commit da94dd5
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 66 deletions.
2 changes: 1 addition & 1 deletion MockSkript
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import me.glicz.skanalyzer.SkAnalyzer;
import me.glicz.skanalyzer.structure.StructureType;
import me.glicz.skanalyzer.structure.data.StructureData;
import org.bukkit.command.MessageCommandSender;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void finish(Map<String, Map<StructureType, List<StructureData>>> structur
});
});

System.out.println(jsonObject);
SkAnalyzer.get().getLogger().info(jsonObject);
}

private JsonObject getKeyObject(JsonObject jsonObject, String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import ch.njol.skript.log.RedirectingLogHandler;
import ch.njol.skript.structures.StructCommand;
import ch.njol.skript.structures.StructFunction;
import me.glicz.skanalyzer.SkAnalyzer;
import me.glicz.skanalyzer.structure.StructureType;
import me.glicz.skanalyzer.structure.data.EventData;
import me.glicz.skanalyzer.structure.data.FunctionData;
import me.glicz.skanalyzer.structure.data.StructureData;
import org.bukkit.Bukkit;
import org.skriptlang.skript.lang.script.Script;
Expand Down Expand Up @@ -79,32 +81,36 @@ public void parseArgs(List<String> args) {
Skript.getAddonInstance().loadClasses(basePackage + ".economy");
Skript.getAddonInstance().loadClasses(basePackage + ".chat");
Skript.getAddonInstance().loadClasses(basePackage + ".permission");
System.out.println("Force loaded Vault hook.");
SkAnalyzer.get().getLogger().info("Force loaded Vault hook");
} catch (IOException e) {
System.out.println("Something went wrong while trying to force load Vault hook");
e.printStackTrace(System.out);
SkAnalyzer.get().getLogger().error("Something went wrong while trying to force load Vault hook", e);
}
}
if (args.contains("--forceRegionsHook")) {
try {
String basePackage = RegionsPlugin.class.getPackage().getName();
Skript.getAddonInstance().loadClasses(basePackage);
System.out.println("Force loaded Regions hook.");
SkAnalyzer.get().getLogger().info("Force loaded regions hook");
} catch (IOException e) {
System.out.println("Something went wrong while trying to force load Regions hook");
e.printStackTrace(System.out);
SkAnalyzer.get().getLogger().error("Something went wrong while trying to force load regions hook", e);
}
}
}

@Override
public void parseScript(String path) {
File file = new File(path);
if (!file.exists())
if (!file.exists() || !file.getName().endsWith(".sk")) {
SkAnalyzer.get().getLogger().error("Invalid file path");
return;
}
AnalyzerCommandSender sender = new AnalyzerCommandSender();
RedirectingLogHandler logHandler = new RedirectingLogHandler(sender, null).start();
ScriptLoader.loadScripts(file, logHandler, false).thenRun(() -> {
ScriptLoader.loadScripts(file, logHandler, false).whenComplete((info, throwable) -> {
if (throwable != null) {
SkAnalyzer.get().getLogger().error("Something went wrong while trying to parse '%s'".formatted(path), throwable);
return;
}
Map<StructureType, List<StructureData>> structures = new HashMap<>();
Script script = ScriptLoader.getScript(file);
if (script != null) {
Expand All @@ -131,9 +137,10 @@ public void parseScript(String path) {
Signature<?> signature = getFunctionSignature(function);
if (signature == null) return;
structures.putIfAbsent(StructureType.FUNCTION, new ArrayList<>());
structures.get(StructureType.FUNCTION).add(new StructureData(
structures.get(StructureType.FUNCTION).add(new FunctionData(
function.getEntryContainer().getSource().getLine(),
signature.getName()
signature.getName(),
signature.isLocal()
));
}
});
Expand Down
12 changes: 5 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ repositories {
}

dependencies {
compileOnly(project(":MockSkript")) {
exclude("*", "*")
}
api("com.github.seeseemelk:MockBukkit-v1.20:3.9.0")
compileOnly("org.projectlombok:lombok:1.18.30")
annotationProcessor("org.projectlombok:lombok:1.18.30")
api("com.github.seeseemelk:MockBukkit-v1.20:3.44.0")
implementation("org.apache.logging.log4j:log4j-core:3.0.0-alpha1")
implementation("org.slf4j:slf4j-simple:2.0.9")
implementation("commons-io:commons-io:2.14.0")
implementation("commons-lang:commons-lang:2.6")
implementation("org.slf4j:slf4j-simple:2.0.9")
}

java {
Expand All @@ -35,9 +34,8 @@ tasks {
manifest.attributes["Main-Class"] = "me.glicz.skanalyzer.SkAnalyzer"
manifest.attributes["Specification-Version"] = version

dependsOn(project(":MockSkriptBridge").tasks.jar)

subprojects.forEach { subproject ->
dependsOn(subproject.tasks.jar)
from(subproject.tasks.jar.get().outputs.files.singleFile) {
include("*.jar")
rename { "${subproject.name}.jar.embedded" }
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=1.0
version=1.1
group=me.glicz
org.gradle.caching=true
37 changes: 19 additions & 18 deletions src/main/java/me/glicz/skanalyzer/SkAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@

import be.seeseemelk.mockbukkit.MockBukkit;
import com.google.common.base.Preconditions;
import lombok.Getter;
import me.glicz.skanalyzer.loader.AddonsLoader;
import me.glicz.skanalyzer.mockbukkit.AnalyzerServer;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

@Getter
public class SkAnalyzer {
private static SkAnalyzer instance;
private final AnalyzerServer server;
private final Logger logger;

private SkAnalyzer(List<String> args) {
Preconditions.checkArgument(instance == null, "SkAnalyzer instance is already set!");
instance = this;

logger = LogManager.getLogger(args.contains("--enablePlainLogger") ? "PlainLogger" : "SkAnalyzer");

System.out.printf("SkAnalyzer v%s - simple Skript parser. Created by Glicz.%n", getClass().getPackage().getSpecificationVersion());
extractEmbeddedAddons();
System.out.println("Enabling...");
logger.info("Enabling...");

server = MockBukkit.mock(new AnalyzerServer());
AddonsLoader.loadAddons();
AddonsLoader.getMockSkriptBridge().parseArgs(args);

server.startTicking();
System.out.println("Successfully enabled. Have fun!");
logger.info("Successfully enabled. Have fun!");

startReadingInput();
}
Expand All @@ -42,10 +49,6 @@ public static SkAnalyzer get() {
return instance;
}

public AnalyzerServer getServer() {
return server;
}

private void startReadingInput() {
Thread thread = new Thread() {
private final Scanner scanner = new Scanner(System.in);
Expand All @@ -66,22 +69,20 @@ public void run() {
}

private void extractEmbeddedAddons() {
System.out.println("Extracting embedded addons...");
logger.info("Extracting embedded addons...");

try (InputStream embeddedJar = getClass().getClassLoader().getResourceAsStream("MockSkript.jar.embedded")) {
Preconditions.checkArgument(embeddedJar != null, "Couldn't find embedded MockSkript.jar");
FileUtils.copyInputStreamToFile(embeddedJar, new File(AddonsLoader.ADDONS, "MockSkript.jar"));
} catch (IOException e) {
throw new RuntimeException(e);
}
extractEmbeddedAddon(AddonsLoader.MOCK_SKRIPT);
extractEmbeddedAddon(AddonsLoader.MOCK_SKRIPT_BRIDGE);

logger.info("Successfully extracted embedded addons!");
}

try (InputStream embeddedJar = getClass().getClassLoader().getResourceAsStream("MockSkriptBridge.jar.embedded")) {
Preconditions.checkArgument(embeddedJar != null, "Couldn't find embedded MockSkriptBridge.jar");
FileUtils.copyInputStreamToFile(embeddedJar, new File(AddonsLoader.ADDONS, "MockSkriptBridge.jar"));
private void extractEmbeddedAddon(String name) {
try (InputStream embeddedJar = getClass().getClassLoader().getResourceAsStream(name + ".embedded")) {
Preconditions.checkArgument(embeddedJar != null, "Couldn't find embedded %s", name);
FileUtils.copyInputStreamToFile(embeddedJar, new File(AddonsLoader.ADDONS, name));
} catch (IOException e) {
throw new RuntimeException(e);
}

System.out.println("Successfully extracted embedded addons!");
}
}
30 changes: 23 additions & 7 deletions src/main/java/me/glicz/skanalyzer/loader/AddonsLoader.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
package me.glicz.skanalyzer.loader;

import io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader;
import lombok.Getter;
import me.glicz.skanalyzer.SkAnalyzer;
import me.glicz.skanalyzer.bridge.MockSkriptBridge;
import me.glicz.skanalyzer.mockbukkit.AnalyzerClassLoader;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.Arrays;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

public class AddonsLoader {
public static final File USER_HOME = new File(System.getProperty("user.home"));
public static final File ADDONS = new File(USER_HOME, "SkAnalyzer/addons");
public static final String MOCK_SKRIPT = "MockSkript.jar";
public static final String MOCK_SKRIPT_BRIDGE = "MockSkriptBridge.jar";
private static JavaPlugin skript;
@Getter
private static MockSkriptBridge mockSkriptBridge;

@SuppressWarnings("UnstableApiUsage")
public static void loadAddons() {
if (skript != null)
throw new RuntimeException("Addons are already loaded!");
skript = loadAddon(new File(ADDONS, "MockSkript.jar")); // currently only Skript support :(
mockSkriptBridge = (MockSkriptBridge) loadAddon(new File(ADDONS, "MockSkriptBridge.jar"));
skript = loadAddon(new File(ADDONS, MOCK_SKRIPT));
mockSkriptBridge = (MockSkriptBridge) loadAddon(new File(ADDONS, MOCK_SKRIPT_BRIDGE));
FileUtils.listFiles(ADDONS, new String[]{"jar"}, false).forEach(AddonsLoader::loadAddon);
SkAnalyzer.get().getLogger().info(
"Successfully loaded addons: {}",
Arrays.stream(Bukkit.getPluginManager().getPlugins())
.map(plugin -> plugin.getPluginMeta().getName())
.collect(Collectors.joining(", "))
);
}

@SuppressWarnings("UnstableApiUsage")
private static JavaPlugin loadAddon(File file) {
if (skript != null && file.getName().equals(MOCK_SKRIPT)) return null;
if (mockSkriptBridge != null && file.getName().equals(MOCK_SKRIPT_BRIDGE)) return null;
try {
JarFile jarFile = new JarFile(file);
PluginDescriptionFile description = new PluginDescriptionFile(jarFile.getInputStream(jarFile.getEntry("plugin.yml")));
if (Bukkit.getServer().getPluginManager().isPluginEnabled(description.getName()))
throw new RuntimeException("Plugin named '%s' is already loaded".formatted(description.getName()));
AnalyzerClassLoader classLoader = new AnalyzerClassLoader(
SkAnalyzer.class.getClassLoader(),
description,
Expand All @@ -43,12 +63,8 @@ private static JavaPlugin loadAddon(File file) {
SkAnalyzer.get().getServer().getPluginManager().enablePlugin(plugin);
return plugin;
} catch (Exception e) {
e.printStackTrace(System.out);
SkAnalyzer.get().getLogger().error("Something went wrong while trying to load %s".formatted(file.getPath()), e);
}
return null;
}

public static MockSkriptBridge getMockSkriptBridge() {
return mockSkriptBridge;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.papermc.paper.plugin.configuration.PluginMeta;
import io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader;
import io.papermc.paper.plugin.provider.classloader.PluginClassLoaderGroup;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -31,29 +32,26 @@ public class AnalyzerClassLoader extends URLClassLoader implements ConfiguredPlu
ClassLoader.registerAsParallelCapable();
}

private final PluginClassLoaderGroup classLoaderGroup = new MockBukkitPluginClassLoaderGroup();
@Getter(onMethod_ = {@NotNull})
private final PluginClassLoaderGroup group = new MockBukkitPluginClassLoaderGroup();
private final Map<String, Class<?>> classes = new ConcurrentHashMap<>();
private final PluginDescriptionFile description;
@Getter
private final PluginDescriptionFile configuration;
private final File dataFolder, file;
private final URL url;
private final JarFile jarFile;
private final Manifest manifest;

public AnalyzerClassLoader(ClassLoader parent, PluginDescriptionFile description, File dataFolder, File file, JarFile jarFile) throws Exception {
super(file.getName(), new URL[]{file.toURI().toURL()}, parent);
this.description = description;
this.configuration = description;
this.dataFolder = dataFolder;
this.file = file;
this.url = file.toURI().toURL();
this.jarFile = jarFile;
this.manifest = jarFile.getManifest();
}

@Override
public PluginMeta getConfiguration() {
return description;
}

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
return loadClass0(name, resolve, true);
Expand All @@ -73,7 +71,7 @@ private Class<?> loadClass0(String name, boolean resolve, boolean checkGlobal) t
}

if (checkGlobal) {
Class<?> result = this.classLoaderGroup.getClassByName(name, resolve, this);
Class<?> result = group.getClassByName(name, resolve, this);
if (result != null)
return result;
}
Expand Down Expand Up @@ -136,19 +134,22 @@ else definePackage(

@Override
public void init(JavaPlugin plugin) {
plugin.init(Bukkit.getServer(), description, dataFolder, file, this, description, PaperPluginLogger.getLogger(getConfiguration()));
plugin.init(
Bukkit.getServer(),
configuration,
dataFolder,
file,
this,
configuration,
PaperPluginLogger.getLogger((PluginMeta) getConfiguration())
);
}

@Override
public @Nullable JavaPlugin getPlugin() {
return null;
}

@Override
public @NotNull PluginClassLoaderGroup getGroup() {
return classLoaderGroup;
}

@Override
public void close() throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
Expand All @@ -10,8 +11,10 @@
import java.util.Timer;
import java.util.TimerTask;

@Getter
public class AnalyzerServer extends ServerMock {
private final AnalyzerUnsafeValues unsafe = new AnalyzerUnsafeValues();
private final AnalyzerStructureManager structureManager = new AnalyzerStructureManager();

public void startTicking() {
new Timer().scheduleAtFixedRate(new TimerTask() {
Expand All @@ -27,12 +30,6 @@ public void run() {
return "SkAnalyzer";
}

@SuppressWarnings("deprecation")
@Override
public @NotNull AnalyzerUnsafeValues getUnsafe() {
return unsafe;
}

@SuppressWarnings("DataFlowIssue")
@Override
public @NotNull BlockData createBlockData(String data) {
Expand Down
Loading

0 comments on commit da94dd5

Please sign in to comment.