Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug of PNG file loader. And also adding some exception handling things #964

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading