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

Give real file name when an error occurs reading a p3d file #225

Merged
merged 1 commit into from
May 27, 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
5 changes: 5 additions & 0 deletions GameRealisticMap.Arma3.Test/GameEngine/GameFileSystemMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,10 @@ internal void AssertFiles<TestClass>(string testcase)
return new StreamReader(stream).ReadToEnd();
}
}

public string? GetLocationInfoForError(string path)
{
return path;
}
}
}
23 changes: 23 additions & 0 deletions GameRealisticMap.Arma3.Test/TerrainBuilder/ModelInfoLibraryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GameRealisticMap.Arma3.TerrainBuilder;
using GameRealisticMap.Arma3.Test.GameEngine;

namespace GameRealisticMap.Arma3.Test.TerrainBuilder
{
public class ModelInfoLibraryTest
{
[Fact]
public void ReadModelInfoOnly_CorruptedFile()
{
var fs = new GameFileSystemMock();
fs.BinaryFiles.Add("bad.p3d", new MemoryStream());
var lib = new ModelInfoLibrary(fs);
var ex = Assert.Throws<ApplicationException>(() => lib.ReadModelInfoOnly("bad.p3d"));
Assert.Equal("Unable to read file 'bad.p3d': Unable to read beyond the end of the stream.", ex.Message);
}
}
}
5 changes: 4 additions & 1 deletion GameRealisticMap.Arma3/IO/IGameFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
public interface IGameFileSystem
{
IEnumerable<string> FindAll(string pattern);

Stream? OpenFileIfExists(string path);

DateTime? GetLastWriteTimeUtc(string path);
DateTime? GetLastWriteTimeUtc(string path);

string? GetLocationInfoForError(string path);
}
}
10 changes: 10 additions & 0 deletions GameRealisticMap.Arma3/IO/PboFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,15 @@ private void FindAll(Regex regex, List<string> result, string pboPath)
}
return null;
}

public string? GetLocationInfoForError(string path)
{
var pbo = GetPboFile(path);
if (pbo != null)
{
return pbo + "#" + path;
}
return null;
}
}
}
10 changes: 10 additions & 0 deletions GameRealisticMap.Arma3/IO/ProjectDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,15 @@ public IEnumerable<string> FindAll(string pattern)
}
return secondarySource?.GetLastWriteTimeUtc(path);
}

public string? GetLocationInfoForError(string path)
{
var fullPath = GetFullPath(path);
if (File.Exists(fullPath))
{
return fullPath;
}
return secondarySource?.GetLocationInfoForError(path);
}
}
}
21 changes: 17 additions & 4 deletions GameRealisticMap.Arma3/TerrainBuilder/ModelInfoLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ private string UniqueName(string initialName)
return name;
}

private T Read<T>(Stream stream, string path) where T : IReadObject, new()
{
try
{
return StreamHelper.Read<T>(stream);
}
catch (Exception ex)
{
var physicalPath = fileSystem.GetLocationInfoForError(path) ?? path;
throw new ApplicationException($"Unable to read file '{physicalPath}': {ex.Message}", ex);
}
}

public BIS.P3D.ODOL.ModelInfo? ReadModelInfoOnly(string path)
{
using (var stream = fileSystem.OpenFileIfExists(path))
{
if (stream != null)
{
var result = StreamHelper.Read<P3DInfosOnly>(stream).ModelInfo as BIS.P3D.ODOL.ModelInfo;
var result = Read<P3DInfosOnly>(stream, path).ModelInfo as BIS.P3D.ODOL.ModelInfo;
if (result != null)
{
return result;
Expand All @@ -101,7 +114,7 @@ private string UniqueName(string initialName)
{
if (streamTemp != null)
{
return StreamHelper.Read<P3DInfosOnly>(streamTemp).ModelInfo as BIS.P3D.ODOL.ModelInfo;
return Read<P3DInfosOnly>(streamTemp, "temp\\" + path).ModelInfo as BIS.P3D.ODOL.ModelInfo;
}
}
// TODO: Binarize on the fly
Expand All @@ -118,14 +131,14 @@ private string UniqueName(string initialName)
{
if (P3D.IsODOL(stream))
{
return StreamHelper.Read<ODOL>(stream);
return Read<ODOL>(stream, path);
}
// Mikero Tools binarize into project drive temp, binarized file might be there
using (var streamTemp = fileSystem.OpenFileIfExists("temp\\" + path))
{
if (streamTemp != null && P3D.IsODOL(streamTemp))
{
return StreamHelper.Read<ODOL>(streamTemp);
return Read<ODOL>(streamTemp, "temp\\" + path);
}
}
// TODO: Binarize on the fly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public IEnumerable<string> FindAll(string pattern)
return null;
}

public string? GetLocationInfoForError(string path)
{
return path;
}

public Stream? OpenFileIfExists(string path)
{
if (files.Contains(path))
Expand Down
Loading