1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-12-02 13:51:36 +00:00
6502bench/SourceGen/AsmGen/AssemblerConfig.cs
Andy McFadden 4f9af30455 Progress toward new assembler configuration
Rather than have each assembler get its own app config string for
the cross-assembler executable, we now have a collection of per-
assembler config items, of which the executable path name is one
member.  The Asm Config tab has an auto-generated pop-up to select
the assembler.

The per-assembler settings block is serialized with the rather
unpleasant JSON-in-JSON approach, but nobody should have to look
at it.

This also adds assembler-specific column widths to the settings
dialog, though they aren't actually used yet.
2018-10-20 20:35:32 -07:00

112 lines
4.1 KiB
C#

/*
* Copyright 2018 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;
namespace SourceGen.AsmGen {
/// <summary>
/// Assembler configuration holder. Serializes and deserializes information held in
/// application settings.
/// </summary>
public class AssemblerConfig {
// Fields are deserialized from JSON. Changing the names will break compatibility.
/// <summary>
/// Path to cross-assembler executable. Will be null or empty if this assembler
/// is not configured.
/// </summary>
public string ExecutablePath { get; set; }
/// <summary>
/// Column display widths.
/// </summary>
public int[] ColumnWidths { get; set; }
public const int NUM_COLUMNS = 4; // label, opcode, operand, comment
/// <summary>
/// Nullary constructor, for serialization.
/// </summary>
public AssemblerConfig() { }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="exePath">Path to executable. May be empty.</param>
/// <param name="widths">Column widths.</param>
public AssemblerConfig(string exePath, int[] widths) {
if (exePath == null) {
throw new Exception("Bad exe path");
}
if (widths.Length != NUM_COLUMNS) {
throw new Exception("Bad widths.Length " + widths.Length);
}
ExecutablePath = exePath;
ColumnWidths = widths;
}
private static string GetSettingName(AssemblerInfo.Id id) {
return AppSettings.ASM_CONFIG_PREFIX + id.ToString();
}
/// <summary>
/// Creates a populated AssemblerConfig from the app settings for the specified ID.
/// </summary>
/// <param name="settings">Settings object to pull the values from.</param>
/// <param name="id">Assembler ID.</param>
/// <returns>The AssemblerConfig, or null if not configured.</returns>
public static AssemblerConfig GetConfig(AppSettings settings, AssemblerInfo.Id id) {
string cereal = settings.GetString(GetSettingName(id), null);
if (string.IsNullOrEmpty(cereal)) {
return null;
}
JavaScriptSerializer ser = new JavaScriptSerializer();
try {
AssemblerConfig config = ser.Deserialize<AssemblerConfig>(cereal);
if (config.ColumnWidths == null || config.ColumnWidths.Length != NUM_COLUMNS) {
throw new Exception("Bad column widths");
}
if (config.ExecutablePath == null) {
throw new Exception("Missing exe path");
}
return config;
} catch (Exception ex) {
Debug.WriteLine("AssemblerConfig deserialization failed: " + ex.Message);
return null;
}
}
/// <summary>
/// Updates the assembler settings for the specified ID.
/// </summary>
/// <param name="settings">Settings object to update.</param>
/// <param name="id">Assembler ID.</param>
/// <param name="config">Asm configuration.</param>
public static void SetConfig(AppSettings settings, AssemblerInfo.Id id,
AssemblerConfig config) {
JavaScriptSerializer ser = new JavaScriptSerializer();
string cereal = ser.Serialize(config);
settings.SetString(GetSettingName(id), cereal);
}
}
}