From 37cec08d7c5ff59ed9c7293cf4aa78a619721cb5 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Mon, 26 Jan 2026 03:01:38 +0100 Subject: [PATCH] Fix Pck save & save as functionality :') --- PCK-Studio/Controls/EditorControl.cs | 23 +++++++++-- PCK-Studio/Controls/PckEditor.cs | 39 +++++-------------- PCK-Studio/MainForm.cs | 32 ++++++++++----- PCK-Studio/MainForm.resx | 25 ++++++------ PckStudio.Core/DelegatedFileSaveContext.cs | 13 +------ PckStudio.Core/Interfaces/IEditor.cs | 10 +++-- .../PckStudio.Rendering.csproj | 2 +- Vendor/OMI-Lib | 2 +- 8 files changed, 74 insertions(+), 72 deletions(-) diff --git a/PCK-Studio/Controls/EditorControl.cs b/PCK-Studio/Controls/EditorControl.cs index 6a7259fa..fffacd64 100644 --- a/PCK-Studio/Controls/EditorControl.cs +++ b/PCK-Studio/Controls/EditorControl.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PckStudio.Interfaces; +using System.IO; namespace PckStudio.Controls { @@ -15,15 +16,18 @@ namespace PckStudio.Controls { public T EditorValue { get; } - public ISaveContext SaveContext { get; } + public ISaveContext SaveContext { get; private set; } + + public string TitleName { get; } public EditorControl() { } - protected EditorControl(T value, ISaveContext saveContext) + protected EditorControl(string titleName, T value, ISaveContext saveContext) { _ = value ?? throw new ArgumentNullException(nameof(value)); + TitleName = titleName; EditorValue = value; SaveContext = saveContext; } @@ -35,9 +39,20 @@ namespace PckStudio.Controls base.OnControlRemoved(e); } - public void Save() => SaveContext.Save(EditorValue); + public void SetSaveContext(ISaveContext saveContext) => SaveContext = saveContext; - public virtual void SaveAs() => throw new NotImplementedException(); + protected virtual void PreSave() + { } + + protected virtual void PostSave() + { } + + public void Save() + { + PreSave(); + SaveContext.Save(EditorValue); + PostSave(); + } public virtual void Close() => throw new NotImplementedException(); diff --git a/PCK-Studio/Controls/PckEditor.cs b/PCK-Studio/Controls/PckEditor.cs index dc461bac..d6f1fac6 100644 --- a/PCK-Studio/Controls/PckEditor.cs +++ b/PCK-Studio/Controls/PckEditor.cs @@ -52,9 +52,6 @@ namespace PckStudio.Controls { internal partial class PckEditor : EditorControl { - - private string _location = string.Empty; - private readonly OMI.ByteOrder _originalEndianness; private OMI.ByteOrder _currentEndianness; private bool __modified = false; @@ -78,8 +75,8 @@ namespace PckStudio.Controls private readonly Dictionary> _pckAssetTypeHandler; - public PckEditor(PackInfo packInfo, ISaveContext saveContext) - : base(packInfo, saveContext) + public PckEditor(string name, PackInfo packInfo, ISaveContext saveContext) + : base(name, packInfo, saveContext) { InitializeComponent(); _onModifiedChangeDelegate = OnModify; @@ -140,26 +137,13 @@ namespace PckStudio.Controls [PckAssetType.MaterialFile] = HandleMaterialFile, }; } - - public new void Save() + + protected override void PostSave() { - base.Save(); _timesSaved++; _wasModified = false; - } - - public override void SaveAs() - { - using SaveFileDialog saveFileDialog = new SaveFileDialog - { - Filter = "PCK (Minecraft Console Package)|*.pck", - DefaultExt = ".pck", - }; - if (saveFileDialog.ShowDialog() == DialogResult.OK) - { - SaveTo(saveFileDialog.FileName); - pckFileLabel.Text = "Current PCK File: " + Path.GetFileName(_location); - } + MessageBox.Show("Pck Saved.", "Saved"); + Debug.WriteLine($"_timesSaved: {_timesSaved}"); } public override void Close() @@ -178,12 +162,6 @@ namespace PckStudio.Controls BuildMainTreeView(); } - private void SaveTo(string filepath) - { - _location = filepath; - Save(); - } - private void HandleInnerPckFile(PckAsset asset) { if (asset.Type != PckAssetType.SkinDataFile && asset.Type != PckAssetType.TexturePackInfoFile || asset.Size <= 0 || !Settings.Default.LoadSubPcks) @@ -199,7 +177,7 @@ namespace PckStudio.Controls }); string caption = Path.GetFileName(asset.Filename); - string identifier = _location + Path.GetFileName(asset.Filename); + string identifier = TitleName + Path.GetFileName(asset.Filename); PckFile pckFile = asset.GetData(new PckFileReader(_originalEndianness)); PackInfo packInfo = PackInfo.Create(pckFile, _originalEndianness, false); @@ -1313,7 +1291,8 @@ namespace PckStudio.Controls return; } - if (string.IsNullOrEmpty(_location)) + // TODO: ! -null + if (true) { MessageBox.Show(this, "You must save your pck before creating or opening a music cues PCK file", "Can't create audio.pck"); return; diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 4508f1c4..361e889c 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -30,7 +30,8 @@ namespace PckStudio { public partial class MainForm : MetroFramework.Forms.MetroForm { - private PckManager PckManager = null; + private const string DEFAULT_PCK_SAVECONTEXT_DESCRIPTION = "PCK (Minecraft Console Package)"; + private PckManager PckManager = null; private Dictionary openTabPages = new Dictionary(); @@ -67,14 +68,14 @@ namespace PckStudio tabControl.SelectTab(openTabPages[identifier]); return; } - var editor = new PckEditor(packInfo, saveContext); + var editor = new PckEditor(caption, packInfo: packInfo, saveContext: saveContext); AddPage(caption, identifier, editor); } private void AddEditorPage(string caption, string identifier, PackInfo packInfo, ISaveContext saveContext = null) { - saveContext ??= GetDefaultSaveContext("./new.pck", "PCK (Minecraft Console Package)"); - var editor = new PckEditor(packInfo, saveContext); + saveContext ??= GetDefaultSaveContext("./new.pck", DEFAULT_PCK_SAVECONTEXT_DESCRIPTION); + var editor = new PckEditor("New Pack", packInfo: packInfo, saveContext: saveContext); AddPage(caption, identifier, editor); } @@ -142,8 +143,8 @@ namespace PckStudio if (TryOpenPck(filepath, out PackInfo packInfo)) { - ISaveContext saveContext = GetDefaultSaveContext(filepath, "PCK (Minecraft Console Package)"); - var editor = new PckEditor(packInfo, saveContext); + ISaveContext saveContext = GetDefaultSaveContext(filepath, DEFAULT_PCK_SAVECONTEXT_DESCRIPTION); + var editor = new PckEditor(Path.GetFileNameWithoutExtension(filepath), packInfo: packInfo, saveContext: saveContext); TabPage page = AddPage(Path.GetFileName(filepath), filepath, editor); return; } @@ -152,7 +153,7 @@ namespace PckStudio private static ISaveContext GetDefaultSaveContext(string filepath, string description) { - return new DelegatedFileSaveContext(filepath, false, new FileDialogFilter(description, "*"+Path.GetExtension(filepath)),(packInfo, stream) => new PckFileWriter(packInfo.File, packInfo.Endianness).WriteToStream(stream)); + return new DelegatedFileSaveContext(filepath, false,(packInfo, stream) => new PckFileWriter(packInfo.File, packInfo.Endianness).WriteToStream(stream)); } private TabPage AddPage(string caption, string identifier, Control control) @@ -165,6 +166,7 @@ namespace PckStudio page.Name = identifier; page.Controls.Add(control); tabControl.TabPages.Add(page); + openTabPages.Add(identifier, page); tabControl.SelectTab(page); return page; } @@ -297,7 +299,7 @@ namespace PckStudio { using var ofd = new OpenFileDialog(); ofd.CheckFileExists = true; - ofd.Filter = "PCK (Minecraft Console Package)|*.pck"; + ofd.Filter = $"{DEFAULT_PCK_SAVECONTEXT_DESCRIPTION}|*.pck"; if (ofd.ShowDialog(this) == DialogResult.OK) { LoadPckFromFile(ofd.FileName); @@ -542,11 +544,21 @@ namespace PckStudio } } - private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) + private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { if (TryGetCurrentEditor(out IEditor editor)) { - editor.SaveAs(); + using SaveFileDialog saveFileDialog = new SaveFileDialog + { + FileName = editor.TitleName, + Filter = "PCK (Minecraft Console Package)|*.pck", + DefaultExt = ".pck", + }; + if (saveFileDialog.ShowDialog() != DialogResult.OK) + return; + + editor.SetSaveContext(GetDefaultSaveContext(saveFileDialog.FileName, DEFAULT_PCK_SAVECONTEXT_DESCRIPTION)); + editor.Save(); } } diff --git a/PCK-Studio/MainForm.resx b/PCK-Studio/MainForm.resx index a485fc38..79954053 100644 --- a/PCK-Studio/MainForm.resx +++ b/PCK-Studio/MainForm.resx @@ -128,13 +128,13 @@ False - 177, 6 + 183, 6 False - 177, 6 + 183, 6 202, 6 @@ -179,7 +179,7 @@ - 180, 22 + 186, 22 New @@ -196,7 +196,7 @@ Ctrl+O - 180, 22 + 186, 22 Open @@ -217,7 +217,7 @@ - 180, 22 + 186, 22 Recently open @@ -233,10 +233,10 @@ - Ctrl+Shift+S + Ctrl+S - 180, 22 + 186, 22 Save @@ -245,8 +245,11 @@ False + + Ctrl+Shift+S + - 180, 22 + 186, 22 Save As @@ -276,7 +279,7 @@ - 180, 22 + 186, 22 Close @@ -285,7 +288,7 @@ False - 180, 22 + 186, 22 Close all @@ -303,7 +306,7 @@ - 180, 22 + 186, 22 Exit diff --git a/PckStudio.Core/DelegatedFileSaveContext.cs b/PckStudio.Core/DelegatedFileSaveContext.cs index 98f78cd6..f1116f9e 100644 --- a/PckStudio.Core/DelegatedFileSaveContext.cs +++ b/PckStudio.Core/DelegatedFileSaveContext.cs @@ -8,6 +8,7 @@ using System.Windows.Forms; using Newtonsoft.Json.Linq; using PckStudio.Interfaces; using PckStudio.Core; +using System.Diagnostics; namespace PckStudio.Core { @@ -18,26 +19,16 @@ namespace PckStudio.Core public bool AutoSave { get; } public string Filepath { get; private set; } private SerializeDataToStreamDelegate _serializeDataDelegate; - private FileDialogFilter _dialogFilter; - public DelegatedFileSaveContext(string filepath, bool autoSave, FileDialogFilter dialogFilter, SerializeDataToStreamDelegate serializeDataDelegate) + public DelegatedFileSaveContext(string filepath, bool autoSave, SerializeDataToStreamDelegate serializeDataDelegate) { AutoSave = autoSave; Filepath = filepath; _serializeDataDelegate = serializeDataDelegate; - _dialogFilter = dialogFilter; } public void Save(T value) { - if (!File.Exists(Filepath)) - { - SaveFileDialog saveFileDialog = new SaveFileDialog(); - saveFileDialog.Filter = _dialogFilter.ToString(); - if (saveFileDialog.ShowDialog() != DialogResult.OK) - return; - Filepath = saveFileDialog.FileName; - } using (Stream stream = File.OpenWrite(Filepath)) { _serializeDataDelegate(value, stream); diff --git a/PckStudio.Core/Interfaces/IEditor.cs b/PckStudio.Core/Interfaces/IEditor.cs index dd19944d..2d6301ea 100644 --- a/PckStudio.Core/Interfaces/IEditor.cs +++ b/PckStudio.Core/Interfaces/IEditor.cs @@ -1,21 +1,23 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; -using OMI.Formats.Pck; namespace PckStudio.Interfaces { - public interface IEditor where T : class + public interface IEditor where T : notnull { + string TitleName { get; } + T EditorValue { get; } ISaveContext SaveContext { get; } - void Save(); + void SetSaveContext(ISaveContext saveContext); - void SaveAs(); + void Save(); void Close(); diff --git a/PckStudio.Rendering/PckStudio.Rendering.csproj b/PckStudio.Rendering/PckStudio.Rendering.csproj index 3e83c5d7..83a2fcf3 100644 --- a/PckStudio.Rendering/PckStudio.Rendering.csproj +++ b/PckStudio.Rendering/PckStudio.Rendering.csproj @@ -6,7 +6,7 @@ AnyCPU {B1E19D0F-6DD5-4D91-9B45-9818759CA8EF} Library - NDEBUG + NDEBUG Properties PckStudio.Rendering PckStudio.Rendering diff --git a/Vendor/OMI-Lib b/Vendor/OMI-Lib index de0894be..bedf961a 160000 --- a/Vendor/OMI-Lib +++ b/Vendor/OMI-Lib @@ -1 +1 @@ -Subproject commit de0894bea66464eb8ea97eb9e92618aa368cf445 +Subproject commit bedf961a0466f96f21d635e64e69978403bb3124