Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Full Release v1.0
Browse files Browse the repository at this point in the history
Performance improvements
Save support
Uninstaller
  • Loading branch information
dtzxporter committed Mar 23, 2019
1 parent 4f342d0 commit f9b4129
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 59 deletions.
83 changes: 41 additions & 42 deletions src/FileTypeDDS/FileTypeDDS/FileTypeDDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,29 @@ using namespace System::IO;
using namespace FileTypeDDS;
using namespace PaintDotNet;

DDSFileType::DDSFileType() : FileType("DirectDraw Surface (DDS)", FileTypeFlags::SupportsLoading, Extensions)
DDSFileType::DDSFileType() : PropertyBasedFileType("DirectDraw Surface (DDS)", FileTypeFlags::SupportsLoading | FileTypeFlags::SupportsSaving | FileTypeFlags::SavesWithProgress, Extensions)
{
// Perform initialization here, for this, we're going to reflect into PaintDotNet and verify that the EXE was patched
// Perform initialization here, for this, we're going to reflect into PaintDotNet and borrow the old DDS saving logic
Assembly^ CurrentAssembly = Assembly::GetEntryAssembly();
// Reflect to the "PdnFileTypes" class
try
{
//// Fetch the type
//auto FileType = CurrentAssembly->GetType("PaintDotNet.Data.PdnFileTypes");
#if _DEBUG
System::Windows::Forms::MessageBox::Show("FileTypeDDS -- DEBUG MODE --");
#endif

//auto field = FileType->GetField("Dds");
// Fetch the type
auto FileType = CurrentAssembly->GetType("PaintDotNet.Data.Dds.DdsFileType");
this->InternalFileType = (PropertyBasedFileType^)Activator::CreateInstance(FileType);

//auto res = field->FieldHandle.Value.ToInt64();

//System::Windows::Forms::MessageBox::Show("fff " + field->Name + " 0x" + res.ToString("X"));
#if _DEBUG
System::Windows::Forms::MessageBox::Show("FileTypeDDS -- Loaded internal file handler --");
#endif
}
catch (Exception^ ex)
{
// Display the error, this shouldn't happen with release builds
System::Windows::Forms::MessageBox::Show("FileTypeDDS - Failed to verify installation. (Report this) [" + ex->Message + "]");
// Display the error
System::Windows::Forms::MessageBox::Show("FileTypeDDS - Failed to initialize plugin. (Report this on Github) [" + ex->Message + "]", "FileTypeDDS");
}
}

Expand Down Expand Up @@ -130,7 +133,7 @@ Document^ DDSFileType::OnLoad(Stream^ Input)
auto TemporaryImage = std::make_unique<DirectX::ScratchImage>();

// We must use this format for global support, it's a standard for images
DXGI_FORMAT ResultFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
DXGI_FORMAT ResultFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
// Decompress the image
auto Result = DirectX::Decompress(FirstImage, ImageCount, ImageMetadata, ResultFormat, *TemporaryImage);

Expand All @@ -152,13 +155,13 @@ Document^ DDSFileType::OnLoad(Stream^ Input)
return nullptr;
}
}
else if (ImageMetadata.format != DXGI_FORMAT::DXGI_FORMAT_R8G8B8A8_UNORM)
else if (ImageMetadata.format != DXGI_FORMAT::DXGI_FORMAT_B8G8R8A8_UNORM)
{
// Allocate a temporary buffer
auto TemporaryImage = std::make_unique<DirectX::ScratchImage>();

// We must use this format for global support, it's a standard for images
DXGI_FORMAT ResultFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
DXGI_FORMAT ResultFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
// Convert to our format
auto Result = DirectX::Convert(Image->GetImages(), Image->GetImageCount(), Image->GetMetadata(), ResultFormat, DirectX::TEX_FILTER_FLAGS::TEX_FILTER_DEFAULT, DirectX::TEX_THRESHOLD_DEFAULT, *TemporaryImage);

Expand All @@ -181,39 +184,20 @@ Document^ DDSFileType::OnLoad(Stream^ Input)
}
}

