0
0
mirror of https://github.com/sp-tarkov/assembly-tool.git synced 2025-02-12 20:50:44 -05:00

WIP: Remap classes based on parents in item.json

This commit is contained in:
Cj 2024-11-05 20:19:35 -05:00
parent 53c9685580
commit 663148d3ed
4 changed files with 524637 additions and 4 deletions

524563
Assets/Templates/items.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
namespace ReCodeIt.Models;
public class ItemTemplateModel
{
public string _id;
public string _name;
public string _parent;
public string _type;
}

View File

@ -6,6 +6,7 @@ using ReCodeIt.ReMapper.Search;
using ReCodeIt.Utils;
using ReCodeItLib.Remapper.Search;
using System.Diagnostics;
using System.Reflection;
namespace ReCodeIt.ReMapper;
@ -61,6 +62,8 @@ public class ReCodeItRemapper
Logger.Log("You must de-obfuscate the assembly before remapping it.\n", ConsoleColor.Red);
return;
}
HandleTypeTableRemaps(assemblyPath, types);
var tasks = new List<Task>(remapModels.Count);
foreach (var remap in remapModels)
@ -95,7 +98,7 @@ public class ReCodeItRemapper
);
}
Task.WaitAll(renameTasks.ToArray());
// Don't publicize and unseal until after the remapping, so we can use those as search parameters
if (Settings.MappingSettings.Publicize)
{
@ -103,7 +106,7 @@ public class ReCodeItRemapper
SPTPublicizer.PublicizeClasses(Module);
}
// We are done, write the assembly
WriteAssembly();
}
@ -427,6 +430,50 @@ public class ReCodeItRemapper
}
}
private void HandleTypeTableRemaps(string path, IEnumerable<TypeDef> types)
{
// HACK: Because this is written in net8 and the assembly is net472 we must resolve the type this way instead of
// filtering types directly using GetTypes() Otherwise, it causes serialization issues.
// This is also necessary because we can't access non-compile time constants with dnlib.
var templateMappingTypeDef = types.Single(t => t.FindField("TypeTable") != null);
var assembly = Assembly.LoadFrom(path);
var templateMappingClass = assembly.Modules
.First()
.GetType(templateMappingTypeDef.Name);
if (templateMappingClass is null)
{
Logger.Log($"Could not find {templateMappingTypeDef.Name} in the assembly.", ConsoleColor.Red);
return;
}
var typeTable = (Dictionary<string, Type>)templateMappingClass
.GetField("TypeTable")
.GetValue(templateMappingClass);
foreach (var type in typeTable)
{
if (DataProvider.ItemTemplates!.TryGetValue(type.Key, out var template))
{
if (!type.Value.Name.StartsWith("GClass")) continue;
Logger.Log($"Key: {type.Key} Type: {type.Value.Name} Associated to {template._name}", ConsoleColor.Green);
var remap = new RemapModel
{
OriginalTypeName = type.Value.Name,
NewTypeName = $"{template._name}",
UseForceRename = true
};
_remaps.Add(remap);
continue;
}
Logger.Log($"Found no association for key: {type.Key} Type: {type.Value}", ConsoleColor.Yellow);
}
}
/// <summary>
/// Choose the best possible match from all remaps
/// </summary>

View File

@ -7,6 +7,11 @@ namespace ReCodeIt.Utils;
public static class DataProvider
{
static DataProvider()
{
LoadItems();
}
/// <summary>
/// Is this running in the CLI?
/// </summary>
@ -15,7 +20,8 @@ public static class DataProvider
public static string DataPath => Path.Combine(AppContext.BaseDirectory, "Data");
public static List<RemapModel> Remaps { get; set; } = [];
public static Dictionary<string, ItemTemplateModel>? ItemTemplates { get; private set; }
public static Settings Settings { get; private set; }
public static void LoadAppSettings()
@ -116,7 +122,7 @@ public static class DataProvider
public static ModuleDefMD LoadModule(string path)
{
var mcOptions = new ModuleCreationOptions(ModuleDef.CreateModuleContext());
ModuleDefMD module = ModuleDefMD.Load(path, mcOptions);
var module = ModuleDefMD.Load(path, mcOptions);
module.Context = mcOptions.Context;
@ -127,4 +133,12 @@ public static class DataProvider
return module;
}
private static void LoadItems()
{
var itemsPath = Path.Combine(DataPath, "items.json");
var jsonText = File.ReadAllText(itemsPath);
ItemTemplates = JsonConvert.DeserializeObject<Dictionary<string, ItemTemplateModel>>(jsonText);
}
}