Skip to content

Commit 4a26b4c

Browse files
committed
fix more storage file things
1 parent 8ec2afa commit 4a26b4c

3 files changed

Lines changed: 88 additions & 41 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/api/physics/force/QueuedForceGroup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void applyAndRecordPointForce(final Vector3dc point, final Vector3dc forc
2626
this.forceTotal.applyImpulseAtPoint(this.subLevel.getMassTracker(), point, force);
2727
this.recordPointForce(point, force);
2828
}
29+
2930
public void recordPointForce(final Vector3dc point, final Vector3dc force) {
3031
if (!this.subLevel.isTrackingIndividualQueuedForces()) {
3132
return;

common/src/main/java/dev/ryanhcode/sable/physics/floating_block/FloatingBlockController.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void physicsTick(final double partialPhysicsTick, final double timeStep,
3939
this.containers.clear();
4040
this.containers.add(this.sublevelContainer);
4141

42-
for(final KinematicContraption contraption : this.subLevel.getPlot().getContraptions()) {
42+
for (final KinematicContraption contraption : this.subLevel.getPlot().getContraptions()) {
4343
final FloatingClusterContainer container = contraption.sable$getFloatingClusterContainer();
4444

4545
final Vector3dc lastPosition = new Vector3d(contraption.sable$getPosition(partialPhysicsTick - 1.0f));
@@ -64,7 +64,7 @@ public void physicsTick(final double partialPhysicsTick, final double timeStep,
6464

6565
localGravity.set(DimensionPhysicsData.getGravity(this.subLevel.getLevel(), this.subLevel.logicalPose().position()));
6666
this.subLevel.logicalPose().orientation().transformInverse(localGravity);
67-
if(!this.needsTicking())
67+
if (!this.needsTicking())
6868
return;
6969

7070
this.subLevel.logicalPose().orientation().transformInverse(linearVelocity, localLinearVelocity);
@@ -82,10 +82,10 @@ public void physicsTick(final double partialPhysicsTick, final double timeStep,
8282
if (cluster.getMaterial().scaleWithPressure())
8383
cluster.getBlockData().computePressureScale(this.subLevel);
8484

85-
this.applyFriction(container,cluster, localGravity, localLinearVelocity, localAngularVelocity, clusterFrictionForce, clusterFrictionTorque);
85+
this.applyFriction(container, cluster, localGravity, localLinearVelocity, localAngularVelocity, clusterFrictionForce, clusterFrictionTorque);
8686

8787
final Vector3d recordedClusterFrictionForce = new Vector3d(clusterFrictionForce);
88-
this.recordForce(container,cluster, dragGroup, recordedClusterFrictionForce);
88+
this.recordForce(container, cluster, dragGroup, recordedClusterFrictionForce);
8989
recordedFrictionForces.add(recordedClusterFrictionForce);
9090

9191
frictionForce.add(clusterFrictionForce);
@@ -113,19 +113,18 @@ public void physicsTick(final double partialPhysicsTick, final double timeStep,
113113
for (final Vector3d force : recordedFrictionForces) {
114114
force.mul(timeStep);//forceScale *
115115
}
116-
if(localGravity.lengthSquared()>0)
116+
if (localGravity.lengthSquared() > 0)
117117
this.applyLift(localGravity, linearImpulse, angularImpulse, timeStep);
118118

119119
linearImpulse.fma(timeStep, frictionForce);
120120
angularImpulse.fma(timeStep, frictionTorque);
121121
}
122122

123-
public boolean needsTicking()
124-
{
125-
if(this.sublevelContainer.needsTicking())
123+
public boolean needsTicking() {
124+
if (this.sublevelContainer.needsTicking())
126125
return true;
127126
for (final FloatingClusterContainer container : this.containers) {
128-
if(container.needsTicking())
127+
if (container.needsTicking())
129128
return true;
130129
}
131130
return false;
@@ -174,7 +173,7 @@ private void applyLift(final Vector3d localGravity, final Vector3d linearImpulse
174173
//unit: strength * weight
175174
final double weightedForce = clusterForce * cluster.getBlockData().totalScale;
176175

177-
this.getTrueWeightedClusterPosition(container,cluster,weightedPositionTemp);
176+
this.getTrueWeightedClusterPosition(container, cluster, weightedPositionTemp);
178177

179178
if (material.preventSelfLift()) {
180179
totalForce += weightedForce;
@@ -186,7 +185,7 @@ private void applyLift(final Vector3d localGravity, final Vector3d linearImpulse
186185
if (this.subLevel.isTrackingIndividualQueuedForces()) {
187186
final QueuedForceGroup levitationGroup = this.subLevel.getOrCreateQueuedForceGroup(ForceGroups.LEVITATION.get());
188187

189-
this.recordForce(container,cluster, levitationGroup, new Vector3d(localGravity).mul(-weightedForce * timeStep));
188+
this.recordForce(container, cluster, levitationGroup, new Vector3d(localGravity).mul(-weightedForce * timeStep));
190189
}
191190

192191
localGravity.cross(weightedPositionTemp, torqueTemp);//torqueTemp unit: weight * position * gravity
@@ -226,7 +225,7 @@ private void applyLift(final Vector3d localGravity, final Vector3d linearImpulse
226225
final Vector3d force = new Vector3d(localGravity).mul(timeStep * -cluster.getBlockData().totalScale * material.liftStrength());
227226
force.mul(scaleFactor);
228227

229-
this.recordForce(container,cluster, levitationGroup, force);
228+
this.recordForce(container, cluster, levitationGroup, force);
230229
}
231230
}
232231
}
@@ -237,13 +236,13 @@ private void applyLift(final Vector3d localGravity, final Vector3d linearImpulse
237236
angularImpulse.fma(timeStep, liftingTorque);
238237
}
239238

240-
private void recordForce(final FloatingClusterContainer container,final FloatingBlockCluster cluster, final QueuedForceGroup forceGroup, final Vector3d force) {
241-
forceGroup.recordPointForce(this.getTrueWeightedClusterPosition(container,cluster,new Vector3d()).div(cluster.getBlockData().totalScale).add(this.subLevel.getMassTracker().getCenterOfMass()), force);
239+
private void recordForce(final FloatingClusterContainer container, final FloatingBlockCluster cluster, final QueuedForceGroup forceGroup, final Vector3d force) {
240+
forceGroup.recordPointForce(this.getTrueWeightedClusterPosition(container, cluster, new Vector3d()).div(cluster.getBlockData().totalScale).add(this.subLevel.getMassTracker().getCenterOfMass()), force);
242241
}
243-
private Vector3d getTrueWeightedClusterPosition(final FloatingClusterContainer container,final FloatingBlockCluster cluster,final Vector3d pos)
244-
{
245-
container.rotationOffset.transform(cluster.getBlockData().weightedPosition,pos);
246-
return pos.fma(cluster.getBlockData().totalScale,container.positionOffset);
242+
243+
private Vector3d getTrueWeightedClusterPosition(final FloatingClusterContainer container, final FloatingBlockCluster cluster, final Vector3d pos) {
244+
container.rotationOffset.transform(cluster.getBlockData().weightedPosition, pos);
245+
return pos.fma(cluster.getBlockData().totalScale, container.positionOffset);
247246
}
248247

249248
private static final Matrix3d containerRotation = new Matrix3d();
@@ -261,10 +260,10 @@ private Vector3d getTrueWeightedClusterPosition(final FloatingClusterContainer c
261260
private static final Vector3d shiftedCenter = new Vector3d();
262261
private static final Vector3d linearSlowDrag = new Vector3d();
263262

264-
private void applyFriction(final FloatingClusterContainer container,final FloatingBlockCluster cluster, final Vector3dc localGravity, final Vector3dc linearVelocity, final Vector3dc angularVelocity, final Vector3d frictionForce, final Vector3d frictionTorque) {
263+
private void applyFriction(final FloatingClusterContainer container, final FloatingBlockCluster cluster, final Vector3dc localGravity, final Vector3dc linearVelocity, final Vector3dc angularVelocity, final Vector3d frictionForce, final Vector3d frictionTorque) {
265264

266265
double frictionScale = 1;
267-
if(cluster.getMaterial().scaleWithGravity())
266+
if (cluster.getMaterial().scaleWithGravity())
268267
frictionScale = localGravity.length();
269268
if (cluster.getMaterial().scaleWithPressure())
270269
frictionScale *= cluster.getBlockData().getPressureScale();
@@ -274,7 +273,7 @@ private void applyFriction(final FloatingClusterContainer container,final Floati
274273
speedScale = 0;
275274

276275
totalAngularVelocity.set(angularVelocity).add(container.angularVelocity);
277-
this.getTrueWeightedClusterPosition(container,cluster,clusterCenter).div(cluster.getBlockData().totalScale);
276+
this.getTrueWeightedClusterPosition(container, cluster, clusterCenter).div(cluster.getBlockData().totalScale);
278277

279278
cluster.getBlockData().outerProduct.scale(1 / cluster.getBlockData().totalScale, averagePositionMatrix);
280279

@@ -291,8 +290,8 @@ private void applyFriction(final FloatingClusterContainer container,final Floati
291290

292291
//velocity of the center of lift in local space
293292
angularVelocity.cross(clusterCenter, meanVelocity);
294-
container.rotationOffset.transform(cluster.getBlockData().weightedPosition,rotatedPos).div(cluster.getBlockData().totalScale);
295-
final Vector3d extraContainerVelocity = container.angularVelocity.cross(rotatedPos,rotatedPos);
293+
container.rotationOffset.transform(cluster.getBlockData().weightedPosition, rotatedPos).div(cluster.getBlockData().totalScale);
294+
final Vector3d extraContainerVelocity = container.angularVelocity.cross(rotatedPos, rotatedPos);
296295
meanVelocity.add(linearVelocity).add(container.velocity).add(extraContainerVelocity);
297296

298297
//center of the shifted position distribution relative to clusterCenter, variance is shiftedPositionMatrix
@@ -343,7 +342,7 @@ private void matrixThingy(final Matrix3dc X, final Matrix3dc Y, final Matrix3d o
343342
}
344343

345344
private Matrix3d getGravityMatrix(final Vector3dc g, final double verticalDrag, final double horizontalDrag, final Matrix3d target) {
346-
if(g.lengthSquared() > 0.00001)
345+
if (g.lengthSquared() > 0.00001)
347346
SableMathUtils.setOuterProduct(g, g, (horizontalDrag - verticalDrag) / g.dot(g), target);
348347
else
349348
target.identity();
@@ -365,31 +364,31 @@ private double getClampingFactor(final Vector3dc currentVelocity, final Vector3d
365364
return v * (1 - Math.exp(-k / v)) / k;
366365
}
367366

368-
private double getKineticClampingFactor(final Vector3dc currentLinearVelocity,final Vector3dc currentAngularVelocity,final Vector3d frictionForce,final Vector3d frictionTorque,final double timestep) {
367+
private double getKineticClampingFactor(final Vector3dc currentLinearVelocity, final Vector3dc currentAngularVelocity, final Vector3d frictionForce, final Vector3d frictionTorque, final double timestep) {
369368

370369
final double numerator = currentLinearVelocity.dot(frictionForce) + currentAngularVelocity.dot(frictionTorque);
371-
double denominator = frictionForce.dot(frictionForce)* this.subLevel.getMassTracker().getInverseMass() +
372-
SableMathUtils.multiplyInnerProduct(frictionTorque, this.subLevel.getMassTracker().getInverseInertiaTensor(),frictionTorque);
373-
denominator*=timestep;
374-
if(denominator < 1E-10)
370+
double denominator = frictionForce.dot(frictionForce) * this.subLevel.getMassTracker().getInverseMass() +
371+
SableMathUtils.multiplyInnerProduct(frictionTorque, this.subLevel.getMassTracker().getInverseInertiaTensor(), frictionTorque);
372+
denominator *= timestep;
373+
if (denominator < 1E-10)
375374
return 1;
376-
final double t = -numerator/denominator;
377-
return Math.max(Math.min(t,1),0);
375+
final double t = -numerator / denominator;
376+
return Math.max(Math.min(t, 1), 0);
378377
}
379378

380379
public void addFloatingBlock(final BlockState state, final Vector3d pos) {
381-
this.sublevelContainer.addFloatingBlock(state,pos);
380+
this.sublevelContainer.addFloatingBlock(state, pos);
382381
}
383382

384383
public void removeFloatingBlock(final BlockState state, final Vector3d pos) {
385-
this.sublevelContainer.removeFloatingBlock(state,pos);
384+
this.sublevelContainer.removeFloatingBlock(state, pos);
386385
}
387386

388387
public void queueAddFloatingBlock(final BlockState state, final BlockPos pos) {
389-
this.sublevelContainer.queueAddFloatingBlock(state,pos);
388+
this.sublevelContainer.queueAddFloatingBlock(state, pos);
390389
}
391390

392391
public void queueRemoveFloatingBlock(final BlockState state, final BlockPos pos) {
393-
this.sublevelContainer.queueRemoveFloatingBlock(state,pos);
392+
this.sublevelContainer.queueRemoveFloatingBlock(state, pos);
394393
}
395394
}

common/src/main/java/dev/ryanhcode/sable/sublevel/storage/region/SubLevelStorageFile.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.StandardCopyOption;
1616
import java.nio.file.StandardOpenOption;
1717
import java.util.BitSet;
18+
import java.util.stream.Stream;
1819

1920
/**
2021
* A storage file for sub-levels.
@@ -255,10 +256,14 @@ protected void write(final int index, final ByteBuffer byteBuffer) throws IOExce
255256
this.usedIndices.set(index, true);
256257
this.writeHeader();
257258

259+
final Path externalPath = this.getExternalFilePath(index);
258260
if (savingToExternalFile) {
259-
Files.move(temporaryExternalFile, this.getExternalFilePath(index), StandardCopyOption.REPLACE_EXISTING);
261+
Files.move(temporaryExternalFile, externalPath, StandardCopyOption.REPLACE_EXISTING);
260262
} else {
261-
Files.deleteIfExists(this.getExternalFilePath(index));
263+
// we're not saving to an external file, but the previous time we were writing to this index there
264+
// could've been an external file. so we remove it just in case, to prevent the detached files sticking
265+
// around forever
266+
Files.deleteIfExists(externalPath);
262267
}
263268

264269
// clear the previous span of sectors if we used to store data there for this sub-level index
@@ -355,6 +360,8 @@ private void clear(final int index) throws IOException {
355360
this.usedSectors.clear(spanStart, spanStart + this.getSpanLength(span));
356361

357362
this.writeHeader();
363+
364+
Files.deleteIfExists(this.getExternalFilePath(index));
358365
}
359366
}
360367

@@ -415,10 +422,50 @@ public void close() throws IOException {
415422
}
416423
}
417424

418-
public void delete() throws IOException {
419-
this.file.close();
420-
Files.delete(this.path);
421-
Files.deleteIfExists(this.externalFileDir);
425+
public void delete() {
426+
try {
427+
this.file.close();
428+
} catch (final IOException e) {
429+
Sable.LOGGER.error("Failed to close sub-level storage file {} before deletion", this.path, e);
430+
}
431+
432+
try {
433+
Files.deleteIfExists(this.path);
434+
} catch (final IOException e) {
435+
Sable.LOGGER.error("Failed to delete sub-level storage file {}", this.path, e);
436+
}
437+
438+
// Attempt to delete all the external files
439+
// Technically, I don't think it's possible for this to matter? Because by the time the storage file is deleted,
440+
// it's assumed to be empty. So all the external files should be gone anyway. But let's do it anyway to be safe
441+
this.deleteExternalFiles();
442+
}
443+
444+
/**
445+
* Attempts to delete all the external storage files
446+
*/
447+
private void deleteExternalFiles() {
448+
if (!Files.isDirectory(this.externalFileDir)) {
449+
return;
450+
}
451+
452+
try (final Stream<Path> list = Files.list(this.externalFileDir)) {
453+
list.forEach(path -> {
454+
try {
455+
Files.deleteIfExists(path);
456+
} catch (final IOException e) {
457+
Sable.LOGGER.error("Failed to delete external sub-level storage file {}", path, e);
458+
}
459+
});
460+
} catch (final IOException e) {
461+
Sable.LOGGER.error("Failed to list external sub-level storage directory {}", this.externalFileDir, e);
462+
}
463+
464+
try {
465+
Files.deleteIfExists(this.externalFileDir);
466+
} catch (final IOException e) {
467+
Sable.LOGGER.error("Failed to delete external sub-level storage directory {}", this.externalFileDir, e);
468+
}
422469
}
423470

424471
public void flush() throws IOException {

0 commit comments

Comments
 (0)