Skip to content

Commit

Permalink
Fix: Handle rooted paths in obj files
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Dec 9, 2023
1 parent 6a27b7a commit 1a5692b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/AssimpParser/Wavefront/ObjFileMtlImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using OpenBveApi;
using OpenBveApi.Colors;
using OpenBveApi.Interface;

namespace AssimpNET.Obj
{
Expand Down Expand Up @@ -488,6 +490,12 @@ private void GetTextureName(ref string result)
{
result = texture;
}

if (System.IO.Path.IsPathRooted(result))
{
// rooted path- try looking beside the object instead
result = Path.GetFileName(result);
}
}

// Creates a material from loaded data.
Expand Down
6 changes: 6 additions & 0 deletions source/Plugins/Object.Wavefront/Parsers/WavefrontObjParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ private static void LoadMaterials(string FileName, ref Dictionary<string, Materi
break;
case WavefrontMtlCommands.Map_Kd:
case WavefrontMtlCommands.Map_Ka:
if(Path.IsPathRooted(Arguments[Arguments.Count - 1]))
{
// Rooted path should not be used- Try looking beside the object instead
Plugin.currentHost.AddMessage(MessageType.Warning, false, "Encountered rooted path for " + currentKey);
Arguments[Arguments.Count - 1] = Path.GetFileName(Arguments[Arguments.Count - 1]);
}
string tday = OpenBveApi.Path.CombineFile(Path.GetDirectoryName(FileName), Arguments[Arguments.Count - 1]);
if (File.Exists(tday))
{
Expand Down

0 comments on commit 1a5692b

Please sign in to comment.