Skip to content

Commit f29ca91

Browse files
authored
Merge pull request #132 from quentin452/1.7.10-pathfinding-rust-rewrite
1.7.10 pathfinding rust rewrite
2 parents 1d9c87e + c70d4c0 commit f29ca91

65 files changed

Lines changed: 5543 additions & 4879 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ src/main/resources/mixins.*([!.]).json
3333
!gradlew.bat
3434
libs/ChromatiCraft 1.7.10 V33a.jar
3535
libs/DragonAPI 1.7.10 V33b.jar
36+
/optimizationsandtweaks_pathfinding/target
37+
/CoroUtil

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ The original ArchaicFix doesn't work with my mod due to MixinMapGenStructure on
99
# Optimization / Feature List
1010
[Wiki](https://github.com/quentin452/OptimizationsAndTweaks/wiki)
1111

12+
- Notable feature: **Rewrite Minecraft Pathfinding in Rust asynchronously**
13+
1214
# Links for my project
1315
- [GitHub](https://github.com/quentin452/OptimizationsAndTweaks)
1416
- [Modrinth](https://modrinth.com/mod/optimizationsandtweaks)

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[target.x86_64-apple-darwin]
2+
linker = "x86_64-apple-darwin23.5-clang"
3+
ar = "x86_64-apple-darwin23.5-ar"
4+
5+
[target.aarch64-apple-darwin]
6+
linker = "aarch64-apple-darwin23.5-clang"
7+
ar = "aarch64-apple-darwin23.5-ar"
8+
9+
[target.x86_64-pc-windows-gnu]
10+
linker = "x86_64-w64-mingw32-gcc"
11+
ar = "x86_64-w64-mingw32-ar"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Rust
2+
target/
3+
Cargo.lock
4+
5+
# Java
6+
*.class
7+
*.jar
8+
9+
# IDE
10+
.idea/
11+
.vscode/
12+
*.iml
13+
14+
# OS
15+
.DS_Store
16+
Thumbs.db
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "optimizationsandtweaks_pathfinding"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "optimizationsandtweaks_pathfinding"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
jni = "0.21"
12+
lazy_static = "1.5.0"
13+
leaktracer = "0.1"
14+
rayon = "1.10"
15+
crossbeam = "0.8"
16+
lru = "0.12"
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import subprocess
4+
import shutil
5+
import sys
6+
7+
print("=== Building Rust FFI Library for All Platforms ===")
8+
9+
# Get the directory where this script is located
10+
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
11+
os.chdir(SCRIPT_DIR)
12+
13+
# Destination base directory
14+
DEST_BASE = os.path.abspath("../src/main/resources/assets/optimizationsandtweaks/natives")
15+
16+
# Setup osxcross PATH if available
17+
OSXCROSS_PATH = os.path.expanduser("~/osxcross/target/bin")
18+
if os.path.isdir(OSXCROSS_PATH):
19+
os.environ["PATH"] = f"{OSXCROSS_PATH}:{os.environ.get('PATH', '')}"
20+
print(f"✓ osxcross found at {OSXCROSS_PATH}")
21+
22+
def run(cmd, check=True):
23+
print(f"$ {' '.join(cmd)}")
24+
subprocess.run(cmd, check=check)
25+
26+
def check_target(target):
27+
result = subprocess.run(["rustup", "target", "list"], capture_output=True, text=True)
28+
if f"{target} (installed)" not in result.stdout:
29+
print(f"Installing target {target}...")
30+
run(["rustup", "target", "add", target])
31+
32+
def build_for_target(target, os_dir, lib_name):
33+
print(f"\n=== Building for {target} ===")
34+
run(["cargo", "build", "--release", "--target", target])
35+
36+
dest_dir = os.path.join(DEST_BASE, os_dir)
37+
os.makedirs(dest_dir, exist_ok=True)
38+
39+
# Try with "lib" prefix first (Linux/macOS convention)
40+
src_path = os.path.join(SCRIPT_DIR, "target", target, "release", lib_name)
41+
src_path_with_lib = os.path.join(SCRIPT_DIR, "target", target, "release", f"lib{lib_name}")
42+
43+
# Check which file exists
44+
if os.path.isfile(src_path_with_lib):
45+
src_path = src_path_with_lib
46+
elif not os.path.isfile(src_path):
47+
print(f"✗ Warning: Library not found at {src_path} or {src_path_with_lib}")
48+
return
49+
50+
dest_path = os.path.join(dest_dir, lib_name)
51+
shutil.copy2(src_path, dest_path)
52+
print(f"✓ Copied {lib_name} to {dest_path}")
53+
54+
# Linux x86_64
55+
check_target("x86_64-unknown-linux-gnu")
56+
build_for_target("x86_64-unknown-linux-gnu", "linux", "liboptimizationsandtweaks_pathfinding.so")
57+
58+
# Windows x86_64
59+
check_target("x86_64-pc-windows-gnu")
60+
if shutil.which("x86_64-w64-mingw32-gcc"):
61+
build_for_target("x86_64-pc-windows-gnu", "windows", "optimizationsandtweaks_pathfinding.dll")
62+
else:
63+
print("⚠ Warning: MinGW-w64 not found. Skipping Windows build.\n Install with: sudo apt-get install mingw-w64")
64+
65+
# macOS - Build both architectures and create universal binary
66+
macos_x86_built = False
67+
macos_arm_built = False
68+
69+
check_target("x86_64-apple-darwin")
70+
if sys.platform == "darwin" or shutil.which("x86_64-apple-darwin23.5-clang"):
71+
print("\n=== Building for x86_64-apple-darwin ===")
72+
run(["cargo", "build", "--release", "--target", "x86_64-apple-darwin"])
73+
macos_x86_built = True
74+
else:
75+
print("⚠ Warning: macOS x86_64 build requires macOS host or osxcross. Skipping.")
76+
77+
check_target("aarch64-apple-darwin")
78+
if sys.platform == "darwin" or shutil.which("aarch64-apple-darwin23.5-clang"):
79+
print("\n=== Building for aarch64-apple-darwin ===")
80+
run(["cargo", "build", "--release", "--target", "aarch64-apple-darwin"])
81+
macos_arm_built = True
82+
else:
83+
print("⚠ Warning: macOS ARM64 build requires macOS host or osxcross. Skipping.")
84+
85+
# Create universal binary if both architectures were built
86+
if macos_x86_built and macos_arm_built:
87+
print("\n=== Creating macOS Universal Binary ===")
88+
dest_dir = os.path.join(DEST_BASE, "macos")
89+
os.makedirs(dest_dir, exist_ok=True)
90+
91+
x86_lib = os.path.join(SCRIPT_DIR, "target", "x86_64-apple-darwin", "release", "liboptimizationsandtweaks_pathfinding.dylib")
92+
arm_lib = os.path.join(SCRIPT_DIR, "target", "aarch64-apple-darwin", "release", "liboptimizationsandtweaks_pathfinding.dylib")
93+
universal_lib = os.path.join(dest_dir, "liboptimizationsandtweaks_pathfinding.dylib")
94+
95+
if shutil.which("lipo"):
96+
run(["lipo", "-create", "-output", universal_lib, x86_lib, arm_lib])
97+
print(f"✓ Created universal binary at {universal_lib}")
98+
else:
99+
print("⚠ Warning: 'lipo' not found. Copying ARM64 version only.")
100+
shutil.copy2(arm_lib, universal_lib)
101+
print(f"✓ Copied ARM64 library to {universal_lib}")
102+
elif macos_x86_built:
103+
dest_dir = os.path.join(DEST_BASE, "macos")
104+
os.makedirs(dest_dir, exist_ok=True)
105+
x86_lib = os.path.join(SCRIPT_DIR, "target", "x86_64-apple-darwin", "release", "liboptimizationsandtweaks_pathfinding.dylib")
106+
dest_path = os.path.join(dest_dir, "liboptimizationsandtweaks_pathfinding.dylib")
107+
shutil.copy2(x86_lib, dest_path)
108+
print(f"✓ Copied x86_64 library to {dest_path}")
109+
elif macos_arm_built:
110+
dest_dir = os.path.join(DEST_BASE, "macos")
111+
os.makedirs(dest_dir, exist_ok=True)
112+
arm_lib = os.path.join(SCRIPT_DIR, "target", "aarch64-apple-darwin", "release", "liboptimizationsandtweaks_pathfinding.dylib")
113+
dest_path = os.path.join(dest_dir, "liboptimizationsandtweaks_pathfinding.dylib")
114+
shutil.copy2(arm_lib, dest_path)
115+
print(f"✓ Copied ARM64 library to {dest_path}")
116+
117+
print("\n=== Build Complete ===")
118+
print(f"Native libraries have been copied to:\n {DEST_BASE}/linux/\n {DEST_BASE}/windows/\n {DEST_BASE}/macos/")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=src/lib.rs");
5+
6+
// Get the target information from environment variables set by cargo
7+
let target = env::var("TARGET").unwrap();
8+
let profile = env::var("PROFILE").unwrap();
9+
10+
// Only copy the library after the build is complete (not during build.rs execution)
11+
// This prevents the infinite loop issue
12+
println!("cargo:warning=Building for target: {}", target);
13+
println!("cargo:warning=Build profile: {}", profile);
14+
15+
// Determine OS-specific subdirectory and library name based on target triple
16+
let (os_dir, lib_name) = if target.contains("windows") {
17+
("windows", "optimizationsandtweaks_pathfinding.dll")
18+
} else if target.contains("darwin") || target.contains("apple") {
19+
("macos", "optimizationsandtweaks_pathfinding.dylib")
20+
} else if target.contains("linux") {
21+
("linux", "optimizationsandtweaks_pathfinding.so")
22+
} else {
23+
panic!("Unsupported target OS: {}", target);
24+
};
25+
26+
// Store the OS directory and library name for post-build script
27+
println!("cargo:rustc-env=TARGET_OS_DIR={}", os_dir);
28+
println!("cargo:rustc-env=TARGET_LIB_NAME={}", lib_name);
29+
}

0 commit comments

Comments
 (0)