-
Notifications
You must be signed in to change notification settings - Fork 497
/
ModelCacheHelper.java
88 lines (74 loc) · 3.44 KB
/
ModelCacheHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package buildcraft.core.lib.client.model;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.vertex.VertexFormat;
import buildcraft.api.core.BCLog;
import buildcraft.core.lib.config.DetailedConfigOption;
/** Implements a caching system for models with potentially infinite variants. Automatically expires entries after a
* configurable time period, and up to a maximum number. */
public class ModelCacheHelper<K> implements IModelCache<K> {
private final DetailedConfigOption optionCacheExpire, optionCacheSize;
private final IModelGenerator<K> generator;
private final LoadingCache<K, ModelValue> modelCache;
public ModelCacheHelper(String detailedName, IModelGenerator<K> generator) {
this.generator = generator;
optionCacheExpire = new DetailedConfigOption("render.cache." + detailedName + ".keepalive", "60");
optionCacheSize = new DetailedConfigOption("render.cache." + detailedName + ".maxsize", "160");
int expireTime = optionCacheExpire.getAsInt();
if (expireTime < 10) expireTime = 10;
int maxSize = optionCacheSize.getAsInt();
if (maxSize < 0) maxSize = 0;
BCLog.logger.info("Making cache " + detailedName + " with expiry after " + expireTime + ", maxSize of " + maxSize);
modelCache = CacheBuilder.newBuilder()// Use our own
.expireAfterAccess(expireTime, TimeUnit.SECONDS)// Use our own
.maximumSize(maxSize)// Use our own
.build(CacheLoader.from(this::load));
}
private ModelValue load(K key) {
return new ModelValue(generator.generate(key));
}
@Override
public void appendAsMutable(K key, List<MutableQuad> quads) {
quads.addAll(modelCache.getUnchecked(key).mutableQuads);
}
@Override
public ImmutableList<BakedQuad> bake(K key, VertexFormat format) {
ModelValue value = modelCache.getUnchecked(key);
return value.bake(format);
}
@Override
public void render(K key, WorldRenderer wr) {
for (MutableQuad q : modelCache.getUnchecked(key).mutableQuads) {
q.render(wr);
}
}
public interface IModelGenerator<T> {
List<MutableQuad> generate(T key);
}
private static class ModelValue {
private final ImmutableList<MutableQuad> mutableQuads;
// Identity because VertexFormat is mutable, so we cannot guarentee that nothing changes it.
private Map<VertexFormat, ImmutableList<BakedQuad>> bakedQuads = new IdentityHashMap<>();
public ModelValue(List<MutableQuad> quads) {
mutableQuads = ImmutableList.copyOf(quads);
}
public ImmutableList<BakedQuad> bake(VertexFormat format) {
if (!bakedQuads.containsKey(format)) {
ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
for (MutableQuad mutable : mutableQuads) {
builder.add(mutable.toUnpacked(format));
}
bakedQuads.put(format, builder.build());
}
return bakedQuads.get(format);
}
}
}