Skip to content

Commit

Permalink
Merge pull request #964 from kenjiuno/fix-mdlx-imp-202401
Browse files Browse the repository at this point in the history
Fix a bug of PNG file loader. And also adding some exception handling things
  • Loading branch information
Vladabdf authored Jan 6, 2024
2 parents f21e76e + d7dfe86 commit b061dd8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
6 changes: 3 additions & 3 deletions OpenKh.Imaging/PngImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public PngImage(Stream stream)
IHdr ihdr = null;
byte[] PLTE = null;
byte[] tRNS = null;
byte[] IDAT = null;
var IDAT = new MemoryStream();

while (stream.Position < stream.Length)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ public PngImage(Stream stream)
break;

case Chunk.IDAT:
IDAT = chunk.RawData;
IDAT.Write(chunk.RawData);
break;
}
}
Expand All @@ -140,7 +140,7 @@ public PngImage(Stream stream)
throw new NotSupportedException($"comprMethod {comprMethod} not supported!");
}

using var deflated = new MemoryStream(ZlibStream.UncompressBuffer(IDAT));
using var deflated = new MemoryStream(ZlibStream.UncompressBuffer(IDAT.ToArray()));
deflated.FromBegin();

var bits = ihdr.Bits;
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Tools.Kh2MdlxEditor/Utils/ImageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static Imgd pngToImgd(string filePath)
}
catch (Exception excep)
{
throw new Exception("Error loading texture: " + filePath);
throw new Exception("Error loading texture: " + filePath, excep);
}
}

Expand Down
8 changes: 7 additions & 1 deletion OpenKh.Tools.Kh2MdlxEditor/Utils/MdlxEditorImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ModelTexture createModelTexture(Assimp.Scene scene, string filePat
{
materialToTexture = new Dictionary<int, int>();

string directoryPath = Path.GetDirectoryName(filePath);
string directoryPath = Path.GetDirectoryName(filePath) ?? throw new DirectoryNotFoundException(filePath);

List<Imgd> imgdList = new List<Imgd>();

Expand All @@ -37,6 +37,12 @@ public static ModelTexture createModelTexture(Assimp.Scene scene, string filePat
{
throw new Exception("Texture is not PNG");
}

if (!File.Exists(texturePath))
{
texturePath = Path.Combine(directoryPath, $"texture{i:000}.png");
}

if (materialPaths.Contains(texturePath))
{
materialToTexture.Add(i, materialPaths.IndexOf(texturePath));
Expand Down
33 changes: 21 additions & 12 deletions OpenKh.Tools.Kh2MdlxEditor/Views/Main2_Window.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,30 @@ private void Window_Drop(object sender, DragEventArgs e)
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
string firstFile = files?.FirstOrDefault();

if(firstFile.ToLower().EndsWith(".mdlx"))
{
loadFile(firstFile);
}
else if(firstFile.ToLower().EndsWith(".fbx") || firstFile.ToLower().EndsWith(".dae"))
{
mainVM.replaceModel(firstFile);
if (files?.FirstOrDefault() is string firstFile)
{
try
{
if (firstFile.ToLower().EndsWith(".mdlx"))
{
loadFile(firstFile);
}
else if (firstFile.ToLower().EndsWith(".fbx") || firstFile.ToLower().EndsWith(".dae"))
{
mainVM ??= new Main2_VM();
mainVM.replaceModel(firstFile);
}

reloadModelControl();
}
catch (Exception ex)
{
MessageBox.Show($"There is an error while import file: {firstFile}\n\n{ex}");
}
}

reloadModelControl();
}
}
catch(Exception exc)
catch
{
}
}
Expand Down

0 comments on commit b061dd8

Please sign in to comment.