diff --git a/Z64Utils/Common/BitUtils.cs b/Z64Utils/Common/BitUtils.cs index 149e34f..05b3c9e 100644 --- a/Z64Utils/Common/BitUtils.cs +++ b/Z64Utils/Common/BitUtils.cs @@ -22,7 +22,7 @@ public override string ToString() if (Value.HasFlag(v)) { var name = Enum.GetName(typeof(T), v); - Debug.Assert(name != null); + Utils.Assert(name != null); if (count == 0) ret = name; else @@ -33,7 +33,7 @@ public override string ToString() if (count == 0) { var name = Enum.GetName(typeof(T), 0); - Debug.Assert(name != null); + Utils.Assert(name != null); return name; } diff --git a/Z64Utils/Common/Utils.cs b/Z64Utils/Common/Utils.cs index 82d6798..a8169f6 100644 --- a/Z64Utils/Common/Utils.cs +++ b/Z64Utils/Common/Utils.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Runtime.InteropServices; @@ -130,5 +131,18 @@ public static byte[] ZlibDecompress(byte[] input, int decSize) return outStream.GetBuffer(); } } + + class AssertFailedException : Exception { } + + // Avoid using Debug.Assert because that uses FailFast which kills the application + // without giving us a chance to log the exception. + [Conditional("DEBUG")] + public static void Assert([DoesNotReturnIf(false)] bool condition) + { + if (!condition) + { + throw new AssertFailedException(); + } + } } } diff --git a/Z64Utils/F3DZEX/Command/CmdInfo.cs b/Z64Utils/F3DZEX/Command/CmdInfo.cs index 4c63fed..3501b4a 100644 --- a/Z64Utils/F3DZEX/Command/CmdInfo.cs +++ b/Z64Utils/F3DZEX/Command/CmdInfo.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Common; using RDP; namespace F3DZEX.Command @@ -407,7 +408,7 @@ private static object ConvertCommand(Type t, CmdInfo cmd) throw new InvalidF3DZEXOpCodeException("Invalid ID"); object? obj = Activator.CreateInstance(t); - Debug.Assert(obj != null); + Utils.Assert(obj != null); foreach (var prop in t.GetProperties()) { diff --git a/Z64Utils/F3DZEX/Disassembler/Disassembler.cs b/Z64Utils/F3DZEX/Disassembler/Disassembler.cs index 8b1824f..abd909a 100644 --- a/Z64Utils/F3DZEX/Disassembler/Disassembler.cs +++ b/Z64Utils/F3DZEX/Disassembler/Disassembler.cs @@ -73,7 +73,7 @@ public List Disassemble() if (macroDis != null) { dis = macroDis; - Debug.Assert(cmdCount > 0); + Utils.Assert(cmdCount > 0); toSkip = cmdCount - 1; comments = new List() { diff --git a/Z64Utils/F3DZEX/Memory.cs b/Z64Utils/F3DZEX/Memory.cs index 601e85c..78c3eac 100644 --- a/Z64Utils/F3DZEX/Memory.cs +++ b/Z64Utils/F3DZEX/Memory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Text; +using Common; using RDP; using Z64; @@ -118,7 +119,7 @@ public byte[] ReadBytes(uint vaddr, int count) { case SegmentType.Fill: { - Debug.Assert(seg.Data != null); + Utils.Assert(seg.Data != null); byte[] buff = new byte[count]; int rest = count; int dstOff = 0; @@ -139,7 +140,7 @@ public byte[] ReadBytes(uint vaddr, int count) return buff; } case SegmentType.Buffer: - Debug.Assert(seg.Data != null); + Utils.Assert(seg.Data != null); if (addr.SegmentOff + count <= seg.Data.Length) { byte[] buff = new byte[count]; diff --git a/Z64Utils/F3DZEX/Render/Renderer.cs b/Z64Utils/F3DZEX/Render/Renderer.cs index 0369303..836ddb8 100644 --- a/Z64Utils/F3DZEX/Render/Renderer.cs +++ b/Z64Utils/F3DZEX/Render/Renderer.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Common; using F3DZEX.Command; using N64; using OpenTK; @@ -324,12 +325,12 @@ void AssertInitialized() { if (!_initialized) throw new Exception("Not initialized (yet)!"); - Debug.Assert(_rdpVtxDrawer != null); - Debug.Assert(_gridDrawer != null); - Debug.Assert(_axisDrawer != null); - Debug.Assert(_textDrawer != null); - Debug.Assert(_tex0 != null); - Debug.Assert(_tex1 != null); + Utils.Assert(_rdpVtxDrawer != null); + Utils.Assert(_gridDrawer != null); + Utils.Assert(_axisDrawer != null); + Utils.Assert(_textDrawer != null); + Utils.Assert(_tex0 != null); + Utils.Assert(_tex1 != null); } public void ClearErrors() => ErrorMsg = null; diff --git a/Z64Utils/F3DZEX/Render/TextDrawer.cs b/Z64Utils/F3DZEX/Render/TextDrawer.cs index f4a6eba..97daf13 100644 --- a/Z64Utils/F3DZEX/Render/TextDrawer.cs +++ b/Z64Utils/F3DZEX/Render/TextDrawer.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; +using Common; using OpenTK; using OpenTK.Graphics.OpenGL; using OpenTK.Mathematics; @@ -57,9 +58,9 @@ public TextDrawer() _tex = new TextureHandler(); TextFont = new Font("Arial", 50); - Debug.Assert(_font != null); - Debug.Assert(_charSpaces != null); - Debug.Assert(_texData != null); + Utils.Assert(_font != null); + Utils.Assert(_charSpaces != null); + Utils.Assert(_texData != null); _lastStr = null; Scale = 0.35f; diff --git a/Z64Utils/Forms/AnalyzerSettingsForm.cs b/Z64Utils/Forms/AnalyzerSettingsForm.cs index 9436728..2b33dde 100644 --- a/Z64Utils/Forms/AnalyzerSettingsForm.cs +++ b/Z64Utils/Forms/AnalyzerSettingsForm.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Common; using F3DZEX.Command; namespace Z64.Forms @@ -79,7 +80,7 @@ private void buttonRestrivtive_Click(object sender, EventArgs e) foreach (var patternStr in patternsStr) { var pattern = Z64ObjectAnalyzer.Config.OpCodePattern.Parse(patternStr); - Debug.Assert(pattern != null); + Utils.Assert(pattern != null); Result.Patterns.Add(pattern); } UpdateTextBoxes(); diff --git a/Z64Utils/Forms/CollisionViewerForm.cs b/Z64Utils/Forms/CollisionViewerForm.cs index 9f07467..d8c7ea2 100644 --- a/Z64Utils/Forms/CollisionViewerForm.cs +++ b/Z64Utils/Forms/CollisionViewerForm.cs @@ -128,8 +128,8 @@ private void NewRender() { _polygons = new RenderColPoly[_colHeader.NbPolygons]; - Debug.Assert(_colHeader.PolygonsHolder != null); - Debug.Assert(_colHeader.VerticesHolder != null); + Utils.Assert(_colHeader.PolygonsHolder != null); + Utils.Assert(_colHeader.VerticesHolder != null); for (int i = 0; i < _colHeader.NbPolygons; i++) { Z64Object.CollisionPolygonsHolder.CollisionPoly colPoly = _colHeader diff --git a/Z64Utils/Forms/DListViewerForm.cs b/Z64Utils/Forms/DListViewerForm.cs index 1824137..fb7beba 100644 --- a/Z64Utils/Forms/DListViewerForm.cs +++ b/Z64Utils/Forms/DListViewerForm.cs @@ -136,7 +136,7 @@ void RenderCallback(Matrix4 proj, Matrix4 view) _renderer.ModelMtxStack.Push( Matrix4.CreateTranslation(routine.X, routine.Y, routine.Z) ); - Debug.Assert(routine.Dlist != null); + Utils.Assert(routine.Dlist != null); _renderer.RenderDList(routine.Dlist); _renderer.ModelMtxStack.Pop(); } @@ -367,13 +367,13 @@ private void AddRoutineMenuItem_Click(object sender, System.EventArgs e) ); if (form.ShowDialog() == DialogResult.OK) { - Debug.Assert(form.Result != null); + Utils.Assert(form.Result != null); var parts = form.Result.Replace(" ", "").Split(";"); int x = 0, y = 0, z = 0; var addr = SegmentedAddress.Parse(parts[0], true); - Debug.Assert(addr != null); + Utils.Assert(addr != null); if (parts.Length > 1) { x = int.Parse(parts[1]); @@ -401,12 +401,12 @@ private void EditRoutineMenuItem_Click(object sender, EventArgs e) if (form.ShowDialog() == DialogResult.OK) { - Debug.Assert(form.Result != null); + Utils.Assert(form.Result != null); var parts = form.Result.Replace(" ", "").Split(";"); routine.X = routine.Y = routine.Z = 0; var addr = SegmentedAddress.Parse(parts[0], true); - Debug.Assert(addr != null); + Utils.Assert(addr != null); routine.Address = addr; if (parts.Length > 1) diff --git a/Z64Utils/Forms/MainForm.cs b/Z64Utils/Forms/MainForm.cs index 785ed23..a67db02 100644 --- a/Z64Utils/Forms/MainForm.cs +++ b/Z64Utils/Forms/MainForm.cs @@ -84,8 +84,8 @@ private void UpdateControls(bool inTask = false) private void UpdateFileList(bool forceReload) { - Debug.Assert(_game != null); - Debug.Assert(_fileItemsText != null); + Utils.Assert(_game != null); + Utils.Assert(_fileItemsText != null); if (forceReload) _lastSearch = null; @@ -153,7 +153,7 @@ private void OpenObjectAnalyzer(Z64Game game, byte[] data, string fileName, stri if (valueForm.ShowDialog() == DialogResult.OK) { - Debug.Assert(valueForm.Result != null); + Utils.Assert(valueForm.Result != null); var form = new ObjectAnalyzerForm( game, data, @@ -237,7 +237,7 @@ private void RomOpenItem_Click(object sender, EventArgs e) private void RomSaveItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); saveFileDialog1.FileName = ""; saveFileDialog1.Filter = $"{Filters.N64}|{Filters.ALL}"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) @@ -250,7 +250,7 @@ private void RomSaveItem_Click(object sender, EventArgs e) private void RomExportFsItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); folderBrowserDialog1.SelectedPath = ""; if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { @@ -280,7 +280,7 @@ private void RomExportFsItem_Click(object sender, EventArgs e) private void RomImportNamesItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); openFileDialog1.FileName = ""; openFileDialog1.Filter = $"{Filters.TXT}|{Filters.ALL}"; if (openFileDialog1.ShowDialog() == DialogResult.OK) @@ -292,7 +292,7 @@ private void RomImportNamesItem_Click(object sender, EventArgs e) private void RomExportNamesItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); saveFileDialog1.FileName = ""; saveFileDialog1.Filter = $"{Filters.TXT}|{Filters.ALL}"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) @@ -303,7 +303,7 @@ private void RomExportNamesItem_Click(object sender, EventArgs e) private void InjectToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); if (listView_files.SelectedIndices.Count != 1) return; var item = listView_files.SelectedItems[0]; @@ -320,7 +320,7 @@ private void InjectToolStripMenuItem_Click(object sender, EventArgs e) private void SaveToolStripMenuItem1_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); if (listView_files.SelectedIndices.Count != 1) return; var item = listView_files.SelectedItems[0]; @@ -362,7 +362,7 @@ private void OpenObjectViewerForExternalFile(byte[] data, string fileName) if (valueForm.ShowDialog() == DialogResult.OK) { - Debug.Assert(valueForm.Result != null); + Utils.Assert(valueForm.Result != null); var form = new ObjectAnalyzerForm( _game, data, @@ -376,7 +376,7 @@ private void OpenObjectViewerForExternalFile(byte[] data, string fileName) private void OpenObjectViewerToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); if (listView_files.SelectedIndices.Count != 1) return; var item = listView_files.SelectedItems[0]; @@ -401,7 +401,7 @@ private void OpenObjectViewerToolStripMenuItem_Click(object sender, EventArgs e) private void renameToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); if (listView_files.SelectedIndices.Count != 1) return; var item = listView_files.SelectedItems[0]; @@ -416,7 +416,7 @@ private void renameToolStripMenuItem_Click(object sender, EventArgs e) ); if (valueForm.ShowDialog() == DialogResult.OK) { - Debug.Assert(valueForm.Result != null); + Utils.Assert(valueForm.Result != null); _game.Version.RenameFile(file.VRomStart, valueForm.Result); listView_files.SelectedItems[0].Text = valueForm.Result; } @@ -429,7 +429,7 @@ private void TextBox_fileFilter_TextChanged(object sender, EventArgs e) private void OpenDlistViewerToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); DListViewerForm.OpenInstance(_game); } @@ -440,13 +440,13 @@ private void f3DEXDisassemblerToolStripMenuItem_Click(object sender, EventArgs e private void ROMRAMConversionsToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); new ConversionForm(_game).Show(); } private void textureViewerToolStripMenuItem_Click(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); new TextureViewer(_game).Show(); } @@ -492,7 +492,7 @@ private void objectAnalyzerToolStripMenuItem_Click(object sender, EventArgs e) private void listView_files_DoubleClick(object sender, EventArgs e) { - Debug.Assert(_game != null); + Utils.Assert(_game != null); if (listView_files.SelectedIndices.Count != 1) return; var item = listView_files.SelectedItems[0]; diff --git a/Z64Utils/Forms/ModelViewerControl.cs b/Z64Utils/Forms/ModelViewerControl.cs index b154afd..cb3176b 100644 --- a/Z64Utils/Forms/ModelViewerControl.cs +++ b/Z64Utils/Forms/ModelViewerControl.cs @@ -18,6 +18,8 @@ namespace Z64.Forms { public partial class ModelViewerControl : GLControl { + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + public Matrix4 Projection => _projectionMtx; public Matrix4 View => _viewMtx; @@ -120,7 +122,7 @@ public void Render() } catch (Exception ex) { - Console.WriteLine(ex.Message); + Logger.Warn(ex); } if (Height == 0) diff --git a/Z64Utils/Forms/ObjectAnalyzerForm.cs b/Z64Utils/Forms/ObjectAnalyzerForm.cs index 9e1b293..e66c96e 100644 --- a/Z64Utils/Forms/ObjectAnalyzerForm.cs +++ b/Z64Utils/Forms/ObjectAnalyzerForm.cs @@ -613,18 +613,18 @@ private void listView_map_SelectedIndexChanged(object sender, EventArgs e) sw.WriteLine($"Sibling: 0x{limb.Sibling:X2}"); if (limb.Type != Z64Object.EntryType.SkinLimb) { - Debug.Assert(limb.DListSeg != null); + Utils.Assert(limb.DListSeg != null); sw.WriteLine($"DList : 0x{limb.DListSeg.VAddr:X8}"); } if (limb.Type == Z64Object.EntryType.LODLimb) { - Debug.Assert(limb.DListFarSeg != null); + Utils.Assert(limb.DListFarSeg != null); sw.WriteLine($"Far DList : 0x{limb.DListFarSeg.VAddr:X8}"); } else if (limb.Type == Z64Object.EntryType.SkinLimb) { - Debug.Assert(limb.SkinSeg != null); + Utils.Assert(limb.SkinSeg != null); sw.WriteLine($"Data Type : {limb.SegmentType}"); // TODO describe data instead of printing number? sw.WriteLine($"Data Segment : 0x{limb.SkinSeg.VAddr:X8}"); } @@ -728,13 +728,13 @@ private void openSkeletonViewerMenuItem_Click(object sender, EventArgs e) private void openSkeletonViewer() { var holder = GetCurrentHolder(); - Debug.Assert(holder != null); + Utils.Assert(holder != null); switch (holder.GetEntryType()) { case Z64Object.EntryType.FlexSkeletonHeader: { var skel = GetCurrentHolder(); - Debug.Assert(skel != null); + Utils.Assert(skel != null); List anims = new List(); _obj.Entries.ForEach(e => { @@ -755,7 +755,7 @@ private void openSkeletonViewer() case Z64Object.EntryType.SkeletonHeader: { var skel = GetCurrentHolder(); - Debug.Assert(skel != null); + Utils.Assert(skel != null); List anims = new List(); _obj.Entries.ForEach(e => { @@ -779,7 +779,7 @@ private void openSkeletonViewer() private void addToDisplayViewerMenuItem_Click(object sender, EventArgs e) { var holder = GetCurrentHolder(); - Debug.Assert(holder != null); + Utils.Assert(holder != null); if (holder.GetEntryType() == Z64Object.EntryType.DList) { DListViewerForm.OpenInstance(_game); @@ -789,7 +789,7 @@ private void addToDisplayViewerMenuItem_Click(object sender, EventArgs e) ); var dlist = GetCurrentHolder(); - Debug.Assert(dlist != null); + Utils.Assert(dlist != null); DListViewerForm.Instance.AddDList( new SegmentedAddress(_segment, _obj.OffsetOf(dlist)).VAddr ); @@ -804,7 +804,7 @@ private void openInDisplayViewerMenuItem_Click(object sender, EventArgs e) private void openDisplayList() { var holder = GetCurrentHolder(); - Debug.Assert(holder != null); + Utils.Assert(holder != null); if (holder.GetEntryType() == Z64Object.EntryType.DList) { DListViewerForm.OpenInstance(_game); @@ -814,7 +814,7 @@ private void openDisplayList() ); var dlist = GetCurrentHolder(); - Debug.Assert(dlist != null); + Utils.Assert(dlist != null); DListViewerForm.Instance.SetSingleDlist( new SegmentedAddress(_segment, _obj.OffsetOf(dlist)).VAddr ); @@ -829,7 +829,7 @@ private void showInCollisionViewerToolStripMenuItem_Click(object sender, EventAr private void openCollisionHeader() { var holder = GetCurrentHolder(); - Debug.Assert(holder != null); + Utils.Assert(holder != null); if (holder.GetEntryType() == Z64Object.EntryType.CollisionHeader) { CollisionViewerForm.OpenInstance(_game); @@ -1004,7 +1004,7 @@ private void exportCToolStripMenuItem_Click(object sender, EventArgs e) case Z64Object.EntryType.StandardLimb: { var holder = (Z64Object.SkeletonLimbHolder)entry; - Debug.Assert(holder.DListSeg != null); + Utils.Assert(holder.DListSeg != null); sw.WriteLine( $"StandardLimb {entry.Name} = {{ {{ {holder.JointX}, {holder.JointY}, {holder.JointZ} }}, {holder.Child}, {holder.Sibling}, 0x{holder.DListSeg.VAddr:X8} }};" ); @@ -1013,8 +1013,8 @@ private void exportCToolStripMenuItem_Click(object sender, EventArgs e) case Z64Object.EntryType.LODLimb: { var holder = (Z64Object.SkeletonLimbHolder)entry; - Debug.Assert(holder.DListSeg != null); - Debug.Assert(holder.DListFarSeg != null); + Utils.Assert(holder.DListSeg != null); + Utils.Assert(holder.DListFarSeg != null); sw.WriteLine( $"LodLimb {entry.Name} = {{ {{ {holder.JointX}, {holder.JointY}, {holder.JointZ} }}, {holder.Child}, {holder.Sibling}, 0x{holder.DListSeg.VAddr:X8}, 0x{holder.DListFarSeg.VAddr:X8} }};" ); @@ -1023,7 +1023,7 @@ private void exportCToolStripMenuItem_Click(object sender, EventArgs e) case Z64Object.EntryType.SkinLimb: { var holder = (Z64Object.SkeletonLimbHolder)entry; - Debug.Assert(holder.SkinSeg != null); + Utils.Assert(holder.SkinSeg != null); sw.WriteLine( $"SkinLimb {entry.Name} = {{ {{ {holder.JointX}, {holder.JointY}, {holder.JointZ} }}, {holder.Child}, {holder.Sibling}, 0x{holder.SegmentType}, 0x{holder.SkinSeg.VAddr:X8} }};" ); diff --git a/Z64Utils/Forms/SegmentControl.cs b/Z64Utils/Forms/SegmentControl.cs index 55f00cc..994ef6b 100644 --- a/Z64Utils/Forms/SegmentControl.cs +++ b/Z64Utils/Forms/SegmentControl.cs @@ -59,7 +59,7 @@ private void importFileButton_Click(object sender, EventArgs e) form.Text += " " + SegmentID; if (form.ShowDialog() == DialogResult.OK) { - Debug.Assert(form.ResultSegment != null); + Utils.Assert(form.ResultSegment != null); SetSegment(form.ResultSegment); SegmentChanged?.Invoke(this, new(_segmentId, form.ResultSegment)); } diff --git a/Z64Utils/Forms/SegmentEditForm.cs b/Z64Utils/Forms/SegmentEditForm.cs index 9b95726..c429d7f 100644 --- a/Z64Utils/Forms/SegmentEditForm.cs +++ b/Z64Utils/Forms/SegmentEditForm.cs @@ -104,12 +104,12 @@ private void button1_Click(object sender, EventArgs e) { if (comboBox1.SelectedItem == (object)SRC_ROM_FS) // ROM FS { - Debug.Assert(_game != null); + Utils.Assert(_game != null); DmaFileSelectForm form = new DmaFileSelectForm(_game); if (form.ShowDialog() == DialogResult.OK) { - Debug.Assert(form.SelectedFile != null); - Debug.Assert(form.SelectedFile.Valid()); // DmaFileSelectForm only allows selecting valid files + Utils.Assert(form.SelectedFile != null); + Utils.Assert(form.SelectedFile.Valid()); // DmaFileSelectForm only allows selecting valid files _dmaFileName = _game.GetFileName(form.SelectedFile.VRomStart); ResultSegment = Memory.Segment.FromBytes(_dmaFileName, form.SelectedFile.Data); button1.ForeColor = Color.Green; @@ -153,7 +153,7 @@ private void okBtn_Click(object sender, EventArgs e) { case SRC_ADDR: var sa = SegmentedAddress.Parse(addressValue.Text); - Debug.Assert(sa != null); // OK button is only enabled if the value is valid + Utils.Assert(sa != null); // OK button is only enabled if the value is valid uint addr = sa.VAddr; ResultSegment = Memory.Segment.FromVram($"{addr:X8}", addr); break; diff --git a/Z64Utils/Forms/SkeletonViewerForm.cs b/Z64Utils/Forms/SkeletonViewerForm.cs index 05dfd7a..ee80683 100644 --- a/Z64Utils/Forms/SkeletonViewerForm.cs +++ b/Z64Utils/Forms/SkeletonViewerForm.cs @@ -5,6 +5,7 @@ using System.Drawing; using System.IO; using System.Windows.Forms; +using Common; using OpenTK; using OpenTK.Graphics.OpenGL; using OpenTK.Mathematics; @@ -272,7 +273,7 @@ void UpdateCurPose() } else { - Debug.Assert(_curPlayerAnim != null); + Utils.Assert(_curPlayerAnim != null); _curPose = Z64SkeletonPose .Get(_skeleton, _curPlayerAnim, trackBar_anim.Value) .LimbsPose; @@ -372,7 +373,7 @@ void UpdateLimbsDlists() { if (limb.Type != EntryType.StandardLimb && limb.Type != EntryType.LODLimb) throw new Exception($"Unimplemented limb type in skeleton viewer {limb.Type}"); - Debug.Assert(limb.DListSeg != null); // always set for Standard and LOD limbs + Utils.Assert(limb.DListSeg != null); // always set for Standard and LOD limbs F3DZEX.Command.Dlist? dlist = null; try { @@ -431,7 +432,7 @@ void AddLimbRoutine(TreeNode parent, Z64SkeletonTreeLimb treeLimb) // Update anims -> matrices void UpdateRegularAnim() { - Debug.Assert(_curRegularAnim != null); + Utils.Assert(_curRegularAnim != null); trackBar_anim.Minimum = 0; trackBar_anim.Maximum = _curRegularAnim.FrameCount - 1; @@ -442,7 +443,7 @@ void UpdateRegularAnim() void UpdatePlayerAnim() { - Debug.Assert(_curPlayerAnim != null); + Utils.Assert(_curPlayerAnim != null); trackBar_anim.Minimum = 0; trackBar_anim.Maximum = _curPlayerAnim.FrameCount - 1; @@ -611,7 +612,7 @@ private void listBox_anims_SelectedIndexChanged(object sender, EventArgs e) if (savedSegment != null) { - Debug.Assert(savedData != null); + Utils.Assert(savedData != null); _renderer.Memory.Segments[(int)savedSegment] = savedData; } @@ -622,7 +623,7 @@ private void listBox_anims_SelectedIndexChanged(object sender, EventArgs e) { var curPlayerAnimationEntry = curAnimEntry as ISkeletonViewerPlayerAnimationEntry; - Debug.Assert(curPlayerAnimationEntry != null); + Utils.Assert(curPlayerAnimationEntry != null); var curPAH = curPlayerAnimationEntry.GetPlayerAnim(); @@ -631,8 +632,8 @@ private void listBox_anims_SelectedIndexChanged(object sender, EventArgs e) var Saved = _renderer.Memory.Segments[curPAH.PlayerAnimationSegment.SegmentId]; var link_animetionFile = _game.GetFileByName("link_animetion"); - Debug.Assert(link_animetionFile != null); - Debug.Assert(link_animetionFile.Valid()); + Utils.Assert(link_animetionFile != null); + Utils.Assert(link_animetionFile.Valid()); _renderer.Memory.Segments[curPAH.PlayerAnimationSegment.SegmentId] = F3DZEX.Memory.Segment.FromBytes("link_animetion", link_animetionFile.Data); diff --git a/Z64Utils/Forms/TextureViewer.cs b/Z64Utils/Forms/TextureViewer.cs index 4f1763e..290f392 100644 --- a/Z64Utils/Forms/TextureViewer.cs +++ b/Z64Utils/Forms/TextureViewer.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Common; using N64; namespace Z64.Forms @@ -45,7 +46,7 @@ private byte[] ReadBytes(uint addr, int size) if (addr >= file.VRomStart && addr < file.VRomEnd) { byte[] buffer = new byte[size]; - Debug.Assert(file.Valid()); + Utils.Assert(file.Valid()); Buffer.BlockCopy( file.Data, (int)(addr - (uint)file.VRomStart), diff --git a/Z64Utils/N64/N64Texture.cs b/Z64Utils/N64/N64Texture.cs index 8740d47..1579434 100644 --- a/Z64Utils/N64/N64Texture.cs +++ b/Z64Utils/N64/N64Texture.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Common; using F3DZEX.Command; using Syroot.BinaryData; @@ -245,10 +246,10 @@ public static byte[] Decode( switch (siz) { case G_IM_SIZ.G_IM_SIZ_4b: - Debug.Assert(tlut != null); + Utils.Assert(tlut != null); return DecodeCI4(texels, buff, tlut); case G_IM_SIZ.G_IM_SIZ_8b: - Debug.Assert(tlut != null); + Utils.Assert(tlut != null); return DecodeCI8(texels, buff, tlut); default: throw new N64TextureException( diff --git a/Z64Utils/NLog-Debug.config b/Z64Utils/NLog-Debug.config new file mode 100644 index 0000000..00d46d9 --- /dev/null +++ b/Z64Utils/NLog-Debug.config @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Z64Utils/NLog-Release.config b/Z64Utils/NLog-Release.config new file mode 100644 index 0000000..532a8f5 --- /dev/null +++ b/Z64Utils/NLog-Release.config @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Z64Utils/Program.cs b/Z64Utils/Program.cs index 79053c3..53f3936 100644 --- a/Z64Utils/Program.cs +++ b/Z64Utils/Program.cs @@ -7,6 +7,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using Common; using Z64; using Z64.Forms; @@ -14,6 +15,7 @@ namespace Z64 { static class Program { + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); public static readonly string Version = Assembly .GetExecutingAssembly() @@ -31,6 +33,24 @@ static class Program /// [STAThread] static void Main() + { + try + { + Logger.Info("Starting up Z64Utils Version {Version}...", Version); + MainImpl(); + } + catch (Exception e) + { + Logger.Fatal(e); + throw; + } + finally + { + NLog.LogManager.Shutdown(); + } + } + + static void MainImpl() { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); diff --git a/Z64Utils/Z64/Z64Game.cs b/Z64Utils/Z64/Z64Game.cs index e91d994..2ac9096 100644 --- a/Z64Utils/Z64/Z64Game.cs +++ b/Z64Utils/Z64/Z64Game.cs @@ -215,7 +215,7 @@ public void InjectFile(int vrom, byte[] data) if (file == null) throw new Z64GameException("Invalid VROM"); - Debug.Assert(file.Data != null); + Utils.Assert(file.Data != null); int oldSize = file.Compressed ? file.RomEnd - file.RomStart : file.Data.Length; @@ -273,8 +273,8 @@ public void InjectFile(int vrom, byte[] data) private void FixDmaDataTable() { var dmatable = GetFile(GetVrom("dmadata")); - Debug.Assert(dmatable != null); - Debug.Assert(dmatable.Valid()); + Utils.Assert(dmatable != null); + Utils.Assert(dmatable.Valid()); byte[] newTable; using (MemoryStream ms = new MemoryStream()) @@ -363,7 +363,7 @@ private void GetFs(BinaryStream br, Action? progressCalback = nul { if (i == 2) //dmadata { - Debug.Assert(file.Data != null); + Utils.Assert(file.Data != null); filecount = file.Data.Length / 0x10; lastprogressI = -filecount; // force a progress update diff --git a/Z64Utils/Z64/Z64Object.cs b/Z64Utils/Z64/Z64Object.cs index 8ce2065..b62264c 100644 --- a/Z64Utils/Z64/Z64Object.cs +++ b/Z64Utils/Z64/Z64Object.cs @@ -299,7 +299,7 @@ public class SkeletonLimbHolder : ObjectHolder public SkeletonLimbHolder(string name, byte[] data, EntryType type) : base(name) { - Debug.Assert( + Utils.Assert( type == EntryType.StandardLimb || type == EntryType.LODLimb || type == EntryType.SkinLimb @@ -323,19 +323,19 @@ public override byte[] GetData() if (Type != EntryType.SkinLimb) { - Debug.Assert(DListSeg != null); + Utils.Assert(DListSeg != null); bw.Write(DListSeg.VAddr); } if (Type == EntryType.LODLimb) { - Debug.Assert(DListFarSeg != null); + Utils.Assert(DListFarSeg != null); bw.Write(DListFarSeg.VAddr); } else if (Type == EntryType.SkinLimb) { - Debug.Assert(SegmentType != null); - Debug.Assert(SkinSeg != null); + Utils.Assert(SegmentType != null); + Utils.Assert(SkinSeg != null); bw.Write((int)SegmentType); bw.Write(SkinSeg.VAddr); } diff --git a/Z64Utils/Z64/Z64ObjectAnalyzer.cs b/Z64Utils/Z64/Z64ObjectAnalyzer.cs index 68219a2..61e0f9d 100644 --- a/Z64Utils/Z64/Z64ObjectAnalyzer.cs +++ b/Z64Utils/Z64/Z64ObjectAnalyzer.cs @@ -627,7 +627,7 @@ public static void FindPlayerAnimations(Z64Object obj, byte[] data, int segmentI { if (obj.Game == null) return; - Debug.Assert(obj.FileName != null); + Utils.Assert(obj.FileName != null); // only search in gameplay_keep if (!obj.FileName.Contains("gameplay_keep")) @@ -681,7 +681,7 @@ public static void FindCollisionData(Z64Object obj, byte[] data, int segmentId) if (!obj.IsOffsetFree(i)) { Z64Object.ObjectHolder? holder = obj.GetHolderAtOffset(i); - Debug.Assert(holder != null); + Utils.Assert(holder != null); i = obj.OffsetOf(holder) + holder.GetSize(); if (i % 4 != 0) i += (4 - i % 4); @@ -859,7 +859,7 @@ Dictionary textureLoads if (!obj.IsOffsetFree(i)) { Z64Object.ObjectHolder? holder = obj.GetHolderAtOffset(i); - Debug.Assert(holder != null); + Utils.Assert(holder != null); i = obj.OffsetOf(holder) + holder.GetSize(); if (i % 4 != 0) i += (4 - i % 4); diff --git a/Z64Utils/Z64/Z64Version.cs b/Z64Utils/Z64/Z64Version.cs index 3f2f7ef..f3ca120 100644 --- a/Z64Utils/Z64/Z64Version.cs +++ b/Z64Utils/Z64/Z64Version.cs @@ -42,6 +42,8 @@ public enum Z64FileCompression public class Z64Version { + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + #region JSON Data Type private class AddrToStringConverter : JsonConverter @@ -101,8 +103,8 @@ public class VersionIdentifier [MemberNotNull(nameof(BuildDate))] public void AssertValid() { - Debug.Assert(BuildTeam != null); - Debug.Assert(BuildDate != null); + Utils.Assert(BuildTeam != null); + Utils.Assert(BuildDate != null); } } @@ -189,8 +191,8 @@ public Z64Version() [MemberNotNull(nameof(Identifier))] public void AssertValid() { - Debug.Assert(VersionName != null); - Debug.Assert(Identifier != null); + Utils.Assert(VersionName != null); + Utils.Assert(Identifier != null); } #endregion JSON Properties @@ -297,7 +299,7 @@ public void Save() }; options.Converters.Add(new JsonStringEnumConverter()); - Debug.Assert(_versions != null); + Utils.Assert(_versions != null); string path = _versions.First(v => v.Value == this).Key; // sort entries by vrom @@ -352,7 +354,7 @@ private static void LoadVersions() { string json = File.ReadAllText(file); var ver = JsonSerializer.Deserialize(json, options); - Debug.Assert(ver != null); + Utils.Assert(ver != null); ver.AssertValid(); ver.Identifier.AssertValid(); @@ -515,7 +517,7 @@ private static void ExportHashes(Z64Game game) if (file.Data.Length <= 0x10) { - Debug.WriteLine($"Skipping small file {name}"); + Logger.Debug($"Skipping small file {name}"); continue; } @@ -527,7 +529,7 @@ private static void ExportHashes(Z64Game game) { if (existing.fileName != entry.fileName) { - Debug.WriteLine( + Logger.Debug( $"name missmatch : {existing.fileName} ({existing.sha256}) -> {entry.fileName}" ); } @@ -536,7 +538,7 @@ private static void ExportHashes(Z64Game game) && existing.type == Z64FileType.Unknow ) { - Debug.WriteLine($"new type found : {existing.type} -> {entry.type}"); + Logger.Debug($"new type found : {existing.type} -> {entry.type}"); entries.Remove(existing); entries.Add(entry); modifCount++; @@ -552,7 +554,7 @@ private static void ExportHashes(Z64Game game) FileHashEntry.WriteEntries(hashPath, entries); - Debug.WriteLine($"{addCount} hash exported and {modifCount} hashes modifed!"); + Logger.Debug($"{addCount} hash exported and {modifCount} hashes modifed!"); #endif } @@ -608,7 +610,7 @@ private static void ImportHashes(Z64Game game) } } - Debug.WriteLine($"{foundCount} hashes imported!"); + Logger.Debug($"{foundCount} hashes imported!"); game.Version.Save(); #endif diff --git a/Z64Utils/Z64Utils.csproj b/Z64Utils/Z64Utils.csproj index b241157..52fa514 100644 --- a/Z64Utils/Z64Utils.csproj +++ b/Z64Utils/Z64Utils.csproj @@ -26,11 +26,20 @@ + + + + + + + + + True