Skip to content

Commit

Permalink
woah
Browse files Browse the repository at this point in the history
  • Loading branch information
Progames723 committed Oct 10, 2024
1 parent b7208b2 commit 69261fd
Show file tree
Hide file tree
Showing 16 changed files with 1,091 additions and 261 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/buildLibrary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ jobs:
distribution: 'temurin'
- uses: actions/checkout@v4
- name: Build
# shell: msys2 {0}
shell: msys2 {0}
run: |
cd nativeLibSrc
make -f ./windows/Makefile
make -f ./windows/Makefile make_obj
make -f ./windows/Makefile compile
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
Expand All @@ -53,7 +54,8 @@ jobs:
- name: Build
run: |
cd nativeLibSrc
make -f ./linux/Makefile
make -f ./linux/Makefile make_obj
make -f ./linux/Makefile compile
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
Expand Down
117 changes: 117 additions & 0 deletions common/src/main/java/dev/progames723/hmmm/GMP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package dev.progames723.hmmm;

import java.lang.annotation.Native;
import java.math.BigDecimal;

public class GMP extends Number {
static {
registerNatives();
}

public static void init() {
//yes
}

@Native
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
private boolean isCleared = false;

@Override
public native int intValue();

@Override
public native long longValue();

@Override
public native float floatValue();

@Override
public native double doubleValue();

public native String getAsString();

private static native void registerNatives();

public BigDecimal getAsBigDecimal() {
if (getAsString().matches("[^\\-0-9.]")) return BigDecimal.ZERO;//default impl
return new BigDecimal(getAsString());
}

public GMP(String string) {
if (!string.matches("[^\\-0-9.]")) create(string);
else create(0);
}

public GMP() {
this(0);
}

public GMP(int i) {
this((long) i);
}

public GMP(long l) {
this((double) l);
}

public GMP(double d) {
create(d);
}

public GMP(float f) {
this((double) f);
}

private native void create(double d);

private native void create(String s);

//math operations
public native GMP multiply(GMP gmp);
public native GMP multiply(String string);
public native GMP divide(GMP gmp);
public native GMP divide(String string);
public native GMP pow(GMP gmp);
public native GMP pow(String string);
public native GMP sqrt();
public GMP nthRoot(GMP gmp) {
GMP temp = new GMP(1);
try {return pow(temp.divide(gmp));} finally {temp.clear();}
}
public GMP nthRoot(String string) {
GMP temp = new GMP(1);
try {return pow(temp.divide(string));} finally {temp.clear();}
}
public native GMP add(GMP gmp);
public native GMP add(String string);
public native GMP subtract(GMP gmp);
public native GMP subtract(String string);
public native GMP ceil();
public native GMP floor();
public native GMP abs();
public native GMP truncate();
//end of math operations

//helper methods
public native void clear();
public static void clear(GMP... gmps) {
for (GMP gmp : gmps) gmp.clear();
}
public native GMP set(GMP gmp);
public native GMP set(String str);
//end of helper methods


@Override
public native boolean equals(Object obj);

@Override
public int hashCode() {
double precision = (super.hashCode() - getAsBigDecimal().hashCode()) * doubleValue();
return (int) precision;
}

public boolean isCleared() {
return isCleared;
}
}
37 changes: 23 additions & 14 deletions common/src/main/java/dev/progames723/hmmm/HmmmLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.architectury.event.EventHandler;
import dev.architectury.utils.EnvExecutor;
import dev.progames723.hmmm.utils.MathUtil;
import dev.progames723.hmmm.utils.NativeUtil;
import dev.progames723.hmmm.utils.PlatformUtil;
import dev.progames723.hmmm.utils.TestUtil;
import org.slf4j.Logger;
Expand All @@ -23,18 +23,27 @@ public class HmmmLibrary {
public static final boolean UNSAFE_REFLECT;

static {
TEST_ARG = EnvExecutor.getEnvSpecific(() -> () -> {
try {return net.minecraft.client.main.Main.class.getDeclaredField("test_hmmm").getBoolean(null);
} catch (Exception e) {return false;}}, () -> () -> {
try {return net.minecraft.server.Main.class.getDeclaredField("test_hmmm").getBoolean(null);
} catch (Exception e) {return false;}
});
UNSAFE_REFLECT = EnvExecutor.getEnvSpecific(() -> () -> {
try {return net.minecraft.client.main.Main.class.getDeclaredField("enableUnsafeReflect").getBoolean(null);
} catch (Exception e) {return false;}}, () -> () -> {
try {return net.minecraft.server.Main.class.getDeclaredField("enableUnsafeReflect").getBoolean(null);
} catch (Exception e) {return false;}
});
boolean temp_test;
boolean temp_reflect;
try {
temp_test = EnvExecutor.getEnvSpecific(() -> () -> {
try {return net.minecraft.client.main.Main.class.getDeclaredField("test_hmmm").getBoolean(null);
} catch (Exception e) {return false;}}, () -> () -> {
try {return net.minecraft.server.Main.class.getDeclaredField("test_hmmm").getBoolean(null);
} catch (Exception e) {return false;}
});
temp_reflect = EnvExecutor.getEnvSpecific(() -> () -> {
try {return net.minecraft.client.main.Main.class.getDeclaredField("enableUnsafeReflect").getBoolean(null);
} catch (Exception e) {return false;}}, () -> () -> {
try {return net.minecraft.server.Main.class.getDeclaredField("enableUnsafeReflect").getBoolean(null);
} catch (Exception e) {return false;}
});
} catch (AssertionError e) {
temp_test = false;
temp_reflect = false;
}
TEST_ARG = temp_test;
UNSAFE_REFLECT = temp_reflect;
}

public static void main(String[] args) {
Expand All @@ -51,7 +60,7 @@ public static void main(String[] args) {
public static void init() {
LOGGER.info("Initializing HmmmLibrary");
EventHandler.init();
MathUtil.loadLibrary();
NativeUtil.init();
LOGGER.info("Running system architecture: {}", PlatformUtil.getArchitecture());
if (TEST_ARG) TestUtil.testAll();
LOGGER.info("Initialized HmmmLibrary!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package dev.progames723.hmmm.event.api;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* Do not use as forge like events!
*/
@ApiStatus.Experimental
public abstract class ReturnableEvent extends Event {
public abstract class ReturnableEvent<T> extends Event {
private final boolean nullable;
protected final boolean isVoid;
protected T value;

protected ReturnableEvent(boolean isVoid, boolean isCancellable, boolean nullable) {
super(isCancellable);
Expand All @@ -21,4 +23,14 @@ protected ReturnableEvent(boolean isVoid, boolean isCancellable, boolean nullabl
public boolean isNullable() {
return nullable;
}

@Nullable
public T getValue() {
return value;
}

public void setValue(T value) {
if (isVoid) return;
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.Nullable;

public class LivingEntityEvent<T> extends ReturnableEvent {
@Nullable private T value;
public class LivingEntityEvent<T> extends ReturnableEvent<T> {
@Nullable private final LivingEntity entity;

public LivingEntityEvent(boolean isVoid, @Nullable T value, @Nullable LivingEntity entity) {
Expand All @@ -32,16 +31,6 @@ public boolean returnsNull() {
return false;
}

@Nullable
public T getValue() {
return value;
}

public void setValue(T value) {
if (isVoid) return;
this.value = value;
}

@Nullable
public LivingEntity getEntity() {
return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class EventUtils {
@CallerSensitive
public static <T> LivingEntityEvent<T> createLivingEntityEvent(T value, LivingEntity entity) {
CallerSensitive.Utils.throwExceptionIfNotAllowed(ReflectUtil.getCallerClass());
return new LivingEntityEvent<>(false, value, entity);
return new LivingEntityEvent<>(false, false, value, entity);
}

@CallerSensitive
public static <T> LivingEntityEvent<T> createVoidLivingEntityEvent(T value, LivingEntity entity) {
CallerSensitive.Utils.throwExceptionIfNotAllowed(ReflectUtil.getCallerClass());
return new LivingEntityEvent<>(true, value, entity);
return new LivingEntityEvent<>(false, true, value, entity);
}

@CallerSensitive
Expand Down
Loading

0 comments on commit 69261fd

Please sign in to comment.