refactor: some changes that may help #2289 (#2307)

This commit is contained in:
Jordan 2023-07-03 16:37:42 +01:00 committed by GitHub
parent a0acbc7168
commit d7cc65d2f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 40 deletions

View File

@ -105,8 +105,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class LocalSession implements TextureHolder {
private static final transient int CUI_VERSION_UNINITIALIZED = -1;
public static transient int MAX_HISTORY_SIZE = 15;
public static int MAX_HISTORY_SIZE = 15;
private static final int CUI_VERSION_UNINITIALIZED = -1;
// Non-session related fields
private transient LocalConfiguration config;

View File

@ -45,8 +45,8 @@ import com.sk89q.worldedit.world.item.ItemTypes;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -354,7 +354,7 @@ public class SessionManager {
@Subscribe
public void onConfigurationLoad(ConfigurationLoadEvent event) {
LocalConfiguration config = event.getConfiguration();
File dir = new File(config.getWorkingDirectoryPath().toFile(), "sessions");
Path dir = config.getWorkingDirectoryPath().resolve("sessions");
store = new JsonFileSessionStore(dir);
}

View File

@ -36,6 +36,10 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkNotNull;
@ -49,20 +53,31 @@ public class JsonFileSessionStore implements SessionStore {
private static final Logger LOGGER = LogManagerCompat.getLogger();
private final Gson gson;
private final File dir;
private final Path dir;
/**
* Create a new session store.
*
* @param dir the directory
* @deprecated Use {@link #JsonFileSessionStore(Path)}
*/
@Deprecated
public JsonFileSessionStore(File dir) {
this(dir.toPath());
}
/**
* Create a new session store.
*
* @param dir the directory
*/
public JsonFileSessionStore(File dir) {
public JsonFileSessionStore(Path dir) {
checkNotNull(dir);
if (!dir.isDirectory()) {
if (!dir.mkdirs()) {
LOGGER.warn("Failed to create directory '" + dir.getPath() + "' for sessions");
}
try {
Files.createDirectories(dir);
} catch (IOException e) {
LOGGER.warn("Failed to create directory '" + dir + "' for sessions", e);
}
this.dir = dir;
@ -77,21 +92,19 @@ public class JsonFileSessionStore implements SessionStore {
* @param id the ID
* @return the file
*/
private File getPath(UUID id) {
private Path getPath(UUID id) {
checkNotNull(id);
return new File(dir, id + ".json");
return dir.resolve(id + ".json");
}
@Override
public LocalSession load(UUID id) throws IOException {
File file = getPath(id);
try (Closer closer = Closer.create()) {
FileReader fr = closer.register(new FileReader(file));
BufferedReader br = closer.register(new BufferedReader(fr));
LocalSession session = gson.fromJson(br, LocalSession.class);
Path file = getPath(id);
try (var reader = Files.newBufferedReader(file)) {
LocalSession session = gson.fromJson(reader, LocalSession.class);
if (session == null) {
LOGGER.warn("Loaded a null session from {}, creating new session", file);
if (!file.delete()) {
if (!Files.deleteIfExists(file)) {
LOGGER.warn("Failed to delete corrupted session {}", file);
}
session = new LocalSession();
@ -99,7 +112,7 @@ public class JsonFileSessionStore implements SessionStore {
return session;
} catch (JsonParseException e) {
throw new IOException(e);
} catch (FileNotFoundException e) {
} catch (NoSuchFileException e) {
return new LocalSession();
}
}
@ -107,29 +120,26 @@ public class JsonFileSessionStore implements SessionStore {
@Override
public void save(UUID id, LocalSession session) throws IOException {
checkNotNull(session);
File finalFile = getPath(id);
File tempFile = new File(finalFile.getParentFile(), finalFile.getName() + ".tmp");
Path finalFile = getPath(id);
Path tempFile = finalFile.getParent().resolve(finalFile.getFileName() + ".tmp");
try (Closer closer = Closer.create()) {
FileWriter fr = closer.register(new FileWriter(tempFile));
BufferedWriter bw = closer.register(new BufferedWriter(fr));
gson.toJson(session, bw);
try (var writer = Files.newBufferedWriter(tempFile)) {
gson.toJson(session, writer);
} catch (JsonIOException e) {
throw new IOException(e);
}
if (finalFile.exists()) {
if (!finalFile.delete()) {
LOGGER.warn("Failed to delete " + finalFile.getPath() + " so the .tmp file can replace it");
}
}
if (tempFile.length() == 0) {
if (Files.size(tempFile) == 0) {
throw new IllegalStateException("Gson wrote zero bytes");
}
if (!tempFile.renameTo(finalFile)) {
LOGGER.warn("Failed to rename temporary session file to " + finalFile.getPath());
try {
Files.move(
tempFile, finalFile,
StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING
);
} catch (IOException e) {
LOGGER.warn("Failed to rename temporary session file to " + finalFile.toAbsolutePath(), e);
}
}

View File

@ -43,7 +43,7 @@ public class ItemType implements RegistryItem, Keyed {
private final String id;
@SuppressWarnings("deprecation")
private final LazyReference<String> name = LazyReference.from(() -> {
private transient final LazyReference<String> name = LazyReference.from(() -> {
String name = GuavaUtil.firstNonNull(
WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS)
.getRegistries().getItemRegistry().getName(this),
@ -51,18 +51,18 @@ public class ItemType implements RegistryItem, Keyed {
);
return name.isEmpty() ? getId() : name;
});
private final LazyReference<Component> richName = LazyReference.from(() ->
private transient final LazyReference<Component> richName = LazyReference.from(() ->
WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS)
.getRegistries().getItemRegistry().getRichName(this)
);
private final LazyReference<ItemMaterial> itemMaterial = LazyReference.from(() ->
private transient final LazyReference<ItemMaterial> itemMaterial = LazyReference.from(() ->
WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.GAME_HOOKS)
.getRegistries().getItemRegistry().getMaterial(this)
);
//FAWE start
private BlockType blockType;
private boolean initBlockType;
private BaseItem defaultState;
private transient BlockType blockType;
private transient boolean initBlockType;
private transient BaseItem defaultState;
//FAWE end
public ItemType(String id) {