-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split mapping class to reduce complexity per file
- Loading branch information
Showing
3 changed files
with
188 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using libNOM.map.Extensions; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
namespace libNOM.map; | ||
|
||
|
||
/// <summary> | ||
/// Holds all necessary mapping data and provides obfuscation and deobfuscation. | ||
/// </summary> | ||
public static partial class Mapping | ||
{ | ||
// // | ||
|
||
#region Getter | ||
|
||
/// <summary> | ||
/// Iterates over all JSON properties to collect a list for deobfuscation. | ||
/// </summary> | ||
/// <param name="token">Current property that should be deobfuscated.</param> | ||
/// <param name="jProperties">List of properties that need to be deobfuscated.</param> | ||
/// <param name="unknownKeys">List of keys that cannot be deobfuscated.</param> | ||
private static void GetPropertiesToDeobfuscate(JToken token, List<JProperty> jProperties, Dictionary<string, string> mapForDeobfuscation, HashSet<string> unknownKeys) | ||
{ | ||
if (token.Type == JTokenType.Property) | ||
{ | ||
var property = (JProperty)(token); | ||
|
||
if (mapForDeobfuscation.ContainsKey(property.Name)) | ||
{ | ||
jProperties.Add(property); | ||
} | ||
// Only add if it is not a target value as well. | ||
else if (!mapForDeobfuscation.ContainsValue(property.Name)) | ||
{ | ||
unknownKeys.Add(property.Name); | ||
} | ||
} | ||
|
||
foreach (var child in token.Children().Where(i => i.HasValues)) | ||
GetPropertiesToDeobfuscate(child, jProperties, mapForDeobfuscation, unknownKeys); | ||
} | ||
|
||
#endregion | ||
|
||
#region Mapping | ||
|
||
private static Dictionary<string, string> GetMapForDeobfuscation(bool useAccount) => (useAccount ? _mapForCommonAccount.Concat(_mapForDeobfuscationAccount) : _mapForCommon.Concat(_mapForDeobfuscation)).ToDictionary(i => i.Key, i => i.Value); | ||
|
||
/// <inheritdoc cref="GetMappedKeyForDeobfuscationOrInput(string, bool)"/> | ||
public static string GetMappedKeyForDeobfuscationOrInput(string key) => GetMappedKeyForDeobfuscationOrInput(key, false); | ||
|
||
/// <summary> | ||
/// Maps the specified key. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="useAccount"></param> | ||
/// <returns>The deobfuscated key or the input if no mapping found.</returns> | ||
public static string GetMappedKeyForDeobfuscationOrInput(string key, bool useAccount) | ||
{ | ||
if (GetMapForDeobfuscation(useAccount).TryGetValue(key, out var resultFromDeobfuscation)) | ||
return resultFromDeobfuscation; | ||
|
||
return key; | ||
} | ||
|
||
// // | ||
|
||
/// <inheritdoc cref="Deobfuscate(JToken, bool)"/> | ||
public static HashSet<string> Deobfuscate(JToken node) => Deobfuscate(node, false); | ||
|
||
/// <summary> | ||
/// Deobfuscates JSON to make it human-readable. | ||
/// </summary> | ||
/// <param name="node">A node within a JSON object or the root itself.</param> | ||
/// <param name="useAccount"></param> | ||
/// <returns>List of unknown keys.</returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public static HashSet<string> Deobfuscate(JToken node, bool useAccount) | ||
{ | ||
EnsurePreconditions(node); | ||
|
||
var jProperties = new List<JProperty>(); | ||
var mapForDeobfuscation = GetMapForDeobfuscation(useAccount); | ||
var unknownKeys = new HashSet<string>(); | ||
|
||
// Collect all jProperties that need to be renamed. | ||
foreach (var child in node.Children().Where(i => i.HasValues)) | ||
GetPropertiesToDeobfuscate(child, jProperties, mapForDeobfuscation, unknownKeys); | ||
|
||
// Actually rename each jProperty. | ||
foreach (var jProperty in jProperties) | ||
jProperty.Rename(mapForDeobfuscation[jProperty.Name]); | ||
|
||
return unknownKeys; | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using libNOM.map.Extensions; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
namespace libNOM.map; | ||
|
||
|
||
/// <summary> | ||
/// Holds all necessary mapping data and provides obfuscation and deobfuscation. | ||
/// </summary> | ||
public static partial class Mapping | ||
{ | ||
// // | ||
|
||
#region Getter | ||
|
||
/// <summary> | ||
/// Iterates over all JSON properties to collect a list for obfuscation. | ||
/// </summary> | ||
/// <param name="token">Current property that should be obfuscated.</param> | ||
/// <param name="jProperties">List of properties that need to be obfuscated.</param> | ||
private static void GetPropertiesToObfuscate(JToken token, List<JProperty> jProperties, Dictionary<string, string> mapForObfuscation) | ||
{ | ||
if (token.Type == JTokenType.Property) | ||
{ | ||
var property = (JProperty)(token); | ||
|
||
if (mapForObfuscation.ContainsKey(property.Name)) | ||
{ | ||
jProperties.Add(property); | ||
} | ||
} | ||
|
||
foreach (var child in token.Children().Where(i => i.HasValues)) | ||
GetPropertiesToObfuscate(child, jProperties, mapForObfuscation); | ||
} | ||
|
||
#endregion | ||
|
||
#region Mapping | ||
|
||
private static Dictionary<string, string> GetMapForObfuscation(bool useAccount) => (useAccount ? _mapForCommonAccount.Concat(_mapForObfuscationAccount) : _mapForCommon.Concat(_mapForObfuscation)).ToDictionary(i => i.Value, i => i.Key); // switch to have the origin as Key | ||
|
||
/// <inheritdoc cref="GetMappedKeyForObfuscationOrInput(string, bool)"/> | ||
public static string GetMappedKeyForObfuscationOrInput(string key) => GetMappedKeyForObfuscationOrInput(key, false); | ||
|
||
/// <summary> | ||
/// Maps the specified key. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="useAccount"></param> | ||
/// <returns>The obfuscated key or the input if no mapping found.</returns> | ||
public static string GetMappedKeyForObfuscationOrInput(string key, bool useAccount) | ||
{ | ||
if (GetMapForObfuscation(useAccount).TryGetValue(key, out var resultFromObfuscation)) | ||
return resultFromObfuscation; | ||
|
||
return key; | ||
} | ||
|
||
// // | ||
|
||
/// <inheritdoc cref="Obfuscate(JToken, bool)"/> | ||
public static void Obfuscate(JToken node) => Obfuscate(node, false); | ||
|
||
/// <summary> | ||
/// Obfuscates JSON to make it readable by the game. | ||
/// </summary> | ||
/// <param name="node">A node within a JSON object or the root itself.</param> | ||
/// <param name="useAccount"></param> | ||
public static void Obfuscate(JToken node, bool useAccount) | ||
{ | ||
EnsurePreconditions(node); | ||
|
||
var jProperties = new List<JProperty>(); | ||
var mapForObfuscation = GetMapForObfuscation(useAccount); | ||
|
||
// Collect all jProperties that need to be renamed. | ||
foreach (var child in node.Children().Where(i => i.HasValues)) | ||
GetPropertiesToObfuscate(child, jProperties, mapForObfuscation); | ||
|
||
// Actually rename each jProperty. | ||
foreach (var jProperty in jProperties) | ||
jProperty.Rename(mapForObfuscation[jProperty.Name]); | ||
} | ||
|
||
#endregion | ||
} |