Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package net.ornithemc.osl.registries.impl.access;

import java.io.IOException;
import java.io.File;

public interface WorldStorageAccess {

void osl$registries$loadRegistryMappings() throws IOException;

void osl$registries$saveRegistryMappings() throws IOException;
File osl$registries$getDirectory();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ornithemc.osl.registries.impl.mixin.client;

import java.io.File;
import java.io.IOException;

import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -13,9 +14,14 @@
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.world.storage.WorldStorage;

import net.ornithemc.conditionalmixin.annotations.Conditional;
import net.ornithemc.conditionalmixin.annotations.Version;

import net.ornithemc.osl.registries.impl.access.WorldStorageAccess;
import net.ornithemc.osl.registries.impl.registry.SyncedRegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingStorage;

@Conditional(minecraftVersion = @Version(">=12w25a"))
@Mixin(IntegratedServer.class)
public class IntegratedServerMixin {

Expand All @@ -30,8 +36,10 @@ public class IntegratedServerMixin {
)
private void osl$registries$loadRegistryMappings(CallbackInfo ci, @Local WorldStorage storage) {
if (storage instanceof WorldStorageAccess) {
File dir = ((WorldStorageAccess) storage).osl$registries$getDirectory();

try {
((WorldStorageAccess) storage).osl$registries$loadRegistryMappings();
RegistryMappingStorage.loadRegistryMappings(dir);
} catch (IOException e) {
throw new RuntimeException("Exception initializing level", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,116 +1,23 @@
package net.ornithemc.osl.registries.impl.mixin.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.storage.AlphaWorldStorage;

import net.ornithemc.osl.registries.impl.Constants;
import net.ornithemc.osl.registries.impl.access.WorldStorageAccess;
import net.ornithemc.osl.registries.impl.registry.RegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.SyncedRegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingException;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingSource;
import net.ornithemc.osl.registries.impl.registry.sync.SyncedRegistriesNbtSerializer;

@Mixin(AlphaWorldStorage.class)
public class AlphaWorldStorageMixin implements WorldStorageAccess {

@Shadow @Final
private String name;

@Shadow
private File getDirectory() { return null; }
private File dir;

@Override
public void osl$registries$loadRegistryMappings() throws IOException {
File dir = this.getDirectory();
File file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME);

if (!this.readRegistryMappings(file, false)) {
file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_old");

if (!this.readRegistryMappings(file, true)) {
RegistriesImpl.LOGGER.debug("no registry mappings read for '" + this.name + "'");
}
}
}

@Unique
private boolean readRegistryMappings(File file, boolean throwOnException) throws IOException {
if (file.exists()) {
try (InputStream is = new FileInputStream(file)) {
NbtCompound nbt = NbtIo.readCompressed(is);

SyncedRegistriesNbtSerializer.deserialize(nbt, RegistryMappingSource.WORLD_SAVE);
SyncedRegistriesImpl.applyMappings();

return true;
} catch (IOException e) {
if (throwOnException) {
throw new IOException("Unable to read registry mappings for '" + this.name + "'", e);
} else {
RegistriesImpl.LOGGER.warn("error while reading registry mappings for '" + this.name + "'", e);
}
} catch (RegistryMappingException e) {
if (throwOnException) {
throw new IOException("Invalid registry mappings for '" + this.name + "'", e);
} else {
RegistriesImpl.LOGGER.warn("invalid registry mappings for '" + this.name + "'", e);
}
}
}

return false;
}

@Override
public void osl$registries$saveRegistryMappings() throws IOException {
File dir = this.getDirectory();
File file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME);
File tmp = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_tmp");
File backup = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_old");

this.writeRegistryMappings(file, tmp, backup);
}

@Unique
private void writeRegistryMappings(File file, File newFile, File oldFile) throws IOException {
NbtCompound nbt = new NbtCompound();

try {
SyncedRegistriesNbtSerializer.serialize(nbt);

try (OutputStream os = new FileOutputStream(newFile)) {
NbtIo.writeCompressed(nbt, os);
}

if (oldFile.exists()) {
oldFile.delete();
}

file.renameTo(oldFile);
if (file.exists()) {
file.delete();
}

newFile.renameTo(file);
if (newFile.exists()) {
newFile.delete();
}
} catch (IOException e) {
RegistriesImpl.LOGGER.warn("error while writing registry mappings for '" + this.name + "'", e);
}
public File osl$registries$getDirectory() {
return this.dir;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ornithemc.osl.registries.impl.mixin.common;

import java.io.File;
import java.io.IOException;

import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -15,6 +16,7 @@

import net.ornithemc.osl.registries.impl.access.WorldStorageAccess;
import net.ornithemc.osl.registries.impl.registry.RegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingStorage;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {
Expand All @@ -28,8 +30,10 @@ public class MinecraftServerMixin {
)
private void osl$registries$loadRegistryMappings(CallbackInfo ci, @Local WorldStorage storage) {
if (storage instanceof WorldStorageAccess) {
File dir = ((WorldStorageAccess) storage).osl$registries$getDirectory();

try {
((WorldStorageAccess) storage).osl$registries$loadRegistryMappings();
RegistryMappingStorage.loadRegistryMappings(dir);
} catch (IOException e) {
throw new RuntimeException("Exception initializing level", e);
}
Expand All @@ -49,8 +53,10 @@ public class MinecraftServerMixin {
WorldStorage storage = overworld.getStorage();

if (storage instanceof WorldStorageAccess) {
File dir = ((WorldStorageAccess) storage).osl$registries$getDirectory();

try {
((WorldStorageAccess) storage).osl$registries$saveRegistryMappings();
RegistryMappingStorage.saveRegistryMappings(dir);
} catch (IOException e) {
RegistriesImpl.LOGGER.error("Exception saving level", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.ornithemc.osl.registries.impl.mixin.common;
package net.ornithemc.osl.registries.impl.registry.sync;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -7,48 +7,28 @@
import java.io.InputStream;
import java.io.OutputStream;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.storage.AlphaWorldStorage;

import net.ornithemc.osl.registries.impl.Constants;
import net.ornithemc.osl.registries.impl.access.WorldStorageAccess;
import net.ornithemc.osl.registries.impl.registry.RegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.SyncedRegistriesImpl;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingException;
import net.ornithemc.osl.registries.impl.registry.sync.RegistryMappingSource;
import net.ornithemc.osl.registries.impl.registry.sync.SyncedRegistriesNbtSerializer;

@Mixin(AlphaWorldStorage.class)
public class AlphaWorldStorageMixin implements WorldStorageAccess {

@Shadow @Final
private String name;

@Shadow
private File m_52081685() { return null; }
public final class RegistryMappingStorage {

@Override
public void osl$registries$loadRegistryMappings() throws IOException {
File dir = this.m_52081685();
public static void loadRegistryMappings(File dir) throws IOException {
File file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME);

if (!this.readRegistryMappings(file, false)) {
if (!readRegistryMappings(dir, file, false)) {
file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_old");

if (!this.readRegistryMappings(file, true)) {
RegistriesImpl.LOGGER.debug("no registry mappings read for '" + this.name + "'");
if (!readRegistryMappings(dir, file, true)) {
RegistriesImpl.LOGGER.debug("no registry mappings read for '" + dir.getName() + "'");
}
}
}

@Unique
private boolean readRegistryMappings(File file, boolean throwOnException) throws IOException {
private static boolean readRegistryMappings(File dir, File file, boolean throwOnException) throws IOException {
if (file.exists()) {
try (InputStream is = new FileInputStream(file)) {
NbtCompound nbt = NbtIo.readCompressed(is);
Expand All @@ -59,34 +39,31 @@ private boolean readRegistryMappings(File file, boolean throwOnException) throws
return true;
} catch (IOException e) {
if (throwOnException) {
throw new IOException("Unable to read registry mappings for '" + this.name + "'", e);
throw new IOException("Unable to read registry mappings for '" + dir.getName() + "'", e);
} else {
RegistriesImpl.LOGGER.warn("error while reading registry mappings for '" + this.name + "'", e);
RegistriesImpl.LOGGER.warn("error while reading registry mappings for '" + dir.getName() + "'", e);
}
} catch (RegistryMappingException e) {
if (throwOnException) {
throw new IOException("Invalid registry mappings for '" + this.name + "'", e);
throw new IOException("Invalid registry mappings for '" + dir.getName() + "'", e);
} else {
RegistriesImpl.LOGGER.warn("invalid registry mappings for '" + this.name + "'", e);
RegistriesImpl.LOGGER.warn("invalid registry mappings for '" + dir.getName() + "'", e);
}
}
}

return false;
}

@Override
public void osl$registries$saveRegistryMappings() throws IOException {
File dir = this.m_52081685();
public static void saveRegistryMappings(File dir) throws IOException {
File file = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME);
File tmp = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_tmp");
File backup = new File(dir, Constants.REGISTRY_MAPPINGS_FILE_NAME + "_old");

this.writeRegistryMappings(file, tmp, backup);
writeRegistryMappings(dir, file, tmp, backup);
}

@Unique
private void writeRegistryMappings(File file, File newFile, File oldFile) throws IOException {
private static void writeRegistryMappings(File dir, File file, File newFile, File oldFile) throws IOException {
NbtCompound nbt = new NbtCompound();

try {
Expand All @@ -110,7 +87,7 @@ private void writeRegistryMappings(File file, File newFile, File oldFile) throws
newFile.delete();
}
} catch (IOException e) {
RegistriesImpl.LOGGER.warn("error while writing registry mappings for '" + this.name + "'", e);
RegistriesImpl.LOGGER.warn("error while writing registry mappings for '" + dir.getName() + "'", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"minVersion": "0.8",
"package": "net.ornithemc.osl.registries.impl.mixin",
"compatibilityLevel": "JAVA_8",
"plugin": "net.ornithemc.osl.registries.impl.RegistriesMixinPlugin",
"plugin": "net.ornithemc.conditionalmixin.mixin.ConditionalMixinPlugin",
"mixins": [
"common.AlphaWorldStorageMixin",
"common.MinecraftServerMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.text.LiteralText;

import net.ornithemc.osl.core.impl.util.MinecraftVersion;
import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer;
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
import net.ornithemc.osl.networking.api.client.ClientPlayNetworking;
Expand All @@ -13,6 +14,8 @@

public final class RegistriesEntrypoint implements ClientModInitializer {

public static final boolean OLD_CRUDE_IDENTITY_HASH_MAP = MinecraftVersion.resolve().compareTo("1.7.10-pre3") < 0 || (MinecraftVersion.resolve().compareTo("14w02a") >= 0 && MinecraftVersion.resolve().compareTo("14w25a") < 0);

@Override
public void initClient() {
ClientPlayNetworking.registerListener(Constants.OSL_REGISTRY_SYNC_CHANNEL, (context, buffer) -> {
Expand Down
Loading
Loading