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 RedDocumentViewModel.GetFileFromDepotPath #850

Merged
merged 1 commit into from
Jul 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public InkTextureAtlasMapperViewModel(inkTextureAtlasMapper itam, CBitmapTexture
Width = Math.Round(itam.ClippingRectInUVCoords.Right * xbm.Width) - Left;
Height = Math.Round(itam.ClippingRectInUVCoords.Bottom * xbm.Height) - Top;
Name = $"{itam.PartName} ({(uint)Width}x{(uint)Height})";
SaveImageCommand = new DelegateCommand(ExecuteSaveImage, CanSaveImage).ObservesProperty(() => Image);
SaveImageCommand = new DelegateCommand(ExecuteSaveImage, CanSaveImage); // .ObservesProperty(() => Image); WKit crashs when using this, don't know why...

try
{
Expand Down
73 changes: 29 additions & 44 deletions WolvenKit.App/ViewModels/Documents/RedDocumentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,17 @@ public void HandleEmbeddedFile(DialogViewModel sender)

public CR2WFile GetFileFromDepotPath(CName depotPath, bool original = false)
{
CR2WFile cr2wFile = null;

try
{
CR2WFile cr2wFile = null;

if (!original)
{
var projectManager = Locator.Current.GetService<IProjectManager>();
if (projectManager.ActiveProject != null)
{
string path = null;
if ((string)depotPath != null)
if (!string.IsNullOrEmpty(depotPath))
{
path = Path.Combine(projectManager.ActiveProject.ModDirectory, (string)depotPath);
}
Expand All @@ -357,52 +357,31 @@ public CR2WFile GetFileFromDepotPath(CName depotPath, bool original = false)

if (path != null && File.Exists(path))
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using var reader = new BinaryReader(stream);
cr2wFile = _parser.ReadRed4File(reader);
}

cr2wFile.MetaData.FileName = depotPath;

lock (Files)
{
foreach (var res in cr2wFile.EmbeddedFiles)
{
if (!Files.ContainsKey(res.FileName))
{
Files.Add(res.FileName, new CR2WFile()
{
RootChunk = res.Content
});
}
}
}

return cr2wFile;
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new BinaryReader(stream);
cr2wFile = _parser.ReadRed4File(reader);
}
}
}
}
catch (NullReferenceException)
{
var _archiveManager = Locator.Current.GetService<IArchiveManager>();
var file = _archiveManager.Lookup(depotPath.GetRedHash());
if (file.HasValue && file.Value is FileEntry fe)

if (cr2wFile == null)
{
using (var stream = new MemoryStream())
var _archiveManager = Locator.Current.GetService<IArchiveManager>();
var file = _archiveManager.Lookup(depotPath.GetRedHash());
if (file.HasValue && file.Value is FileEntry fe)
{
using var stream = new MemoryStream();
fe.Extract(stream);
using var reader = new BinaryReader(stream);
cr2wFile = _parser.ReadRed4File(reader);
if (cr2wFile == null)
{
return null;
}
if ((string)depotPath != null)
{
cr2wFile.MetaData.FileName = depotPath;
}

cr2wFile = _parser.ReadRed4File(stream);
}
}

if (cr2wFile != null)
{
if (!string.IsNullOrEmpty(depotPath))
{
cr2wFile.MetaData.FileName = depotPath;
}

lock (Files)
Expand All @@ -418,10 +397,16 @@ public CR2WFile GetFileFromDepotPath(CName depotPath, bool original = false)
}
}
}

return cr2wFile;
}
}
catch (Exception)
{
// ignore
}

return cr2wFile;
return null;
}

public RedDocumentTabViewModel OpenRefAsTab(string path)
Expand Down