Skip to content

Commit

Permalink
refactor: split mapping class to reduce complexity per file
Browse files Browse the repository at this point in the history
  • Loading branch information
cengelha committed Nov 22, 2024
1 parent c07cdc0 commit ac0ce94
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 146 deletions.
147 changes: 1 addition & 146 deletions libNOM.map/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace libNOM.map;
/// <summary>
/// Holds all necessary mapping data and provides obfuscation and deobfuscation.
/// </summary>
public static class Mapping
public static partial class Mapping
{
#region Field

Expand Down Expand Up @@ -135,64 +135,8 @@ private static void AddToMap(IEnumerable<KeyValuePair<string, string>> data, boo

#endregion

#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);
}

/// <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> GetMapForDeobfuscation(bool useAccount) => (useAccount ? _mapForCommonAccount.Concat(_mapForDeobfuscationAccount) : _mapForCommon.Concat(_mapForDeobfuscation)).ToDictionary(i => i.Key, i => i.Value);

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="GetMappedKeyOrInput(string, bool)"/>
public static string GetMappedKeyOrInput(string key) => GetMappedKeyOrInput(key, false);

Expand All @@ -213,71 +157,6 @@ public static string GetMappedKeyOrInput(string key, bool useAccount)
return key;
}

/// <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="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="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;
}

/// <summary>
/// Ensures that the update task is complete and both maps are created.
/// </summary>
Expand Down Expand Up @@ -305,30 +184,6 @@ private static void EnsurePreconditions(JToken? node)
_lock.ExitWriteLock();
}

/// <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

#region Update
Expand Down
99 changes: 99 additions & 0 deletions libNOM.map/Mapping_Deobfuscation.cs
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
}
88 changes: 88 additions & 0 deletions libNOM.map/Mapping_Obfuscation.cs
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
}

0 comments on commit ac0ce94

Please sign in to comment.