Fix Pck save & save as functionality :')

This commit is contained in:
miku-666
2026-01-26 03:01:38 +01:00
parent cb70382a8b
commit 37cec08d7c
8 changed files with 74 additions and 72 deletions

View File

@@ -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<T> SaveContext { get; }
public ISaveContext<T> SaveContext { get; private set; }
public string TitleName { get; }
public EditorControl()
{
}
protected EditorControl(T value, ISaveContext<T> saveContext)
protected EditorControl(string titleName, T value, ISaveContext<T> 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<T> 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();

View File

@@ -52,9 +52,6 @@ namespace PckStudio.Controls
{
internal partial class PckEditor : EditorControl<PackInfo>
{
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<PckAssetType, Action<PckAsset>> _pckAssetTypeHandler;
public PckEditor(PackInfo packInfo, ISaveContext<PackInfo> saveContext)
: base(packInfo, saveContext)
public PckEditor(string name, PackInfo packInfo, ISaveContext<PackInfo> 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;

View File

@@ -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<string, TabPage> openTabPages = new Dictionary<string, TabPage>();
@@ -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<PackInfo> 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<PackInfo> saveContext = GetDefaultSaveContext(filepath, "PCK (Minecraft Console Package)");
var editor = new PckEditor(packInfo, saveContext);
ISaveContext<PackInfo> 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<PackInfo> GetDefaultSaveContext(string filepath, string description)
{
return new DelegatedFileSaveContext<PackInfo>(filepath, false, new FileDialogFilter(description, "*"+Path.GetExtension(filepath)),(packInfo, stream) => new PckFileWriter(packInfo.File, packInfo.Endianness).WriteToStream(stream));
return new DelegatedFileSaveContext<PackInfo>(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<PackInfo> 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();
}
}

View File

@@ -128,13 +128,13 @@
<value>False</value>
</metadata>
<data name="toolStripSeparator2.Size" type="System.Drawing.Size, System.Drawing">
<value>177, 6</value>
<value>183, 6</value>
</data>
<metadata name="toolStripSeparator4.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<data name="toolStripSeparator4.Size" type="System.Drawing.Size, System.Drawing">
<value>177, 6</value>
<value>183, 6</value>
</data>
<data name="toolStripSeparator3.Size" type="System.Drawing.Size, System.Drawing">
<value>202, 6</value>
@@ -179,7 +179,7 @@
</value>
</data>
<data name="newToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="newToolStripMenuItem.Text" xml:space="preserve">
<value>New</value>
@@ -196,7 +196,7 @@
<value>Ctrl+O</value>
</data>
<data name="openToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="openToolStripMenuItem.Text" xml:space="preserve">
<value>Open</value>
@@ -217,7 +217,7 @@
</value>
</data>
<data name="recentlyOpenToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="recentlyOpenToolStripMenuItem.Text" xml:space="preserve">
<value>Recently open</value>
@@ -233,10 +233,10 @@
</value>
</data>
<data name="saveToolStripMenuItem.ShortcutKeys" type="System.Windows.Forms.Keys, System.Windows.Forms">
<value>Ctrl+Shift+S</value>
<value>Ctrl+S</value>
</data>
<data name="saveToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="saveToolStripMenuItem.Text" xml:space="preserve">
<value>Save</value>
@@ -245,8 +245,11 @@
<data name="saveToolStripMenuItem.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="saveAsToolStripMenuItem.ShortcutKeys" type="System.Windows.Forms.Keys, System.Windows.Forms">
<value>Ctrl+Shift+S</value>
</data>
<data name="saveAsToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="saveAsToolStripMenuItem.Text" xml:space="preserve">
<value>Save As</value>
@@ -276,7 +279,7 @@
</value>
</data>
<data name="closeToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="closeToolStripMenuItem.Text" xml:space="preserve">
<value>Close</value>
@@ -285,7 +288,7 @@
<value>False</value>
</data>
<data name="closeAllToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="closeAllToolStripMenuItem.Text" xml:space="preserve">
<value>Close all</value>
@@ -303,7 +306,7 @@
</value>
</data>
<data name="exitToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
<value>186, 22</value>
</data>
<data name="exitToolStripMenuItem.Text" xml:space="preserve">
<value>Exit</value>

View File

@@ -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);

View File

@@ -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<T> where T : class
public interface IEditor<T> where T : notnull
{
string TitleName { get; }
T EditorValue { get; }
ISaveContext<T> SaveContext { get; }
void Save();
void SetSaveContext(ISaveContext<T> saveContext);
void SaveAs();
void Save();
void Close();

View File

@@ -6,7 +6,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{B1E19D0F-6DD5-4D91-9B45-9818759CA8EF}</ProjectGuid>
<OutputType>Library</OutputType>
<DefineConstants Condition="'$(Configuration)' != 'Debug'">NDEBUG</DefineConstants>
<DefineConstants Condition="'$(Configuration)' != 'Debug'">NDEBUG</DefineConstants>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PckStudio.Rendering</RootNamespace>
<AssemblyName>PckStudio.Rendering</AssemblyName>

2
Vendor/OMI-Lib vendored