Skip to content

Commit

Permalink
[squishlib] compile directory (same output as harepacker & creator)
Browse files Browse the repository at this point in the history
  • Loading branch information
lastbattle committed Mar 15, 2021
1 parent 416ba97 commit 0edc041
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 139 deletions.
2 changes: 1 addition & 1 deletion HaCreator/MapSimulator/MapSimulatorLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
using HaCreator.MapSimulator.Objects.FieldObject;
using HaCreator.MapSimulator.Objects.UIObject;
using HaCreator.Wz;
using HaSharedLibrary.Converter;
using HaSharedLibrary.Render.DX;
using HaSharedLibrary.Util;
using MapleLib.WzLib;
using MapleLib.WzLib.Spine;
using MapleLib.WzLib.WzProperties;
using MapleLib.WzLib.WzStructure;
using MapleLib.WzLib.WzStructure.Data;
using MapleLib.Converters;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Spine;
Expand Down
2 changes: 1 addition & 1 deletion HaCreator/MapSimulator/UIFrameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HaSharedLibrary.Converter;
using MapleLib.Converters;

namespace HaCreator.MapSimulator
{
Expand Down
8 changes: 4 additions & 4 deletions HaRepacker/GUI/Panels/MainPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MapleLib.WzLib;
using MapleLib.WzLib.Spine;
using MapleLib.WzLib.WzProperties;
using MapleLib.Converters;
using Microsoft.Xna.Framework;
using Spine;
using System;
Expand All @@ -29,7 +30,6 @@
using System.Windows.Threading;
using static MapleLib.Configuration.UserSettings;
using System.Reflection;
using HaSharedLibrary.Converter;
using System.Text.RegularExpressions;
using System.IO;

Expand Down Expand Up @@ -1419,10 +1419,10 @@ private void ShowObjectValue(WzObject obj)
{
System.Drawing.Image img = canvasProp.GetLinkedWzCanvasBitmap();
if (img != null)
canvasPropBox.Image = HaSharedLibrary.Converter.ImageConverter.ToWpfBitmap((System.Drawing.Bitmap)img);
canvasPropBox.Image = ((System.Drawing.Bitmap)img).ToWpfBitmap();
}
else
canvasPropBox.Image = HaSharedLibrary.Converter.ImageConverter.ToWpfBitmap(canvasProp.GetLinkedWzCanvasBitmap());
canvasPropBox.Image = canvasProp.GetLinkedWzCanvasBitmap().ToWpfBitmap();

SetImageRenderView(canvasProp);
}
Expand All @@ -1435,7 +1435,7 @@ private void ShowObjectValue(WzObject obj)
if (linkValue is WzCanvasProperty canvasUOL)
{
canvasPropBox.Visibility = Visibility.Visible;
canvasPropBox.Image = HaSharedLibrary.Converter.ImageConverter.ToWpfBitmap(canvasUOL.GetLinkedWzCanvasBitmap()); // in any event that the WzCanvasProperty is an '_inlink' or '_outlink'
canvasPropBox.Image = canvasUOL.GetLinkedWzCanvasBitmap().ToWpfBitmap(); // in any event that the WzCanvasProperty is an '_inlink' or '_outlink'
menuItem_saveImage.Visibility = Visibility.Visible; // dont show change image, as its a UOL

SetImageRenderView(canvasUOL);
Expand Down
1 change: 0 additions & 1 deletion HaSharedLibrary/HaSharedLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Converter\ImageConverter.cs" />
<Compile Include="GUI\FieldTypePanel.xaml.cs">
<DependentUpon>FieldTypePanel.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace HaSharedLibrary.Converter
namespace MapleLib.Converters
{
public static class ImageConverter
{
Expand Down
4 changes: 4 additions & 0 deletions MapleLib/MapleLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.4.2.0\lib\net45\SharpDX.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -190,11 +192,13 @@
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.XML" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Configuration\ApplicationSettings.cs" />
<Compile Include="Configuration\ConfigurationManager.cs" />
<Compile Include="Configuration\UserSettings.cs" />
<Compile Include="Converters\ImageConverter.cs" />
<Compile Include="Helpers\ByteUtils.cs" />
<Compile Include="Helpers\ErrorLogger.cs" />
<Compile Include="PacketLib\HexTool.cs" />
Expand Down
127 changes: 62 additions & 65 deletions MapleLib/SquishPNGWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,77 @@ namespace MapleLib
/// </summary>
public class SquishPNGWrapper
{
/// <summary>
/// Returns true if the squish library can be used on this HaRepacker assembly
/// </summary>
private static bool CanUseSquishNativeLibrary()
private enum SquishLibLoadingState
{
if (_bSquishLibLoaded)
return true; // shouldnt check again once its loaded anyway.

//similarly to find process architecture
var assemblyArchitecture = Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture;
if (!(assemblyArchitecture == ProcessorArchitecture.X86 || assemblyArchitecture == ProcessorArchitecture.Amd64))
{
return false;
}
return true;
Uninitialised = 0x0,
Initialised = 0x1,
WrongProcessorArchitecture = 0x2,
UnknownError = 0x3,
}

private static bool _bSquishLibLoaded = false;
private static SquishLibLoadingState _SquishLibLoadingState = SquishLibLoadingState.Uninitialised;

/// <summary>
/// Load library
/// Returns true if the squish library can be used on this HaRepacker assembly
/// </summary>
public static bool CheckAndLoadLibrary()
{
if (!CanUseSquishNativeLibrary())
{
return false;
}
if (_SquishLibLoadingState == SquishLibLoadingState.Initialised)
return true; // shouldnt check again once its loaded anyway.

if (_bSquishLibLoaded)
return true;
AssemblyName executingAssemblyName = Assembly.GetExecutingAssembly().GetName();
//similarly to find process architecture
var assemblyArchitecture = executingAssemblyName.ProcessorArchitecture;
if (!(assemblyArchitecture == ProcessorArchitecture.X86 || assemblyArchitecture == ProcessorArchitecture.Amd64))
else if (_SquishLibLoadingState == SquishLibLoadingState.Uninitialised) // not init yet
{
throw new Exception("squish.dll is only available for x86 or x64 version of harepacker assembly.");
//similarly to find process architecture
var assemblyArchitecture = Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture;
if (!(assemblyArchitecture == ProcessorArchitecture.X86 || assemblyArchitecture == ProcessorArchitecture.Amd64))
{
_SquishLibLoadingState = SquishLibLoadingState.WrongProcessorArchitecture;
return false;
}
// Load library here
IntPtr apnglib = LoadLibrary("squish.dll");
if (apnglib != IntPtr.Zero)
{
IntPtr FixFlagsPtr = GetProcAddress(apnglib, "_DLLEXPORT_FixFlags");
if (FixFlagsPtr != null)
FixFlags = (_DLLEXPORT_FixFlags)Marshal.GetDelegateForFunctionPointer(FixFlagsPtr, typeof(_DLLEXPORT_FixFlags));

IntPtr CompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Compress");
if (CompressPtr != null)
Compress = (_DLLEXPORT_Compress)Marshal.GetDelegateForFunctionPointer(CompressPtr, typeof(_DLLEXPORT_Compress));

IntPtr CompressMaskedPtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressMasked");
if (CompressMaskedPtr != null)
CompressMasked = (_DLLEXPORT_CompressMasked)Marshal.GetDelegateForFunctionPointer(CompressMaskedPtr, typeof(_DLLEXPORT_CompressMasked));

IntPtr DecompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Decompress");
if (DecompressPtr != null)
Decompress = (_DLLEXPORT_Decompress)Marshal.GetDelegateForFunctionPointer(DecompressPtr, typeof(_DLLEXPORT_Decompress));

IntPtr StorageRequirementsPtr = GetProcAddress(apnglib, "_DLLEXPORT_GetStorageRequirements");
if (StorageRequirementsPtr != null)
GetStorageRequirements = (_DLLEXPORT_GetStorageRequirements)Marshal.GetDelegateForFunctionPointer(StorageRequirementsPtr, typeof(_DLLEXPORT_GetStorageRequirements));

IntPtr CompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressImage");
if (CompressImagePtr != null)
CompressImage = (_DLLEXPORT_CompressImage)Marshal.GetDelegateForFunctionPointer(CompressImagePtr, typeof(_DLLEXPORT_CompressImage));

IntPtr DecompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_DecompressImage");
if (DecompressImagePtr != null)
DecompressImage = (_DLLEXPORT_DecompressImage)Marshal.GetDelegateForFunctionPointer(DecompressImagePtr, typeof(_DLLEXPORT_DecompressImage));


_SquishLibLoadingState = SquishLibLoadingState.Initialised; // flag as initialised
return true;
}
else
{
_SquishLibLoadingState = SquishLibLoadingState.UnknownError;
return false;
//throw new Exception("squish.dll not found in the program directory.");
}
}

IntPtr apnglib = LoadLibrary("squish.dll");
if (apnglib != IntPtr.Zero)
{
IntPtr FixFlagsPtr = GetProcAddress(apnglib, "_DLLEXPORT_FixFlags");
if (FixFlagsPtr != null)
FixFlags = (_DLLEXPORT_FixFlags)Marshal.GetDelegateForFunctionPointer(FixFlagsPtr, typeof(_DLLEXPORT_FixFlags));

IntPtr CompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Compress");
if (CompressPtr != null)
Compress = (_DLLEXPORT_Compress)Marshal.GetDelegateForFunctionPointer(CompressPtr, typeof(_DLLEXPORT_Compress));

IntPtr CompressMaskedPtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressMasked");
if (CompressMaskedPtr != null)
CompressMasked = (_DLLEXPORT_CompressMasked)Marshal.GetDelegateForFunctionPointer(CompressMaskedPtr, typeof(_DLLEXPORT_CompressMasked));

IntPtr DecompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Decompress");
if (DecompressPtr != null)
Decompress = (_DLLEXPORT_Decompress)Marshal.GetDelegateForFunctionPointer(DecompressPtr, typeof(_DLLEXPORT_Decompress));

IntPtr StorageRequirementsPtr = GetProcAddress(apnglib, "_DLLEXPORT_GetStorageRequirements");
if (StorageRequirementsPtr != null)
GetStorageRequirements = (_DLLEXPORT_GetStorageRequirements)Marshal.GetDelegateForFunctionPointer(StorageRequirementsPtr, typeof(_DLLEXPORT_GetStorageRequirements));

IntPtr CompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressImage");
if (CompressImagePtr != null)
CompressImage = (_DLLEXPORT_CompressImage)Marshal.GetDelegateForFunctionPointer(CompressImagePtr, typeof(_DLLEXPORT_CompressImage));

IntPtr DecompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_DecompressImage");
if (DecompressImagePtr != null)
DecompressImage = (_DLLEXPORT_DecompressImage)Marshal.GetDelegateForFunctionPointer(DecompressImagePtr, typeof(_DLLEXPORT_DecompressImage));
}
else
throw new Exception("squish.dll not found in the program directory.");

_bSquishLibLoaded = true;
return true;
return false;
}

#region Exports
Expand Down Expand Up @@ -132,7 +129,7 @@ public static bool CheckAndLoadLibrary()
public delegate int _DLLEXPORT_GetStorageRequirements(int width, int height, int flags);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void _DLLEXPORT_CompressImage(byte[] rgba, int width, int height, byte[] blocks, int flags);
public delegate void _DLLEXPORT_CompressImage(byte[] rgba_src, int width, int height, byte[] blocks, int flags);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void _DLLEXPORT_DecompressImage(byte[] rgba, int width, int height, byte[] blocks, int flags);
Expand Down
Loading

0 comments on commit 0edc041

Please sign in to comment.