Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

仍然没写完 #2

Merged
merged 4 commits into from
Aug 20, 2023
Merged
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
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ subprojects {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-${minecraft_version}:${parchment_version}@zip")
}

implementation 'io.lettuce:lettuce-core:6.2.3.RELEASE'
shadowCommon 'io.lettuce:lettuce-core:6.2.3.RELEASE'
}
}

Expand Down
2 changes: 0 additions & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
dependencies {
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
modApi "${rootProject.architectury_id}:architectury:${rootProject.architectury_version}"

implementation 'io.lettuce:lettuce-core:6.2.3.RELEASE'
}

architectury {
Expand Down
12 changes: 10 additions & 2 deletions common/src/main/java/cn/zbx1425/worldcomment/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cn.zbx1425.worldcomment;

import cn.zbx1425.worldcomment.data.ServerWorldData;
import cn.zbx1425.worldcomment.data.sync.RedisSynchronizer;
import cn.zbx1425.worldcomment.data.sync.Synchronizer;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import cn.zbx1425.worldcomment.mixin.CreativeModeTabsAccessor;
import cn.zbx1425.worldcomment.network.PacketCollectionRequestC2S;
import cn.zbx1425.worldcomment.network.PacketEntryActionC2S;
import cn.zbx1425.worldcomment.network.PacketRegionRequestC2S;
Expand All @@ -10,10 +13,12 @@
import cn.zbx1425.worldcomment.util.RegistryObject;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.storage.LevelResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;

public class Main {

Expand All @@ -25,7 +30,7 @@ public class Main {
public static final RegistryObject<Item> ITEM_COMMENT_TOOL = new RegistryObject<>(CommentToolItem::new);

public static void init(RegistriesWrapper registries) {
registries.registerItem("comment_tool", ITEM_COMMENT_TOOL, CreativeModeTabs.TOOLS_AND_UTILITIES);
registries.registerItem("comment_tool", ITEM_COMMENT_TOOL, CreativeModeTabsAccessor.getTOOLS_AND_UTILITIES());

ServerPlatform.registerNetworkReceiver(
PacketRegionRequestC2S.IDENTIFIER, PacketRegionRequestC2S::handle);
Expand All @@ -38,7 +43,10 @@ public static void init(RegistriesWrapper registries) {

ServerPlatform.registerServerStartingEvent(server -> {
try {
DATABASE = new ServerWorldData(server);
//Todo: config inject here

Synchronizer synchronizer = new RedisSynchronizer("redis://192.168.1.148:6379/0", Path.of(server.getWorldPath(LevelResource.ROOT).toString(), "world-comment"), true);
DATABASE = new ServerWorldData(server, synchronizer);
DATABASE.load();
} catch (IOException e) {
LOGGER.error("Failed to open data storage", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.zbx1425.worldcomment.data;

import cn.zbx1425.worldcomment.data.network.ThumbImage;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.netty.buffer.Unpooled;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -111,4 +113,29 @@ public void updateInFile(RandomAccessFile oFile) throws IOException {
oFile.writeInt(like);
}

public String toJson() {
//Todo: wth is this
JsonObject json = new JsonObject();
json.addProperty("id", id);
json.addProperty("timestamp", timestamp);
json.addProperty("level", level.toString());
json.addProperty("region", region.toString());
json.addProperty("location", location.toString());
json.addProperty("initiator", initiator.toString());
json.addProperty("initiatorName", initiatorName);
json.addProperty("messageType", messageType);
json.addProperty("message", message);
json.addProperty("image", image.url);
json.addProperty("thumb", image.thumbUrl);
json.addProperty("deleted", deleted);
json.addProperty("like", like);
return json.toString();
}


public static CommentEntry fromJson(String json) {
Gson g = new Gson();

return g.fromJson(json, CommentEntry.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,35 @@ public class ServerWorldData {
public final CommentCache comments = new CommentCache();

public final FileSerializer fileSerializer;
public final RedisSynchronizer synchronizer;
public final Synchronizer synchronizer;

public ServerWorldData(MinecraftServer server) {
public ServerWorldData(MinecraftServer server, Synchronizer synchronizer) {
this.server = server;
this.basePath = Path.of(server.getWorldPath(LevelResource.ROOT).toString(), "world-comment");
fileSerializer = new FileSerializer(basePath);
//Todo: sync config inject
this.synchronizer = new RedisSynchronizer("", true, basePath)
this.synchronizer = synchronizer;
}

public void load() throws IOException {
if (isHost) {
fileSerializer.loadInto(comments);
synchronizer.kvWriteAll(comments.timeIndex);
} else {
//will cover all data
synchronizer.kvReadAllInto(comments);
}
}

public void insert(CommentEntry newEntry, boolean fromPeer) throws IOException {
public void insert(CommentEntry newEntry) throws IOException {
comments.insert(newEntry);
if (isHost) {
fileSerializer.insert(newEntry);
synchronizer.kvWriteEntry(newEntry);
}
if (!fromPeer) {
synchronizer.notifyInsert(newEntry);
}
fileSerializer.insert(newEntry);
synchronizer.notifyInsert(newEntry);
}

public void update(CommentEntry newEntry, boolean fromPeer) throws IOException {
public void update(CommentEntry newEntry) throws IOException {
CommentEntry trustedEntry = comments.update(newEntry);
if (isHost) {
fileSerializer.update(trustedEntry);
synchronizer.kvWriteEntry(trustedEntry);
}
if (!fromPeer) {
synchronizer.notifyUpdate(trustedEntry);
}
fileSerializer.update(trustedEntry);
synchronizer.notifyUpdate(newEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ public void insert(CommentEntry newEntry) throws IOException {
}
}

public void cover(CommentEntry newEntry, boolean append) throws IOException {
try {
Files.createDirectory(getLevelPath(newEntry.level));
} catch (FileAlreadyExistsException ignored) { }

Path targetFile = getLevelRegionPath(newEntry.level, newEntry.region);
try (FileOutputStream oStream = new FileOutputStream(targetFile.toFile(), append)) {
newEntry.writeFileStream(oStream);
}
}

public void update(CommentEntry existingEntry) throws IOException {
assert existingEntry.fileOffset > 0;
Path targetFile = getLevelRegionPath(existingEntry.level, existingEntry.region);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ public static String Updated(String ID) {
return "UPDATED#"+ID;
}

public static String Delete(String ID) {
return "DELETE#"+ID;
}

public static long IsUpdated(String Command) {
if (!Command.startsWith("UPDATED#")) {
return 0;
};

return Long.parseLong(Command.substring(8));
}

public static long IsUpdate(String Command) {
if (!Command.startsWith("UPDATE#")) {
return 0;
};

return Long.parseLong(Command.substring(7));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Map;

//Todo: Notice that this class is COMPLETELY SYNC currently
public class RedisChannelInterface {
public RedisChannelMessageQueue Queue = new RedisChannelMessageQueue();
private final StatefulRedisPubSubConnection<String, String> channel;
Expand Down Expand Up @@ -41,12 +42,20 @@ public void publish(String Channel, String Data) {
this.channel.sync().publish(Channel, Data);
}

public void del(String Key) {
this.instance.sync().del(Key);
}

public void hdel(String Key, String Field) {
this.instance.sync().hdel(Key, Field);
}

public synchronized void recvChannel(String[] Channels) {
if (StartedListening) {
this.stop();
}

RedisPubSubAdapter<String, String> adapter = new RedisPubSubAdapter<>() {
/*RedisPubSubAdapter<String, String> adapter = new RedisPubSubAdapter<>() {
@Override
public void message(String channel, String message) {
Queue.append(channel, message);
Expand All @@ -63,7 +72,7 @@ public void message(String channel, String message) {
Thread.onSpinWait();
}

this.channel.removeListener(adapter);
this.channel.removeListener(adapter);*/
}

public void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public synchronized void append(String Channel, String Data) {
}

public synchronized String next() {
if (CachedMessage.isEmpty()) {
return "";
}



String ret = CachedMessage.get(0);
CachedMessage.remove(0);
return ret;
Expand Down
Loading
Loading