Skip to content

Commit

Permalink
Implement even more handlers for HFW types and misc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Dec 17, 2024
1 parent 3b2385e commit 8c75e74
Show file tree
Hide file tree
Showing 63 changed files with 1,865 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main(String[] args) throws IOException {
ObjectStreamingSystem system = new ObjectStreamingSystem(device, graph);
StreamingObjectReader reader = new StreamingObjectReader(system, factory);

HFWTypeReader.ObjectInfo result = reader.readObject("fc8546a6-d890-4f7a-aa4b-febc111cf96a");
var result = reader.readObject("fc8546a6-d890-4f7a-aa4b-febc111cf96a");
System.out.println(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.shade.decima.game.hfw.data.jolt;

import com.shade.decima.game.hfw.data.jolt.geometry.AABox;
import com.shade.decima.game.hfw.data.jolt.math.Mat44;
import com.shade.decima.game.hfw.data.jolt.math.Quat;
import com.shade.decima.game.hfw.data.jolt.math.Vec3;
import com.shade.decima.game.hfw.data.jolt.math.Vec4;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

import java.io.IOException;
import java.util.List;

public final class JoltUtils {
private JoltUtils() {
// prevents instantiation
}

@NotNull
public static Vec3 readVec3(@NotNull BinaryReader reader) throws IOException {
var x = reader.readFloat();
var y = reader.readFloat();
var z = reader.readFloat();

return new Vec3(x, y, z);
}

@NotNull
public static Vec3 readAlignedVector3(@NotNull BinaryReader reader) throws IOException {
var x = reader.readFloat();
var y = reader.readFloat();
var z = reader.readFloat();
var w = reader.readFloat();

if (z != w) {
throw new IllegalArgumentException("z and w must be equal");
}

return new Vec3(x, y, z);
}

@NotNull
public static Vec4 readVec4(@NotNull BinaryReader reader) throws IOException {
var x = reader.readFloat();
var y = reader.readFloat();
var z = reader.readFloat();
var w = reader.readFloat();

return new Vec4(x, y, z, w);
}

@NotNull
public static Quat readQuaternion(@NotNull BinaryReader reader) throws IOException {
var x = reader.readFloat();
var y = reader.readFloat();
var z = reader.readFloat();
var w = reader.readFloat();

return new Quat(x, y, z, w);
}

@NotNull
public static Mat44 readMatrix4(@NotNull BinaryReader reader) throws IOException {
var col0 = readVec4(reader);
var col1 = readVec4(reader);
var col2 = readVec4(reader);
var col3 = readVec4(reader);

return new Mat44(col0, col1, col2, col3);
}

@NotNull
public static AABox readAABox(@NotNull BinaryReader reader) throws IOException {
return new AABox(readAlignedVector3(reader), readAlignedVector3(reader));
}

@NotNull
public static <T> List<T> readObjects(@NotNull BinaryReader reader, @NotNull BinaryReader.ObjectMapper<T> mapper) throws IOException {
return reader.readObjects(Math.toIntExact(reader.readLong()), mapper);
}

@NotNull
public static byte[] readBytes(@NotNull BinaryReader reader) throws IOException {
return reader.readBytes(Math.toIntExact(reader.readLong()));
}

@NotNull
public static String readString(@NotNull BinaryReader reader) throws IOException {
return reader.readString(Math.toIntExact(reader.readLong()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.shade.decima.game.hfw.data.jolt.core;

import com.shade.util.NotNull;
import com.shade.util.hash.Hashing;

import java.util.HashMap;
import java.util.Map;

public class Factory {
private static final String[] TYPE_NAMES = {
"SkeletalAnimation",
"Skeleton",
"CompoundShapeSettings",
"StaticCompoundShapeSettings",
"MutableCompoundShapeSettings",
"TriangleShapeSettings",
"SphereShapeSettings",
"BoxShapeSettings",
"CapsuleShapeSettings",
"TaperedCapsuleShapeSettings",
"CylinderShapeSettings",
"ScaledShapeSettings",
"MeshShapeSettings",
"ConvexHullShapeSettings",
"HeightFieldShapeSettings",
"RotatedTranslatedShapeSettings",
"OffsetCenterOfMassShapeSettings",
"RagdollSettings",
"PointConstraintSettings",
"SixDOFConstraintSettings",
"SliderConstraintSettings",
"SwingTwistConstraintSettings",
"DistanceConstraintSettings",
"HingeConstraintSettings",
"FixedConstraintSettings",
"ConeConstraintSettings",
"PathConstraintSettings",
"VehicleConstraintSettings",
"WheeledVehicleControllerSettings",
"PathConstraintPath",
"PathConstraintPathHermite",
"MotorSettings",
"PhysicsScene",
"PhysicsMaterial",
"PhysicsMaterialSimple",
"GroupFilter",
"GroupFilterTable",
};

private static final Map<Integer, String> HASH_TO_TYPE_NAME_MAP = new HashMap<>();

static {
for (String name : TYPE_NAMES) {
HASH_TO_TYPE_NAME_MAP.put(computeHash(name), name);
}
}

@NotNull
public static String getTypeName(int hash) {
var name = HASH_TO_TYPE_NAME_MAP.get(hash);
if (name == null) {
throw new IllegalArgumentException("Unknown type hash: %#010x".formatted(hash));
}
return name;
}

@SuppressWarnings("UseHashCodeMethodInspection")
private static int computeHash(@NotNull String name) {
var hash = Hashing.fnv1a().hashString(name).asLong();
return (int) (hash ^ (hash >>> 32));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.shade.decima.game.hfw.data.jolt.geometry;

import com.shade.decima.game.hfw.data.jolt.math.Vec3;
import com.shade.util.NotNull;

public record AABox(@NotNull Vec3 min, @NotNull Vec3 max) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.shade.decima.game.hfw.data.jolt.geometry;

import com.shade.decima.game.hfw.data.jolt.math.Vec3;
import com.shade.util.NotNull;

public record Plane(@NotNull Vec3 normal, float distance) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.shade.decima.game.hfw.data.jolt.math;

import com.shade.util.NotNull;

public record Mat44(@NotNull Vec4 col0, @NotNull Vec4 col1, @NotNull Vec4 col2, @NotNull Vec4 col3) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.shade.decima.game.hfw.data.jolt.math;

public record Quat(float x, float y, float z, float w) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.shade.decima.game.hfw.data.jolt.math;

public record Vec3(float x, float y, float z) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.shade.decima.game.hfw.data.jolt.math;

public record Vec4(float x, float y, float z, float w) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This package contains a minimal implementation of the JoltPhysics data structures.
*
* @see <a href="https://github.com/jrouwe/JoltPhysics">Jolt Physics</a>
*/
package com.shade.decima.game.hfw.data.jolt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.shade.decima.game.hfw.data.jolt.physics.body;

import com.shade.decima.game.hfw.data.jolt.JoltUtils;
import com.shade.decima.game.hfw.data.jolt.math.Quat;
import com.shade.decima.game.hfw.data.jolt.math.Vec3;
import com.shade.decima.game.hfw.data.jolt.physics.collision.CollisionGroup;
import com.shade.decima.game.hfw.data.jolt.physics.collision.GroupFilter;
import com.shade.decima.game.hfw.data.jolt.physics.collision.PhysicsMaterial;
import com.shade.decima.game.hfw.data.jolt.physics.collision.shape.Shape;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

import java.io.IOException;
import java.util.List;

public class BodyCreationSettings {
public enum OverrideMassProperties {
CalculateMassAndInertia,
CalculateInertia,
MassAndInertiaProvided
}

public Vec3 position;
public Quat rotation;
public CollisionGroup collisionGroup;
public short objectLayer;
public MotionType motionType;
public boolean allowDynamicOrKinematic;
public MotionQuality motionQuality;
public boolean allowSleeping;
public float friction;
public float restitution;
public float linearDamping;
public float angularDamping;
public float maxLinearVelocity;
public float maxAngularVelocity;
public float gravityFactor;
public OverrideMassProperties overrideMassProperties;
public float inertiaMultiplier;
public MassProperties massPropertiesOverride;
public Shape shape;

@NotNull
public static BodyCreationSettings sRestoreWithChildren(
@NotNull BinaryReader reader,
@NotNull List<Shape> shapeMap,
@NotNull List<PhysicsMaterial> materialMap,
@NotNull List<GroupFilter> groupFilterMap
) throws IOException {
BodyCreationSettings settings = new BodyCreationSettings();
settings.restoreBinaryState(reader);
settings.shape = Shape.sRestoreWithChildren(reader, shapeMap, materialMap);

int groupFilterId = reader.readInt();
if (groupFilterId != ~0) {
GroupFilter groupFilter;

if (groupFilterId >= groupFilterMap.size()) {
assert groupFilterId == groupFilterMap.size();
groupFilter = GroupFilter.sRestoreFromBinaryState(reader);
groupFilterMap.add(groupFilter);
} else {
groupFilter = groupFilterMap.get(groupFilterId);
}

settings.collisionGroup.groupFilter = groupFilter;
}

return settings;
}

public void restoreBinaryState(@NotNull BinaryReader reader) throws IOException {
position = JoltUtils.readAlignedVector3(reader);
rotation = JoltUtils.readQuaternion(reader);
collisionGroup = CollisionGroup.restoreFromBinaryState(reader);
objectLayer = reader.readShort();
motionType = MotionType.values()[reader.readByte()];
allowDynamicOrKinematic = reader.readByteBoolean();
motionQuality = MotionQuality.values()[reader.readByte()];
allowSleeping = reader.readByteBoolean();
friction = reader.readFloat();
restitution = reader.readFloat();
linearDamping = reader.readFloat();
angularDamping = reader.readFloat();
maxLinearVelocity = reader.readFloat();
maxAngularVelocity = reader.readFloat();
gravityFactor = reader.readFloat();
overrideMassProperties = OverrideMassProperties.values()[reader.readByte()];
inertiaMultiplier = reader.readFloat();
massPropertiesOverride = MassProperties.restoreFromBinaryState(reader);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.shade.decima.game.hfw.data.jolt.physics.body;

import com.shade.decima.game.hfw.data.jolt.JoltUtils;
import com.shade.decima.game.hfw.data.jolt.math.Mat44;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

import java.io.IOException;

public record MassProperties(float mass, @NotNull Mat44 inertia) {
@NotNull
public static MassProperties restoreFromBinaryState(@NotNull BinaryReader reader) throws IOException {
var mass = reader.readFloat();
var inertia = JoltUtils.readMatrix4(reader);

return new MassProperties(mass, inertia);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.shade.decima.game.hfw.data.jolt.physics.body;

public enum MotionQuality {
Discrete,
LinearCast
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.shade.decima.game.hfw.data.jolt.physics.body;

public enum MotionType {
Static,
Kinematic,
Dynamic
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.shade.decima.game.hfw.data.jolt.physics.collision;

import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

import java.io.IOException;

public final class CollisionGroup {
public int groupId;
public int subGroupId;
public GroupFilter groupFilter;

@NotNull
public static CollisionGroup restoreFromBinaryState(@NotNull BinaryReader reader) throws IOException {
CollisionGroup result = new CollisionGroup();
result.groupId = reader.readInt();
result.subGroupId = reader.readInt();

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.shade.decima.game.hfw.data.jolt.physics.collision;

import com.shade.decima.game.hfw.data.jolt.core.Factory;
import com.shade.util.NotImplementedException;
import com.shade.util.NotNull;
import com.shade.util.io.BinaryReader;

import java.io.IOException;

public class GroupFilter {
@NotNull
public static GroupFilter sRestoreFromBinaryState(@NotNull BinaryReader reader) throws IOException {
var hash = reader.readInt();
var name = Factory.getTypeName(hash);
var result = switch (name) {
case "GroupFilterTable" -> new GroupFilterTable();
default -> throw new NotImplementedException();
};
result.restoreBinaryState(reader);
return result;
}

public void restoreBinaryState(@NotNull BinaryReader reader) throws IOException {
// no-op
}
}
Loading

0 comments on commit 8c75e74

Please sign in to comment.