wip
This commit is contained in:
@@ -4,6 +4,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
@@ -44,7 +45,29 @@ namespace AnotherReplayReader
|
||||
|
||||
public BitmapSource? TryReadTarga(string path, Mod mod, double dpiX = 96.0, double dpiY = 96.0)
|
||||
{
|
||||
using var targa = TryGetTarga(path, mod);
|
||||
using var memoryStream = new MemoryStream();
|
||||
{
|
||||
using var stream = TryGetStream(path, mod);
|
||||
if (stream is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
stream.CopyTo(memoryStream);
|
||||
}
|
||||
|
||||
bool isJpgPng = IsJpgPng(memoryStream);
|
||||
memoryStream.Position = 0;
|
||||
if (isJpgPng)
|
||||
{
|
||||
try
|
||||
{
|
||||
var decoder = BitmapDecoder.Create(memoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
|
||||
return decoder.Frames[0];
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
memoryStream.Position = 0;
|
||||
using var targa = Targa.Create(memoryStream, new PfimConfig());
|
||||
if (targa == null)
|
||||
{
|
||||
return null;
|
||||
@@ -63,7 +86,7 @@ namespace AnotherReplayReader
|
||||
}
|
||||
}
|
||||
|
||||
private Targa? TryGetTarga(string path, Mod mod)
|
||||
private Stream? TryGetStream(string path, Mod mod)
|
||||
{
|
||||
const string customMapPrefix = "data/maps/internal/";
|
||||
if (Directory.Exists(_mapFolderPath) && path.StartsWith(customMapPrefix))
|
||||
@@ -71,7 +94,7 @@ namespace AnotherReplayReader
|
||||
var minimapPath = Path.Combine(_mapFolderPath, path.Substring(customMapPrefix.Length));
|
||||
if (File.Exists(minimapPath))
|
||||
{
|
||||
return Targa.Create(File.ReadAllBytes(minimapPath), new PfimConfig());
|
||||
return File.OpenRead(minimapPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,8 +116,7 @@ namespace AnotherReplayReader
|
||||
{
|
||||
continue;
|
||||
}
|
||||
using var stream = fs.OpenStream(path, VirtualFileModeType.Open);
|
||||
return Targa.Create(stream, new PfimConfig());
|
||||
return fs.OpenStream(path, VirtualFileModeType.Open);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -105,13 +127,38 @@ namespace AnotherReplayReader
|
||||
|
||||
if (_cache != null && _cache.TryGetEntry(path, out var big))
|
||||
{
|
||||
using (big)
|
||||
{
|
||||
return Targa.Create(big, new PfimConfig());
|
||||
}
|
||||
return big;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool IsJpgPng(Stream stream)
|
||||
{
|
||||
var header = new byte[4];
|
||||
var pos = stream.Position;
|
||||
|
||||
stream.Read(header, 0, header.Length);
|
||||
stream.Position = pos;
|
||||
|
||||
// PNG
|
||||
if (header[0] == 0x89 &&
|
||||
header[1] == 0x50 &&
|
||||
header[2] == 0x4E &&
|
||||
header[3] == 0x47)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// JPEG
|
||||
if (header[0] == 0xFF &&
|
||||
header[1] == 0xD8)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// TGA(很粗略判断:通常无统一magic,只能 fallback)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user