not bullet proof but good enough

This commit is contained in:
4sval
2022-08-07 04:50:39 +02:00
parent a8c60ac3a9
commit 947d5011c2
14 changed files with 256 additions and 166 deletions

View File

@@ -51,7 +51,7 @@ public class AesManagerViewModel : ViewModel
DynamicKeys = null
};
_mainKey.Key = FixKey(_keysFromSettings.MainKey);
_mainKey.Key = Helper.FixKey(_keysFromSettings.MainKey);
AesKeys = new FullyObservableCollection<FileItem>(EnumerateAesKeys());
AesKeys.ItemPropertyChanged += AesKeysOnItemPropertyChanged;
AesKeysView = new ListCollectionView(AesKeys) { SortDescriptions = { new SortDescription("Name", ListSortDirection.Ascending) } };
@@ -63,11 +63,11 @@ public class AesManagerViewModel : ViewModel
if (e.PropertyName != "Key" || sender is not FullyObservableCollection<FileItem> collection)
return;
var key = FixKey(collection[e.CollectionIndex].Key);
var key = Helper.FixKey(collection[e.CollectionIndex].Key);
if (e.CollectionIndex == 0)
{
if (!HasChange)
HasChange = FixKey(_keysFromSettings.MainKey) != key;
HasChange = Helper.FixKey(_keysFromSettings.MainKey) != key;
_keysFromSettings.MainKey = key;
}
@@ -87,7 +87,7 @@ public class AesManagerViewModel : ViewModel
else if (_keysFromSettings.DynamicKeys.FirstOrDefault(x => x.Guid == collection[e.CollectionIndex].Guid.ToString()) is { } d)
{
if (!HasChange)
HasChange = FixKey(d.Key) != key;
HasChange = Helper.FixKey(d.Key) != key;
d.Key = key;
}
@@ -117,17 +117,6 @@ public class AesManagerViewModel : ViewModel
Log.Information("{@Json}", UserSettings.Default);
}
private string FixKey(string key)
{
if (string.IsNullOrEmpty(key))
return string.Empty;
if (key.StartsWith("0x"))
key = key[2..];
return "0x" + key.ToUpper().Trim();
}
private IEnumerable<FileItem> EnumerateAesKeys()
{
yield return _mainKey;
@@ -145,7 +134,7 @@ public class AesManagerViewModel : ViewModel
k = dynamicKey.Key;
}
file.Key = FixKey(k);
file.Key = Helper.FixKey(k);
yield return file;
}
}

View File

@@ -1,7 +1,9 @@
using System.Threading;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FModel.Framework;
using FModel.ViewModels.ApiEndpoints.Models;
using Newtonsoft.Json.Linq;
using RestSharp;
using Serilog;
@@ -13,35 +15,55 @@ public class DynamicApiEndpoint : AbstractApiProvider
{
}
public async Task<AesResponse> GetAesKeysAsync(CancellationToken token, string url)
public async Task<AesResponse> GetAesKeysAsync(CancellationToken token, string url, string path)
{
var request = new FRestRequest(url)
{
OnBeforeDeserialization = resp => { resp.ContentType = "application/json; charset=utf-8"; }
};
var response = await _client.ExecuteAsync<AesResponse>(request, token).ConfigureAwait(false);
var response = await _client.ExecuteAsync(request, token).ConfigureAwait(false);
var body = JToken.Parse(response.Content!);
Log.Information("[{Method}] [{Status}({StatusCode})] '{Resource}'", request.Method, response.StatusDescription, (int) response.StatusCode, response.ResponseUri?.OriginalString);
return response.Data;
var tokens = body.SelectTokens(path);
var ret = new AesResponse { MainKey = Helper.FixKey(tokens.ElementAtOrDefault(0).ToString()) };
if (tokens.ElementAtOrDefault(1) is JArray dynamicKeys)
{
foreach (var dynamicKey in dynamicKeys)
{
if (dynamicKey["guid"] is not { } guid || dynamicKey["key"] is not { } key)
continue;
ret.DynamicKeys.Add(new DynamicKey{Guid = guid.ToString(), Key = Helper.FixKey(key.ToString())});
}
}
return ret;
}
public AesResponse GetAesKeys(CancellationToken token, string url)
public AesResponse GetAesKeys(CancellationToken token, string url, string path)
{
return GetAesKeysAsync(token, url).GetAwaiter().GetResult();
return GetAesKeysAsync(token, url, path).GetAwaiter().GetResult();
}
public async Task<MappingsResponse[]> GetMappingsAsync(CancellationToken token, string url)
public async Task<MappingsResponse[]> GetMappingsAsync(CancellationToken token, string url, string path)
{
var request = new FRestRequest(url)
{
OnBeforeDeserialization = resp => { resp.ContentType = "application/json; charset=utf-8"; }
};
var response = await _client.ExecuteAsync<MappingsResponse[]>(request, token).ConfigureAwait(false);
var response = await _client.ExecuteAsync(request, token).ConfigureAwait(false);
var body = JToken.Parse(response.Content!);
Log.Information("[{Method}] [{Status}({StatusCode})] '{Resource}'", request.Method, response.StatusDescription, (int) response.StatusCode, response.ResponseUri?.OriginalString);
return response.Data;
var tokens = body.SelectTokens(path);
var ret = new MappingsResponse[] {new()};
ret[0].Url = tokens.ElementAtOrDefault(0).ToString();
ret[0].FileName = tokens.ElementAtOrDefault(1).ToString();
return ret;
}
public MappingsResponse[] GetMappings(CancellationToken token, string url)
public MappingsResponse[] GetMappings(CancellationToken token, string url, string path)
{
return GetMappingsAsync(token, url).GetAwaiter().GetResult();
return GetMappingsAsync(token, url, path).GetAwaiter().GetResult();
}
}

