Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Add support for beebyte maps
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Apr 13, 2021
1 parent c938770 commit 0726886
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 32 deletions.
105 changes: 105 additions & 0 deletions Reactor.Greenhouse/BeebyteMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Rocks;
using Reactor.OxygenFilter;

namespace Reactor.Greenhouse
{
public class BeebyteMappings
{
public Dictionary<string, string> Names { get; }

public BeebyteMappings(Dictionary<string, string> names)
{
Names = names;
}

public string GetOrDefault(string name)
{
if (name.IsObfuscated() && Names.TryGetValue(name, out var value))
{
return value.Clean();
}

return null;
}

public void Compile(Mappings result, ModuleDefinition module)
{
foreach (var typeDefinition in module.GetAllTypes())
{
var type = new MappedType(typeDefinition.FullName, GetOrDefault(typeDefinition.Name));

foreach (var methodDefinition in typeDefinition.Methods)
{
if (methodDefinition.Name.IsObfuscated() && !Names.ContainsKey(methodDefinition.Name))
continue; // dead method

var method = new MappedMethod(methodDefinition, GetOrDefault(methodDefinition.Name));

foreach (var parameterDefinition in methodDefinition.Parameters)
{
method.Parameters.Add(GetOrDefault(parameterDefinition.Name) ?? parameterDefinition.Name);
}

if (!methodDefinition.Name.IsObfuscated() && !method.Parameters.Any())
continue;

type.Methods.Add(method);
}

foreach (var fieldDefinition in typeDefinition.Fields)
{
var field = new MappedMember(fieldDefinition.Name, GetOrDefault(fieldDefinition.Name) ?? fieldDefinition.Name.Clean());

if (!fieldDefinition.Name.IsObfuscated() && fieldDefinition.Name == field.Mapped)
continue;

type.Fields.Add(field);
}

foreach (var propertyDefinition in typeDefinition.Properties)
{
var field = new MappedMember(propertyDefinition.Name, GetOrDefault(propertyDefinition.Name));

if (!propertyDefinition.Name.IsObfuscated())
continue;

type.Properties.Add(field);
}

if (!typeDefinition.Name.IsObfuscated() && !type.Fields.Any() && !type.Methods.Any() && !type.Properties.Any() && !type.Nested.Any())
continue;

result.Types.Add(type);
}
}

public static BeebyteMappings Parse(string[] text)
{
var names = new Dictionary<string, string>();
foreach (var line in text)
{
// comment
if (line.StartsWith("#"))
{
continue;
}

var values = line.Split("⇨");
if (values.Length != 2)
{
throw new FormatException();
}

var preobfuscated = values[1];

names.Add(values[0], preobfuscated.Contains("/") ? preobfuscated[(preobfuscated.IndexOf("/", StringComparison.Ordinal) + 1)..] : preobfuscated);
}

return new BeebyteMappings(names);
}
}
}
11 changes: 11 additions & 0 deletions Reactor.Greenhouse/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@ public static void ForceExtractToFile(this ZipArchiveEntry source, string destin
Directory.GetParent(destinationFileName)!.Create();
source.ExtractToFile(destinationFileName, true);
}

public static string Clean(this string name)
{
return name switch
{
"<>1__state" => "__state",
"<>2__current" => "__current",
"<>4__this" => "__this",
_ => name.Replace("<", "_").Replace(">", "_")
};
}
}
}
6 changes: 1 addition & 5 deletions Reactor.Greenhouse/Generation/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ private static bool IsAssemblyCSharp(this IMetadataScope scope)
return scope.Name != "Assembly-CSharp.dll";
}

public static Mappings Generate(GenerationContext context)
public static void Generate(Mappings result, GenerationContext context)
{
var result = new Mappings();

var lookupTypes = Extensions.Time(() =>
{
LookupTypes(context);
Expand Down Expand Up @@ -76,8 +74,6 @@ public static Mappings Generate(GenerationContext context)

result.Types.Add(mappedType);
}

return result;
}

private static double TestEnum(TypeDefinition cleanType, TypeDefinition obfuscatedType)
Expand Down
24 changes: 3 additions & 21 deletions Reactor.Greenhouse/Generation/TypeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ public void UpdateNested()
}
}