// Prepare a new layer for the document, this will house the RGBA data
BitmapLayer^ DocumentLayer = Layer::CreateBackgroundLayer((int)ImageMetadata.width, (int)ImageMetadata.height);
// Color buffer
ColorBgra ColorBuffer;
// A pointer to the raw pixels in RGBA format
auto RawPixels = Image->GetPixels();
// The index of the pixels
uint32_t PixelIndex = 0;
// Loop for height and width and copy over the RGBA pixel data
for (int i = 0; i < (int)ImageMetadata.height; i++)
{
for (int j = 0; j < (int)ImageMetadata.width; j++)
{
// Set
ColorBuffer.R = RawPixels[PixelIndex];
ColorBuffer.G = RawPixels[PixelIndex + 1];
ColorBuffer.B = RawPixels[PixelIndex + 2];
ColorBuffer.A = RawPixels[PixelIndex + 3];
// Advance
PixelIndex += 4;
// Set the item
DocumentLayer->Surface[j, i] = ColorBuffer;
}
}
// Prepare a new layer for the document, this will house the BGRA data
Document^ ResultDocument = gcnew Document((int)ImageMetadata.width, (int)ImageMetadata.height);
ResultDocument->Layers->Add(Layer::CreateBackgroundLayer((int)ImageMetadata.width, (int)ImageMetadata.height));

// A pointer to the raw pixels in BGRA format
auto Buffer = Image->GetPixels();
auto DestPtr = ((BitmapLayer^)ResultDocument->Layers[0])->Surface->Scan0->VoidStar;

// Copy the raw pixels
std::memcpy(DestPtr, Buffer, ImageMetadata.width * ImageMetadata.height * 4);

// Force clean up
Image.reset();

// Make a document
Document^ ResultDocument = gcnew Document((int)ImageMetadata.width, (int)ImageMetadata.height);
// Add the layer
ResultDocument->Layers->Add(DocumentLayer);

// Return a test doc
return ResultDocument;
}
Expand All @@ -222,6 +206,21 @@ Document^ DDSFileType::OnLoad(Stream^ Input)
return nullptr;
}

void DDSFileType::OnSaveT(Document^ Input, Stream^ Output, PropertyBasedSaveConfigToken^ Token, Surface^ Surface, ProgressEventHandler^ Callback)
{
this->InternalFileType->Save(Input, Output, Token, Surface, Callback, false);
}

ControlInfo^ DDSFileType::OnCreateSaveConfigUI(PropertyCollection^ Props)
{
return this->InternalFileType->OnCreateSaveConfigUI(Props);
}

PropertyCollection^ DDSFileType::OnCreateSavePropertyCollection()
{
return this->InternalFileType->OnCreateSavePropertyCollection();
}

