Skip to content

Commit

Permalink
Fix loading for distUniversal package.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymia committed Mar 3, 2022
1 parent 6e05db9 commit 4a393ad
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
11 changes: 8 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import scala.sys.process._

val version_baseVersion = "0.1.0"
val config_scalaVersion = "2.13.8"
val config_distributePackages = Seq("cards-against-humanity.pedit-pkg")

// Project-specific keys
val gitDir = SettingKey[File]("git-dir")
Expand Down Expand Up @@ -389,9 +390,9 @@ InputKey[Unit]("dist") := {
val nativeBinName = properties.get(s"$osTarget.nativeBin").toString
IO.copyFile(baseDirectory.value / "modules" / "native" / "target" / nativeBinName, outDir / nativeBinName)

IO.createDirectory(outDir / "packages")
for (pkg <- Seq("cards-against-humanity.pedit-pkg"))
IO.copyDirectory(file("packages") / pkg, outDir / "packages" / pkg)
// copy default packages
for (pkg <- config_distributePackages)
IO.copyDirectory(baseDirectory.value / "packages" / pkg, outDir / "packages" / pkg)

// we call out to zip to save the executable flag for *nix
if (zipOut.exists) IO.delete(zipOut)
Expand Down Expand Up @@ -437,6 +438,10 @@ InputKey[Unit]("distUniversal") := {
if (nativePath.exists) IO.copyFile(nativePath, outDir / "lib" / native)
}

// copy default packages
for (pkg <- config_distributePackages)
IO.copyDirectory(baseDirectory.value / "packages" / pkg, outDir / "packages" / pkg)

// we call out to zip to save the executable flag for *nix
if (zipOut.exists) IO.delete(zipOut)
runProcess(Seq("zip", "-r", zipOut, dirName), dir)
Expand Down
25 changes: 13 additions & 12 deletions modules/loader/src/main/java/moe/lymia/princess/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ class LoaderException extends RuntimeException {
class LoaderData {
final String mainClass;
final String[] classPath;
final String nativeBin;

LoaderData(String mainClass, String[] classPath) {
LoaderData(String mainClass, String[] classPath, String nativeBin) {
this.mainClass = mainClass;
this.classPath = classPath;
this.nativeBin = nativeBin;
}
}

Expand All @@ -60,7 +62,7 @@ private static RuntimeException error(String error, Exception e) {
private Path getExecutablePath() {
try {
URL sourceURL = Loader.class.getProtectionDomain().getCodeSource().getLocation();
return Paths.get(sourceURL.toURI());
return Paths.get(sourceURL.toURI()).toFile().toPath();
} catch (Exception e) {
throw error("Could not find executable directory.", e);
}
Expand Down Expand Up @@ -93,19 +95,18 @@ private LoaderData getLoaderData(String os, Path rootPath) {

String mainClass = prop.getProperty("main");
String classPath = prop.getProperty(os+".classpath");
String nativeBin = prop.getProperty(os+".nativeBin");

if (classPath == null)
throw error("Your system configuration (" + os + ") is not supported in this build.", null);

return new LoaderData(mainClass, classPath.split(":"));
return new LoaderData(mainClass, classPath.split(":"), nativeBin);
} catch (Exception e) {
throw error("Could not load library manifest.", e);
}
}

private Class<?> loadMainClass(String os, Path rootPath) {
LoaderData data = getLoaderData(os, rootPath);

private Class<?> loadMainClass(LoaderData data, Path rootPath) {
try {
URL[] urls = new URL[data.classPath.length];
for (int i = 0; i < data.classPath.length; i++)
Expand All @@ -125,13 +126,13 @@ private void start(String[] args) throws InvocationTargetException {
throw error("PrincessEdit requires a 64-bit ARM or Intel/AMD CPU.\n" +
"If you have one, please install a 64-bit version of Java.", null);

Path bin = getExecutablePath();
Path dir = bin.getParent();
Path executableDir = getExecutablePath().getParent();
Path libDir = executableDir.resolve("lib");
LoaderData data = getLoaderData(os, libDir);
System.setProperty("moe.lymia.princess.startedFromLoader", "true");
System.setProperty("moe.lymia.princess.loaderBinary", bin.toUri().toString());
System.setProperty("moe.lymia.princess.rootDirectory", dir.toUri().toString());
System.setProperty("moe.lymia.princess.libDirectory", dir.resolve("lib").toUri().toString());
Class<?> main = loadMainClass(os, dir.resolve("lib"));
System.setProperty("moe.lymia.princess.rootDirectory", executableDir.toString());
System.setProperty("moe.lymia.princess.nativeBinary", libDir.resolve(data.nativeBin).toString());
Class<?> main = loadMainClass(data, libDir);

Method m;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,40 @@ object Environment {
private lazy val isSbtLaunch: Boolean =
System.getProperty("princessedit.baseDirectory") != null &&
System.getProperty("princessedit.native.bin") != null
private lazy val isUniversalLaunch: Boolean =
System.getProperty("moe.lymia.princess.startedFromLoader") != null &&
System.getProperty("moe.lymia.princess.rootDirectory") != null &&
System.getProperty("moe.lymia.princess.nativeBinary") != null

private lazy val configurationRoot = Platform.platform.configurationRoot
def configDirectory(name: String): Path = configurationRoot.resolve(name)

private lazy val locationFromCodeSource: Path =
private lazy val nativeImageExecutableDirectory: Path =
new File(PrincessEdit.getClass.getProtectionDomain.getCodeSource.getLocation.toURI).toPath.getParent
private lazy val locationFromSbtEnvironment =
private lazy val sbtBaseDirectory =
Paths.get(System.getProperty("princessedit.baseDirectory"))
private lazy val universalRootDirectory =
Paths.get(System.getProperty("moe.lymia.princess.rootDirectory"))

lazy val rootDirectory: Path =
if (isNativeImage) locationFromCodeSource
else if (isSbtLaunch) locationFromSbtEnvironment
if (isNativeImage) nativeImageExecutableDirectory
else if (isUniversalLaunch) universalRootDirectory
else if (isSbtLaunch) sbtBaseDirectory
else sys.error("Could not locate root directory!")

lazy val libDirectory: Path =
if (isNativeImage) locationFromCodeSource
else if (isSbtLaunch) locationFromSbtEnvironment.resolve("modules")
if (isNativeImage) nativeImageExecutableDirectory
else if (isUniversalLaunch) universalRootDirectory.resolve("lib")
else if (isSbtLaunch) sbtBaseDirectory.resolve("modules")
else sys.error("Could not locate library directory!")

lazy val nativeLibrary: Path =
if (isNativeImage) ???
if (isNativeImage) nativeImageExecutableDirectory.resolve(Platform.platform match {
case Platform.Windows => "princessedit_native.windows.x86_64.dll"
case Platform.MacOS => "libprincessedit_native.macos.x86_64.dylib"
case Platform.Linux => "libprincessedit_native.linux.x86_64.so"
})
else if (isUniversalLaunch) Paths.get(System.getProperty("moe.lymia.princess.nativeBinary"))
else if (isSbtLaunch) Paths.get(System.getProperty("princessedit.native.bin"))
else sys.error("Could not locate native library!")
}

0 comments on commit 4a393ad

Please sign in to comment.