/* * This file is a part of the SteamWar software. * * Copyright (C) 2025 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.steamwar.data; import de.steamwar.sql.SchematicType; import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; public class YMLWrapperImpl extends YMLWrapper { public static YMLWrapperImpl ofTyped(File file) { return new YMLWrapperImpl<>(file, ToSchematicType); } public static YMLWrapperImpl ofRaw(File file) { return new YMLWrapperImpl<>(file, ToString); } private final boolean canLoad; private final Map document; private YMLWrapperImpl(File file, Function schematicTypeMapper) { super(file, ToString, schematicTypeMapper, ToString); if (file == null || !file.exists()) { canLoad = false; document = new HashMap<>(); } else { canLoad = true; Yaml yaml = new Yaml(); try { document = yaml.load(new FileInputStream(file)); } catch (IOException e) { throw new SecurityException(e.getMessage(), e); } } } private YMLWrapperImpl(boolean canLoad, Map document, Function schematicTypeMapper) { super(null, ToString, schematicTypeMapper, ToString); this.canLoad = canLoad; this.document = document; } @Override public boolean canLoad() { return canLoad; } @Override public YMLWrapper with(String path) { if (document.containsKey(path)) { Object value = this.document.get(path); if (value instanceof Map) { return new YMLWrapperImpl(this.canLoad, (Map) value, schematicTypeMapper); } } return new YMLWrapperImpl(false, Collections.emptyMap(), schematicTypeMapper); } @Override public String getDefaultGameName() { return file.getName().replace(".yml", ""); } @Override public T get(String path, T defaultValue, Function mapper) { Object value = this.document.get(path); if (value == null) return defaultValue; try { return mapper.apply(value); } catch (ClassCastException e) { return defaultValue; } } @Override public List get(String path, Function> mapper) { Object value = this.document.get(path); if (value == null) return Collections.emptyList(); try { return Collections.unmodifiableList(mapper.apply(value)); } catch (ClassCastException e) { return Collections.emptyList(); } } @Override public List> getMapList(String path) { Object value = this.document.get(path); if (value instanceof List) { return Collections.unmodifiableList((List>) value); } else { return Collections.emptyList(); } } }