|
| 1 | +using FFXIVClientStructs.FFXIV.Client.System.Resource; |
| 2 | +using Penumbra.Api.Enums; |
| 3 | +using Penumbra.Collections; |
| 4 | +using Penumbra.GameData.Files; |
| 5 | +using Penumbra.GameData.Files.Utility; |
| 6 | +using Penumbra.Interop.Hooks.ResourceLoading; |
| 7 | +using Penumbra.String; |
| 8 | +using Penumbra.String.Classes; |
| 9 | +using Penumbra.UI; |
| 10 | + |
| 11 | +namespace Penumbra.Interop.Processing; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Path pre-processor for shader packages that reverts redirects to known invalid files, as bad ShPks can crash the game. |
| 15 | +/// </summary> |
| 16 | +public sealed class ShpkPathPreProcessor(ResourceManagerService resourceManager, ChatWarningService chatWarningService) : IPathPreProcessor |
| 17 | +{ |
| 18 | + public ResourceType Type |
| 19 | + => ResourceType.Shpk; |
| 20 | + |
| 21 | + public unsafe FullPath? PreProcess(ResolveData resolveData, CiByteString path, Utf8GamePath originalGamePath, bool nonDefault, FullPath? resolved) |
| 22 | + { |
| 23 | + chatWarningService.CleanLastFileWarnings(false); |
| 24 | + |
| 25 | + if (!resolved.HasValue) |
| 26 | + return null; |
| 27 | + |
| 28 | + // Skip the sanity check for game files. We are not considering the case where the user has modified game file: it's at their own risk. |
| 29 | + var resolvedPath = resolved.Value; |
| 30 | + if (!resolvedPath.IsRooted) |
| 31 | + return resolvedPath; |
| 32 | + |
| 33 | + // If the ShPk is already loaded, it means that it already passed the sanity check. |
| 34 | + var existingResource = resourceManager.FindResource(ResourceCategory.Shader, ResourceType.Shpk, unchecked((uint)resolvedPath.InternalName.Crc32)); |
| 35 | + if (existingResource != null) |
| 36 | + return resolvedPath; |
| 37 | + |
| 38 | + var checkResult = SanityCheck(resolvedPath.FullName); |
| 39 | + if (checkResult == SanityCheckResult.Success) |
| 40 | + return resolvedPath; |
| 41 | + |
| 42 | + Penumbra.Log.Warning($"Refusing to honor file redirection because of failed sanity check (result: {checkResult}). Original path: {originalGamePath} Redirected path: {resolvedPath}"); |
| 43 | + chatWarningService.PrintFileWarning(resolvedPath.FullName, originalGamePath, WarningMessageComplement(checkResult)); |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + private static SanityCheckResult SanityCheck(string path) |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + using var file = MmioMemoryManager.CreateFromFile(path); |
| 53 | + var bytes = file.GetSpan(); |
| 54 | + |
| 55 | + return ShpkFile.FastIsLegacy(bytes) |
| 56 | + ? SanityCheckResult.Legacy |
| 57 | + : SanityCheckResult.Success; |
| 58 | + } |
| 59 | + catch (FileNotFoundException) |
| 60 | + { |
| 61 | + return SanityCheckResult.NotFound; |
| 62 | + } |
| 63 | + catch (IOException) |
| 64 | + { |
| 65 | + return SanityCheckResult.IoError; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static string WarningMessageComplement(SanityCheckResult result) |
| 70 | + => result switch |
| 71 | + { |
| 72 | + SanityCheckResult.IoError => "cannot read the modded file.", |
| 73 | + SanityCheckResult.NotFound => "the modded file does not exist.", |
| 74 | + SanityCheckResult.Legacy => "this mod is not compatible with Dawntrail. Get an updated version, if possible, or disable it.", |
| 75 | + _ => string.Empty, |
| 76 | + }; |
| 77 | + |
| 78 | + private enum SanityCheckResult |
| 79 | + { |
| 80 | + Success, |
| 81 | + IoError, |
| 82 | + NotFound, |
| 83 | + Legacy, |
| 84 | + } |
| 85 | +} |
0 commit comments