array<FileType^>^ DDSFileTypes::GetFileTypeInstances()
{
// Return it
Expand Down
15 changes: 13 additions & 2 deletions src/FileTypeDDS/FileTypeDDS/FileTypeDDS.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::IO;
using namespace PaintDotNet;
using namespace PaintDotNet::PropertySystem;
using namespace PaintDotNet::IndirectUI;

namespace FileTypeDDS
{
public ref class DDSFileType sealed : public FileType
public ref class DDSFileType sealed : public PropertyBasedFileType
{
public:
DDSFileType();

// Override save functions
virtual ControlInfo^ OnCreateSaveConfigUI(PropertyCollection^ Props) override;
virtual PropertyCollection^ OnCreateSavePropertyCollection() override;

protected:
// Override the onload
// Override the onload and onsave
virtual Document^ OnLoad(Stream^ Input) override;
virtual void OnSaveT(Document^ Input, Stream^ Output, PropertyBasedSaveConfigToken^ Token, Surface^ Surface, ProgressEventHandler^ Callback) override;


private:
// An internal reference to our supported file type
PropertyBasedFileType^ InternalFileType;

// A list of supported extensions
static array<String^>^ Extensions = gcnew array<String^>(1) { ".dds" };
};
Expand Down
24 changes: 12 additions & 12 deletions src/FileTypeDDS/FileTypeDDSInstaller/Main.Designer.cs

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

116 changes: 113 additions & 3 deletions src/FileTypeDDS/FileTypeDDSInstaller/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ public Main()

private void BackOut_Click(object sender, EventArgs e)
{
// Just close
this.Close();
// Disable us
this.InstallGo.Enabled = false;
this.UninstallButton.Enabled = false;
// Make visible
this.ProgressLoad.Visible = true;

PreOps();

// Run the patcher
Task.Run((Action)RunUnPatcher);
}

private string GetNGENPath()
Expand All @@ -47,6 +55,105 @@ private void RunProcess(string ProcessPath, string Args)
process.WaitForExit();
}

private void RunUnPatcher()
{
// Locate the Paint.NET exe
var ProgramFilesBase = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
var ProgramFilesNew = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

// Final path
var ProgramPath = "";

// Check each path
if (File.Exists(Path.Combine(ProgramFilesBase, "paint.net\\PaintDotNet.exe")))
{
ProgramPath = Path.Combine(ProgramFilesBase, "paint.net\\PaintDotNet.exe");
}
else if (File.Exists(Path.Combine(ProgramFilesNew, "paint.net\\PaintDotNet.exe")))
{
ProgramPath = Path.Combine(ProgramFilesNew, "paint.net\\PaintDotNet.exe");
}
else
{
// Ask the user to locate it
this.Invoke((Action)delegate
{
// Ask
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFileDialog.FileName.Contains("PaintDotNet.exe"))
{
// Set
ProgramPath = openFileDialog.FileName;
}
else
{
// Alert and close
MessageBox.Show("Failed to locate Paint.NET. Please verify that it is installed properly.", "FileTypeDDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Close
this.Close();
}
});
}

// Perform NGen installation
var NGenPath = GetNGENPath();

// Uninstall first, then install
RunProcess(NGenPath, "uninstall \"" + ProgramPath + "\"");

// Replace with the backup image, if available...
if (File.Exists(ProgramPath + ".bak"))
{
try
{
File.Copy(ProgramPath + ".bak", ProgramPath, true);
}
catch
{
// Nothing...
}
}

// Reinstall...
RunProcess(NGenPath, "install \"" + ProgramPath + "\"");

try
{
if (UIntPtr.Size == 8)
{
File.Delete(Path.Combine(Path.GetDirectoryName(ProgramPath), "FileTypes\\FileTypeDDS64.dll"));
}
else
{
File.Delete(Path.Combine(Path.GetDirectoryName(ProgramPath), "FileTypes\\FileTypeDDS32.dll"));
}
}
catch
{
}

// Done, invoke and set dialog
this.Invoke((Action)delegate
{
// Alert
MessageBox.Show("FileTypeDDS has been removed. You may now relaunch Paint.NET.", "FileTypeDDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Close
this.Close();
});
}

private void PreOps()
{
try
{
foreach (Process proc in Process.GetProcessesByName("PaintDotNet"))
proc.Kill();
}
catch
{

}
}

private void RunPatcher()
{
// Locate the Paint.NET exe
Expand Down Expand Up @@ -166,9 +273,12 @@ private void InstallGo_Click(object sender, EventArgs e)
{
// Disable us
this.InstallGo.Enabled = false;
this.BackOut.Enabled = false;
this.UninstallButton.Enabled = false;
// Make visible
this.ProgressLoad.Visible = true;

PreOps();

// Run the patcher
Task.Run((Action)RunPatcher);
}
Expand Down

0 comments on commit f9b4129

Please sign in to comment.