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

Dieser Commit ist enthalten in:
Jordan 2023-07-03 16:37:42 +01:00 committet von GitHub
Ursprung a0acbc7168
Commit d7cc65d2f2
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
4 geänderte Dateien mit 50 neuen und 40 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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