Skip to content

Commit

Permalink
Round fractional frame times down to match vanilla
Browse files Browse the repository at this point in the history
  • Loading branch information
soir20 committed Dec 1, 2023
1 parent 74d67bb commit 6e6f662
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private AnimationMetadata readAnimationProperties(MetadataView metadata, int ima
+ imageHeight);
}

int defaultTime = metadata.integerValue("frametime").orElse(1);
int defaultTime = (int) (double) metadata.doubleValue("frametime").orElse(1d);
if (defaultTime <= 0) {
throw new InvalidMetadataException("Frame time must be positive but was: " + defaultTime);
}
Expand Down Expand Up @@ -278,7 +278,7 @@ private Pair<Integer, Integer> parseFrameObj(MetadataView frameObj, int defaultT
throw new InvalidMetadataException("Frame index cannot be negative, but was " + index);
}

int frameTime = frameObj.integerValue("time").orElse(defaultTime);
int frameTime = (int) (double) frameObj.doubleValue("time").orElse((double) defaultTime);
if (frameTime <= 0) {
throw new InvalidMetadataException("Frame time must be greater than zero, but was " + frameTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,16 @@ public void analyze_NoPartsNoDefaultTime_Uses1Tick() throws InvalidMetadataExcep
@Test
public void analyze_NoPartsZeroDefaultTime_InvalidMetadataException() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of("frametime", 0)
ImmutableMap.of("frametime", 0d)
);
expectedException.expect(InvalidMetadataException.class);
ANALYZER.analyze(metadataView, 10, 20);
}

@Test
public void analyze_NoPartsLessThanOneDefaultTime_InvalidMetadataException() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of("frametime", 0.6d)
);
expectedException.expect(InvalidMetadataException.class);
ANALYZER.analyze(metadataView, 10, 20);
Expand All @@ -202,7 +211,7 @@ public void analyze_NoPartsZeroDefaultTime_InvalidMetadataException() throws Inv
@Test
public void analyze_NoPartsNegativeDefaultTime_InvalidMetadataException() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of("frametime", -1)
ImmutableMap.of("frametime", -1d)
);
expectedException.expect(InvalidMetadataException.class);
ANALYZER.analyze(metadataView, 10, 20);
Expand All @@ -211,7 +220,17 @@ public void analyze_NoPartsNegativeDefaultTime_InvalidMetadataException() throws
@Test
public void analyze_NoPartsPositiveDefaultTime_UsesProvidedTime() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of("frametime", 7)
ImmutableMap.of("frametime", 7d)
);
AnimationGroupMetadata metadata = (AnimationGroupMetadata) ANALYZER.analyze(metadataView, 10, 20);

assertEquals(7, metadata.parts().get(0).defaultTime());
}

@Test
public void analyze_NoPartsPositiveDefaultTime_RoundsTimeDown() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of("frametime", 7.7d)
);
AnimationGroupMetadata metadata = (AnimationGroupMetadata) ANALYZER.analyze(metadataView, 10, 20);

Expand Down Expand Up @@ -403,9 +422,29 @@ public void analyze_NoPartsPredefinedFramesOnlyObjects_UsesProvidedTimeForAllFra
ImmutableMap.of(
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"1", new MockMetadataView(ImmutableMap.of("index", 2, "time", 4)),
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", new MockMetadataView(ImmutableMap.of("index", 2, "time", 4d)),
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
);
AnimationGroupMetadata metadata = (AnimationGroupMetadata) ANALYZER.analyze(metadataView, 10, 100);

assertEquals(
List.of(Pair.of(5, 12), Pair.of(2, 4), Pair.of(0, 7)),
metadata.parts().get(0).predefinedFrames()
);
}

@Test
public void analyze_NoPartsPredefinedFramesOnlyObjects_RoundsTimeForAllFrames() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of(
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12.1d)),
"1", new MockMetadataView(ImmutableMap.of("index", 2, "time", 4.8d)),
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
);
Expand Down Expand Up @@ -511,9 +550,9 @@ public void analyze_NoPartsPredefinedFramesObjectMissingTimeNoDefault_Uses1Tick(
ImmutableMap.of(
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", new MockMetadataView(ImmutableMap.of("index", 2)),
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
);
Expand All @@ -529,12 +568,12 @@ public void analyze_NoPartsPredefinedFramesObjectMissingTimeNoDefault_Uses1Tick(
public void analyze_NoPartsPredefinedFramesObjectMissingTimeHasDefault_UsesDefault() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of(
"frametime", 13,
"frametime", 13d,
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", new MockMetadataView(ImmutableMap.of("index", 2)),
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
);
Expand All @@ -550,12 +589,12 @@ public void analyze_NoPartsPredefinedFramesObjectMissingTimeHasDefault_UsesDefau
public void analyze_NoPartsPredefinedFramesMixedIndicesObjects_HandlesBoth() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of(
"frametime", 13,
"frametime", 13d,
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", 2,
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
);
Expand All @@ -571,13 +610,13 @@ public void analyze_NoPartsPredefinedFramesMixedIndicesObjects_HandlesBoth() thr
public void analyze_NoPartsPredefinedFramesOtherTypeMixedIn_HandlesIgnoresOtherType() throws InvalidMetadataException {
MetadataView metadataView = new MockMetadataView(
ImmutableMap.of(
"frametime", 13,
"frametime", 13d,
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", 2,
"2", "7",
"3", new MockMetadataView(ImmutableMap.of("index", 8, "time", 2))
"3", new MockMetadataView(ImmutableMap.of("index", 8, "time", 2d))
))
)
);
Expand Down Expand Up @@ -640,16 +679,16 @@ public void analyze_NoPartsAllFieldsPresent_AllUsed() throws InvalidMetadataExce
new ImmutableMap.Builder<String, Object>()
.put("width", 18)
.put("height", 27)
.put("frametime", 5)
.put("frametime", 5d)
.put("interpolate", true)
.put("smoothAlpha", true)
.put("daytimeSync", false)
.put(
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", 2,
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
.put("skip", 12)
Expand Down Expand Up @@ -1130,15 +1169,15 @@ public void analyze_HasPartsAllFieldsPresent_AllUsed() throws InvalidMetadataExc
.put("y", 2)
.put("width", 18)
.put("height", 27)
.put("frametime", 5)
.put("frametime", 5d)
.put("interpolate", true)
.put("smoothAlpha", true)
.put(
"frames",
new MockMetadataView(ImmutableMap.of(
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12)),
"0", new MockMetadataView(ImmutableMap.of("index", 5, "time", 12d)),
"1", 2,
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7))
"2", new MockMetadataView(ImmutableMap.of("index", 0, "time", 7d))
))
)
.put("skip", 12)
Expand All @@ -1149,7 +1188,7 @@ public void analyze_HasPartsAllFieldsPresent_AllUsed() throws InvalidMetadataExc
.put("texture", MOCK_TEXTURE.apply(10, 60))
.put("x", 4)
.put("y", 4)
.put("frametime", 15)
.put("frametime", 15d)
.put("interpolate", true)
.put("daytimeSync", true)
.build()
Expand Down

0 comments on commit 6e6f662

Please sign in to comment.