Skip to content

Commit

Permalink
Merge pull request #6 from LostLuma/feature/native-development-builds
Browse files Browse the repository at this point in the history
Improve native dev workflow, make natives work in production
  • Loading branch information
Pixaurora authored Aug 9, 2024
2 parents 59c450a + 809b17f commit 801409e
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 77 deletions.
34 changes: 9 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,23 @@
icon.png
quilt.mod.json

# Cargo
target/

# Gradle
.gradle/
build/
out/
classes/

# Quilt Loom
remappedSrc/
run/

# Eclipse
*.launch

# IntelliJ Idea
.idea/
*.iml
*.ipr
*.iws

# Fleet
.fleet/
# macOS
*.DS_Store
/.apt_generated/

# Quilt Loom
run/

# Visual Studio Code
.settings/
.vscode/
bin/
.classpath
.project

# Eclipse JDT LS
.factorypath
workspace/

# macOS
*.DS_Store
/.apt_generated/
1 change: 0 additions & 1 deletion projects/kitten-heart/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ mod {
dependencies {
required("quilt_loader").versionAbove(libs.versions.quilt.loader.get())


required("kit_tunes_api")
required("kitten_square")
required("kitten_sounds")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import net.pixaurora.kitten_heart.impl.resource.ResourcePathImpl;
import net.pixaurora.kitten_heart.impl.service.MinecraftUICompat;
import net.pixaurora.kitten_heart.impl.service.ServiceLoading;
import net.pixaurora.kitten_thoughts.KittenThoughts;
import net.pixaurora.kitten_thoughts.scrobbler.ScrobblerSetup;
import net.pixaurora.kitten_thoughts.impl.KittenThoughts;
import net.pixaurora.kitten_thoughts.impl.scrobbler.ScrobblerSetup;

public class KitTunes {
public static final Logger LOGGER = LoggerFactory.getLogger(Constants.MOD_ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.pixaurora.kitten_heart.impl.network;

import net.pixaurora.kitten_thoughts.impl.util.CryptoUtil;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryption {
public static String signMd5(String input) {
return bytesToHex(getMd5Digest(input));
return CryptoUtil.toHex(getMd5Digest(input));
}

public static byte[] getMd5Digest(String input) {
Expand All @@ -21,18 +23,4 @@ public static byte[] getMd5Digest(String input) {

return digester.digest();
}

public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder(bytes.length * 2);

for (byte rawByte : bytes) {
int processedByte = Byte.toUnsignedInt(rawByte);

// Example byte: 11 01 10 00
hexString.append(Integer.toHexString(processedByte >> 0b100)); // Grabs the part that is 11 01
hexString.append(Integer.toHexString(processedByte & 0b1111)); // Grabs the part that is 10 00
}

return hexString.toString();
}
}
24 changes: 24 additions & 0 deletions projects/kitten-thoughts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.io.ByteArrayOutputStream

plugins {
id("kit_tunes.java.08")
id("kit_tunes.base")
Expand All @@ -13,4 +15,26 @@ mod {

dependencies {
implementation(libs.annotations)
implementation(libs.quilt.loader)
}

tasks.register("buildDevNatives") {
inputs.file(file("Cargo.toml"))
inputs.file(file("Cargo.lock"))

inputs.dir(file("src/main/rust"))
outputs.dir(file("target/debug"))

doLast {
val stream = ByteArrayOutputStream()

exec {
errorOutput = stream
standardOutput = stream

commandLine("cargo", "build")
}

logger.info(stream.toString())
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.pixaurora.kitten_thoughts.impl;

import org.quiltmc.loader.api.QuiltLoader;

import java.nio.file.Path;

public class Constants {
public static final String MOD_ID = "kitten_thoughts";

public static final String NATIVES_VERSION = "0.1.0";
public static final String NATIVES_DIRECTORY_PROPERTY = "kitten_thoughts.natives_path";

public static final Path NATIVES_CACHE_DIR = QuiltLoader.getCacheDir().resolve(MOD_ID);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.pixaurora.kitten_thoughts.impl;

import net.pixaurora.kitten_thoughts.impl.util.NativeUtil;

public class KittenThoughts {
public static void init() {
NativeUtil.load();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.pixaurora.kitten_thoughts.impl.error;

public class LibraryLoadError extends Error {
public LibraryLoadError(String message) {
super(message);
}

public LibraryLoadError(Throwable exception) {
super(exception);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.pixaurora.kitten_thoughts.scrobbler;
package net.pixaurora.kitten_thoughts.impl.scrobbler;

public class ScrobblerSetup {
public static native String hello(String input);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.pixaurora.kitten_thoughts.impl.util;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CryptoUtil {
public static String sha512(Path path) throws IOException {
return sha512(Files.readAllBytes(path));
}

public static String sha512(byte[] data) {
MessageDigest digest;

try {
digest = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No SHA512 algorithm found.", e);
}

digest.update(data);
return toHex(digest.digest());
}

public static String toHex(byte[] data) {
StringBuilder builder = new StringBuilder(data.length * 2);

for (byte value : data) {
int number = Byte.toUnsignedInt(value);

builder.append(Integer.toHexString(number >> 0b100)); // Former four bits
builder.append(Integer.toHexString(number & 0b1111)); // Latter four bits
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.pixaurora.kitten_thoughts.impl.util;

import net.pixaurora.kitten_thoughts.impl.Constants;
import org.quiltmc.loader.api.ModMetadata;
import org.quiltmc.loader.api.QuiltLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class HttpUtil {
private static final Duration TIMEOUT = Duration.of(2, ChronoUnit.MINUTES);

public static void download(URL url, Path into) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setReadTimeout((int) TIMEOUT.toMillis());
connection.setRequestProperty("User-Agent", buildUserAgent());

connection.connect();
int status = connection.getResponseCode();

if (status != 200) {
throw new IOException("Library download error: " + status);
}

int size;
String length = connection.getHeaderField("Content-Length");

try {
size = Integer.parseInt(length);
} catch (NumberFormatException e) {
throw new IOException("Received invalid Content-Length header!");
}

try (InputStream stream = connection.getInputStream()) {
int input;
int index = 0;

byte[] data = new byte[size];

while ((input = stream.read()) != -1) {
data[index] = (byte) input;
index ++;
}

Files.write(into, data);
}
}

private static String buildUserAgent() {
ModMetadata metadata = QuiltLoader.getModContainer(Constants.MOD_ID).get().metadata();

String version = metadata.version().raw();
String homepage = metadata.getContactInfo("homepage");

return String.format("Kit Tunes/%s (+%s)", version, homepage);
}
}
Loading

0 comments on commit 801409e

Please sign in to comment.