From 2b36e9d1d8720a2f8c02b3c52ee8fb9ca647dc5c Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Wed, 10 Dec 2025 07:30:09 +0100 Subject: [PATCH] Core(DLCManager) - Add CloseDLCPackage function --- PCK-Studio/MainForm.cs | 406 ++++++++++++----------- PCK-Studio/Program.cs | 5 +- PckStudio.Core/DLC/DLCManager.cs | 2 + PckStudio.Core/DLC/DLCPackageRegistry.cs | 5 + 4 files changed, 225 insertions(+), 193 deletions(-) diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index b48570e8..0a69924a 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -31,24 +31,29 @@ using PckStudio.Properties; namespace PckStudio { - public partial class MainForm : ImmersiveForm - { - private PckManager PckManager = null; + public partial class MainForm : ImmersiveForm + { + private PckManager PckManager = null; private Dictionary openTabPages = new Dictionary(); + private DLCManager _dlcManager; public MainForm() - { - InitializeComponent(); + { + InitializeComponent(); Text = Application.ProductName; - - labelVersion.Text = $"{Application.ProductName}: {ApplicationScope.CurrentVersion}"; - ChangelogRichTextBox.Text = Resources.CHANGELOG; - - pckOpen.AllowDrop = true; - } + labelVersion.Text = $"{Application.ProductName}: {ApplicationScope.CurrentVersion}"; + + ChangelogRichTextBox.Text = Resources.CHANGELOG; + + pckOpen.AllowDrop = true; + + _dlcManager = new DLCManager(Settings.Default.Platform, Settings.Default.UserLanguage); + Internal.SettingsManager.Default.RegisterPropertyChangedCallback(nameof(Settings.Default.UserLanguage), _dlcManager.SetPreferredLanguage); + Internal.SettingsManager.Default.RegisterPropertyChangedCallback(nameof(Settings.Default.Platform), _dlcManager.SetPlatform); + } public void LoadPckFromFile(IEnumerable filepaths) { @@ -60,17 +65,19 @@ namespace PckStudio public void LoadPckFromFile(string filepath) { - IDLCPackage dlcPackage = DLCManager.Default.OpenDLCPackage(filepath); + IDLCPackage dlcPackage = _dlcManager.OpenDLCPackage(filepath); Debug.WriteLine(dlcPackage?.GetDLCPackageType()); if (dlcPackage is null) + { + MessageBox.Show($"Failed to open '{Path.GetFileName(filepath)}'"); return; + } SaveToRecentFiles(filepath); - if (dlcPackage.GetDLCPackageType() == DLCPackageType.RawAssets) - AddEditorPage(dlcPackage); + AddEditorPage(dlcPackage); } - internal void OpenNewPckTab(string caption, string identifier, RawAssetDLCPackage packInfo, ISaveContext saveContext) - { + internal void OpenNewPckTab(string caption, string identifier, RawAssetDLCPackage packInfo, ISaveContext saveContext) + { if (openTabPages.ContainsKey(identifier)) { tabControl.SelectTab(openTabPages[identifier]); @@ -89,14 +96,34 @@ namespace PckStudio private void AddEditorPage(IDLCPackage dlcPackage) { - ISaveContext saveContext = GetDefaultSaveContext("", "PCK (Minecraft Console Package)"); - var editor = new RawAssetsEditor(dlcPackage as RawAssetDLCPackage, saveContext); - TabPage page = AddPage(dlcPackage.Name, dlcPackage.Name, editor); + switch (dlcPackage.GetDLCPackageType()) + { + case DLCPackageType.RawAssets: + ISaveContext saveContext = GetDefaultSaveContext("", "PCK (Minecraft Console Package)"); + var editor = new RawAssetsEditor(dlcPackage as RawAssetDLCPackage, saveContext); + TabPage page = AddPage(dlcPackage.Name, dlcPackage.Name, editor); + break; + case DLCPackageType.SkinPack: + break; + case DLCPackageType.TexturePack: + break; + case DLCPackageType.MashUpPack: + break; + case DLCPackageType.MG01: + case DLCPackageType.MG02: + case DLCPackageType.MG03: + MessageBox.Show("Not Implemented..."); + break; + default: + Trace.TraceError("Invalid Package Type"); + break; + } + } private static ISaveContext GetDefaultSaveContext(string filepath, string description) { - return new DelegatedFileSaveContext(filepath, false, new FileDialogFilter(description, "*"+Path.GetExtension(filepath)),(packInfo, stream) => new PckFileWriter(packInfo.PckFile, packInfo.ByteOrder).WriteToStream(stream)); + return new DelegatedFileSaveContext(filepath, false, new FileDialogFilter(description, "*" + Path.GetExtension(filepath)), (packInfo, stream) => new PckFileWriter(packInfo.PckFile, packInfo.ByteOrder).WriteToStream(stream)); } private TabPage AddPage(string caption, string identifier, Control control) @@ -127,9 +154,9 @@ namespace PckStudio private bool TryGetCurrentEditor(out IEditor editor) => TryGetEditor(tabControl.SelectedTab, out editor); private void MainForm_Load(object sender, EventArgs e) - { - LoadRecentFileList(); - } + { + LoadRecentFileList(); + } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { @@ -173,55 +200,55 @@ namespace PckStudio { Settings.Default.RecentFiles ??= new System.Collections.Specialized.StringCollection(); recentlyOpenToolStripMenuItem.DropDownItems.Clear(); - for (int i = 0; i < Settings.Default.RecentFiles.Count && i < 5; i++) - { - string filepath = Settings.Default.RecentFiles[i]; - if (!string.IsNullOrWhiteSpace(filepath) && File.Exists(filepath)) - { - string displayFilepath = Regex.Replace(filepath, @"([A-Z]{1}\:\\[Uu]sers\\)([^\\]*\\)(.*)", "~\\$3"); + for (int i = 0; i < Settings.Default.RecentFiles.Count && i < 5; i++) + { + string filepath = Settings.Default.RecentFiles[i]; + if (!string.IsNullOrWhiteSpace(filepath) && File.Exists(filepath)) + { + string displayFilepath = Regex.Replace(filepath, @"([A-Z]{1}\:\\[Uu]sers\\)([^\\]*\\)(.*)", "~\\$3"); ToolStripItem item = recentlyOpenToolStripMenuItem.DropDownItems.Add(displayFilepath, null, HandleOpenFile); - item.Tag = filepath; - } - } - if (recentlyOpenToolStripMenuItem.DropDownItems.Count > 0) - { - void clearRecentFileList(object sender, EventArgs e) - { - Settings.Default.RecentFiles.Clear(); - LoadRecentFileList(); + item.Tag = filepath; } - recentlyOpenToolStripMenuItem.DropDownItems.Add(new ToolStripSeparator()); - recentlyOpenToolStripMenuItem.DropDownItems.Add("Clear", null, clearRecentFileList); - } + } + if (recentlyOpenToolStripMenuItem.DropDownItems.Count > 0) + { + void clearRecentFileList(object sender, EventArgs e) + { + Settings.Default.RecentFiles.Clear(); + LoadRecentFileList(); + } + recentlyOpenToolStripMenuItem.DropDownItems.Add(new ToolStripSeparator()); + recentlyOpenToolStripMenuItem.DropDownItems.Add("Clear", null, clearRecentFileList); + } } - private void HandleOpenFile(object sender, EventArgs e) - { - if (sender is ToolStripItem menuItem && menuItem.Tag is string filepath) - { - if (!File.Exists(filepath)) - { - menuItem.Available = false; - return; - } - LoadPckFromFile(filepath); - } + private void HandleOpenFile(object sender, EventArgs e) + { + if (sender is ToolStripItem menuItem && menuItem.Tag is string filepath) + { + if (!File.Exists(filepath)) + { + menuItem.Available = false; + return; + } + LoadPckFromFile(filepath); + } } - private void SaveToRecentFiles(string filepath) - { - if (filepath is null || string.IsNullOrWhiteSpace(filepath)) - return; + private void SaveToRecentFiles(string filepath) + { + if (filepath is null || string.IsNullOrWhiteSpace(filepath)) + return; if (Settings.Default.RecentFiles.Contains(filepath)) Settings.Default.RecentFiles.Remove(filepath); - Settings.Default.RecentFiles.Insert(0, filepath); + Settings.Default.RecentFiles.Insert(0, filepath); - for (int i = Settings.Default.RecentFiles.Count - 1; i >= 5; i--) - { + for (int i = Settings.Default.RecentFiles.Count - 1; i >= 5; i--) + { Settings.Default.RecentFiles.RemoveAt(i); } - Settings.Default.Save(); - LoadRecentFileList(); + Settings.Default.Save(); + LoadRecentFileList(); } private void openPckManagerToolStripMenuItem_Click(object sender, EventArgs e) @@ -238,7 +265,7 @@ namespace PckStudio } private void openToolStripMenuItem_Click(object sender, EventArgs e) - { + { using var ofd = new OpenFileDialog(); ofd.CheckFileExists = true; ofd.Filter = "PCK (Minecraft Console Package)|*.pck"; @@ -253,6 +280,7 @@ namespace PckStudio if (TryGetEditor(e.Page, out IEditor editor)) { editor.Close(); + _dlcManager.CloseDLCPackage(editor.EditorValue.Identifier); RemoveOpenFile(); } } @@ -271,66 +299,66 @@ namespace PckStudio } private static PckFile InitializePack(int packId, int packVersion, string packName) - { - var pack = new PckFile(3); + { + var pack = new PckFile(3); PckAsset zeroAsset = pack.CreateNewAsset("0", PckAssetType.InfoFile); - zeroAsset.AddProperty("PACKID", packId); - zeroAsset.AddProperty("PACKVERSION", packVersion); + zeroAsset.AddProperty("PACKID", packId); + zeroAsset.AddProperty("PACKVERSION", packVersion); - var locFile = new LOCFile(); - locFile.InitializeDefault(packName); - pack.CreateNewAsset("localisation.loc", PckAssetType.LocalisationFile, new LOCFileWriter(locFile, 2)); - return pack; - } + var locFile = new LOCFile(); + locFile.InitializeDefault(packName); + pack.CreateNewAsset("localisation.loc", PckAssetType.LocalisationFile, new LOCFileWriter(locFile, 2)); + return pack; + } private static PckFile InitializeTexturePack(int packId, int packVersion, string packName, string res) - { + { PckFile pack = InitializePack(packId, packVersion, packName); - PckFile infoPCK = new PckFile(3); + PckFile infoPCK = new PckFile(3); pack.GetAsset("localisation.loc", PckAssetType.LocalisationFile).Filename = "languages.loc"; PckAsset iconAsset = infoPCK.CreateNewAsset("icon.png", PckAssetType.TextureFile); - iconAsset.SetTexture(Core.Properties.Resources.TexturePackIcon); + iconAsset.SetTexture(Core.Properties.Resources.TexturePackIcon); PckAsset comparisonAsset = infoPCK.CreateNewAsset("comparison.png", PckAssetType.TextureFile); - comparisonAsset.SetTexture(Core.Properties.Resources.Comparison); + comparisonAsset.SetTexture(Core.Properties.Resources.Comparison); PckAsset texturepackInfoAsset = pack.CreateNewAsset($"{res}/{res}Info.pck", PckAssetType.TexturePackInfoFile); - texturepackInfoAsset.AddProperty("PACKID", "0"); - texturepackInfoAsset.AddProperty("DATAPATH", $"{res}Data.pck"); + texturepackInfoAsset.AddProperty("PACKID", "0"); + texturepackInfoAsset.AddProperty("DATAPATH", $"{res}Data.pck"); - texturepackInfoAsset.SetData(new PckFileWriter(infoPCK, OMI.ByteOrder.BigEndian)); + texturepackInfoAsset.SetData(new PckFileWriter(infoPCK, OMI.ByteOrder.BigEndian)); - return pack; - } + return pack; + } - private static PckFile InitializeMashUpPack(int packId, int packVersion, string packName, string res) - { + private static PckFile InitializeMashUpPack(int packId, int packVersion, string packName, string res) + { PckFile pack = InitializeTexturePack(packId, packVersion, packName, res); PckAsset gameRuleAsset = pack.CreateNewAsset("GameRules.grf", PckAssetType.GameRulesFile); GameRuleFile grfFile = new GameRuleFile(); - grfFile.AddRule("MapOptions", - new KeyValuePair("seed", "0"), - new KeyValuePair("baseSaveName", string.Empty), - new KeyValuePair("flatworld", "false"), - new KeyValuePair("texturePackId", packId.ToString()) - ); - grfFile.AddRule("LevelRules") - .AddRule("UpdatePlayer", - new KeyValuePair("yRot", "0"), - new KeyValuePair("xRot", "0"), - new KeyValuePair("spawnX", "0"), - new KeyValuePair("spawnY", "0"), - new KeyValuePair("spawnZ", "0") - ); + grfFile.AddRule("MapOptions", + new KeyValuePair("seed", "0"), + new KeyValuePair("baseSaveName", string.Empty), + new KeyValuePair("flatworld", "false"), + new KeyValuePair("texturePackId", packId.ToString()) + ); + grfFile.AddRule("LevelRules") + .AddRule("UpdatePlayer", + new KeyValuePair("yRot", "0"), + new KeyValuePair("xRot", "0"), + new KeyValuePair("spawnX", "0"), + new KeyValuePair("spawnY", "0"), + new KeyValuePair("spawnZ", "0") + ); - gameRuleAsset.SetData(new GameRuleFileWriter(grfFile)); + gameRuleAsset.SetData(new GameRuleFileWriter(grfFile)); - return pack; - } + return pack; + } private void skinPackToolStripMenuItem_Click(object sender, EventArgs e) { @@ -366,8 +394,8 @@ namespace PckStudio } } - private void quickChangeToolStripMenuItem_Click(object sender, EventArgs e) - { + private void quickChangeToolStripMenuItem_Click(object sender, EventArgs e) + { if (TryGetCurrentEditor(out IEditor editor)) { using AdvancedOptions advanced = new AdvancedOptions(editor.EditorValue.PckFile); @@ -379,16 +407,16 @@ namespace PckStudio } } - private void closeToolStripMenuItem_Click(object sender, EventArgs e) - { + private void closeToolStripMenuItem_Click(object sender, EventArgs e) + { CloseTab(tabControl.TabPages, tabControl.SelectedTab); } - private void aboutToolStripMenuItem_Click(object sender, EventArgs e) - { - using ContributorsForm info = new ContributorsForm(); - info.ShowDialog(this); - } + private void aboutToolStripMenuItem_Click(object sender, EventArgs e) + { + using ContributorsForm info = new ContributorsForm(); + info.ShowDialog(this); + } private void howToMakeABasicSkinPackToolStripMenuItem_Click(object sender, EventArgs e) => Process.Start("https://www.youtube.com/watch?v=A43aHRHkKxk"); @@ -415,113 +443,113 @@ namespace PckStudio private void trelloBoardToolStripMenuItem_Click(object sender, EventArgs e) => Process.Start("https://trello.com/b/0XLNOEbe/pck-studio"); private void OpenPck_MouseEnter(object sender, EventArgs e) - { - pckOpen.Image = Resources.pckOpen; - } + { + pckOpen.Image = Resources.pckOpen; + } - private void OpenPck_MouseLeave(object sender, EventArgs e) - { - pckOpen.Image = Resources.pckClosed; - } + private void OpenPck_MouseLeave(object sender, EventArgs e) + { + pckOpen.Image = Resources.pckClosed; + } - private void OpenPck_DragEnter(object sender, DragEventArgs e) - { - pckOpen.Image = Resources.pckDrop; + private void OpenPck_DragEnter(object sender, DragEventArgs e) + { + pckOpen.Image = Resources.pckDrop; string[] files = (string[])e.Data.GetData(DataFormats.FileDrop) ?? Array.Empty(); - e.Effect = files.Any(file => Path.GetExtension(file).Equals(".pck", StringComparison.CurrentCultureIgnoreCase)) - ? DragDropEffects.Copy - : DragDropEffects.None; - } + e.Effect = files.Any(file => Path.GetExtension(file).Equals(".pck", StringComparison.CurrentCultureIgnoreCase)) + ? DragDropEffects.Copy + : DragDropEffects.None; + } - private void OpenPck_DragDrop(object sender, DragEventArgs e) - { - if (e.Data.GetData(DataFormats.FileDrop, false) is string[] filepaths) - LoadPckFromFile(filepaths); - } + private void OpenPck_DragDrop(object sender, DragEventArgs e) + { + if (e.Data.GetData(DataFormats.FileDrop, false) is string[] filepaths) + LoadPckFromFile(filepaths); + } - private void OpenPck_DragLeave(object sender, EventArgs e) - { - pckOpen.Image = Resources.pckClosed; - } + private void OpenPck_DragLeave(object sender, EventArgs e) + { + pckOpen.Image = Resources.pckClosed; + } - private void OpenPck_Click(object sender, EventArgs e) - { - openToolStripMenuItem_Click(sender, e); - } + private void OpenPck_Click(object sender, EventArgs e) + { + openToolStripMenuItem_Click(sender, e); + } - private void saveToolStripMenuItem_Click(object sender, EventArgs e) - { + private void saveToolStripMenuItem_Click(object sender, EventArgs e) + { if (TryGetCurrentEditor(out IEditor editor)) { editor.Save(); } } - private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) - { + private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) + { if (TryGetCurrentEditor(out IEditor editor)) { editor.SaveAs(); } } - private void wavBinkaToolStripMenuItem_Click(object sender, EventArgs e) - { - using OpenFileDialog fileDialog = new OpenFileDialog - { - Multiselect = true, - Filter = "WAV files (*.wav)|*.wav", - Title = "Please choose WAV files to convert to BINKA" - }; - if (fileDialog.ShowDialog(this) == DialogResult.OK) - { - using ItemSelectionPopUp dialog = new ItemSelectionPopUp( - "Level 1 (Best Quality)", "Level 2", "Level 3", "Level 4", "Level 5", - "Level 6", "Level 7", "Level 8", "Level 9 (Worst Quality)") - { - LabelText = "Compression", - ButtonText = "OK" - }; - - if (dialog.ShowDialog(this) == DialogResult.OK) + private void wavBinkaToolStripMenuItem_Click(object sender, EventArgs e) + { + using OpenFileDialog fileDialog = new OpenFileDialog + { + Multiselect = true, + Filter = "WAV files (*.wav)|*.wav", + Title = "Please choose WAV files to convert to BINKA" + }; + if (fileDialog.ShowDialog(this) == DialogResult.OK) + { + using ItemSelectionPopUp dialog = new ItemSelectionPopUp( + "Level 1 (Best Quality)", "Level 2", "Level 3", "Level 4", "Level 5", + "Level 6", "Level 7", "Level 8", "Level 9 (Worst Quality)") { - BinkaConverter.ToBinka( - fileDialog.FileNames, - new DirectoryInfo(Path.GetDirectoryName(fileDialog.FileName)), - dialog.SelectedIndex + 1 // compression level - ); - } - } - } + LabelText = "Compression", + ButtonText = "OK" + }; - private void binkaWavToolStripMenuItem_Click(object sender, EventArgs e) - { - using OpenFileDialog fileDialog = new OpenFileDialog - { - Multiselect = true, - Filter = "BINKA files (*.binka)|*.binka", - Title = "Please choose BINKA files to convert to WAV" - }; - if (fileDialog.ShowDialog(this) == DialogResult.OK) - { - BinkaConverter.ToWav(fileDialog.FileNames, new DirectoryInfo(Path.GetDirectoryName(fileDialog.FileName))); - } - } + if (dialog.ShowDialog(this) == DialogResult.OK) + { + BinkaConverter.ToBinka( + fileDialog.FileNames, + new DirectoryInfo(Path.GetDirectoryName(fileDialog.FileName)), + dialog.SelectedIndex + 1 // compression level + ); + } + } + } - private void settingsToolStripMenuItem_Click(object sender, EventArgs e) - { - var appSettings = new AppSettingsForm(); - appSettings.ShowDialog(this); - } + private void binkaWavToolStripMenuItem_Click(object sender, EventArgs e) + { + using OpenFileDialog fileDialog = new OpenFileDialog + { + Multiselect = true, + Filter = "BINKA files (*.binka)|*.binka", + Title = "Please choose BINKA files to convert to WAV" + }; + if (fileDialog.ShowDialog(this) == DialogResult.OK) + { + BinkaConverter.ToWav(fileDialog.FileNames, new DirectoryInfo(Path.GetDirectoryName(fileDialog.FileName))); + } + } - private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) - { - Updater.UpdateToLatest(); - } + private void settingsToolStripMenuItem_Click(object sender, EventArgs e) + { + var appSettings = new AppSettingsForm(); + appSettings.ShowDialog(this); + } + + private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) + { + Updater.UpdateToLatest(); + } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { - PckManager?.Close(); + PckManager?.Close(); Application.Exit(); } } diff --git a/PCK-Studio/Program.cs b/PCK-Studio/Program.cs index c7d075c6..e6987a8e 100644 --- a/PCK-Studio/Program.cs +++ b/PCK-Studio/Program.cs @@ -38,18 +38,15 @@ namespace PckStudio if (Settings.Default.Platform == Core.ConsolePlatform.Unknown) { MessageBox.Show("Please choose on which console you're playing on.", "Select Platform", MessageBoxButtons.OK, MessageBoxIcon.Question); - var platformChooser = new ItemSelectionPopUp(Enum.GetNames(typeof(Core.ConsolePlatform))); + var platformChooser = new ItemSelectionPopUp(Enum.GetNames(typeof(Core.ConsolePlatform)).Skip(1).ToArray()); if (platformChooser.ShowDialog() == DialogResult.OK && Enum.IsDefined(typeof(ConsolePlatform), platformChooser.SelectedItem)) Settings.Default.Platform = (Core.ConsolePlatform)Enum.Parse(typeof(ConsolePlatform), platformChooser.SelectedItem); } ApplicationScope.Initialize(); - DLCManager.Default.SetPlatform(Settings.Default.Platform); Trace.TraceInformation("Startup"); RPC.Initialize(); MainInstance = new MainForm(); - Internal.SettingsManager.Default.RegisterPropertyChangedCallback(nameof(Settings.Default.UserLanguage), DLCManager.Default.SetPreferredLanguage); - Internal.SettingsManager.Default.RegisterPropertyChangedCallback(nameof(Settings.Default.Platform), DLCManager.Default.SetPlatform); Updater.SetOwner(MainInstance); if (args.Length > 0) { diff --git a/PckStudio.Core/DLC/DLCManager.cs b/PckStudio.Core/DLC/DLCManager.cs index 93d4bc30..1047b043 100644 --- a/PckStudio.Core/DLC/DLCManager.cs +++ b/PckStudio.Core/DLC/DLCManager.cs @@ -467,5 +467,7 @@ namespace PckStudio.Core.DLC _platform = platform; _byteOrder = GetByteOrderForPlatform(platform); } + + public bool CloseDLCPackage(int identifier) => _packageRegistry.UnregisterPackage(identifier); } } \ No newline at end of file diff --git a/PckStudio.Core/DLC/DLCPackageRegistry.cs b/PckStudio.Core/DLC/DLCPackageRegistry.cs index c38cc740..b7f33fd5 100644 --- a/PckStudio.Core/DLC/DLCPackageRegistry.cs +++ b/PckStudio.Core/DLC/DLCPackageRegistry.cs @@ -29,5 +29,10 @@ namespace PckStudio.Core.DLC internal bool ContainsPackage(int identifier) => _openPackages.ContainsKey(identifier) && _localisationFiles.ContainsKey(identifier); internal LOCFile GetLocalisation(int identifier) => _localisationFiles[identifier]; + + internal bool UnregisterPackage(int identifier) + { + return _openPackages.Remove(identifier) && _localisationFiles.Remove(identifier); + } } }