/*
* Util.Settings.cs
* Authors: Adam Nabinger
* Copyright (c) 2007-2008 Cornell University
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Diagnostics;
using System.Security.Cryptography;
using Microsoft.Xna.Framework.Graphics;
namespace Util
{
///
/// Identifiers for the parallel port to use.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue")]
public enum ParallelPort
{
///
/// LPT1
///
Port1 = 0x378,
///
/// LPT2
///
Port2 = 0x278,
///
/// LPT3
///
Port3 = 0x3bc
}
///
/// Holder for global 'const' values that are loaded from the config files at run time, such as resolution.
///
public static class Settings
{
#region Constants
#if DEBUG
// String constant for the path name of the debug configuration script
private const string FileName = @"\Configs\DebugConfig.script";
#else
// String constant for the path name of the release configuration script
private const string LabFileName = @"\Configs\LabConfig.script";
// String constant for the path name of the release configuration script
private const string FileName = @"\Configs\Config.script";
#endif
#endregion
#region Fields
// Whether codes should be written out the parallel port.
private static bool parallelPortEnabled;
// Which parallel port to use.
private static ParallelPort selectedPort = ParallelPort.Port1;
// The width for the resolution.
private static int preferredWidth;
// The height for the resolution.
private static int preferredHeight;
// Whether the game should be full screen.
private static bool preferFullScreen;
// The width of the physical screen.
private static double physicalScreenWidth;
// The height of the physical screen.
private static double physicalScreenHeight;
// The distance of the viewer from the physical screen.
private static double physicalScreenDistance;
#endregion
#region Properties
///
/// Gets/Sets whether codes should be written out the parallel port.
///
public static bool ParallelPortEnabled
{
get { return parallelPortEnabled; }
set { parallelPortEnabled = value; }
}
///
/// Gets/Sets which parallel port to use.
///
public static ParallelPort ParallelPort
{
get { return selectedPort; }
set { selectedPort = value; }
}
///
/// Gets/Sets the width for the resolution.
///
public static int ResolutionWidth
{
get { return preferredWidth; }
set { preferredWidth = value; }
}
///
/// Gets/Sets the height for the resolution.
///
public static int ResolutionHeight
{
get { return preferredHeight; }
set { preferredHeight = value; }
}
///
/// Gets/Sets whether the game should be full screen.
///
public static bool FullScreen
{
get { return preferFullScreen; }
set { preferFullScreen = value; }
}
///
/// Gets/Sets the width of the physical screen.
///
public static double PhysicalScreenWidth
{
get { return physicalScreenWidth; }
set { physicalScreenWidth = value; }
}
///
/// Gets/Sets the height of the physical screen.
///
public static double PhysicalScreenHeight
{
get { return physicalScreenHeight; }
set { physicalScreenHeight = value; }
}
///
/// Gets/Sets the distance of the viewer from the physical screen.
///
public static double PhysicalScreenDistance
{
get { return physicalScreenDistance; }
set { physicalScreenDistance = value; }
}
#endregion
#region File I/O
///
/// Load the settings from the settings file in the given directory.
///
/// The directory in which to load the file from
[DebuggerHidden]
public static void LoadFrom(string directory)
{
#if !DEBUG
try
{
try
{
CSharpHelper.DoSimpleString(Decrypt.DecryptFile(directory + LabFileName));
return;
}
catch (CryptographicException)
{
// Failed to decrypt file
}
CSharpHelper.DoSimpleFile(directory + LabFileName);
return;
}
catch (Exception)
{
// Failed to load lab file
}
#endif
try
{
CSharpHelper.DoSimpleString(Decrypt.DecryptFile(directory + FileName));
return;
}
catch (CryptographicException)
{
// Failed to decrypt file
}
CSharpHelper.DoSimpleFile(directory + FileName);
}
#endregion
#region Update
///
/// Apply any Graphics Changes requested by the settings file.
///
/// Whether or not the changes were made.
public static bool ApplyGraphicsChanges()
{
bool changed = false;
if (preferredHeight != 0 && preferredWidth != 0 &&
(MainGame.TheGame.gdm.PreferredBackBufferHeight != preferredHeight ||
MainGame.TheGame.gdm.PreferredBackBufferWidth != preferredWidth))
{
changed = true;
MainGame.TheGame.gdm.PreferredBackBufferHeight = preferredHeight;
MainGame.TheGame.gdm.PreferredBackBufferWidth = preferredWidth;
MainGame.TheGame.gdm.ApplyChanges();
}
if (MainGame.TheGame.gdm.IsFullScreen != FullScreen)
{
changed = true;
MainGame.TheGame.gdm.ToggleFullScreen();
}
return changed;
}
#endregion
#region Setters
///
/// Set the resolution for the game to be displayed.
///
/// The preferred width.
/// The preferred height.
public static void SetResolution(int width, int height)
{
preferredWidth = width;
preferredHeight = height;
}
///
/// Whether the game should be full screen.
///
/// Whether full screen mode should be enabled.
public static void SetFullScreen(bool enable)
{
preferFullScreen = enable;
}
///
/// Set the parameters for the display. Values assumed to be centimeters.
///
/// Width of the screen.
/// Height of the screen.
/// Distance of the viewer from the screen.
public static void SetScreenDimensions(double physicalWidth, double physicalHeight, double physicalDistance)
{
physicalScreenWidth = physicalWidth;
physicalScreenHeight = physicalHeight;
physicalScreenDistance = physicalDistance;
#region Unused
// This code would check the given values against the information that could be read from the device.
// Currently unused due to incompatibility issues with certain systems.
//#if DEBUG
// bool set = false;
//#endif
//Console.WriteLine("The configured size of your display is " + physicalScreenWidth + "cm wide by " + physicalScreenHeight + "cm high (" + Math.Round((Math.Sqrt((physicalScreenWidth * physicalScreenWidth) + (physicalScreenHeight * physicalScreenHeight)) / 2.54), MidpointRounding.AwayFromZero) + "').");
// double reportedWidth;
// double reportedHeight;
// ExtendedDisplayIdentificationData.SetActiveDevice(GraphicsDevice);
// if (ExtendedDisplayIdentificationData.HasProfile)
// {
// reportedWidth = ExtendedDisplayIdentificationData.HorizontalSize / 10.0;
// reportedHeight = ExtendedDisplayIdentificationData.VerticalSize / 10.0;
// if (physicalScreenWidth != reportedWidth || physicalScreenHeight != reportedHeight)
// {
// Console.WriteLine("The EDID reported size of your display is " + reportedWidth + "cm wide by " + reportedHeight + "cm high (" + Math.Round((Math.Sqrt((reportedWidth * reportedWidth) + (reportedHeight * reportedHeight)) / 2.54), MidpointRounding.AwayFromZero) + "').");
// }
//#if DEBUG
// if (reportedHeight > 0 && reportedWidth > 0)
// {
// physicalScreenHeight = reportedHeight;
// physicalScreenWidth = reportedWidth;
// Console.WriteLine("Set parameters to EDID values.");
// set = true;
// }
//#endif
// }
// reportedWidth = DeviceInfo.PhysicalHorizontalSize / 10.0;
// reportedHeight = DeviceInfo.PhysicalVerticalSize / 10.0;
// if (physicalScreenWidth != reportedWidth || physicalScreenHeight != reportedHeight)
// {
// Console.WriteLine("The GDI reported size of your display is " + reportedWidth + "cm wide by " + reportedHeight + "cm high (" + Math.Round((Math.Sqrt((reportedWidth * reportedWidth) + (reportedHeight * reportedHeight)) / 2.54), MidpointRounding.AwayFromZero) + "').");
// }
//#if DEBUG
// if (reportedHeight > 0 && reportedWidth > 0 && !set)
// {
// physicalScreenHeight = reportedHeight;
// physicalScreenWidth = reportedWidth;
// Console.WriteLine("Set parameters to GDI values.");
// }
//#endif
#endregion
}
#endregion
}
}