改了一亿个东西,修了一亿个 BUG

This commit is contained in:
2021-10-19 17:42:18 +02:00
parent 5b907309e0
commit 888ddce4ef
40 changed files with 1870 additions and 1715 deletions

View File

@@ -1,13 +1,13 @@
using System;
using AnotherReplayReader.ReplayFile;
using Pfim;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using OpenSage.FileFormats.Big;
using Pfim;
using TechnologyAssembler.Core.IO;
namespace AnotherReplayReader
{
@@ -24,104 +24,93 @@ namespace AnotherReplayReader
};
private readonly BigMinimapCache _cache;
private readonly string _ra3InstallPath;
private readonly string _mapFolderPath;
private readonly string _modFolderPath;
public MinimapReader(BigMinimapCache cache, string ra3InstallPath, string mapFolderPath, string modFolderPath)
public MinimapReader(BigMinimapCache cache, string mapFolderPath, string modFolderPath)
{
_cache = cache;
_ra3InstallPath = ra3InstallPath;
_mapFolderPath = mapFolderPath;
_modFolderPath = modFolderPath;
}
public BitmapSource TryReadTarga(string path, Mod mod, double dpiX = 96.0, double dpiY = 96.0)
public Task<BitmapSource?> TryReadTargaAsync(Replay replay, double dpiX = 96.0, double dpiY = 96.0)
{
using (var targa = TryGetTarga(path, mod))
{
if(targa == null)
{
return null;
}
var mapPath = replay.MapPath.TrimEnd('/');
var mapName = mapPath.Substring(mapPath.LastIndexOf('/') + 1);
var minimapPath = $"{mapPath}/{mapName}_art.tga";
return Task.Run(() => TryReadTarga(minimapPath, replay.Mod, dpiX, dpiY));
}
try
{
return BitmapSource.Create(targa.Width, targa.Height, dpiX, dpiY, FormatMapper[targa.Format], null, targa.Data, targa.Stride);
}
catch (Exception exception)
{
Debug.Instance.DebugMessage += $"Exception creating BitmapSource from minimap:\r\n {exception}\r\n";
return null;
}
public BitmapSource? TryReadTarga(string path, Mod mod, double dpiX = 96.0, double dpiY = 96.0)
{
using var targa = TryGetTarga(path, mod);
if (targa == null)
{
return null;
}
try
{
var bitmap = BitmapSource.Create(targa.Width, targa.Height, dpiX, dpiY, FormatMapper[targa.Format], null, targa.Data, targa.Stride);
bitmap.Freeze();
return bitmap;
}
catch (Exception exception)
{
Debug.Instance.DebugMessage += $"Exception creating BitmapSource from minimap:\r\n {exception}\r\n";
return null;
}
}
private Targa TryGetTarga(string path, Mod mod)
private Targa? TryGetTarga(string path, Mod mod)
{
var tga = null as Targa;
const string customMapPrefix = "data/maps/internal/";
if (Directory.Exists(_mapFolderPath) && path.StartsWith(customMapPrefix))
{
var minimapPath = Path.Combine(_mapFolderPath, path.Substring(customMapPrefix.Length));
if(File.Exists(minimapPath))
{
return Targa.Create(File.ReadAllBytes(minimapPath), new PfimConfig());
}
}
// now, normalize paths
path = path.Replace('/', '\\');
if(!mod.IsRA3)
{
try
{
var modSkudefPaths = Enumerable.Empty<string>();
foreach (var subFolder in Directory.EnumerateDirectories(_modFolderPath))
{
modSkudefPaths = modSkudefPaths.Concat(Directory.EnumerateFiles(subFolder, $"{mod.ModName}_{mod.ModVersion}.SkuDef"));
}
foreach (var modSkudefPath in modSkudefPaths)
{
var modBigPaths = BigMinimapCache.ParseSkudefs(new[] { modSkudefPath });
foreach (var modBigPath in modBigPaths)
{
using (var modBig = new BigArchive(modBigPath))
{
var entry = modBig.GetEntry(path);
if (entry != null)
{
return Targa.Create(entry.Open(), new PfimConfig());
}
}
}
}
}
catch (Exception exception)
{
Debug.Instance.DebugMessage += $"Exception when reading minimap from Skudef big:\r\n {exception}\r\n";
}
}
if (_cache != null && _cache.TryGetBigByEntryPath(path, out var big))
{
using (big)
{
return Targa.Create(big.GetEntry(path).Open(), new PfimConfig());
}
}
if (Directory.Exists(_ra3InstallPath))
{
var minimapPath = Path.Combine(_mapFolderPath, path);
if (File.Exists(minimapPath))
{
return Targa.Create(File.ReadAllBytes(minimapPath), new PfimConfig());
}
}
if (!mod.IsRA3)
{
var modSkudefPaths = Enumerable.Empty<string>();
foreach (var subFolder in Directory.EnumerateDirectories(_modFolderPath))
{
modSkudefPaths = modSkudefPaths.Concat(Directory.EnumerateFiles(subFolder, $"{mod.ModName}_{mod.ModVersion}.SkuDef"));
}
DronePlatform.BuildTechnologyAssembler();
foreach (var modSkudefPath in modSkudefPaths)
{
try
{
using var fs = new SkuDefFileSystemProvider("modConfig", modSkudefPath);
if (!fs.FileExists(path))
{
continue;
}
using var stream = fs.OpenStream(path, VirtualFileModeType.Open);
return Targa.Create(stream, new PfimConfig());
}
catch (Exception exception)
{
Debug.Instance.DebugMessage += $"Exception when reading minimap from mod bigs:\r\n {exception}\r\n";
}
}
}
if (_cache != null && _cache.TryGetEntry(path, out var big))
{
using (big)
{
return Targa.Create(big, new PfimConfig());
}
}
return null;
}
}