-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to merge duplicate resources in a pack
- Loading branch information
Showing
18 changed files
with
385 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
common/src/main/java/net/infumia/pack/FileResourceMerger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.infumia.pack; | ||
|
||
import java.util.Collection; | ||
import team.unnamed.creative.atlas.Atlas; | ||
import team.unnamed.creative.model.Model; | ||
|
||
/** | ||
* Represents a file resource merger that can merge a collection of file resources. | ||
*/ | ||
public interface FileResourceMerger { | ||
/** | ||
* Merges a collection of {@link FileResources} into a unified resource. | ||
* <p> | ||
* Tries to merge duplicate {@link Atlas}s and {@link Model}s. | ||
* | ||
* @param resources the collection of {@link FileResources} to merge. Cannot be null. | ||
* | ||
* @return the merged collection of {@link FileResource}s. Cannot be null. | ||
*/ | ||
Collection<FileResource> merge(Collection<FileResource> resources); | ||
} |
97 changes: 97 additions & 0 deletions
97
common/src/main/java/net/infumia/pack/FileResourceMergerDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package net.infumia.pack; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.stream.Collectors; | ||
import net.kyori.adventure.key.Key; | ||
import team.unnamed.creative.atlas.Atlas; | ||
import team.unnamed.creative.model.Model; | ||
|
||
final class FileResourceMergerDefault implements FileResourceMerger { | ||
|
||
static final FileResourceMerger INSTANCE = new FileResourceMergerDefault(); | ||
|
||
private FileResourceMergerDefault() {} | ||
|
||
@Override | ||
public Collection<FileResource> merge(final Collection<FileResource> resources) { | ||
final Collection<FileResource> simplified = new HashSet<>(resources.size()); | ||
for (final FileResource resource : resources) { | ||
simplified.addAll(this.simplify(resource)); | ||
} | ||
|
||
final MultiMap<Key, Atlas> atlases = new MultiMap<>(); | ||
final MultiMap<Key, Model> models = new MultiMap<>(); | ||
for (final FileResource resource : simplified) { | ||
if (resource instanceof FileResourceAtlas) { | ||
final Atlas atlas = ((FileResourceAtlas) resource).atlas; | ||
atlases.put(atlas.key(), atlas); | ||
} else if (resource instanceof FileResourceModel) { | ||
final Model model = ((FileResourceModel) resource).model; | ||
models.put(model.key(), model); | ||
} | ||
// TODO: portlek, Merge more things. | ||
} | ||
|
||
final Collection<Atlas> mergedAtlases = new HashSet<>(atlases.keys().size()); | ||
for (final Key key : atlases.keys()) { | ||
final Collection<Atlas> duplicates = atlases.get(key); | ||
final Atlas merged = Atlas.atlas() | ||
.key(key) | ||
.sources( | ||
duplicates | ||
.stream() | ||
.map(Atlas::sources) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()) | ||
) | ||
.build(); | ||
mergedAtlases.add(merged); | ||
} | ||
|
||
final Collection<Model> mergedModels = new HashSet<>(models.keys().size()); | ||
for (final Key key : models.keys()) { | ||
final Collection<Model> duplicates = models.get(key); | ||
final Model.Builder builder = Model.model().key(key); | ||
// TODO: portlek, Find a way to merge these too. | ||
for (final Model duplicate : duplicates) { | ||
builder | ||
.guiLight(duplicate.guiLight()) | ||
.elements(duplicate.elements()) | ||
.parent(duplicate.parent()) | ||
.ambientOcclusion(duplicate.ambientOcclusion()) | ||
.display(duplicate.display()) | ||
.textures(duplicate.textures()); | ||
} | ||
builder.overrides( | ||
duplicates | ||
.stream() | ||
.map(Model::overrides) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toList()) | ||
); | ||
mergedModels.add(builder.build()); | ||
} | ||
|
||
final Collection<FileResource> mergedResources = mergedAtlases | ||
.stream() | ||
.map(FileResources::atlas) | ||
.collect(Collectors.toSet()); | ||
mergedResources.addAll( | ||
mergedModels.stream().map(FileResources::model).collect(Collectors.toSet()) | ||
); | ||
return mergedResources; | ||
} | ||
|
||
private Collection<FileResource> simplify(final FileResource resource) { | ||
if (resource instanceof FileResourceAll) { | ||
return ((FileResourceAll) resource).resources.stream() | ||
.map(this::simplify) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toSet()); | ||
} else { | ||
return Collections.singleton(resource); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
common/src/main/java/net/infumia/pack/FileResourceMergers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.infumia.pack; | ||
|
||
/** | ||
* Utility class for providing various implementations of {@link FileResourceMerger}. | ||
*/ | ||
public final class FileResourceMergers { | ||
|
||
/** | ||
* Returns a simple file resource merger implementation. | ||
* | ||
* @return A {@link FileResourceMerger} representing the simple file resource merger. | ||
*/ | ||
public static FileResourceMerger simple() { | ||
return FileResourceMergerDefault.INSTANCE; | ||
} | ||
|
||
private FileResourceMergers() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.infumia.pack; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
|
||
final class MultiMap<K, V> { | ||
|
||
private final Map<K, Collection<V>> map = new HashMap<>(); | ||
|
||
MultiMap() {} | ||
|
||
void put(final K key, final V value) { | ||
this.map.computeIfAbsent(key, __ -> new HashSet<>()).add(value); | ||
} | ||
|
||
Collection<V> get(final K key) { | ||
return this.map.get(key); | ||
} | ||
|
||
boolean remove(final K key, final V value) { | ||
final Collection<V> values = this.map.get(key); | ||
if (values == null) { | ||
return false; | ||
} | ||
final boolean removed = values.remove(value); | ||
if (values.isEmpty()) { | ||
this.map.remove(key); | ||
} | ||
return removed; | ||
} | ||
|
||
void clear() { | ||
this.map.clear(); | ||
} | ||
|
||
Collection<K> keys() { | ||
return this.map.keySet(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.