View File

@@ -19,7 +19,7 @@ public class AesResponse
}
[I] public bool HasDynamicKeys => DynamicKeys is { Count: > 0 };
[I] public bool IsValid => !string.IsNullOrEmpty(MainKey);
[I] public bool IsValid => MainKey.Length == 66;
}
[DebuggerDisplay("{" + nameof(Key) + "}")]
@@ -29,6 +29,5 @@ public class DynamicKey
[J("guid")] public string Guid { get; set; }
[J("key")] public string Key { get; set; }
[I] public bool IsValid => !string.IsNullOrEmpty(Guid) &&
!string.IsNullOrEmpty(Key);
[I] public bool IsValid => Guid.Length == 32 && Key.Length == 66;
}

View File

@@ -22,8 +22,7 @@ public class MappingsResponse
}
[I] public bool IsValid => !string.IsNullOrEmpty(Url) &&
!string.IsNullOrEmpty(FileName) &&
Meta != null;
!string.IsNullOrEmpty(FileName);
}
[DebuggerDisplay("{" + nameof(CompressionMethod) + "}")]
@@ -31,11 +30,4 @@ public class Meta
{
[I][J] public string Version { get; private set; }
[J] public string CompressionMethod { get; set; }
public Meta()
{
CompressionMethod = "Oodle";
}
[I] public bool IsValid => CompressionMethod == "Oodle";
}

View File

@@ -295,12 +295,12 @@ public class CUE4ParseViewModel : ViewModel
{
// game directory dependent, we don't have the provider game name yet since we don't have aes keys
// except when this comes from the AES Manager
if (!UserSettings.IsEndpointEnabled(Game, EEndpointType.Aes, out var endpoint))
if (!UserSettings.IsEndpointValid(Game, EEndpointType.Aes, out var endpoint))
return;
await _threadWorkerView.Begin(cancellationToken =>
{
var aes = _apiEndpointView.DynamicApi.GetAesKeys(cancellationToken, endpoint.Url);
var aes = _apiEndpointView.DynamicApi.GetAesKeys(cancellationToken, endpoint.Url, endpoint.Path);
if (aes is not { IsValid: true }) return;
UserSettings.Default.AesKeys[Game] = aes;
@@ -323,26 +323,26 @@ public class CUE4ParseViewModel : ViewModel
public async Task InitBenMappings()
{
if (!UserSettings.IsEndpointEnabled(Game, EEndpointType.Mapping, out var endpoint))
if (!UserSettings.IsEndpointValid(Game, EEndpointType.Mapping, out var endpoint))
return;
await _threadWorkerView.Begin(cancellationToken =>
{
if (endpoint.Overwrite && File.Exists(endpoint.Path))
if (endpoint.Overwrite && File.Exists(endpoint.FilePath))
{
Provider.MappingsContainer = new FileUsmapTypeMappingsProvider(endpoint.Path);
Provider.MappingsContainer = new FileUsmapTypeMappingsProvider(endpoint.FilePath);
FLogger.AppendInformation();
FLogger.AppendText($"Mappings pulled from '{endpoint.Path.SubstringAfterLast("\\")}'", Constants.WHITE, true);
FLogger.AppendText($"Mappings pulled from '{endpoint.FilePath.SubstringAfterLast("\\")}'", Constants.WHITE, true);
}
else
{
var mappingsFolder = Path.Combine(UserSettings.Default.OutputDirectory, ".data");
var mappings = _apiEndpointView.DynamicApi.GetMappings(cancellationToken, endpoint.Url);
var mappings = _apiEndpointView.DynamicApi.GetMappings(cancellationToken, endpoint.Url, endpoint.Path);
if (mappings is { Length: > 0 })
{
foreach (var mapping in mappings)
{
if (!mapping.IsValid || !mapping.Meta.IsValid) continue;
if (!mapping.IsValid) continue;
var mappingPath = Path.Combine(mappingsFolder, mapping.FileName);
if (!File.Exists(mappingPath))

View File

@@ -324,12 +324,6 @@ public class SettingsViewModel : ViewModel
UserSettings.Default.OverridedOptions[_game] = SelectedOptions;
}
if (UserSettings.Default.CustomEndpoints.TryGetValue(_game, out var endpoints))
{
endpoints[0] = AesEndpoint;
endpoints[1] = MappingEndpoint;
}
UserSettings.Default.AssetLanguage = SelectedAssetLanguage;
UserSettings.Default.CompressedAudioMode = SelectedCompressedAudio;
UserSettings.Default.CosmeticStyle = SelectedCosmeticStyle;