Skip to content

Commit

Permalink
-add: Preview for images
Browse files Browse the repository at this point in the history
Added support for previewing jpg, png, bmp, tga and dds images.
Added scrollbars to the image preview.
Added Pfim for reading dds files instead of Freeimage, as suggested by Indrek Ardel for AAEmu-Tools.
  • Loading branch information
ZeromusXYZ committed Jun 1, 2023
1 parent 705706b commit c1e32d3
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 41 deletions.
51 changes: 50 additions & 1 deletion AAPacker/AAPakFileFormatReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ namespace AAPacker;
/// </summary>
public enum AAPakFileHeaderElement
{
/// <summary>
/// Any byte value
/// </summary>
AnyByte,
/// <summary>
/// Byte must be zero
/// </summary>
NullByte,
/// <summary>
/// Header information
/// </summary>
Header,
/// <summary>
/// Number of normal files in the pak
/// </summary>
FilesCount,
/// <summary>
/// Number of "extra" files in the pak
/// </summary>
ExtraFilesCount
}

Expand All @@ -19,15 +34,45 @@ public enum AAPakFileHeaderElement
/// </summary>
public enum AAPakFileInfoElement
{
/// <summary>
/// The filename
/// </summary>
FileName,
/// <summary>
/// Starting offset in the pak
/// </summary>
Offset,
/// <summary>
/// Actual file size
/// </summary>
Size,
/// <summary>
/// Duplicate of file size (possibly compressed size)?
/// </summary>
SizeDuplicate,
/// <summary>
/// Number of bytes used a padding
/// </summary>
PaddingSize,
/// <summary>
/// MD5 checksum value
/// </summary>
Md5,
/// <summary>
/// Unused1
/// </summary>
Dummy1,
CreateTime,
/// <summary>
/// Last Modified Time
/// </summary>
ModifyTime,
/// <summary>
/// File created time
/// </summary>
CreateTime,
/// <summary>
/// Ununsed2
/// </summary>
Dummy2,
}

Expand All @@ -36,6 +81,10 @@ public enum AAPakFileInfoElement
/// </summary>
public class AAPakFileFormatReader
{
/// <summary>
/// Class containing structure on how to read/write the data
/// </summary>
/// <param name="initializeWithDefaults">If true, pre-fills the data</param>
public AAPakFileFormatReader(bool initializeWithDefaults)
{
// Changed default constructor to fix issues related to arrays and json populating
Expand Down
6 changes: 5 additions & 1 deletion AAPakEditor/AAPakEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@
<HintPath>..\packages\FCTB.2.16.24\lib\FastColoredTextBox.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Pfim, Version=0.11.2.0, Culture=neutral, PublicKeyToken=c0ebc20d008ae134, processorArchitecture=MSIL">
<HintPath>..\packages\Pfim.0.11.2\lib\netstandard2.0\Pfim.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -151,6 +154,7 @@
<Compile Include="Forms\PreviewForm.Designer.cs">
<DependentUpon>PreviewForm.cs</DependentUpon>
</Compile>
<Compile Include="Helpers\ImageHelpers.cs" />
<Compile Include="Helpers\JsonHelpers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
59 changes: 35 additions & 24 deletions AAPakEditor/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 67 additions & 1 deletion AAPakEditor/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using AAPakEditor.Helpers;
using System.Runtime.Remoting.Contexts;
using FastColoredTextBoxNS;
using Vestris.ResourceLib;

namespace AAPakEditor.Forms;

Expand Down Expand Up @@ -114,6 +115,7 @@ private void MMFileOpen_Click(object sender, EventArgs e)
Properties.Settings.Default.SkipEditWarning = false;
Properties.Settings.Default.Save();
}
Application.DoEvents();
Application.UseWaitCursor = true;
Cursor.Current = Cursors.WaitCursor;
LoadPakFile(openGamePakDialog.FileName, openGamePakDialog.ReadOnlyChecked);
Expand Down Expand Up @@ -497,7 +499,15 @@ private void ShowPreview(AAPakFileInfo pfi)
PreviewForm.Instance.tPreview.Text = s;
}
else
if ((fileExt == ".txt") || (fileExt == ".g") || (fileExt == ".cfg") || (fileExt == ".cal"))
if ((fileExt == ".html") || (fileExt == ".htm"))
{
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpBasicText;
PreviewForm.Instance.tPreview.Language = Language.HTML;
var s = StreamToString(Pak.ExportFileAsStream(pfi));
PreviewForm.Instance.tPreview.Text = s;
}
else
if ((fileExt == ".txt") || (fileExt == ".g") || (fileExt == ".cfg") || (fileExt == ".cal") || (fileExt == ".ini"))
{
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpBasicText;
PreviewForm.Instance.tPreview.Language = Language.Custom;
Expand All @@ -514,6 +524,47 @@ private void ShowPreview(AAPakFileInfo pfi)
PreviewForm.Instance.tPreview.Text = s;
}
else
if ((fileExt == ".jpg") || (fileExt == ".png") || (fileExt == ".bmp"))
{
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpImage;
try
{
var imgStream = Pak.ExportFileAsStream(pfi);
var img = Image.FromStream(imgStream);
PreviewForm.Instance.pbPreview.Image = img;
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpImage;
PreviewForm.Instance.tPreview.Text = pfi.Name + "\n" + img.Width + " x " + img.Height;
PreviewForm.Instance.pbPreview.Size = new Size(img.Width, img.Height);
}
catch (Exception e)
{
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpBasicText;
var s = $"Failed to load {pfi.Name}\n{e.Message}";
PreviewForm.Instance.tPreview.Text = s;
}
}
else
if ((fileExt == ".dds") || (fileExt == ".tga"))
{
// Load using Pfim
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpImage;
try
{
var imgStream = Pak.ExportFileAsStream(pfi);
var img = ImageHelpers.ReadDdsFromStream(imgStream);
PreviewForm.Instance.pbPreview.Image = img;
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpImage;
PreviewForm.Instance.tPreview.Text = pfi.Name + "\n" + img.Width + " x " + img.Height;
PreviewForm.Instance.pbPreview.Size = new Size(img.Width, img.Height);
}
catch (Exception e)
{
PreviewForm.Instance.tcViewer.SelectedTab = PreviewForm.Instance.tpBasicText;
var s = $"Failed to load {pfi.Name}\n{e.Message}";
PreviewForm.Instance.tPreview.Text = s;
}
}
else
{
if (PreviewForm.IsActive)
PreviewForm.Instance?.Close();
Expand All @@ -523,6 +574,9 @@ private void ShowPreview(AAPakFileInfo pfi)
PreviewForm.Instance.Show();
PreviewForm.Instance.BringToFront();
PreviewForm.Instance.Location = new Point(this.Location.X + this.Width, this.Location.Y);
Application.DoEvents();
this.Focus();
lbFiles.Focus();
}

private void lbFiles_SelectedIndexChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -1859,4 +1913,16 @@ private void MMToolsConvertMenu_DropDownOpening(object sender, EventArgs e)
MMToolsConvertMenu.DropDownItems.Add(newItem);
}
}

private void MMTools_DropDownOpening(object sender, EventArgs e)
{
MMToolsPreview.Checked = Settings.Default.AllowPreview;
}

private void MMToolsPreview_Click(object sender, EventArgs e)
{
MMToolsPreview.Checked = !MMToolsPreview.Checked;
Settings.Default.AllowPreview = MMToolsPreview.Checked;
Settings.Default.Save();
}
}
Loading

0 comments on commit c1e32d3

Please sign in to comment.