Skip to content

Commit 31747ac

Browse files
committed
1. better fps
2. better disk and memory usage
1 parent 08b9b20 commit 31747ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2017
-348
lines changed

build.gradle

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ dependencies {
2626
// Fabric API. This is technically optional, but you probably want it anyway.
2727
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
2828

29-
// Replay Mod
30-
modCompileOnly "maven.modrinth:replaymod:${project.minecraft_version}-${project.replay_mod_version}"
31-
modLocalRuntime "maven.modrinth:replaymod:${project.minecraft_version}-${project.replay_mod_version}"
29+
// modCompileOnly "maven.modrinth:replaymod:1.20.4-2.6.14"
30+
// modLocalRuntime "maven.modrinth:replaymod:1.20.4-2.6.14"
31+
// modCompileOnly "maven.modrinth:sodium:mc1.20.4-0.5.8"
32+
// modLocalRuntime "maven.modrinth:sodium:mc1.20.4-0.5.8"
33+
// modCompileOnly "maven.modrinth:iris:1.6.15+1.20.4"
34+
// modLocalRuntime "maven.modrinth:iris:1.6.15+1.20.4"
3235
}
3336

3437
processResources {

gradle.properties

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ fabric_version=0.95.0+1.20.4
1212

1313
# Mod Properties
1414
mod_version = 1.0
15-
maven_group = yancey.openparticle.core
15+
maven_group = yancey.openparticle
1616
archives_base_name = OpenParticle
17-
18-
# Replay Mod
19-
replay_mod_version = 2.6.14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package yancey.openparticle.api.common;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import yancey.openparticle.api.common.bridge.Bridge;
5+
import yancey.openparticle.api.common.bridge.Logger;
6+
import yancey.openparticle.api.common.data.DataParticleManager;
7+
import yancey.openparticle.api.common.data.identifier.IdentifierCache;
8+
9+
import java.io.*;
10+
11+
public class OpenParticleAPI {
12+
13+
public final Bridge bridge;
14+
public final Logger logger;
15+
16+
public OpenParticleAPI(Bridge bridge, Logger logger) {
17+
this.bridge = bridge;
18+
this.logger = logger;
19+
}
20+
21+
public long getParticleCount(DataParticleManager dataParticleManager) {
22+
return dataParticleManager.getParticleCount();
23+
}
24+
25+
public void output(@NotNull File file, @NotNull DataParticleManager dataParticleManager) {
26+
if (!file.exists()) {
27+
File parent = file.getParentFile();
28+
if (parent != null && !parent.exists() && !parent.mkdirs()) {
29+
logger.warn("文件夹创建失败 : " + parent.getAbsolutePath());
30+
}
31+
}
32+
try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
33+
IdentifierCache.writeToFile(dataOutputStream);
34+
dataParticleManager.writeToFile(dataOutputStream);
35+
} catch (IOException e) {
36+
logger.warn("文件写入失败 : " + file.getAbsolutePath(), e);
37+
}
38+
}
39+
40+
public DataParticleManager input(@NotNull File file) {
41+
try (DataInputStream dataOutputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) {
42+
IdentifierCache.readFromFile(dataOutputStream);
43+
return new DataParticleManager(dataOutputStream);
44+
} catch (IOException e) {
45+
logger.warn("文件读取失败 : " + file.getAbsolutePath(), e);
46+
}
47+
return null;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package yancey.openparticle.api.common.bridge;
2+
3+
import yancey.openparticle.api.common.data.identifier.Identifier;
4+
5+
public interface Bridge {
6+
7+
int getParticleRawId(Identifier identifier);
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package yancey.openparticle.api.common.bridge;
2+
3+
public interface Logger {
4+
5+
void info(String str);
6+
7+
void warn(String str, Throwable throwable);
8+
9+
void warn(String str);
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package yancey.openparticle.api.common.controller;
2+
3+
4+
import yancey.openparticle.api.common.OpenParticleAPI;
5+
import yancey.openparticle.api.common.data.ParticleState;
6+
7+
public interface ParticleController {
8+
9+
ParticleState getParticleState(int tick);
10+
11+
int getTickStart();
12+
13+
int getAge();
14+
15+
int getParticleTypeRawId(OpenParticleAPI openParticleAPI);
16+
17+
void clearCache();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package yancey.openparticle.api.common.controller;
2+
3+
import yancey.openparticle.api.common.OpenParticleAPI;
4+
import yancey.openparticle.api.common.data.ParticleState;
5+
import yancey.openparticle.api.common.data.particle.DataParticleSingle;
6+
import yancey.openparticle.api.common.math.Vec3;
7+
import yancey.openparticle.api.common.node.Node;
8+
import yancey.openparticle.api.common.util.ColorUtil;
9+
10+
public class SimpleParticleController implements ParticleController {
11+
12+
private final int tickStart;
13+
private final Node node;
14+
private final int age;
15+
private int particleTypeRawId = -1;
16+
17+
public SimpleParticleController(Node node) {
18+
this.node = node;
19+
this.tickStart = node.getTickStart();
20+
this.age = node.getAge();
21+
}
22+
23+
@Override
24+
public ParticleState getParticleState(int tick) {
25+
ParticleState particleState = new ParticleState();
26+
Vec3 position = node.getCachePositionMatrix(tick).apply(Vec3.ZERO);
27+
particleState.x = position.x;
28+
particleState.y = position.y;
29+
particleState.z = position.z;
30+
int color = node.getCacheColor(tick);
31+
particleState.r = (byte) ColorUtil.getRed(color);
32+
particleState.g = (byte) ColorUtil.getGreen(color);
33+
particleState.b = (byte) ColorUtil.getBlue(color);
34+
particleState.a = (byte) ColorUtil.getAlpha(color);
35+
particleState.bright = 15728880;
36+
return particleState;
37+
}
38+
39+
@Override
40+
public int getTickStart() {
41+
return tickStart;
42+
}
43+
44+
@Override
45+
public int getAge() {
46+
return age;
47+
}
48+
49+
@Override
50+
public int getParticleTypeRawId(OpenParticleAPI openParticleAPI) {
51+
if (particleTypeRawId == -1) {
52+
particleTypeRawId = ((DataParticleSingle) node.dataParticle).identifier.getRawId(openParticleAPI);
53+
}
54+
return particleTypeRawId;
55+
}
56+
57+
@Override
58+
public void clearCache() {
59+
node.clearCache();
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package yancey.openparticle.api.common.data;
2+
3+
import yancey.openparticle.api.common.controller.SimpleParticleController;
4+
import yancey.openparticle.api.common.data.particle.DataParticle;
5+
6+
import java.io.DataInputStream;
7+
import java.io.DataOutputStream;
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.Comparator;
11+
import java.util.List;
12+
13+
public class DataParticleManager {
14+
15+
public static List<DataParticle> dataParticleList = new ArrayList<>();
16+
public final List<DataParticle> parents;
17+
18+
public DataParticleManager() {
19+
parents = new ArrayList<>();
20+
}
21+
22+
public DataParticleManager(DataInputStream dataInputStream) throws IOException {
23+
int size1 = dataInputStream.readInt();
24+
dataParticleList = new ArrayList<>(size1);
25+
for (int i = 0; i < size1; i++) {
26+
dataParticleList.add(DataParticle.readFromFile(this, dataInputStream));
27+
}
28+
int size2 = dataInputStream.readInt();
29+
parents = new ArrayList<>(size2);
30+
for (int i = 0; i < size2; i++) {
31+
parents.add(getDataParticle(dataInputStream.readInt()));
32+
}
33+
}
34+
35+
public void add(DataParticle dataParticle) {
36+
parents.add(dataParticle);
37+
}
38+
39+
public void writeToFile(DataOutputStream dataOutputStream) throws IOException {
40+
dataOutputStream.writeInt(dataParticleList.size());
41+
for (DataParticle dataParticle : dataParticleList) {
42+
dataParticle.writeToFile(dataOutputStream);
43+
}
44+
dataOutputStream.writeInt(parents.size());
45+
for (DataParticle parent : parents) {
46+
dataOutputStream.writeInt(parent.id);
47+
}
48+
}
49+
50+
public DataParticle getDataParticle(int id) {
51+
return dataParticleList.get(id);
52+
}
53+
54+
public long getParticleCount() {
55+
return parents.stream()
56+
.flatMap(DataParticle::streamParticleSingle)
57+
.count();
58+
}
59+
60+
public DataRunningPerTick[] getDataRunningList() {
61+
List<DataRunningPerTick> dataRunningList = new ArrayList<>();
62+
parents.stream()
63+
.flatMap(parent -> parent.getNode().second)
64+
.toList()
65+
.stream()
66+
.map(SimpleParticleController::new)
67+
.toList()
68+
.stream()
69+
.forEach(controller -> {
70+
DataRunningPerTick.getFromList(controller.getTickStart(), dataRunningList).controllerList.add(controller);
71+
});
72+
dataRunningList.sort(Comparator.comparingInt(dataRunningPerTick -> dataRunningPerTick.tick));
73+
return dataRunningList.toArray(new DataRunningPerTick[0]);
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package yancey.openparticle.api.common.data;
2+
3+
import yancey.openparticle.api.common.controller.SimpleParticleController;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class DataRunningPerTick {
9+
10+
public final int tick;
11+
public final List<SimpleParticleController> controllerList = new ArrayList<>();
12+
13+
public DataRunningPerTick(int tick) {
14+
this.tick = tick;
15+
}
16+
17+
public static DataRunningPerTick getFromList(int tick, List<DataRunningPerTick> dataRunningList) {
18+
for (DataRunningPerTick dataRunning : dataRunningList) {
19+
if (dataRunning.tick == tick) {
20+
return dataRunning;
21+
}
22+
}
23+
DataRunningPerTick dataRunning = new DataRunningPerTick(tick);
24+
dataRunningList.add(dataRunning);
25+
return dataRunning;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package yancey.openparticle.api.common.data;
2+
3+
public class ParticleState {
4+
public float x, y, z;
5+
public byte r, g, b, a;
6+
public int bright;
7+
8+
@Override
9+
public String toString() {
10+
return String.format("(x, y, z) = (%.2f, %.2f, %.2f) | (r, g, b, a) = (%d, %d, %d, %d) | bright = %d", x, y, z, r, g, b, a, bright);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package yancey.openparticle.api.common.data.color;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
7+
public abstract class DataColor {
8+
9+
public static final int STATIC = 0;
10+
public static final int SIMPLE = 1;
11+
public static final int FREE = 2;
12+
13+
public static DataColor readFromFile(DataInputStream dataInputStream) throws IOException {
14+
byte type = dataInputStream.readByte();
15+
return switch (type) {
16+
case STATIC -> new DataColorStatic(dataInputStream);
17+
case SIMPLE -> new DataColorSimple(dataInputStream);
18+
case FREE -> new DataColorFree(dataInputStream);
19+
default -> throw new IllegalStateException("未知的颜色类型: " + type);
20+
};
21+
}
22+
23+
protected abstract byte getType();
24+
25+
public void writeToFile(DataOutputStream dataOutputStream) throws IOException {
26+
dataOutputStream.writeByte(getType());
27+
}
28+
29+
public abstract int getColor(int tick, int age);
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package yancey.openparticle.api.common.data.color;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
7+
public class DataColorFree extends DataColor {
8+
9+
private final int[] colors;
10+
11+
public DataColorFree(int[] colors) {
12+
this.colors = colors;
13+
}
14+
15+
public DataColorFree(DataInputStream dataInputStream) throws IOException {
16+
this.colors = new int[dataInputStream.readInt()];
17+
for (int i = 0; i < this.colors.length; i++) {
18+
this.colors[i] = dataInputStream.readInt();
19+
}
20+
}
21+
22+
protected byte getType() {
23+
return FREE;
24+
}
25+
26+
@Override
27+
public void writeToFile(DataOutputStream dataOutputStream) throws IOException {
28+
super.writeToFile(dataOutputStream);
29+
for (int color : colors) {
30+
dataOutputStream.writeInt(color);
31+
}
32+
}
33+
34+
@Override
35+
public int getColor(int tick, int age) {
36+
return colors[Math.min(tick, colors.length - 1)];
37+
}
38+
}

0 commit comments

Comments
 (0)