mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-06-18 23:01:07 +00:00
update Script Engine to b5e2cc6e
This commit is contained in:
@@ -3,25 +3,20 @@ package com.volmit.iris.core.scripting;
|
|||||||
import com.volmit.iris.Iris;
|
import com.volmit.iris.Iris;
|
||||||
import com.volmit.iris.core.loader.IrisData;
|
import com.volmit.iris.core.loader.IrisData;
|
||||||
import com.volmit.iris.core.loader.IrisRegistrant;
|
import com.volmit.iris.core.loader.IrisRegistrant;
|
||||||
import com.volmit.iris.util.collection.KList;
|
|
||||||
import com.volmit.iris.util.io.IO;
|
import com.volmit.iris.util.io.IO;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.dom4j.Document;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.net.URI;
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.jar.JarFile;
|
|
||||||
import java.util.jar.JarOutputStream;
|
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class ExecutionEnvironment {
|
public class ExecutionEnvironment {
|
||||||
@@ -47,69 +42,18 @@ public class ExecutionEnvironment {
|
|||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private static URLClassLoader buildLoader() {
|
private static URLClassLoader buildLoader() {
|
||||||
String version = "868b3b9910";
|
String version = "b5e2cc6e";
|
||||||
String url = BASE_URL.formatted(version, version);
|
String url = BASE_URL.formatted(version, version);
|
||||||
String hash = IO.hash("Iris-Scripts.jar@" + version);
|
String hash = IO.hash("Iris-Scripts.jar@" + version);
|
||||||
var file = Iris.instance.getDataFile("cache", hash.substring(0, 2), hash.substring(3, 5), hash + ".jar");
|
var file = Iris.instance.getDataFile("cache", hash.substring(0, 2), hash.substring(3, 5), hash + ".jar");
|
||||||
var libsDir = new File(file.getParentFile(), "libs");
|
|
||||||
|
|
||||||
KList<String> libs = null;
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
libsDir.mkdirs();
|
|
||||||
|
|
||||||
Iris.info("Downloading Script Engine...");
|
Iris.info("Downloading Script Engine...");
|
||||||
var tempFile = Iris.getNonCachedFile(UUID.randomUUID().toString(), url);
|
try (var stream = URI.create(url).toURL().openStream()) {
|
||||||
try (var jar = new JarFile(tempFile); var out = new JarOutputStream(new FileOutputStream(file)) ) {
|
Files.copy(stream, file.toPath());
|
||||||
libs = getLibraries(jar);
|
|
||||||
for (var it = jar.entries().asIterator(); it.hasNext(); ) {
|
|
||||||
var entry = it.next();
|
|
||||||
if (entry.isDirectory()) {
|
|
||||||
out.putNextEntry(entry);
|
|
||||||
out.closeEntry();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try (var in = jar.getInputStream(entry)) {
|
|
||||||
if (libs.contains(entry.getName())) {
|
|
||||||
var target = new File(libsDir, entry.getName());
|
|
||||||
target.getParentFile().mkdirs();
|
|
||||||
Files.copy(in, target.toPath());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
out.putNextEntry(entry);
|
|
||||||
IO.copy(in, out);
|
|
||||||
out.closeEntry();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
IO.deleteUp(tempFile);
|
|
||||||
Iris.info("Downloaded Script Engine!");
|
Iris.info("Downloaded Script Engine!");
|
||||||
}
|
}
|
||||||
|
return new URLClassLoader(new URL[]{file.toURI().toURL()}, Provider.class.getClassLoader());
|
||||||
if (libs == null) {
|
|
||||||
try (var jar = new JarFile(file)) {
|
|
||||||
libs = getLibraries(jar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var urls = new URL[libs.size() + 1];
|
|
||||||
urls[0] = file.toURI().toURL();
|
|
||||||
for (int i = 0; i < libs.size(); i++) {
|
|
||||||
File lib = new File(libsDir, libs.get(i));
|
|
||||||
if (!lib.exists()) {
|
|
||||||
Iris.warn("Missing library: " + lib.getAbsolutePath());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
urls[i + 1] = lib.toURI().toURL();
|
|
||||||
}
|
|
||||||
return new URLClassLoader(urls, Provider.class.getClassLoader());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static KList<String> getLibraries(JarFile jar) throws IOException {
|
|
||||||
return new KList<>(jar.getManifest()
|
|
||||||
.getMainAttributes()
|
|
||||||
.getValue("Libraries")
|
|
||||||
.split(";"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Provider {
|
public interface Provider {
|
||||||
@@ -125,15 +69,7 @@ public class ExecutionEnvironment {
|
|||||||
|
|
||||||
|
|
||||||
public interface Simple {
|
public interface Simple {
|
||||||
default void configureProject(@NonNull File projectDir) {
|
void configureProject(@NonNull File projectDir);
|
||||||
var workspaceFile = new File(projectDir, ".idea" + File.separator + "workspace.xml");
|
|
||||||
var workspaceDoc = IO.read(workspaceFile);
|
|
||||||
if (configureProject(projectDir, workspaceDoc)) {
|
|
||||||
IO.write(workspaceFile, workspaceDoc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean configureProject(@NonNull File projectDir, @NonNull Document workspace);
|
|
||||||
|
|
||||||
void execute(@NonNull String script);
|
void execute(@NonNull String script);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user