private static string Clean(string name)
{
return name.Replace("<", "_").Replace(">", "_");
}

public MappedType ToMappedType()
{
var mappedType = new MappedType(ObfuscatedType.FullName, Clean(CleanType.Name));
var mappedType = new MappedType(ObfuscatedType.FullName, CleanType.Name.Clean());

if (!string.IsNullOrEmpty(CleanType.Namespace))
{
Expand All @@ -76,20 +71,7 @@ public MappedType ToMappedType()

if (ObfuscatedType.DeclaringType != null)
{
switch (obfuscatedField.Name)
{
case "<>1__state":
mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__state"));
continue;
case "<>2__current":
mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__current"));
continue;
case "<>4__this":
mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__this"));
continue;
}

var clean = Clean(obfuscatedField.Name);
var clean = obfuscatedField.Name.Clean();

if (clean != obfuscatedField.Name)
{
Expand Down Expand Up @@ -174,7 +156,7 @@ public MappedType ToMappedType()
{
foreach (var obfuscatedMethod in ObfuscatedType.Methods)
{
var clean = Clean(obfuscatedMethod.Name);
var clean = obfuscatedMethod.Name.Clean();

if (clean != obfuscatedMethod.Name)
{
Expand Down
22 changes: 17 additions & 5 deletions Reactor.Greenhouse/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,25 @@ private static async Task GenerateAsync(Game game, ModuleDefinition cleanModule)

using var moduleDef = ModuleDefinition.ReadModule(File.OpenRead(game.Dll));

var generated = Generator.Generate(new GenerationContext(cleanModule, moduleDef));
generated.GameVersion = version.ToString();
generated.Version = Mappings.CurrentVersion;
var mappings = Read(version.ToString());

await File.WriteAllTextAsync(Path.Combine("work", version + ".generated.json"), JsonConvert.SerializeObject(generated, Formatting.Indented));
var generated = new Mappings
{
GameVersion = version.ToString(),
Version = Mappings.CurrentVersion
};

var mappings = Read(version.ToString());
if (mappings.Beebyte != null)
{
BeebyteMappings.Parse(await File.ReadAllLinesAsync(mappings.Beebyte)).Compile(generated, moduleDef);
}

if (mappings.UseGenerator)
{
Generator.Generate(generated, new GenerationContext(cleanModule, moduleDef));
}

await File.WriteAllTextAsync(Path.Combine("work", version + ".generated.json"), JsonConvert.SerializeObject(generated, Formatting.Indented));

generated.Apply(mappings, Read);

Expand Down
3 changes: 2 additions & 1 deletion Reactor.Greenhouse/Setup/Provider/SteamProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ [new GameVersion("2019.10.10s")] = 3162069540887216240,
[new GameVersion("2020.12.9s")] = 3306639722673334636,
[new GameVersion("2021.3.5s")] = 5200448423569257054,
[new GameVersion("2021.3.31s")] = 9177722260012976528,
[new GameVersion("2021.3.31.3s")] = 3941730972865408291
[new GameVersion("2021.3.31.3s")] = 3941730972865408291,
[new GameVersion("2021.4.12s")] = 2725166875026424465,
};

public ulong Manifest { get; }
Expand Down
4 changes: 4 additions & 0 deletions Reactor.OxygenFilter/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class Mappings

public string[] DependsOn { get; set; } = new string[0];

public string Beebyte { get; set; } = null;

public bool UseGenerator { get; set; } = false;

public List<MappedType> Types { get; set; } = new List<MappedType>();

public MappedType Find(string name, Func<MappedType, string> predicate)
Expand Down

0 comments on commit 0726886

Please sign in to comment.