/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! 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.
*
* opsu! 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 opsu!. If not, see
* By default, sound is disabled on Linux due to possible driver issues. */ private static boolean disableSound = (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1); /** Whether or not to display non-English metadata. */ private static boolean showUnicode = false; /** Left and right game keys. */ private static int keyLeft = Keyboard.KEY_NONE, keyRight = Keyboard.KEY_NONE; // This class should not be instantiated. private Options() {} /** * Returns the target frame rate. * @return the target FPS */ public static int getTargetFPS() { return targetFPS[targetFPSindex]; } /** * Returns the master volume level. * @return the volume [0, 1] */ public static float getMasterVolume() { return masterVolume / 100f; } /** * Sets the master volume level (if within valid range). * @param container the game container * @param volume the volume [0, 1] */ public static void setMasterVolume(GameContainer container, float volume) { if (volume >= 0f && volume <= 1f) { masterVolume = (int) (volume * 100f); container.setMusicVolume(getMasterVolume() * getMusicVolume()); } } /** * Returns the default music volume. * @return the volume [0, 1] */ public static float getMusicVolume() { return musicVolume / 100f; } /** * Returns the default sound effect volume. * @return the sound volume [0, 1] */ public static float getEffectVolume() { return effectVolume / 100f; } /** * Returns the default hit sound volume. * @return the hit sound volume [0, 1] */ public static float getHitSoundVolume() { return hitSoundVolume / 100f; } /** * Returns the music offset time. * @return the offset (in milliseconds) */ public static int getMusicOffset() { return musicOffset; } /** * Returns the screenshot file format. * @return the file extension ("png", "jpg", "bmp") */ public static String getScreenshotFormat() { return screenshotFormat[screenshotFormatIndex]; } /** * Sets the container size and makes the window borderless if the container * size is identical to the screen resolution. *
* If the configured resolution is larger than the screen size, the smallest * available resolution will be used. * @param app the game container */ public static void setDisplayMode(Container app) { int screenWidth = app.getScreenWidth(); int screenHeight = app.getScreenHeight(); // check for larger-than-screen dimensions if (screenWidth < resolution.getWidth() || screenHeight < resolution.getHeight()) resolution = Resolution.RES_800_600; try { app.setDisplayMode(resolution.getWidth(), resolution.getHeight(), false); } catch (SlickException e) { ErrorHandler.error("Failed to set display mode.", e, true); } // set borderless window if dimensions match screen size boolean borderless = (screenWidth == resolution.getWidth() && screenHeight == resolution.getHeight()); System.setProperty("org.lwjgl.opengl.Window.undecorated", Boolean.toString(borderless)); } // /** // * Returns whether or not fullscreen mode is enabled. // * @return true if enabled // */ // public static boolean isFullscreen() { return fullscreen; } /** * Returns whether or not the FPS counter display is enabled. * @return true if enabled */ public static boolean isFPSCounterEnabled() { return showFPS; } /** * Returns whether or not hit lighting effects are enabled. * @return true if enabled */ public static boolean isHitLightingEnabled() { return showHitLighting; } /** * Returns whether or not combo burst effects are enabled. * @return true if enabled */ public static boolean isComboBurstEnabled() { return showComboBursts; } /** * Returns the port number to bind to. * @return the port */ public static int getPort() { return port; } /** * Returns whether or not the new cursor type is enabled. * @return true if enabled */ public static boolean isNewCursorEnabled() { return newCursor; } /** * Returns whether or not the main menu background should be the current track image. * @return true if enabled */ public static boolean isDynamicBackgroundEnabled() { return dynamicBackground; } /** * Returns whether or not to show perfect hit result bursts. * @return true if enabled */ public static boolean isPerfectHitBurstEnabled() { return showPerfectHit; } /** * Returns the background dim level. * @return the alpha level [0, 1] */ public static float getBackgroundDim() { return (100 - backgroundDim) / 100f; } /** * Returns whether or not to override the song background with the default playfield background. * @return true if forced */ public static boolean isDefaultPlayfieldForced() { return forceDefaultPlayfield; } /** * Returns whether or not beatmap skins are ignored. * @return true if ignored */ public static boolean isBeatmapSkinIgnored() { return ignoreBeatmapSkins; } /** * Returns the fixed circle size override, if any. * @return the CS value (0, 10], 0 if disabled */ public static float getFixedCS() { return fixedCS; } /** * Returns the fixed HP drain rate override, if any. * @return the HP value (0, 10], 0 if disabled */ public static float getFixedHP() { return fixedHP; } /** * Returns the fixed approach rate override, if any. * @return the AR value (0, 10], 0 if disabled */ public static float getFixedAR() { return fixedAR; } /** * Returns the fixed overall difficulty override, if any. * @return the OD value (0, 10], 0 if disabled */ public static float getFixedOD() { return fixedOD; } /** * Returns whether or not to render loading text in the splash screen. * @return true if enabled */ public static boolean isLoadVerbose() { return loadVerbose; } /** * Returns the track checkpoint time. * @return the checkpoint time (in ms) */ public static int getCheckpoint() { return checkpoint * 1000; } /** * Returns whether or not all sound effects are disabled. * @return true if disabled */ public static boolean isSoundDisabled() { return disableSound; } /** * Returns whether or not to use non-English metadata where available. * @return true if Unicode preferred */ public static boolean useUnicodeMetadata() { return showUnicode; } /** * Returns whether or not to play the theme song. * @return true if enabled */ public static boolean isThemSongEnabled() { return themeSongEnabled; } /** * Sets the track checkpoint time, if within bounds. * @param time the track position (in ms) * @return true if within bounds */ public static boolean setCheckpoint(int time) { if (time >= 0 && time < 3600) { checkpoint = time; return true; } return false; } /** * Returns the left game key. * @return the left key code */ public static int getGameKeyLeft() { if (keyLeft == Keyboard.KEY_NONE) keyLeft = Input.KEY_Z; return keyLeft; } /** * Returns the right game key. * @return the right key code */ public static int getGameKeyRight() { if (keyRight == Keyboard.KEY_NONE) keyRight = Input.KEY_X; return keyRight; } /** * Sets the left game key. * @param key the keyboard key */ public static void setGameKeyLeft(int key) { keyLeft = key; } /** * Sets the right game key. * @param key the keyboard key */ public static void setGameKeyRight(int key) { keyRight = key; } /** * Returns the beatmap directory. * If invalid, this will attempt to search for the directory, * and if nothing found, will create one. * @return the beatmap directory */ public static File getBeatmapDir() { if (beatmapDir != null && beatmapDir.isDirectory()) return beatmapDir; // search for directory for (int i = 0; i < BEATMAP_DIRS.length; i++) { beatmapDir = new File(BEATMAP_DIRS[i]); if (beatmapDir.isDirectory()) return beatmapDir; } beatmapDir.mkdir(); // none found, create new directory return beatmapDir; } /** * Returns the OSZ archive directory. * If invalid, this will create and return a "SongPacks" directory. * @return the OSZ archive directory */ public static File getOSZDir() { if (oszDir != null && oszDir.isDirectory()) return oszDir; oszDir = new File("SongPacks/"); oszDir.mkdir(); return oszDir; } /** * Returns the screenshot directory. * If invalid, this will return a "Screenshot" directory. * @return the screenshot directory */ public static File getScreenshotDir() { if (screenshotDir != null && screenshotDir.isDirectory()) return screenshotDir; screenshotDir = new File("Screenshots/"); return screenshotDir; } /** * Returns the current skin directory. * If invalid, this will create a "Skins" folder in the root directory. * @return the skin directory */ public static File getSkinDir() { if (skinDir != null && skinDir.isDirectory()) return skinDir; skinDir = new File("Skins/"); skinDir.mkdir(); return skinDir; } /** * Returns a dummy OsuFile containing the theme song. * @return the theme song OsuFile */ public static OsuFile getOsuTheme() { String[] tokens = themeString.split(","); if (tokens.length != 4) { ErrorHandler.error("Theme song string is malformed.", null, false); return null; } OsuFile osu = new OsuFile(null); osu.audioFilename = new File(tokens[0]); osu.title = tokens[1]; osu.artist = tokens[2]; try { osu.endTime = Integer.parseInt(tokens[3]); } catch (NumberFormatException e) { ErrorHandler.error("Theme song length is not a valid integer", e, false); return null; } return osu; } /** * Reads user options from the options file, if it exists. */ public static void parseOptions() { // if no config file, use default settings if (!OPTIONS_FILE.isFile()) { saveOptions(); return; } try (BufferedReader in = new BufferedReader(new FileReader(OPTIONS_FILE))) { String line; String name, value; int i; while ((line = in.readLine()) != null) { line = line.trim(); if (line.length() < 2 || line.charAt(0) == '#') continue; int index = line.indexOf('='); if (index == -1) continue; name = line.substring(0, index).trim(); value = line.substring(index + 1).trim(); switch (name) { case "BeatmapDirectory": beatmapDir = new File(value); break; case "OSZDirectory": oszDir = new File(value); break; case "ScreenshotDirectory": screenshotDir = new File(value); break; case "Skin": skinDir = new File(value); break; case "ThemeSong": themeString = value; break; case "Port": i = Integer.parseInt(value); if (i > 0 && i <= 65535) port = i; break; case "ScreenResolution": try { Resolution res = Resolution.valueOf(String.format("RES_%s", value.replace('x', '_'))); resolution = res; } catch (IllegalArgumentException e) {} break; // case "Fullscreen": // fullscreen = Boolean.parseBoolean(value); // break; case "FrameSync": i = Integer.parseInt(value); for (int j = 0; j < targetFPS.length; j++) { if (i == targetFPS[j]) targetFPSindex = j; } break; case "ScreenshotFormat": i = Integer.parseInt(value); if (i >= 0 && i < screenshotFormat.length) screenshotFormatIndex = i; break; case "FpsCounter": showFPS = Boolean.parseBoolean(value); break; case "ShowUnicode": showUnicode = Boolean.parseBoolean(value); break; case "NewCursor": newCursor = Boolean.parseBoolean(value); break; case "DynamicBackground": dynamicBackground = Boolean.parseBoolean(value); break; case "LoadVerbose": loadVerbose = Boolean.parseBoolean(value); break; case "VolumeUniversal": i = Integer.parseInt(value); if (i >= 0 && i <= 100) masterVolume = i; break; case "VolumeMusic": i = Integer.parseInt(value); if (i >= 0 && i <= 100) musicVolume = i; break; case "VolumeEffect": i = Integer.parseInt(value); if (i >= 0 && i <= 100) effectVolume = i; break; case "VolumeHitSound": i = Integer.parseInt(value); if (i >= 0 && i <= 100) hitSoundVolume = i; break; case "Offset": i = Integer.parseInt(value); if (i >= -500 && i <= 500) musicOffset = i; break; case "DisableSound": disableSound = Boolean.parseBoolean(value); break; case "keyOsuLeft": if ((value.length() == 1 && Character.isLetterOrDigit(value.charAt(0))) || (value.length() == 7 && value.startsWith("NUMPAD"))) { i = Keyboard.getKeyIndex(value); if (keyRight != i) keyLeft = i; } break; case "keyOsuRight": if ((value.length() == 1 && Character.isLetterOrDigit(value.charAt(0))) || (value.length() == 7 && value.startsWith("NUMPAD"))) { i = Keyboard.getKeyIndex(value); if (keyLeft != i) keyRight = i; } break; case "DimLevel": i = Integer.parseInt(value); if (i >= 0 && i <= 100) backgroundDim = i; break; case "ForceDefaultPlayfield": forceDefaultPlayfield = Boolean.parseBoolean(value); break; case "IgnoreBeatmapSkins": ignoreBeatmapSkins = Boolean.parseBoolean(value); break; case "HitLighting": showHitLighting = Boolean.parseBoolean(value); break; case "ComboBurst": showComboBursts = Boolean.parseBoolean(value); break; case "PerfectHit": showPerfectHit = Boolean.parseBoolean(value); break; case "FixedCS": fixedCS = Float.parseFloat(value); break; case "FixedHP": fixedHP = Float.parseFloat(value); break; case "FixedAR": fixedAR = Float.parseFloat(value); break; case "FixedOD": fixedOD = Float.parseFloat(value); break; case "Checkpoint": setCheckpoint(Integer.parseInt(value)); break; case "MenuMusic": themeSongEnabled = Boolean.parseBoolean(value); break; } } } catch (IOException e) { ErrorHandler.error(String.format("Failed to read file '%s'.", OPTIONS_FILE.getAbsolutePath()), e, false); } catch (NumberFormatException e) { Log.warn("Format error in options file.", e); return; } } /** * (Over)writes user options to a file. */ public static void saveOptions() { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(OPTIONS_FILE), "utf-8"))) { // header SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); String date = dateFormat.format(new Date()); writer.write("# opsu! configuration"); writer.newLine(); writer.write("# last updated on "); writer.write(date); writer.newLine(); writer.newLine(); // options writer.write(String.format("BeatmapDirectory = %s", getBeatmapDir().getAbsolutePath())); writer.newLine(); writer.write(String.format("OSZDirectory = %s", getOSZDir().getAbsolutePath())); writer.newLine(); writer.write(String.format("ScreenshotDirectory = %s", getScreenshotDir().getAbsolutePath())); writer.newLine(); writer.write(String.format("Skin = %s", getSkinDir().getAbsolutePath())); writer.newLine(); writer.write(String.format("ThemeSong = %s", themeString)); writer.newLine(); writer.write(String.format("Port = %d", port)); writer.newLine(); writer.write(String.format("ScreenResolution = %s", resolution.toString())); writer.newLine(); // writer.write(String.format("Fullscreen = %b", fullscreen)); // writer.newLine(); writer.write(String.format("FrameSync = %d", targetFPS[targetFPSindex])); writer.newLine(); writer.write(String.format("FpsCounter = %b", showFPS)); writer.newLine(); writer.write(String.format("ShowUnicode = %b", showUnicode)); writer.newLine(); writer.write(String.format("ScreenshotFormat = %d", screenshotFormatIndex)); writer.newLine(); writer.write(String.format("NewCursor = %b", newCursor)); writer.newLine(); writer.write(String.format("DynamicBackground = %b", dynamicBackground)); writer.newLine(); writer.write(String.format("LoadVerbose = %b", loadVerbose)); writer.newLine(); writer.write(String.format("VolumeUniversal = %d", masterVolume)); writer.newLine(); writer.write(String.format("VolumeMusic = %d", musicVolume)); writer.newLine(); writer.write(String.format("VolumeEffect = %d", effectVolume)); writer.newLine(); writer.write(String.format("VolumeHitSound = %d", hitSoundVolume)); writer.newLine(); writer.write(String.format("Offset = %d", musicOffset)); writer.newLine(); writer.write(String.format("DisableSound = %b", disableSound)); writer.newLine(); writer.write(String.format("keyOsuLeft = %s", Keyboard.getKeyName(getGameKeyLeft()))); writer.newLine(); writer.write(String.format("keyOsuRight = %s", Keyboard.getKeyName(getGameKeyRight()))); writer.newLine(); writer.write(String.format("DimLevel = %d", backgroundDim)); writer.newLine(); writer.write(String.format("ForceDefaultPlayfield = %b", forceDefaultPlayfield)); writer.newLine(); writer.write(String.format("IgnoreBeatmapSkins = %b", ignoreBeatmapSkins)); writer.newLine(); writer.write(String.format("HitLighting = %b", showHitLighting)); writer.newLine(); writer.write(String.format("ComboBurst = %b", showComboBursts)); writer.newLine(); writer.write(String.format("PerfectHit = %b", showPerfectHit)); writer.newLine(); writer.write(String.format(Locale.US, "FixedCS = %.1f", fixedCS)); writer.newLine(); writer.write(String.format(Locale.US, "FixedHP = %.1f", fixedHP)); writer.newLine(); writer.write(String.format(Locale.US, "FixedAR = %.1f", fixedAR)); writer.newLine(); writer.write(String.format(Locale.US, "FixedOD = %.1f", fixedOD)); writer.newLine(); writer.write(String.format("Checkpoint = %d", checkpoint)); writer.newLine(); writer.write(String.format("MenuMusic = %b", themeSongEnabled)); writer.newLine(); writer.close(); } catch (IOException e) { ErrorHandler.error(String.format("Failed to write to file '%s'.", OPTIONS_FILE.getAbsolutePath()), e, false); } } }