/* * Pixel Dungeon * Copyright (C) 2012-2015 Oleg Dolya * * Shattered Pixel Dungeon * Copyright (C) 2014-2019 Evan Debenham * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see */ package com.watabou.utils; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Preferences; public class GameSettings { //TODO might want to rename this file. this is the auto-generated name for android atm public static final String DEFAULT_PREFS_FILE = "ShatteredPixelDungeon"; private static Preferences prefs; private static Preferences get() { if (prefs == null) { prefs = Gdx.app.getPreferences( DEFAULT_PREFS_FILE ); } return prefs; } //allows setting up of preferences directly during game initialization public static void set( Preferences prefs ){ GameSettings.prefs = prefs; } public static boolean contains( String key ){ return get().contains( key ); } public static int getInt( String key, int defValue ) { return getInt(key, defValue, Integer.MIN_VALUE, Integer.MAX_VALUE); } public static int getInt( String key, int defValue, int min, int max ) { try { int i = get().getInteger( key, defValue ); if (i < min || i > max){ int val = (int)GameMath.gate(min, i, max); put(key, val); return val; } else { return i; } } catch (ClassCastException e) { //ShatteredPixelDungeon.reportException(e); put(key, defValue); return defValue; } } public static boolean getBoolean( String key, boolean defValue ) { try { return get().getBoolean(key, defValue); } catch (ClassCastException e) { //ShatteredPixelDungeon.reportException(e); put(key, defValue); return defValue; } } public static String getString( String key, String defValue ) { return getString(key, defValue, Integer.MAX_VALUE); } public static String getString( String key, String defValue, int maxLength ) { try { String s = get().getString( key, defValue ); if (s != null && s.length() > maxLength) { put(key, defValue); return defValue; } else { return s; } } catch (ClassCastException e) { //ShatteredPixelDungeon.reportException(e); put(key, defValue); return defValue; } } public static void put( String key, int value ) { get().putInteger(key, value); get().flush(); } public static void put( String key, boolean value ) { get().putBoolean(key, value); get().flush(); } public static void put( String key, String value ) { get().putString(key, value); get().flush(); } }