2020-06-24 00:21:18 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2020 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.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
2020-06-30 01:29:18 +00:00
|
|
|
|
using System.IO;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Windows;
|
2020-06-26 23:58:42 +00:00
|
|
|
|
using System.Windows.Controls;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
using System.Windows.Input;
|
2020-06-30 01:29:18 +00:00
|
|
|
|
using Microsoft.Win32;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
using Asm65;
|
|
|
|
|
|
2020-06-24 00:21:18 +00:00
|
|
|
|
namespace SourceGen.Tools.Omf.WpfGui {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Apple IIgs OMF file viewer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class OmfViewer : Window, INotifyPropertyChanged {
|
2020-06-30 01:29:18 +00:00
|
|
|
|
private const string OUT_FILE_SUFFIX = "_ld";
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
private OmfFile mOmfFile;
|
|
|
|
|
private Formatter mFormatter;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
|
|
|
|
//private Brush mDefaultLabelColor = SystemColors.WindowTextBrush;
|
|
|
|
|
|
|
|
|
|
// INotifyPropertyChanged implementation
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SegmentListItem {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
public OmfSegment OmfSeg { get; private set; }
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-26 00:30:44 +00:00
|
|
|
|
public int SegNum {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
get { return OmfSeg.SegNum; }
|
|
|
|
|
}
|
|
|
|
|
public string Version {
|
|
|
|
|
get { return OmfSegment.VersionToString(OmfSeg.Version); }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
public string Kind {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
get { return OmfSegment.KindToString(OmfSeg.Kind); }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
public string LoadName {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
get { return OmfSeg.LoadName; }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
public string SegName {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
get { return OmfSeg.SegName; }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
2020-06-26 23:58:42 +00:00
|
|
|
|
public int Length {
|
|
|
|
|
get { return OmfSeg.Length; }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
public int FileLength {
|
2020-06-29 03:50:41 +00:00
|
|
|
|
get { return OmfSeg.RawFileLength; }
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SegmentListItem(OmfSegment omfSeg) {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
OmfSeg = omfSeg;
|
|
|
|
|
}
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return "[SegmentListItem " + OmfSeg + "]";
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SegmentListItem> SegmentListItems { get; private set; } = new List<SegmentListItem>();
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
private string mPathName;
|
|
|
|
|
public string PathName {
|
|
|
|
|
get { return mPathName; }
|
|
|
|
|
set { mPathName = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 18:56:51 +00:00
|
|
|
|
private string mFileSummaryStr;
|
|
|
|
|
public string FileSummaryStr {
|
|
|
|
|
get { return mFileSummaryStr; }
|
|
|
|
|
set { mFileSummaryStr = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
private string mMessageStrings;
|
|
|
|
|
public string MessageStrings {
|
|
|
|
|
get { return mMessageStrings; }
|
|
|
|
|
set { mMessageStrings = value; OnPropertyChanged(); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 01:29:18 +00:00
|
|
|
|
public bool IsLoadFile {
|
|
|
|
|
get { return mOmfFile.OmfFileKind == OmfFile.FileKind.Load; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 20:47:19 +00:00
|
|
|
|
public bool OffsetSegmentStart {
|
|
|
|
|
get { return AppSettings.Global.GetBool(AppSettings.OMF_OFFSET_SEGMENT_START, true); }
|
|
|
|
|
set {
|
|
|
|
|
AppSettings.Global.SetBool(AppSettings.OMF_OFFSET_SEGMENT_START, value);
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AddNotes {
|
|
|
|
|
get { return AppSettings.Global.GetBool(AppSettings.OMF_ADD_NOTES, false); }
|
|
|
|
|
set {
|
|
|
|
|
AppSettings.Global.SetBool(AppSettings.OMF_ADD_NOTES, value);
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="owner">Parent window.</param>
|
|
|
|
|
/// <param name="pathName">Path to file on disk. Only used for display.</param>
|
|
|
|
|
/// <param name="data">File contents.</param>
|
2020-06-29 03:50:41 +00:00
|
|
|
|
/// <param name="formatter">Text formatter.</param>
|
2020-06-26 23:58:42 +00:00
|
|
|
|
public OmfViewer(Window owner, string pathName, byte[] data, Formatter formatter) {
|
2020-06-24 00:21:18 +00:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
Owner = owner;
|
|
|
|
|
DataContext = this;
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
PathName = pathName;
|
|
|
|
|
mFormatter = formatter;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
mOmfFile = new OmfFile(data);
|
2020-06-28 01:06:26 +00:00
|
|
|
|
mOmfFile.Analyze(mFormatter);
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
2020-06-28 18:56:51 +00:00
|
|
|
|
string summary;
|
|
|
|
|
if (mOmfFile.OmfFileKind == OmfFile.FileKind.Foreign) {
|
|
|
|
|
summary = (string)FindResource("str_OmfFileNot");
|
|
|
|
|
} else {
|
|
|
|
|
string fileStr;
|
|
|
|
|
switch (mOmfFile.OmfFileKind) {
|
|
|
|
|
case OmfFile.FileKind.Indeterminate:
|
|
|
|
|
fileStr = (string)FindResource("str_OmfFileIndeterminateStr");
|
|
|
|
|
break;
|
|
|
|
|
case OmfFile.FileKind.Load:
|
|
|
|
|
fileStr = (string)FindResource("str_OmfFileLoadStr");
|
|
|
|
|
break;
|
|
|
|
|
case OmfFile.FileKind.Object:
|
|
|
|
|
fileStr = (string)FindResource("str_OmfFileObjectStr");
|
|
|
|
|
break;
|
|
|
|
|
case OmfFile.FileKind.Library:
|
|
|
|
|
fileStr = (string)FindResource("str_OmfFileLibraryStr");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fileStr = (string)FindResource("str_OmfFileUnknownStr");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string fmt;
|
|
|
|
|
if (mOmfFile.SegmentList.Count == 1) {
|
|
|
|
|
fmt = (string)FindResource("str_OmfFileSummaryFmt");
|
|
|
|
|
} else {
|
|
|
|
|
fmt = (string)FindResource("str_OmfFileSummaryPlFmt");
|
|
|
|
|
}
|
|
|
|
|
summary = string.Format(fmt, fileStr, mOmfFile.SegmentList.Count);
|
|
|
|
|
}
|
|
|
|
|
FileSummaryStr = summary;
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
foreach (OmfSegment omfSeg in mOmfFile.SegmentList) {
|
2020-06-26 00:30:44 +00:00
|
|
|
|
SegmentListItems.Add(new SegmentListItem(omfSeg));
|
|
|
|
|
}
|
2020-06-26 23:58:42 +00:00
|
|
|
|
MessageStrings = string.Join("\r\n", mOmfFile.MessageList.ToArray());
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SegmentList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
SegmentListItem item = (SegmentListItem)((DataGrid)sender).SelectedItem;
|
|
|
|
|
OmfSegmentViewer dlg = new OmfSegmentViewer(this, mOmfFile, item.OmfSeg, mFormatter);
|
|
|
|
|
dlg.ShowDialog();
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
2020-06-30 01:29:18 +00:00
|
|
|
|
|
|
|
|
|
private void GenerateProject_Click(object sender, RoutedEventArgs e) {
|
2020-07-20 20:47:19 +00:00
|
|
|
|
Loader.Flags flags = 0;
|
|
|
|
|
if (AddNotes) {
|
|
|
|
|
flags |= Loader.Flags.AddNotes;
|
|
|
|
|
}
|
|
|
|
|
if (OffsetSegmentStart) {
|
|
|
|
|
flags |= Loader.Flags.OffsetSegmentStart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Loader loader = new Loader(mOmfFile, mFormatter, flags);
|
2020-06-30 01:29:18 +00:00
|
|
|
|
if (!loader.Prepare()) {
|
|
|
|
|
// Unexpected. If there's a valid reason for this, we need to add details
|
|
|
|
|
// to the error message.
|
|
|
|
|
string msg = (string)FindResource("str_OmfLoaderFail");
|
|
|
|
|
MessageBox.Show(msg, Res.Strings.OPERATION_FAILED);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaveFileDialog fileDlg = new SaveFileDialog() {
|
|
|
|
|
Filter = Res.Strings.FILE_FILTER_ALL,
|
|
|
|
|
FilterIndex = 1,
|
|
|
|
|
ValidateNames = true,
|
|
|
|
|
AddExtension = true,
|
|
|
|
|
FileName = Path.GetFileName(PathName) + OUT_FILE_SUFFIX
|
|
|
|
|
};
|
|
|
|
|
if (fileDlg.ShowDialog() != true) {
|
|
|
|
|
Debug.WriteLine("Save canceled by user");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string pathName = Path.GetFullPath(fileDlg.FileName);
|
|
|
|
|
|
|
|
|
|
if (!loader.WriteProjectFiles(pathName, pathName + ProjectFile.FILENAME_EXT,
|
|
|
|
|
out string errorMessage)) {
|
|
|
|
|
MessageBox.Show(Res.Strings.ERR_PROJECT_SAVE_FAIL + ": " + errorMessage,
|
|
|
|
|
Res.Strings.OPERATION_FAILED,
|
|
|
|
|
MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Success! Show a message then close the dialog.
|
|
|
|
|
string smsg = (string)FindResource("str_OmfConvertSuccessful");
|
|
|
|
|
MessageBox.Show(smsg, Res.Strings.OPERATION_SUCCEEDED,
|
|
|
|
|
MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
|
DialogResult = true;
|
|
|
|
|
}
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|