All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.yildizgames.common.configuration.BaseConfiguration Maven / Gradle / Ivy

Go to download

Common Configuration library for the yildiz project, contains utilities to handle configuration.

The newest version!
/*
 This file is part of the Yildiz-Engine project, licenced under the MIT License  (MIT)
 Copyright (c) 2021-2024 Grégory Van den Borre
 More infos available: https://engine.yildiz-games.be
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright
 notice and this permission notice shall be included in all copies or substantial portions of the  Software.
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package be.yildizgames.common.configuration;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

/**
 * @author Grégory Van den Borre
 */
public class BaseConfiguration implements LanguageConfiguration {

    public static final String FRENCH = "Français";

    public static final String ENGLISH = "English";

    public static final String TURKISH = "Türkçe";

    public static final String EULA_ACCEPTED = "eula.accepted";

    public static final String LANGUAGE = "language";

    private final List listeners = new ArrayList<>();

    private final List localeChangedListeners = new ArrayList<>();

    private final List supportedLocales = new ArrayList<>();

    private final Properties properties;

    private final String file;

    public BaseConfiguration(Properties properties) {
        this(properties, List.of(Locale.ENGLISH));
    }

    public BaseConfiguration(Properties properties, String file, List supportedLocales) {
        super();
        this.file = file;
        this.properties = properties;
        this.supportedLocales.addAll(supportedLocales);
    }

    public BaseConfiguration(Properties properties, List supportedLocales) {
        this(properties, "config/configuration.properties", supportedLocales);
    }

    protected final String get(String key) {
        return this.properties.getProperty(key);
    }

    protected final String get(String key, String defaultValue) {
        return this.properties.getProperty(key, defaultValue);
    }

    public final void addLocaleChangedListener(LocaleChangedListener listener) {
        this.localeChangedListeners.add(listener);
    }

    public final void addConfigurationChangedListener(ConfigurationChangeListener listener) {
        this.listeners.add(listener);
    }

    @Override
    public final Locale getLocale() {
        return Locale.forLanguageTag(this.properties.getProperty(LANGUAGE, "fr"));
    }

    @Override
    public final List getSupportedLocale() {
        return Collections.unmodifiableList(this.supportedLocales);
    }

    @Override
    public final void setLocale(Locale locale) {
        this.properties.setProperty(LANGUAGE, locale.getLanguage());
        this.store();
        this.localeChangedListeners.forEach(l -> l.setLanguage(locale));
    }

    protected final void store() {
        try (var buf = Files.newBufferedWriter(Path.of(this.file),
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
            this.properties.store(buf, "Properties");
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    protected final void updateValue(String changedProperty, String newValue) {
        this.properties.setProperty(changedProperty, newValue);
        this.store();
        this.listeners.stream()
                .filter(l -> l.getUsedConfigurationProperties().contains(changedProperty))
                .forEach(l -> l.onChange(changedProperty, newValue));
    }

    public final boolean isEulaAccepted(String expectedHash) {
        return this.properties.getProperty(EULA_ACCEPTED).equals(expectedHash);
    }

    /**
     * Set the EULA as accepted.
     * @param hash Hash of the eula to ensure it is the right one accepted.
     * @return True if process went well, false otherwise
     */
    public final boolean setEulaAccepted(String hash) {
        try {
            this.properties.setProperty(EULA_ACCEPTED, hash);
            this.store();
            return true;
        } catch (IllegalStateException e) {
            System.getLogger(this.getClass().getName()).log(System.Logger.Level.ERROR, e);
            return false;
        }
    }

    /**
     * Set the EULA as not accepted.
     */
    public final void setEulaNotAccepted() {
        this.properties.setProperty(EULA_ACCEPTED, "0");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy