mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
feat(java): xxh3 hash checking
This commit is contained in:
parent
fcd2b9f1c9
commit
39f69cc418
6 changed files with 30 additions and 9 deletions
5
dist/java/src/mp/code/BufferController.java
vendored
5
dist/java/src/mp/code/BufferController.java
vendored
|
@ -13,12 +13,12 @@ public class BufferController {
|
||||||
this.ptr = ptr;
|
this.ptr = ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static native String get_name(long self);
|
private static native String get_name(long self);
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return get_name(this.ptr);
|
return get_name(this.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static native String get_content(long self) throws CodeMPException;
|
private static native String get_content(long self) throws CodeMPException;
|
||||||
public String getContent() throws CodeMPException {
|
public String getContent() throws CodeMPException {
|
||||||
return get_content(this.ptr);
|
return get_content(this.ptr);
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,6 @@ public class BufferController {
|
||||||
|
|
||||||
private static native void free(long self);
|
private static native void free(long self);
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("removal")
|
|
||||||
protected void finalize() {
|
protected void finalize() {
|
||||||
free(this.ptr);
|
free(this.ptr);
|
||||||
}
|
}
|
||||||
|
|
1
dist/java/src/mp/code/Client.java
vendored
1
dist/java/src/mp/code/Client.java
vendored
|
@ -36,7 +36,6 @@ public class Client {
|
||||||
|
|
||||||
private static native void free(long self);
|
private static native void free(long self);
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("removal") // muh java 8
|
|
||||||
protected void finalize() {
|
protected void finalize() {
|
||||||
free(this.ptr);
|
free(this.ptr);
|
||||||
}
|
}
|
||||||
|
|
1
dist/java/src/mp/code/CursorController.java
vendored
1
dist/java/src/mp/code/CursorController.java
vendored
|
@ -29,7 +29,6 @@ public class CursorController {
|
||||||
|
|
||||||
private static native void free(long self);
|
private static native void free(long self);
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("removal")
|
|
||||||
protected void finalize() {
|
protected void finalize() {
|
||||||
free(this.ptr);
|
free(this.ptr);
|
||||||
}
|
}
|
||||||
|
|
1
dist/java/src/mp/code/Workspace.java
vendored
1
dist/java/src/mp/code/Workspace.java
vendored
|
@ -79,7 +79,6 @@ public class Workspace {
|
||||||
|
|
||||||
private static native void free(long self);
|
private static native void free(long self);
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("removal")
|
|
||||||
protected void finalize() {
|
protected void finalize() {
|
||||||
free(this.ptr);
|
free(this.ptr);
|
||||||
}
|
}
|
||||||
|
|
10
dist/java/src/mp/code/data/TextChange.java
vendored
10
dist/java/src/mp/code/data/TextChange.java
vendored
|
@ -4,10 +4,18 @@ public class TextChange {
|
||||||
public final long start;
|
public final long start;
|
||||||
public final long end;
|
public final long end;
|
||||||
public final String content;
|
public final String content;
|
||||||
|
private final long hash; // xxh3 hash
|
||||||
|
|
||||||
public TextChange(long start, long end, String content) {
|
public TextChange(long start, long end, String content, long hash) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end = end;
|
this.end = end;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
this.hash = hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native long hash(String content);
|
||||||
|
public boolean hashMatches(String content) {
|
||||||
|
// 0 is Rust default value and a very unlikely hash
|
||||||
|
return hash == 0L || this.hash == hash(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use jni::{objects::{JClass, JObject, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
|
use jni::{objects::{JClass, JObject, JString, JValueGen}, sys::{jlong, jobject, jstring}, JNIEnv};
|
||||||
|
use xxhash_rust::xxh3::xxh3_64;
|
||||||
|
|
||||||
use crate::api::Controller;
|
use crate::api::Controller;
|
||||||
|
|
||||||
|
@ -67,11 +68,12 @@ fn recv_jni(env: &mut JNIEnv, change: Option<crate::api::TextChange>) -> jobject
|
||||||
.and_then(|class| {
|
.and_then(|class| {
|
||||||
env.new_object(
|
env.new_object(
|
||||||
class,
|
class,
|
||||||
"(JJLjava/lang/String;)V",
|
"(JJLjava/lang/String;J)V",
|
||||||
&[
|
&[
|
||||||
JValueGen::Long(jlong::from(event.start)),
|
JValueGen::Long(jlong::from(event.start)),
|
||||||
JValueGen::Long(jlong::from(event.end)),
|
JValueGen::Long(jlong::from(event.end)),
|
||||||
JValueGen::Object(&content),
|
JValueGen::Object(&content),
|
||||||
|
JValueGen::Long(event.hash.unwrap_or_default())
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}).jexcept(env)
|
}).jexcept(env)
|
||||||
|
@ -116,3 +118,18 @@ pub extern "system" fn Java_mp_code_BufferController_free(
|
||||||
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
|
let _ = unsafe { Box::from_raw(self_ptr as *mut crate::cursor::Controller) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Calculates the XXH3 hash for a given String.
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "system" fn Java_mp_code_data_TextChange_hash<'local>(
|
||||||
|
mut env: JNIEnv,
|
||||||
|
_class: JClass<'local>,
|
||||||
|
content: JString<'local>,
|
||||||
|
) -> jlong {
|
||||||
|
let content: String = env.get_string(&content)
|
||||||
|
.map(|s| s.into())
|
||||||
|
.jexcept(&mut env);
|
||||||
|
|
||||||
|
let hash = xxh3_64(content.as_bytes());
|
||||||
|
i64::from_ne_bytes(hash.to_ne_bytes())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue