2019-05-02 22:45:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Web.Script.Serialization;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Application settings registry. This holds both user-accessible settings and saved
|
|
|
|
|
/// values like window widths.
|
|
|
|
|
///
|
|
|
|
|
/// Everything is stored as name/value pairs, where the value is serialized as a string.
|
|
|
|
|
/// Names are case-sensitive.
|
|
|
|
|
///
|
|
|
|
|
/// We don't discard things we don't recognize. If we somehow end up reading a config
|
|
|
|
|
/// file from a newer version of the app, the various settings will be retained.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AppSettings {
|
|
|
|
|
#region Names
|
|
|
|
|
|
|
|
|
|
// Name constants. Having them defined here avoids collisions and misspellings, and
|
|
|
|
|
// makes it easy to find all uses.
|
|
|
|
|
|
|
|
|
|
// Main window.
|
2019-06-19 23:31:56 +00:00
|
|
|
|
public const string MAIN_WINDOW_PLACEMENT = "main-window-placement";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public const string MAIN_LEFT_PANEL_WIDTH = "main-left-panel-width";
|
|
|
|
|
public const string MAIN_RIGHT_PANEL_WIDTH = "main-right-panel-width";
|
2019-06-19 23:31:56 +00:00
|
|
|
|
public const string MAIN_REFERENCES_HEIGHT = "main-references-height";
|
|
|
|
|
public const string MAIN_SYMBOLS_HEIGHT = "main-symbols-height";
|
2019-10-21 01:02:23 +00:00
|
|
|
|
public const string MAIN_HIDE_MESSAGE_WINDOW = "main-hide-message-window";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
// New project dialog.
|
|
|
|
|
public const string NEWP_SELECTED_SYSTEM = "newp-selected-system";
|
|
|
|
|
|
|
|
|
|
// Formatting choices.
|
|
|
|
|
public const string FMT_UPPER_HEX_DIGITS = "fmt-upper-hex-digits";
|
|
|
|
|
public const string FMT_UPPER_OP_MNEMONIC = "fmt-upper-op-mnemonic";
|
|
|
|
|
public const string FMT_UPPER_PSEUDO_OP_MNEMONIC = "fmt-upper-pseudo-op-mnemonic";
|
|
|
|
|
public const string FMT_UPPER_OPERAND_A = "fmt-upper-operand-a";
|
|
|
|
|
public const string FMT_UPPER_OPERAND_S = "fmt-upper-operand-s";
|
|
|
|
|
public const string FMT_UPPER_OPERAND_XY = "fmt-upper-operand-xy";
|
|
|
|
|
public const string FMT_ADD_SPACE_FULL_COMMENT = "fmt-add-space-full-comment";
|
2020-07-20 01:39:27 +00:00
|
|
|
|
public const string FMT_OPERAND_WRAP_LEN = "fmt-operand-wrap-len";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
public const string FMT_OPCODE_SUFFIX_ABS = "fmt-opcode-suffix-abs";
|
|
|
|
|
public const string FMT_OPCODE_SUFFIX_LONG = "fmt-opcode-suffix-long";
|
|
|
|
|
public const string FMT_OPERAND_PREFIX_ABS = "fmt-operand-prefix-abs";
|
|
|
|
|
public const string FMT_OPERAND_PREFIX_LONG = "fmt-operand-prefix-long";
|
|
|
|
|
public const string FMT_EXPRESSION_MODE = "fmt-expression-mode";
|
2024-06-26 15:27:45 +00:00
|
|
|
|
public const string FMT_FULL_COMMENT_DELIM = "fmt-full-comment-delim";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
public const string FMT_PSEUDO_OP_NAMES = "fmt-pseudo-op-names";
|
2019-08-14 22:25:09 +00:00
|
|
|
|
public const string FMT_CHAR_DELIM = "fmt-char-delim";
|
|
|
|
|
public const string FMT_STRING_DELIM = "fmt-string-delim";
|
2019-11-13 01:24:41 +00:00
|
|
|
|
public const string FMT_NON_UNIQUE_LABEL_PREFIX = "fmt-non-unique-label-prefix";
|
2019-08-31 21:54:44 +00:00
|
|
|
|
public const string FMT_LOCAL_VARIABLE_PREFIX = "fmt-local-variable-prefix";
|
2020-08-27 20:13:58 +00:00
|
|
|
|
public const string FMT_SPACES_BETWEEN_BYTES = "fmt-spaces-between-bytes";
|
2019-12-11 01:41:00 +00:00
|
|
|
|
public const string FMT_COMMA_SEP_BULK_DATA = "fmt-comma-sep-bulk-data";
|
2020-08-27 20:13:58 +00:00
|
|
|
|
public const string FMT_SHOW_CYCLE_COUNTS = "fmt-show-cycle-counts";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
public const string CLIP_LINE_FORMAT = "clip-line-format";
|
|
|
|
|
|
2024-08-02 20:47:39 +00:00
|
|
|
|
// Project open/save settings.
|
2019-08-16 00:53:12 +00:00
|
|
|
|
public const string PRVW_RECENT_PROJECT_LIST = "prvw-recent-project-list";
|
2024-08-02 20:47:39 +00:00
|
|
|
|
public const string PROJ_AUTO_SAVE_INTERVAL = "proj-auto-save-interval";
|
2019-08-16 00:53:12 +00:00
|
|
|
|
|
2019-10-13 00:23:32 +00:00
|
|
|
|
public const string SKIN_DARK_COLOR_SCHEME = "skin-dark-color-scheme";
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// Symbol-list window options.
|
|
|
|
|
public const string SYMWIN_SHOW_USER = "symwin-show-user";
|
2019-11-16 04:40:14 +00:00
|
|
|
|
public const string SYMWIN_SHOW_NON_UNIQUE = "symwin-show-non-unique";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public const string SYMWIN_SHOW_AUTO = "symwin-show-auto";
|
|
|
|
|
public const string SYMWIN_SHOW_PROJECT = "symwin-show-project";
|
|
|
|
|
public const string SYMWIN_SHOW_PLATFORM = "symwin-show-platform";
|
2021-10-05 03:41:19 +00:00
|
|
|
|
public const string SYMWIN_SHOW_ADDR_PRE_LABELS = "symwin-show-addr-pre-labels";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public const string SYMWIN_SHOW_CONST = "symwin-show-const";
|
|
|
|
|
public const string SYMWIN_SHOW_ADDR = "symwin-show-addr";
|
|
|
|
|
public const string SYMWIN_SORT_ASCENDING = "symwin-sort-ascending";
|
|
|
|
|
public const string SYMWIN_SORT_COL = "symwin-sort-col";
|
|
|
|
|
|
|
|
|
|
public const string SYMWIN_COL_WIDTHS = "symwin-col-widths";
|
|
|
|
|
|
|
|
|
|
// References window options.
|
|
|
|
|
public const string REFWIN_COL_WIDTHS = "refwin-col-widths";
|
|
|
|
|
|
|
|
|
|
// Notes window options.
|
|
|
|
|
public const string NOTEWIN_COL_WIDTHS = "notewin-col-widths";
|
|
|
|
|
|
|
|
|
|
// Code List View settings.
|
2019-07-14 23:28:54 +00:00
|
|
|
|
public const string CDLV_COL_WIDTHS = "cdlv-col-widths1";
|
2019-06-27 00:11:58 +00:00
|
|
|
|
public const string CDLV_FONT_FAMILY = "cdlv-font-family";
|
|
|
|
|
public const string CDLV_FONT_SIZE = "cdlv-font-size";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-08-16 00:53:12 +00:00
|
|
|
|
// Operand edit settings.
|
|
|
|
|
public const string OPED_DEFAULT_STRING_ENCODING = "oped-default-string-encoding";
|
2019-12-11 01:41:00 +00:00
|
|
|
|
public const string OPED_DENSE_HEX_LIMIT = "oped-dense-hex-limit";
|
2019-08-16 00:53:12 +00:00
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// Hex dump viewer settings.
|
|
|
|
|
public const string HEXD_ASCII_ONLY = "hexd-ascii-only";
|
2019-07-14 23:28:54 +00:00
|
|
|
|
public const string HEXD_CHAR_CONV = "hexd-char-conv1";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2020-08-20 20:18:46 +00:00
|
|
|
|
// Apple II screen chart viewer settings.
|
|
|
|
|
public const string A2SC_MODE = "a2sc-mode";
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// ASCII chart viewer settings.
|
2019-07-15 01:12:03 +00:00
|
|
|
|
public const string ASCCH_MODE = "ascch-mode1";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2020-02-18 21:25:20 +00:00
|
|
|
|
// Instruction chart settings.
|
|
|
|
|
public const string INSTCH_MODE = "instch-mode";
|
|
|
|
|
public const string INSTCH_SHOW_UNDOC = "instch-show-undoc";
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// Source generation settings.
|
|
|
|
|
public const string SRCGEN_DEFAULT_ASM = "srcgen-default-asm";
|
|
|
|
|
public const string SRCGEN_ADD_IDENT_COMMENT = "srcgen-add-ident-comment";
|
2024-04-21 22:30:11 +00:00
|
|
|
|
public const string SRCGEN_LABEL_NEW_LINE = "srcgen-label-new-line";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public const string SRCGEN_SHOW_CYCLE_COUNTS = "srcgen-show-cycle-counts";
|
|
|
|
|
|
2024-04-20 22:36:15 +00:00
|
|
|
|
// Label file generation settings.
|
|
|
|
|
public const string LABGEN_FORMAT = "labgen-format";
|
|
|
|
|
public const string LABGEN_INCLUDE_AUTO = "labgen-include-auto";
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// Assembler settings prefix
|
|
|
|
|
public const string ASM_CONFIG_PREFIX = "asm-config-";
|
|
|
|
|
|
2019-09-12 20:57:52 +00:00
|
|
|
|
// Text/HTML export settings.
|
|
|
|
|
public const string EXPORT_INCLUDE_NOTES = "export-include-notes";
|
|
|
|
|
public const string EXPORT_SHOW_OFFSET = "export-show-offset";
|
|
|
|
|
public const string EXPORT_SHOW_ADDR = "export-show-addr";
|
|
|
|
|
public const string EXPORT_SHOW_BYTES = "export-show-bytes";
|
|
|
|
|
public const string EXPORT_SHOW_FLAGS = "export-show-flags";
|
|
|
|
|
public const string EXPORT_SHOW_ATTR = "export-show-attr";
|
|
|
|
|
public const string EXPORT_COL_WIDTHS = "export-col-widths";
|
|
|
|
|
public const string EXPORT_TEXT_MODE = "export-text-mode";
|
|
|
|
|
public const string EXPORT_SELECTION_ONLY = "export-selection-only";
|
2019-09-18 18:05:52 +00:00
|
|
|
|
public const string EXPORT_LONG_LABEL_NEW_LINE = "export-long-label-new-line";
|
2019-12-14 00:52:50 +00:00
|
|
|
|
public const string EXPORT_GENERATE_IMAGE_FILES = "export-generate-image-files";
|
2019-09-12 20:57:52 +00:00
|
|
|
|
|
2020-07-20 20:47:19 +00:00
|
|
|
|
// OMF export settings.
|
|
|
|
|
public const string OMF_ADD_NOTES = "omf-add-notes";
|
|
|
|
|
public const string OMF_OFFSET_SEGMENT_START = "omf-offset-segment-start";
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
// Internal debugging features.
|
|
|
|
|
public const string DEBUG_MENU_ENABLED = "debug-menu-enabled";
|
|
|
|
|
|
|
|
|
|
#endregion Names
|
|
|
|
|
|
|
|
|
|
#region Implementation
|
|
|
|
|
|
|
|
|
|
// App settings file header.
|
|
|
|
|
public const string MAGIC = "### 6502bench SourceGen settings v1.0 ###";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Single global instance of app settings.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static AppSettings Global {
|
|
|
|
|
get {
|
|
|
|
|
return sSingleton;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static AppSettings sSingleton = new AppSettings();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-10-21 22:15:09 +00:00
|
|
|
|
/// Dirty flag, set to true by every "set" call that changes a value.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Dirty { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Settings storage.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Dictionary<string, string> mSettings = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private AppSettings() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a copy of this object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public AppSettings GetCopy() {
|
2023-04-07 01:26:14 +00:00
|
|
|
|
// TODO: make this a copy constructor?
|
2019-05-02 22:45:40 +00:00
|
|
|
|
AppSettings copy = new AppSettings();
|
|
|
|
|
//copy.mSettings.EnsureCapacity(mSettings.Count);
|
|
|
|
|
foreach (KeyValuePair<string, string> kvp in mSettings) {
|
|
|
|
|
copy.mSettings.Add(kvp.Key, kvp.Value);
|
|
|
|
|
}
|
|
|
|
|
return copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replaces the existing list of settings with a new list.
|
|
|
|
|
///
|
|
|
|
|
/// This can be used to replace the contents of the global settings object without
|
|
|
|
|
/// discarding the object itself, which is useful in case something has cached a
|
|
|
|
|
/// reference to the singleton.
|
|
|
|
|
/// </summary>
|
2023-04-07 01:26:14 +00:00
|
|
|
|
/// <param name="newSettings">Object with new settings.</param>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public void ReplaceSettings(AppSettings newSettings) {
|
|
|
|
|
// Clone the new list, and stuff it into the old object. This way the
|
|
|
|
|
// objects aren't sharing lists.
|
|
|
|
|
mSettings = newSettings.GetCopy().mSettings;
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Merges settings from another settings object into this one.
|
|
|
|
|
/// </summary>
|
2023-04-07 01:26:14 +00:00
|
|
|
|
/// <param name="newSettings">Object with new settings.</param>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public void MergeSettings(AppSettings newSettings) {
|
|
|
|
|
foreach (KeyValuePair<string, string> kvp in newSettings.mSettings) {
|
|
|
|
|
mSettings[kvp.Key] = kvp.Value;
|
|
|
|
|
}
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves an integer setting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="defaultValue">Setting default value.</param>
|
|
|
|
|
/// <returns>The value found, or the default value if no setting with the specified
|
|
|
|
|
/// name exists, or the stored value is not an integer.</returns>
|
|
|
|
|
public int GetInt(string name, int defaultValue) {
|
|
|
|
|
if (!mSettings.TryGetValue(name, out string valueStr)) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
if (!int.TryParse(valueStr, out int value)) {
|
|
|
|
|
Debug.WriteLine("Warning: int parse failed on " + name + "=" + valueStr);
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets an integer setting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="value">Setting value.</param>
|
|
|
|
|
public void SetInt(string name, int value) {
|
2019-10-21 22:15:09 +00:00
|
|
|
|
string newVal = value.ToString();
|
|
|
|
|
if (!mSettings.TryGetValue(name, out string oldValue) || oldValue != newVal) {
|
|
|
|
|
mSettings[name] = newVal;
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a boolean setting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="defaultValue">Setting default value.</param>
|
|
|
|
|
/// <returns>The value found, or the default value if no setting with the specified
|
|
|
|
|
/// name exists, or the stored value is not a boolean.</returns>
|
|
|
|
|
public bool GetBool(string name, bool defaultValue) {
|
|
|
|
|
if (!mSettings.TryGetValue(name, out string valueStr)) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
if (!bool.TryParse(valueStr, out bool value)) {
|
|
|
|
|
Debug.WriteLine("Warning: bool parse failed on " + name + "=" + valueStr);
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets a boolean setting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="value">Setting value.</param>
|
|
|
|
|
public void SetBool(string name, bool value) {
|
2019-10-21 22:15:09 +00:00
|
|
|
|
string newVal = value.ToString();
|
|
|
|
|
if (!mSettings.TryGetValue(name, out string oldValue) || oldValue != newVal) {
|
|
|
|
|
mSettings[name] = newVal;
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves an enumerated value setting.
|
|
|
|
|
/// </summary>
|
2024-04-21 22:30:11 +00:00
|
|
|
|
/// <typeparam name="T">Enumerated type.</typeparam>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="defaultValue">Setting default value.</param>
|
|
|
|
|
/// <returns>The value found, or the default value if no setting with the specified
|
2024-04-21 22:30:11 +00:00
|
|
|
|
/// name exists, or the stored value is not a member of the enumeration.</returns>
|
|
|
|
|
public T GetEnum<T>(string name, T defaultValue) {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
if (!mSettings.TryGetValue(name, out string valueStr)) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2024-04-21 22:30:11 +00:00
|
|
|
|
object o = Enum.Parse(typeof(T), valueStr);
|
|
|
|
|
return (T)o;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
} catch (ArgumentException ae) {
|
2024-04-21 22:30:11 +00:00
|
|
|
|
Debug.WriteLine("Failed to parse '" + valueStr + "' (enum " + typeof(T) + "): " +
|
2019-05-02 22:45:40 +00:00
|
|
|
|
ae.Message);
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets an enumerated setting.
|
|
|
|
|
/// </summary>
|
2024-04-21 22:30:11 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The value is output to the settings file as a string, rather than an integer, allowing
|
|
|
|
|
/// the correct handling even if the enumerated values are renumbered.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <typeparam name="T">Enumerated type.</typeparam>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="value">Setting value (integer enum index).</param>
|
2024-04-21 22:30:11 +00:00
|
|
|
|
public void SetEnum<T>(string name, T value) {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
throw new NotImplementedException("Can't handle a null-valued enum type");
|
|
|
|
|
}
|
|
|
|
|
string newVal = Enum.GetName(typeof(T), value);
|
2023-04-17 16:48:01 +00:00
|
|
|
|
if (newVal == null) {
|
2024-04-21 22:30:11 +00:00
|
|
|
|
Debug.WriteLine("Unable to get enum name type=" + typeof(T) + " value=" + value);
|
2023-04-17 16:48:01 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-21 22:15:09 +00:00
|
|
|
|
if (!mSettings.TryGetValue(name, out string oldValue) || oldValue != newVal) {
|
|
|
|
|
mSettings[name] = newVal;
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a string setting. The default value will be returned if the key
|
|
|
|
|
/// is not found, or if the value is null.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
|
|
|
|
/// <param name="defaultValue">Setting default value.</param>
|
|
|
|
|
/// <returns>The value found, or defaultValue if not value is found.</returns>
|
|
|
|
|
public string GetString(string name, string defaultValue) {
|
|
|
|
|
if (!mSettings.TryGetValue(name, out string valueStr) || valueStr == null) {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return valueStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets a string setting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Setting name.</param>
|
2019-08-31 21:54:44 +00:00
|
|
|
|
/// <param name="value">Setting value. If the value is null, the setting will be
|
|
|
|
|
/// removed.</param>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public void SetString(string name, string value) {
|
|
|
|
|
if (value == null) {
|
|
|
|
|
mSettings.Remove(name);
|
2019-10-21 22:15:09 +00:00
|
|
|
|
Dirty = true;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
} else {
|
2019-10-21 22:15:09 +00:00
|
|
|
|
if (!mSettings.TryGetValue(name, out string oldValue) || oldValue != value) {
|
|
|
|
|
mSettings[name] = value;
|
|
|
|
|
Dirty = true;
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes settings dictionary into a string, for saving settings to a file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Serialized settings.</returns>
|
|
|
|
|
public string Serialize() {
|
|
|
|
|
StringBuilder sb = new StringBuilder(1024);
|
|
|
|
|
sb.Append(MAGIC); // augment with version string, which will be stripped
|
|
|
|
|
sb.Append("\r\n"); // will be ignored by deserializer; might get converted to \n
|
|
|
|
|
|
|
|
|
|
JavaScriptSerializer ser = new JavaScriptSerializer();
|
|
|
|
|
string cereal = ser.Serialize(mSettings);
|
|
|
|
|
|
|
|
|
|
// add some linefeeds to make it easier for humans
|
|
|
|
|
cereal = CommonUtil.TextUtil.NonQuoteReplace(cereal, ",\"", ",\r\n\"");
|
|
|
|
|
sb.Append(cereal);
|
|
|
|
|
|
|
|
|
|
// Stick a linefeed at the end.
|
|
|
|
|
sb.Append("\r\n");
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes settings from a string, for loading settings from a file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cereal">Serialized settings.</param>
|
|
|
|
|
/// <returns>Deserialized settings, or null if deserialization failed.</returns>
|
|
|
|
|
public static AppSettings Deserialize(string cereal) {
|
|
|
|
|
if (!cereal.StartsWith(MAGIC)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip past header.
|
|
|
|
|
cereal = cereal.Substring(MAGIC.Length);
|
|
|
|
|
|
|
|
|
|
AppSettings settings = new AppSettings();
|
|
|
|
|
JavaScriptSerializer ser = new JavaScriptSerializer();
|
|
|
|
|
try {
|
|
|
|
|
settings.mSettings = ser.Deserialize<Dictionary<string, string>>(cereal);
|
|
|
|
|
return settings;
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
Debug.WriteLine("Settings deserialization failed: " + ex.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Implementation
|
|
|
|
|
}
|
|
|
|
|
}
|