/*
* Config.cs - Manages the Level9.Net configuration.
*
* Copyright (C) 2004 - 2011 Andreas Scherrer
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace Level9
{
///
/// Manages the Level9.Net configuration.
///
public sealed class Config
{
// -- Constants ------------------------------------------------------
///
/// The program name.
///
public const string PROGRAM_NAME = "Level9.Net";
///
/// The program version.
///
public const string PROGRAM_VERSION = "0.9.6";
///
/// The Level 9 interpreter DLL filename.
///
public const string LEVEL9_DLL = "Level9.dll";
///
/// The ANTLRv3 DLL filename.
///
public const string ANTLR_DLL = "Antlr3.Runtime.dll";
///
/// The settings filename.
///
public const string SETTINGS_FILE = "settings.xml";
///
/// The log filename.
///
public const string LOG_FILE = "level9net.log";
///
/// The L9cut executable filename.
///
public const string L9CUT_EXE = "L9cut.exe";
///
/// The Level9.Net help filename.
///
public const string HELP_FILE = "Level9Net.chm";
///
/// The maximum number of 'recent files' in the main menu.
///
const int MAX_RECENT_FILES = 10;
// -- Structs, Enums, Inner Classes ----------------------------------
///
/// The palette options for line drawn graphics.
///
public enum Palette
{
/// User defined palette.
UserDefined,
/// Amiga palette.
Amiga,
/// Spectrum palette.
Spectrum
}
///
/// Represents the size of a window.
///
public class WindowSize
{
///
/// The window width.
///
int width;
///
/// The window height.
///
int height;
///
/// Initializes a new instance of the WindowSize class.
///
/// the window width
/// the window height
public WindowSize(int width, int height)
{
this.width = width;
this.height = height;
}
///
/// Gets the window size.
///
public Size ToSize()
{
return new Size(width, height);
}
///
/// Sets the window size.
///
public void FromSize(Size size)
{
width = size.Width;
height = size.Height;
}
///
/// Gets or sets the window width.
///
public int Width
{
get {
return width;
}
set {
width = value;
}
}
///
/// Gets or sets the window height.
///
public int Height
{
get {
return height;
}
set {
height = value;
}
}
}
// -- Attributes -----------------------------------------------------
///
/// The one and only Config instance (singleton).
///
static Config instance = null;
///
/// This property indicates whether logging is enabled or disabled.
///
static bool loggingEnabled = false;
///
/// The application window size.
///
WindowSize clientSize = new WindowSize(644, 600);
///
/// The window size of the script editor dialog.
///
WindowSize scriptEditorSize = new WindowSize(600, 440);
///
/// The window size of the text viewer dialog.
///
WindowSize textViewerSize = new WindowSize(600, 440);
///
/// The window size of the dictionary viewer dialog.
///
WindowSize dictViewerSize = new WindowSize(600, 440);
///
/// This property indicates whether the toolbar is visible or hidden.
///
bool showToolbar = true;
///
/// This property indicates whether the status bar is visible or hidden.
///
bool showStatusBar = true;
///
/// This property indicates whether the title bar is visible or hidden.
///
bool showTitleBar = true;
///
/// This property indicates whether the timer is visible or hidden.
///
bool showTimer = true;
///
/// This property indicates whether the script playback toolbar is
/// visible or hidden.
///
bool showScriptPlayback = true;
///
/// The text color.
///
Color textColor = Color.LightGray;
///
/// The input text color.
///
Color inputColor = Color.Yellow;
///
/// The background color.
///
Color backColor = Color.Black;
///
/// The title/status bar text color.
///
Color barTextColor = Color.Black;
///
/// The title/status bar background color.
///
Color barBackColor = Color.LightGray;
///
/// The Amiga palette colors.
///
Color[] amigaPalette =
{
Color.FromArgb(0x00,0x00,0x00),
Color.FromArgb(0xff,0x00,0x00),
Color.FromArgb(0x30,0xe8,0x30),
Color.FromArgb(0xff,0xff,0x00),
Color.FromArgb(0x00,0x00,0xff),
Color.FromArgb(0xa0,0x68,0x00),
Color.FromArgb(0x00,0xff,0xff),
Color.FromArgb(0xff,0xff,0xff)
};
///
/// The Spectrum palette colors.
///
Color[] spectrumPalette =
{
Color.FromArgb(0x00,0x00,0x00),
Color.FromArgb(0x00,0x00,0xfc),
Color.FromArgb(0x00,0xfc,0x00),
Color.FromArgb(0x00,0xfc,0xfc),
Color.FromArgb(0xfc,0x00,0x00),
Color.FromArgb(0xf0,0x00,0xfc),
Color.FromArgb(0xfc,0xfc,0x00),
Color.FromArgb(0xfc,0xfc,0xfc),
};
///
/// The user defined palette colors.
///
Color[] userPalette =
{
Color.FromArgb(0x00,0x00,0x00),
Color.FromArgb(0xff,0x00,0x00),
Color.FromArgb(0x30,0xe8,0x30),
Color.FromArgb(0xff,0xff,0x00),
Color.FromArgb(0x00,0x00,0xff),
Color.FromArgb(0xa0,0x68,0x00),
Color.FromArgb(0x00,0xff,0xff),
Color.FromArgb(0xff,0xff,0xff)
};
///
/// The selected color palette for line drawn graphics.
///
Palette paletteSelection = Palette.Amiga;
///
/// The available image scaling factors for line drawn and bitmap
/// graphics.
///
int[] imageScalings =
{
1, 2, 3, 4
};
///
/// The selected image scaling factor.
///
int imageScaling = 2;
///
/// The in-game bitmap font.
///
BitmapFont font = BitmapFonts.Get("Atari ST", 1);
///
/// The selected font scaling factor.
///
int fontScaling = 1;
///
/// The available line spacings.
///
int[] lineSpacings =
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
///
/// The selected line spacing.
///
int lineSpacing = 1;
///
/// The list of 'recent files' in the main menu.
///
string[] recentFiles;
///
/// This property indicates whether or not Level9.Net scripts
/// include comments.
///
bool scriptIncludeComments = false;
///
/// This property indicates whether Level9.Net scripts contain
/// one or multiple entries per line.
///
bool scriptOneEntryPerLine = false;
///
/// The input delay for script playback (seconds).
///
int scriptInputDelay = 2;
///
/// The available script input delays.
///
int[] scriptInputDelays =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
// -- Events ---------------------------------------------------------
///
/// Occurs when the configuration has changed.
///
public event ConfigChangedEventHandler ConfigChanged;
// -- Constructors ---------------------------------------------------
///
/// Initializes a new instance of the Config class.
///
private Config()
{
if (File.Exists(GetConfigFilePath())) {
ReadSettings();
}
}
// -- Methods --------------------------------------------------------
///
/// Gets the current palette colors for line drawn graphics.
///
public Color[] GetActivePalette()
{
Color[] palette = amigaPalette;
switch (paletteSelection) {
case Palette.Amiga:
palette = amigaPalette;
break;
case Palette.Spectrum:
palette = spectrumPalette;
break;
case Palette.UserDefined:
palette = userPalette;
break;
}
return palette;
}
///
/// Adds a file to the 'recent files' list.
///
/// the file path path
public void AddToRecentFiles(string file)
{
if (recentFiles == null) {
recentFiles = new string[MAX_RECENT_FILES];
}
if (recentFiles.Length < MAX_RECENT_FILES) {
Array.Resize(ref recentFiles, MAX_RECENT_FILES);
}
int i = 0;
bool addFile = (file != null);
while (addFile && i < recentFiles.Length) {
if (file.Equals(recentFiles[i++])) {
addFile = false;
}
}
if (addFile) {
for (int j = recentFiles.Length - 1; j > 0; j--) {
if (recentFiles[j - 1] != null) {
recentFiles[j] = recentFiles[j - 1];
}
}
recentFiles[0] = file;
}
}
///
/// Notifies listeners that the configuration has changed.
///
public void FireConfigChanged()
{
if (ConfigChanged != null) {
ConfigChanged(new ConfigChangedEventArgs());
}
}
///
/// Writes a XML settings file. The file path is given by
/// GetConfigFilePath().
///
public void WriteSettings()
{
TextWriter writer = null;
try {
XmlSerializer serializer = new XmlSerializer(typeof(ConfigXML));
writer = new StreamWriter(GetConfigFilePath());
serializer.Serialize(writer, new ConfigXML(this));
writer.Close();
}
catch (Exception ex) {
Logger.Log(this, "WriteSettingsXml", ex);
if (writer != null) {
writer.Close();
}
}
}
///
/// Reads a XML settings file. The file path is given by
/// GetConfigFilePath().
///
void ReadSettings()
{
FileStream stream = null;
try {
XmlSerializer serializer = new XmlSerializer(typeof(ConfigXML));
stream = new FileStream(GetConfigFilePath(), FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
ConfigXML settings = (ConfigXML)serializer.Deserialize(reader);
SetMembers(settings);
stream.Close();
}
catch (Exception ex) {
ErrorHandler.Handle(this, "Error reading settings "
+ "file - Using default configuration." , ex);
if (stream != null) {
stream.Close();
}
}
}
///
/// Checks the values of a ConfigXML instance and applies the settings
/// to the current Config instance.
///
/// a ConfigXML instance
void SetMembers(ConfigXML xml)
{
if (xml == null) {
return;
}
showStatusBar = xml.show_status_bar;
showToolbar = xml.show_toolbar;
showTitleBar = xml.show_title_bar;
showScriptPlayback = xml.show_script_playback;
showTimer = xml.show_timer;
scriptIncludeComments = xml.script_include_comments;
scriptOneEntryPerLine = xml.script_one_entry_per_line;
scriptInputDelay = xml.script_input_delay;
if ((xml.back_color & 0xff000000) == 0xff000000) {
backColor = Color.FromArgb(xml.back_color);
}
if ((xml.text_color & 0xff000000) == 0xff000000
&& xml.text_color != xml.back_color) {
textColor = Color.FromArgb(xml.text_color);
}
if ((xml.input_color & 0xff000000) == 0xff000000
&& xml.input_color != xml.back_color) {
inputColor = Color.FromArgb(xml.input_color);
}
if ((xml.bar_back_color & 0xff000000) == 0xff000000) {
barBackColor = Color.FromArgb(xml.bar_back_color);
}
if ((xml.bar_text_color & 0xff000000) == 0xff000000
&& xml.bar_text_color != xml.bar_back_color) {
barTextColor = Color.FromArgb(xml.bar_text_color);
}
if (BitmapFonts.Get(xml.font_type, xml.font_scaling) != null) {
font = BitmapFonts.Get(xml.font_type, xml.font_scaling);
fontScaling = xml.font_scaling;
}
if (xml.line_spacing <= lineSpacings[lineSpacings.Length - 1]
&& xml.line_spacing >= lineSpacings[0]) {
lineSpacing = xml.line_spacing;
}
if (xml.image_scaling <= imageScalings[imageScalings.Length - 1]
&& xml.image_scaling >= imageScalings[0]) {
imageScaling = xml.image_scaling;
}
if (xml.recent_files != null) {
recentFiles = xml.recent_files;
}
if (xml.palette_selection >= (int)Config.Palette.UserDefined
&& xml.palette_selection <= (int)Config.Palette.Spectrum) {
paletteSelection = (Config.Palette)xml.palette_selection;
}
if (xml.user_palette != null && xml.user_palette.Length > 0) {
for (int i = 0; i < xml.user_palette.Length; i++) {
userPalette[i] = Color.FromArgb(xml.user_palette[i]);
}
}
if (xml.client_width > 0 && xml.client_height > 0) {
clientSize.Width = xml.client_width;
clientSize.Height = xml.client_height;
}
if (xml.script_editor_width > 0 && xml.script_editor_height > 0) {
scriptEditorSize.Width = xml.script_editor_width;
scriptEditorSize.Height = xml.script_editor_height;
}
if (xml.text_viewer_width > 0 && xml.text_viewer_height > 0) {
textViewerSize.Width = xml.text_viewer_width;
textViewerSize.Height = xml.text_viewer_height;
}
if (xml.dict_viewer_width > 0 && xml.dict_viewer_height > 0) {
dictViewerSize.Width = xml.dict_viewer_width;
dictViewerSize.Height = xml.dict_viewer_height;
}
}
///
/// Provides the initialization state of the configuration.
///
///
/// true, if the configuration has been initialized; otherwise, false
///
public static bool IsInitialized()
{
return (instance != null);
}
///
/// Provides the application data path.
///
/// the configuration file path
public static string GetAppDataPath()
{
return Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) + "\\"
+ PROGRAM_NAME;
}
///
/// Provides the configuration file path.
///
/// the configuration file path
public static string GetConfigFilePath()
{
return GetAppDataPath() + "\\" + SETTINGS_FILE;
}
///
/// Provides the Level 9 DLL path.
///
/// the Level 9 DLL path
public static string GetLevel9DLLPath()
{
return Application.StartupPath + "\\" + LEVEL9_DLL;
}
///
/// Provides the ANTLRv3 DLL path.
///
/// the ANTLRv3 DLL path
public static string GetAntlrPath()
{
return Application.StartupPath + "\\" + ANTLR_DLL;
}
///
/// Provides the L9cut executable path.
///
/// the L9cut executable path
public static string GetL9CutPath()
{
return Application.StartupPath + "\\" + L9CUT_EXE;
}
///
/// Provides the log file path.
///
/// the log file path
public static string GetLogFilePath()
{
return GetAppDataPath() + "\\" + LOG_FILE;
}
///
/// Provides the help file path.
///
/// the help file path
public static string GetHelpFilePath()
{
return Application.StartupPath + "\\" + HELP_FILE;
}
// -- Accessors ------------------------------------------------------
///
/// Gets the one and only Config instance (singleton).
///
public static Config Instance
{
get {
if (instance == null) {
instance = new Config();
}
return instance;
}
}
///
/// Gets or sets the application window size.
///
public WindowSize ClientSize
{
get {
return clientSize;
}
}
///
/// Gets or sets the window size of the script editor dialog.
///
public WindowSize ScriptEditorSize
{
get {
return scriptEditorSize;
}
}
///
/// Gets or sets the window size of the text viewer dialog.
///
public WindowSize TextViewerSize
{
get {
return textViewerSize;
}
}
///
/// Gets or sets the window size of the dictionary viewer dialog.
///
public WindowSize DictViewerSize
{
get {
return dictViewerSize;
}
}
///
/// Gets or sets a value that indicates whether the toolbar is visible
/// or hidden.
///
public bool ShowToolbar
{
get {
return showToolbar;
}
set {
showToolbar = value;
}
}
///
/// Gets or sets a value that indicates whether the status bar is
/// visible or hidden.
///
public bool ShowStatusBar
{
get {
return showStatusBar;
}
set {
showStatusBar = value;
}
}
///
/// Gets or sets a value that indicates whether the title bar is
/// visible or hidden.
///
public bool ShowTitleBar
{
get {
return showTitleBar;
}
set {
showTitleBar = value;
}
}
///
/// Gets or sets a value that indicates whether the script playback
/// toolbar is visible or hidden.
///
public bool ShowScriptPlayback
{
get {
return showScriptPlayback;
}
set {
showScriptPlayback = value;
}
}
///
/// Gets or sets a value that indicates whether the timer is visible
/// or hidden.
///
public bool ShowTimer
{
get {
return showTimer;
}
set {
showTimer = value;
}
}
///
/// Gets or sets the text color.
///
public Color TextColor
{
get {
return textColor;
}
set {
textColor = value;
}
}
///
/// Gets or sets the background color.
///
public Color BackColor
{
get {
return backColor;
}
set {
backColor = value;
}
}
///
/// Gets or sets the input text color.
///
public Color InputColor
{
get {
return inputColor;
}
set {
inputColor = value;
}
}
///
/// Gets or sets the title/status bar text color.
///
public Color BarTextColor
{
get {
return barTextColor;
}
set {
barTextColor = value;
}
}
///
/// Gets or sets the title/status bar background color.
///
public Color BarBackColor
{
get {
return barBackColor;
}
set {
barBackColor = value;
}
}
///
/// Gets the Amiga palette colors.
///
public Color[] AmigaPalette
{
get {
return amigaPalette;
}
}
///
/// Gets the Spectrum palette colors.
///
public Color[] SpectrumPalette
{
get {
return spectrumPalette;
}
}
///
/// Gets or sets the user defined palette colors.
///
public Color[] UserPalette
{
get {
return userPalette;
}
set {
userPalette = value;
}
}
///
/// Gets or sets the selected color palette for line drawn graphics.
///
public Palette PaletteSelection
{
get {
return paletteSelection;
}
set {
paletteSelection = value;
}
}
///
/// Gets or sets the in-game bitmap font.
///
public BitmapFont Font
{
get {
return font;
}
set {
font = value;
}
}
///
/// Gets or sets the selected font scaling factor.
///
public int FontScaling
{
get {
return fontScaling;
}
set {
fontScaling = value;
}
}
///
/// Gets or sets the selected lines spacing.
///
public int LineSpacing
{
get {
return lineSpacing;
}
set {
lineSpacing = value;
}
}
///
/// Gets the available line spacings.
///
public int[] LineSpacings
{
get {
return lineSpacings;
}
}
///
/// Gets or sets the selected image scaling factor.
///
public int ImageScaling
{
get {
return imageScaling;
}
set {
imageScaling = value;
}
}
///
/// Gets the available image scaling factors for line drawn and bitmap
/// graphics.
///
public int[] ImageScalings
{
get {
return imageScalings;
}
}
///
/// Gets or sets a value that indicates whether or not Level9.Net
/// scripts include comments.
///
public bool ScriptIncludeComments
{
get {
return scriptIncludeComments;
}
set {
scriptIncludeComments = value;
}
}
///
/// Gets or sets a value that indicates whether Level9.Net scripts
/// contain one or multiple entries per line.
///
public bool ScriptOneEntryPerLine
{
get {
return scriptOneEntryPerLine;
}
set {
scriptOneEntryPerLine = value;
}
}
///
/// Gets or sets the input delay for script playback (seconds).
///
public int ScriptInputDelay
{
get {
return scriptInputDelay;
}
set {
scriptInputDelay = value;
}
}
///
/// Gets the available script playback delays.
///
public int[] ScriptInputDelays
{
get {
return scriptInputDelays;
}
}
///
/// Gets or sets the list of 'recent files' in the main menu.
///
public string[] RecentFiles
{
get {
return recentFiles;
}
set {
recentFiles = value;
}
}
///
/// Gets or sets a value that indicates whether logging is enabled or
/// disabled.
///
public static bool LoggingEnabled
{
get {
return loggingEnabled;
}
set {
loggingEnabled = value;
}
}
}
///
/// Allows XML serialization of miscellaneous configuration options.
///
public class ConfigXML
{
// -- Attributes -----------------------------------------------------
///
/// See .
///
public int client_width;
///
/// See .
///
public int client_height;
///
/// See .
///
public int script_editor_width;
///
/// See .
///
public int script_editor_height;
///
/// See .
///
public int text_viewer_width;
///
/// See .
///
public int text_viewer_height;
///
/// See .
///
public int dict_viewer_width;
///
/// See .
///
public int dict_viewer_height;
///
/// See .
///
public bool show_toolbar;
///
/// See .
///
public bool show_status_bar;
///
/// See .
///
public bool show_script_playback;
///
/// See .
///
public bool show_timer;
///
/// See .
///
public bool show_title_bar;
///
/// See .
///
public int back_color;
///
/// See .
///
public int text_color;
///
/// See .
///
public int input_color;
///
/// See .
///
public int bar_text_color;
///
/// See .
///
public int bar_back_color;
///
/// See .
///
public int[] user_palette;
///
/// See .
///
public string font_type;
///
/// See .
///
public int font_scaling;
///
/// See .
///
public int line_spacing;
///
/// See .
///
public int image_scaling;
///
/// See .
///
public int palette_selection;
///
/// See .
///
public string[] recent_files;
///
/// See .
///
public bool script_include_comments;
///
/// See .
///
public bool script_one_entry_per_line;
///
/// See .
///
public int script_input_delay;
// -- Constructors ---------------------------------------------------
///
/// Parameterless constructor for serialization.
///
public ConfigXML() {}
///
/// Initializes a new instance of the ConfigXML class.
///
/// a Config instance
public ConfigXML(Config config)
{
client_height = config.ClientSize.Height;
client_width = config.ClientSize.Width;
script_editor_width = config.ScriptEditorSize.Width;
script_editor_height = config.ScriptEditorSize.Height;
text_viewer_width = config.TextViewerSize.Width;
text_viewer_height = config.TextViewerSize.Height;
dict_viewer_width = config.DictViewerSize.Width;
dict_viewer_height = config.DictViewerSize.Height;
show_status_bar = config.ShowStatusBar;
show_toolbar = config.ShowToolbar;
show_title_bar = config.ShowTitleBar;
show_script_playback = config.ShowScriptPlayback;
show_timer = config.ShowTimer;
back_color = config.BackColor.ToArgb();
text_color = config.TextColor.ToArgb();
input_color = config.InputColor.ToArgb();
bar_text_color = config.BarTextColor.ToArgb();
bar_back_color = config.BarBackColor.ToArgb();
input_color = config.InputColor.ToArgb();
font_type = config.Font.Type;
font_scaling = config.FontScaling;
line_spacing = config.LineSpacing;
image_scaling = config.ImageScaling;
recent_files = config.RecentFiles;
palette_selection = (int)config.PaletteSelection;
user_palette = new int[config.UserPalette.Length];
for (int i = 0; i < config.UserPalette.Length; i++) {
user_palette[i] = config.UserPalette[i].ToArgb();
}
script_include_comments = config.ScriptIncludeComments;
script_one_entry_per_line = config.ScriptOneEntryPerLine;
script_input_delay = config.ScriptInputDelay;
}
}
///
/// Represents the method that handles a ConfigChanged event.
///
public delegate void ConfigChangedEventHandler(ConfigChangedEventArgs e);
///
/// Provides data for the ConfigChanged event.
///
public class ConfigChangedEventArgs : EventArgs
{
// -- Constructors ---------------------------------------------------
///
/// Initializes a new instance of the ConfigChangedEventArgs class.
///
public ConfigChangedEventArgs() { }
}
}