Skip to content

Commit 965f13b

Browse files
committed
chore: Replace custom native packer with a standard zip file
CurseForge moderation doesn't like the packer
1 parent 0133642 commit 965f13b

File tree

5 files changed

+53
-140
lines changed

5 files changed

+53
-140
lines changed

build.gradle.kts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import com.falsepattern.zanama.tasks.ZanamaTranslate
2-
import com.falsepattern.zigbuild.options.ZigBuildOptions
32
import com.falsepattern.zigbuild.tasks.ZigBuildTask
43
import com.falsepattern.zigbuild.toolchain.ZigVersion
54

@@ -127,15 +126,22 @@ tasks.named<JavaCompile>(panamaNatives.compileJavaTaskName) {
127126

128127
panamaNatives.java.srcDirs(translateJavaSourcesCpuID, translateJavaSourcesFalseTweaks)
129128

130-
tasks.processResources.configure {
129+
val packNatives = tasks.register<Zip>("packNatives") {
130+
this.archiveFileName = "natives.zip"
131+
from(zigPrefix.map { it.dir("lib") }) {
132+
include("*.so", "*.dll", "*.dylib")
133+
}
134+
this.entryCompression = ZipEntryCompression.STORED
131135
dependsOn(zigBuildTask)
136+
}
137+
138+
tasks.processResources.configure {
139+
dependsOn(zigBuildTask, packNatives)
132140
into("META-INF/falsepatternlib_repo/mega/megatraceservice/1.2.0/") {
133141
from(configurations.compileClasspath.map { it.filter { file -> file.name.contains("megatraceservice") } })
134142
}
135143
into(minecraft_fp.mod.rootPkg.map { "/assets/falsetweaks" } ) {
136-
from(zigPrefix.map { it.dir("lib") }) {
137-
include("*.pak")
138-
}
144+
from(packNatives.map { it.outputs.files })
139145
}
140146
}
141147

build.zig

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const base_targets = [_]std.Target.Query{
3131
};
3232

3333
pub fn build(b: *std.Build) void {
34-
const packer = createPackerTask(b);
3534
const targets = getTargets(b);
3635

3736
//JNI stubs
@@ -56,11 +55,16 @@ pub fn build(b: *std.Build) void {
5655
.name = name,
5756
.root_module = libjni_mod,
5857
});
59-
packer.addArg(name);
60-
packer.addFileArg(libjni.getEmittedBin());
58+
const inst = b.addInstallArtifact(libjni, .{
59+
.dest_dir = .{ .override = .lib },
60+
.h_dir = .disabled,
61+
.implib_dir = .disabled,
62+
.pdb_dir = .disabled,
63+
});
64+
b.getInstallStep().dependOn(&inst.step);
6165
}
6266

63-
const zanamaBuilder = ZanamaLibBuilder.init(b, packer);
67+
const zanamaBuilder = ZanamaLibBuilder.init(b);
6468

6569
zanamaBuilder.addZanamaLibs(
6670
"cpuid",
@@ -83,22 +87,6 @@ pub fn build(b: *std.Build) void {
8387
);
8488
}
8589

86-
fn createPackerTask(b: *std.Build) *std.Build.Step.Run {
87-
const packer = b.addExecutable(.{
88-
.name = "packer",
89-
.root_module = b.addModule("packer", .{
90-
.root_source_file = b.path("zig-util/packer.zig"),
91-
.target = b.graph.host,
92-
}),
93-
});
94-
const run_packer = b.addRunArtifact(packer);
95-
const natives_file = "natives.pak";
96-
const output_natives_file = run_packer.addOutputFileArg(natives_file);
97-
const install_pack = b.addInstallLibFile(output_natives_file, natives_file);
98-
b.getInstallStep().dependOn(&install_pack.step);
99-
return run_packer;
100-
}
101-
10290
fn getTargets(b: *std.Build) struct { baseline: []std.Build.ResolvedTarget, all: []std.Build.ResolvedTarget } {
10391
var baseline_targets: [base_targets.len]std.Build.ResolvedTarget = undefined;
10492
var targets = std.ArrayList(std.Build.ResolvedTarget).empty;
@@ -125,18 +113,16 @@ fn getTargets(b: *std.Build) struct { baseline: []std.Build.ResolvedTarget, all:
125113

126114
const ZanamaLibBuilder = struct {
127115
b: *std.Build,
128-
packer: *std.Build.Step.Run,
129116
zanama_dep: *std.Build.Dependency,
130117
zanama_api: *std.Build.Module,
131118
zb: zanama.Build,
132119

133-
pub fn init(b: *std.Build, packer: *std.Build.Step.Run) ZanamaLibBuilder {
120+
pub fn init(b: *std.Build) ZanamaLibBuilder {
134121
const zanama_dep = b.dependency("zanama", .{});
135122
const zanama_api = zanama_dep.module("api");
136123
const zb = zanama.Build.init(b, .ReleaseFast, zanama_dep);
137124
return .{
138125
.b = b,
139-
.packer = packer,
140126
.zanama_dep = zanama_dep,
141127
.zanama_api = zanama_api,
142128
.zb = zb,
@@ -146,12 +132,18 @@ const ZanamaLibBuilder = struct {
146132
fn addZanamaLibs(self: *const ZanamaLibBuilder, name: []const u8, module: *std.Build.Module, targets: []std.Build.ResolvedTarget) void {
147133
module.addImport("zanama", self.zanama_api);
148134
const libs = self.zb.createZanamaLibsResolved(name, module, targets);
135+
const install_step = self.b.getInstallStep();
149136
for (libs.artifacts) |artifact| {
150-
self.packer.addArg(artifact.name);
151-
self.packer.addFileArg(artifact.getEmittedBin());
137+
const inst = self.b.addInstallArtifact(artifact, .{
138+
.dest_dir = .{ .override = .lib },
139+
.h_dir = .disabled,
140+
.implib_dir = .disabled,
141+
.pdb_dir = .disabled,
142+
});
143+
install_step.dependOn(&inst.step);
152144
}
153-
const install_json = self.b.addInstallFile(libs.json, std.mem.concat(self.b.allocator, u8, &.{name, ".json"}) catch @panic("OOM"));
145+
const install_json = self.b.addInstallFile(libs.json, std.mem.concat(self.b.allocator, u8, &.{ name, ".json" }) catch @panic("OOM"));
154146
install_json.step.dependOn(libs.json_step);
155-
self.b.getInstallStep().dependOn(&install_json.step);
147+
install_step.dependOn(&install_json.step);
156148
}
157149
};

src/main/java/com/falsepattern/falsetweaks/modules/natives/NativeLoader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public NativeLoader(Class<?> root) throws UnsupportedPlatformException {
6262
val os = OS.getCurrent();
6363
val libc = os.libc();
6464
currentTriple = new Triple(arch, os, libc);
65-
val res = root.getResource("/assets/falsetweaks/natives.pak");
65+
val res = root.getResource("/assets/falsetweaks/natives.zip");
6666
if (res == null) {
6767
throw new UnsupportedPlatformException("No resource package found!");
6868
}
@@ -80,7 +80,7 @@ public String unpackNative(String libName, String cpu) throws UnsupportedPlatfor
8080
val libNameArchive = libName + "-" + currentTriple.toName() + "-" + cpu;
8181
val libFile = nativesDir.resolve(libNameSys);
8282
try {
83-
unpacker.unpack(libNameArchive, libFile);
83+
unpacker.unpack(libNameSys, libFile);
8484
} catch (IOException e) {
8585
throw new UnsupportedPlatformException(e);
8686
}
@@ -91,10 +91,9 @@ public String unpackNative(String libName, String cpu) throws UnsupportedPlatfor
9191
public String loadNative(String libName, String cpu) throws UnsupportedPlatformException {
9292
val libNameSys = currentTriple.toLibName(libName, cpu);
9393
cpu = cpu == null ? currentTriple.arch.baselineModel : cpu;
94-
val libNameArchive = libName + "-" + currentTriple.toName() + "-" + cpu;
9594
val libFile = nativesDir.resolve(libNameSys);
9695
try {
97-
unpacker.unpack(libNameArchive, libFile);
96+
unpacker.unpack(libNameSys, libFile);
9897
} catch (IOException e) {
9998
throw new UnsupportedPlatformException(e);
10099
}

src/main/java/com/falsepattern/falsetweaks/modules/natives/Unpacker.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
import lombok.val;
2727

2828
import java.io.BufferedOutputStream;
29-
import java.io.DataInputStream;
3029
import java.io.EOFException;
3130
import java.io.IOException;
3231
import java.io.InputStream;
3332
import java.io.OutputStream;
3433
import java.net.URL;
3534
import java.nio.file.Files;
3635
import java.nio.file.Path;
36+
import java.util.ArrayList;
37+
import java.util.zip.ZipEntry;
38+
import java.util.zip.ZipInputStream;
3739

3840
public class Unpacker {
3941
private final URL data;
@@ -43,46 +45,34 @@ public Unpacker(URL data) {
4345

4446
public String[] names() throws IOException {
4547
@Cleanup val in = data.openStream();
46-
@Cleanup val dIn = new DataInputStream(in);
47-
val count = dIn.readInt();
48-
val result = new String[count];
49-
for (int i = 0; i < count; i++) {
50-
val nameLength = dIn.readUnsignedByte();
51-
val nameBuf = new byte[nameLength];
52-
dIn.readFully(nameBuf);
53-
val name = new String(nameBuf);
54-
result[i] = name;
55-
val length = dIn.readInt();
56-
dIn.skipBytes(length);
48+
@Cleanup val zipIn = new ZipInputStream(in);
49+
ZipEntry entry;
50+
val result = new ArrayList<String>();
51+
while ((entry = zipIn.getNextEntry()) != null) {
52+
result.add(entry.getName());
5753
}
58-
return result;
54+
return result.toArray(new String[0]);
5955
}
6056

6157
public void unpack(String blobName, Path into) throws IOException {
6258
@Cleanup val in = data.openStream();
63-
@Cleanup val dIn = new DataInputStream(in);
64-
val count = dIn.readInt();
65-
for (int i = 0; i < count; i++) {
66-
val nameLength = dIn.readUnsignedByte();
67-
val nameBuf = new byte[nameLength];
68-
dIn.readFully(nameBuf);
69-
val name = new String(nameBuf);
70-
val length = dIn.readInt();
71-
if (!blobName.equals(name)) {
72-
dIn.skipBytes(length);
73-
continue;
59+
@Cleanup val zipIn = new ZipInputStream(in);
60+
ZipEntry entry;
61+
while ((entry = zipIn.getNextEntry()) != null) {
62+
if (entry.getName().equals(blobName)) {
63+
val size = entry.getSize();
64+
@Cleanup val output = new BufferedOutputStream(Files.newOutputStream(into));
65+
copy(zipIn, output, size);
66+
return;
7467
}
75-
@Cleanup val output = new BufferedOutputStream(Files.newOutputStream(into));
76-
copy(dIn, output, length);
77-
return;
7868
}
7969
throw new IOException("Blob " + blobName + " not found in pak file!");
8070
}
8171

82-
private void copy(InputStream input, OutputStream output, int bytes) throws IOException {
83-
val buf = new byte[Math.min(bytes, 4096)];
72+
private void copy(InputStream input, OutputStream output, long bytes) throws IOException {
73+
val buf = new byte[(int) Math.min(bytes, 4096)];
8474
while (bytes > 0) {
85-
val toRead = Math.min(buf.length, bytes);
75+
val toRead = (int)Math.min(buf.length, bytes);
8676
val read = input.read(buf, 0, toRead);
8777
if (read < 0) {
8878
throw new EOFException();

zig-util/packer.zig

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)