mirror of
https://github.com/array-in-a-matrix/brainwine.git
synced 2025-04-02 11:11:58 -04:00
Switched to static JsonHelper class from numerous ObjectMappers
This commit is contained in:
parent
a95a1c7222
commit
87409e4307
10 changed files with 25 additions and 62 deletions
|
@ -16,10 +16,7 @@ import org.yaml.snakeyaml.LoaderOptions;
|
|||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.command.CommandManager;
|
||||
import brainwine.gameserver.entity.player.Player;
|
||||
|
@ -27,12 +24,12 @@ import brainwine.gameserver.item.Item;
|
|||
import brainwine.gameserver.item.ItemRegistry;
|
||||
import brainwine.gameserver.util.MapHelper;
|
||||
import brainwine.gameserver.util.VersionUtils;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class GameConfiguration {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final Map<String, Object> baseConfig = new HashMap<String, Object>();
|
||||
private static final Map<String, Map<String, Object>> configUpdates = new HashMap<>();
|
||||
private static final Map<String, Map<String, Object>> versionedConfigs = new HashMap<>();
|
||||
|
@ -41,9 +38,6 @@ public class GameConfiguration {
|
|||
public static void init() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
logger.info("Loading game configuration ...");
|
||||
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
|
||||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
|
||||
LoaderOptions options = new LoaderOptions();
|
||||
options.setMaxAliasesForCollections(Short.MAX_VALUE);
|
||||
yaml = new Yaml(options);
|
||||
|
@ -157,12 +151,8 @@ public class GameConfiguration {
|
|||
|
||||
// Register item
|
||||
if(config.containsKey("code")) {
|
||||
InjectableValues.Std injectableValues = new InjectableValues.Std();
|
||||
injectableValues.addValue("name", name);
|
||||
mapper.setInjectableValues(injectableValues);
|
||||
|
||||
try {
|
||||
Item item = mapper.readValue(mapper.writer().writeValueAsString(config), Item.class);
|
||||
Item item = JsonHelper.readValue(config, Item.class, new InjectableValues.Std().addValue("name", name));
|
||||
ItemRegistry.registerItem(item);
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.fatal("Failed to register item {}", name, e);
|
||||
|
|
|
@ -13,11 +13,11 @@ import org.apache.logging.log4j.Logger;
|
|||
import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.GameServer;
|
||||
import brainwine.gameserver.server.pipeline.Connection;
|
||||
import brainwine.gameserver.zone.Zone;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
public class PlayerManager {
|
||||
|
||||
|
@ -51,13 +51,9 @@ public class PlayerManager {
|
|||
|
||||
private void loadPlayer(File file) {
|
||||
String id = file.getName().replace(".json", "");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
InjectableValues.Std injectableValues = new InjectableValues.Std();
|
||||
injectableValues.addValue("documentId", id);
|
||||
mapper.setInjectableValues(injectableValues);
|
||||
|
||||
try {
|
||||
Player player = mapper.readValue(file, Player.class);
|
||||
Player player = JsonHelper.readValue(file, Player.class, new InjectableValues.Std().addValue("documentId", id));
|
||||
|
||||
if(player.getZone() == null) {
|
||||
player.setZone(GameServer.getInstance().getZoneManager().getRandomZone());
|
||||
|
@ -85,10 +81,9 @@ public class PlayerManager {
|
|||
|
||||
public void savePlayer(Player player) {
|
||||
File file = new File("players", player.getDocumentId() + ".json");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
try {
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(file, player);
|
||||
JsonHelper.writeValue(file, player);
|
||||
} catch(Exception e) {
|
||||
logger.error("Could not save player id {}", player.getDocumentId(), e);
|
||||
}
|
||||
|
|
|
@ -13,10 +13,10 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.util.WeightedList;
|
||||
import brainwine.gameserver.zone.Biome;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
public class LootManager {
|
||||
|
||||
|
@ -29,7 +29,6 @@ public class LootManager {
|
|||
|
||||
private void loadLootTables() {
|
||||
logger.info("Loading loot tables ...");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
File file = new File("loottables.json");
|
||||
|
||||
try {
|
||||
|
@ -37,7 +36,7 @@ public class LootManager {
|
|||
Files.copy(LootManager.class.getResourceAsStream("/loottables.json"), file.toPath());
|
||||
}
|
||||
|
||||
Map<String, List<Loot>> loot = mapper.readValue(file, new TypeReference<Map<String, List<Loot>>>(){});
|
||||
Map<String, List<Loot>> loot = JsonHelper.readValue(file, new TypeReference<Map<String, List<Loot>>>(){});
|
||||
lootTables.putAll(loot);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to load loot tables", e);
|
||||
|
|
|
@ -13,15 +13,13 @@ import org.msgpack.unpacker.BufferUnpacker;
|
|||
import org.reflections.Reflections;
|
||||
import org.reflections.scanners.ResourcesScanner;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.msgpack.MessagePackHelper;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
public class PrefabManager {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private final File dataDir = new File("prefabs");
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final Map<String, Prefab> prefabs = new HashMap<>();
|
||||
|
||||
public PrefabManager() {
|
||||
|
@ -65,7 +63,7 @@ public class PrefabManager {
|
|||
File configFile = new File(file, "config.json");
|
||||
|
||||
try {
|
||||
Prefab prefab = mapper.readValue(configFile, Prefab.class);
|
||||
Prefab prefab = JsonHelper.readValue(configFile, Prefab.class);
|
||||
BufferUnpacker unpacker = MessagePackHelper.readFile(new File(file, "blocks.cmp"));
|
||||
unpacker.read(prefab);
|
||||
unpacker.close();
|
||||
|
@ -89,7 +87,7 @@ public class PrefabManager {
|
|||
File outputDir = new File(dataDir, name);
|
||||
outputDir.mkdirs();
|
||||
MessagePackHelper.writeToFile(new File(outputDir, "blocks.cmp"), structure);
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(outputDir, "config.json"), structure);
|
||||
JsonHelper.writeValue(new File(outputDir, "config.json"), structure);
|
||||
}
|
||||
|
||||
public Prefab getPrefab(String name) {
|
||||
|
|
|
@ -7,19 +7,17 @@ import java.util.List;
|
|||
|
||||
import org.msgpack.packer.BufferPacker;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.msgpack.MessagePackHelper;
|
||||
import brainwine.gameserver.server.Message;
|
||||
import brainwine.gameserver.server.NetworkRegistry;
|
||||
import brainwine.gameserver.util.ZipUtils;
|
||||
import brainwine.shared.JsonHelper;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
public class MessageEncoder extends MessageToByteEncoder<Message> {
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private final Connection connection;
|
||||
|
||||
public MessageEncoder(Connection connection) {
|
||||
|
@ -45,7 +43,7 @@ public class MessageEncoder extends MessageToByteEncoder<Message> {
|
|||
data.add(field.get(in));
|
||||
}
|
||||
|
||||
bytes = mapper.writer().writeValueAsString(data).getBytes();
|
||||
bytes = JsonHelper.writeValueAsBytes(data);
|
||||
} else {
|
||||
BufferPacker packer = MessagePackHelper.createBufferPacker();
|
||||
in.pack(packer);
|
||||
|
|
|
@ -10,17 +10,17 @@ import org.apache.logging.log4j.Logger;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class MapHelper {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public static <K, V> Map<K, V> copy(Map<K, V> map) {
|
||||
try {
|
||||
return mapper.readValue(mapper.writeValueAsString(map), new TypeReference<Map<K, V>>(){});
|
||||
return JsonHelper.readValue(map, new TypeReference<Map<K, V>>(){});
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Map copy failed", e);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ import com.fasterxml.jackson.annotation.JacksonInject;
|
|||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.GameServer;
|
||||
import brainwine.gameserver.entity.Entity;
|
||||
|
@ -53,6 +52,7 @@ import brainwine.gameserver.server.messages.ZoneExploredMessage;
|
|||
import brainwine.gameserver.server.messages.ZoneStatusMessage;
|
||||
import brainwine.gameserver.util.MapHelper;
|
||||
import brainwine.gameserver.util.MathUtils;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
@JsonIncludeProperties({"name", "biome", "width", "height"})
|
||||
public class Zone {
|
||||
|
@ -142,7 +142,6 @@ public class Zone {
|
|||
}
|
||||
|
||||
public void load() throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
pendingSunlight.clear();
|
||||
File dataDir = new File("zones", documentId);
|
||||
File shapeFile = new File(dataDir, "shape.cmp");
|
||||
|
@ -151,19 +150,17 @@ public class Zone {
|
|||
unpacker.read(sunlight);
|
||||
pendingSunlight.addAll(Arrays.asList(unpacker.read(Integer[].class)));
|
||||
unpacker.read(chunksExplored);
|
||||
setMetaBlocks(mapper.readerForListOf(MetaBlock.class).readValue(new File(dataDir, "metablocks.json")));
|
||||
setMetaBlocks(JsonHelper.readList(new File(dataDir, "metablocks.json"), MetaBlock.class));
|
||||
indexDungeons();
|
||||
}
|
||||
|
||||
public void save() throws Exception {
|
||||
File dataDir = new File("zones", documentId);
|
||||
dataDir.mkdirs();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(dataDir, "config.json"), this);
|
||||
chunkManager.saveModifiedChunks();
|
||||
removeInactiveChunks();
|
||||
MessagePackHelper.writeToFile(new File(dataDir, "shape.cmp"), surface, sunlight, pendingSunlight, chunksExplored);
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(dataDir, "metablocks.json"), metaBlocks.values());
|
||||
JsonHelper.writeValue(new File(dataDir, "metablocks.json"), metaBlocks.values());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,10 +15,10 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.fasterxml.jackson.databind.InjectableValues;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.zone.gen.AsyncZoneGenerator;
|
||||
import brainwine.gameserver.zone.gen.StaticZoneGenerator;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
public class ZoneManager {
|
||||
|
||||
|
@ -78,14 +78,10 @@ public class ZoneManager {
|
|||
|
||||
private void loadZone(File file) {
|
||||
String id = file.getName();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
InjectableValues.Std injectableValues = new InjectableValues.Std();
|
||||
injectableValues.addValue("documentId", id);
|
||||
mapper.setInjectableValues(injectableValues);
|
||||
|
||||
try {
|
||||
File configFile = new File(file, "config.json");
|
||||
Zone zone = mapper.readValue(configFile, Zone.class);
|
||||
Zone zone = JsonHelper.readValue(configFile, Zone.class, new InjectableValues.Std().addValue("documentId", id));
|
||||
zone.load();
|
||||
putZone(zone);
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -9,13 +9,11 @@ import java.util.Map.Entry;
|
|||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.zone.gen.models.CaveType;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
/**
|
||||
* It's a bit hack-ish, but it works.
|
||||
|
@ -25,16 +23,13 @@ public class CaveDecoratorListDeserializer extends JsonDeserializer<List<CaveDec
|
|||
@Override
|
||||
public List<CaveDecorator> deserialize(JsonParser parser, DeserializationContext ctx) throws IOException, JsonProcessingException {
|
||||
List<CaveDecorator> list = new ArrayList<>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
|
||||
JsonNode node = parser.readValueAsTree();
|
||||
Iterator<Entry<String, JsonNode>> it = node.fields();
|
||||
|
||||
while(it.hasNext()) {
|
||||
Entry<String, JsonNode> entry = it.next();
|
||||
CaveType type = mapper.readValue(String.format("\"%s\"", entry.getKey()), CaveType.class);
|
||||
CaveDecorator decorator = mapper.readValue(entry.getValue().toString(), type.getDecoratorType());
|
||||
CaveType type = JsonHelper.readValue(String.format("\"%s\"", entry.getKey()), CaveType.class);
|
||||
CaveDecorator decorator = JsonHelper.readValue(entry.getValue().toString(), type.getDecoratorType());
|
||||
list.add(decorator);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,12 +12,10 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import brainwine.gameserver.zone.Biome;
|
||||
import brainwine.gameserver.zone.Zone;
|
||||
import brainwine.shared.JsonHelper;
|
||||
|
||||
public class StaticZoneGenerator {
|
||||
|
||||
|
@ -56,16 +54,13 @@ public class StaticZoneGenerator {
|
|||
public static void init() {
|
||||
logger.info("Loading zone generator configurations ...");
|
||||
File file = new File("generators.json");
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
|
||||
|
||||
try {
|
||||
if(!file.exists()) {
|
||||
Files.copy(StaticZoneGenerator.class.getResourceAsStream("/generators.json"), file.toPath());
|
||||
}
|
||||
|
||||
Map<Biome, GeneratorConfig> configs = mapper.readValue(new File("generators.json"), new TypeReference<Map<Biome, GeneratorConfig>>(){});
|
||||
Map<Biome, GeneratorConfig> configs = JsonHelper.readValue(new File("generators.json"), new TypeReference<Map<Biome, GeneratorConfig>>(){});
|
||||
|
||||
for(Entry<Biome, GeneratorConfig> entry : configs.entrySet()) {
|
||||
generators.put(entry.getKey(), new ZoneGenerator(entry.getValue()));
|
||||
|
|
Loading…
Add table
Reference in a new issue