Skip to content

Commit

Permalink
Merge pull request #850 from WolvenKit/fix/preview-fix
Browse files Browse the repository at this point in the history
Fix RedDocumentViewModel.GetFileFromDepotPath
  • Loading branch information
rfuzzo committed Jul 4, 2022
2 parents 8e96f12 + 2573cf4 commit 8c2d65d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
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

0 comments on commit 8c2d65d

Please sign in to comment.