|
| 1 | +using System.Text.Json; |
| 2 | +using Mono.Cecil; |
| 3 | +using Mono.Cecil.Cil; |
| 4 | +using Mono.Collections.Generic; |
| 5 | +using StardewModdingAPI.ModBuildConfig.Analyzer; |
| 6 | +using StardewModdingAPI.Toolkit; |
| 7 | + |
| 8 | +Console.WriteLine("Hello, World!"); |
| 9 | + |
| 10 | +var stardew = AssemblyDefinition.ReadAssembly("Stardew Valley.dll"); |
| 11 | + |
| 12 | +Dictionary<string, string> AssetMap = new Dictionary<string, string>(); |
| 13 | + |
| 14 | +List<string> messagesForLater = new(); |
| 15 | +foreach (var module in stardew.Modules) |
| 16 | +{ |
| 17 | + var types = module.GetTypes().Where(type => type.BaseType != null); |
| 18 | + |
| 19 | + foreach (var type in types) |
| 20 | + { |
| 21 | + foreach (var method in type.Methods) |
| 22 | + { |
| 23 | + if (method.HasBody) |
| 24 | + { |
| 25 | + |
| 26 | + ILProcessor cil = method.Body.GetILProcessor(); |
| 27 | + Collection<Instruction> instructions = cil.Body.Instructions; |
| 28 | + |
| 29 | + Instruction prevInstruction = Instruction.Create(OpCodes.Nop); |
| 30 | + foreach (var instruction in instructions) |
| 31 | + { |
| 32 | + if (instruction.OpCode.Code == Code.Nop) |
| 33 | + continue; |
| 34 | + if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt || instruction.OpCode == OpCodes.Newobj) |
| 35 | + { |
| 36 | + var methodInstruction = (MethodReference)instruction.Operand; |
| 37 | + if (methodInstruction.Name == "Load") { |
| 38 | + if (methodInstruction.DeclaringType.FullName != "StardewValley.LocalizedContentManager") |
| 39 | + { |
| 40 | + Console.WriteLine("Found a Load that was not StardewValley.LocalizedContentManager " + method.DeclaringType.FullName); |
| 41 | + continue; |
| 42 | + } |
| 43 | + var genericMethod = (GenericInstanceMethod)methodInstruction; |
| 44 | + string genericParameter = genericMethod.GenericArguments[0]?.FullName ?? "<unknown>"; |
| 45 | + string? assetName = null; |
| 46 | + if (prevInstruction.OpCode == OpCodes.Ldstr) |
| 47 | + { |
| 48 | + assetName = (string)prevInstruction.Operand; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + messagesForLater.Add($"{method.FullName} is calling Load {methodInstruction} (prev instruction: {prevInstruction})"); |
| 53 | + } |
| 54 | + if (assetName != null) |
| 55 | + { |
| 56 | + if (AssetMap.TryGetValue(assetName, out string existingType)) |
| 57 | + { |
| 58 | + if (genericParameter != existingType) |
| 59 | + { |
| 60 | + Console.WriteLine($"Found a new use of {assetName}: Previously {existingType} now {genericParameter}"); |
| 61 | + } |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + AssetMap[assetName] = genericParameter; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + prevInstruction = instruction; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +Directory.CreateDirectory("outputs"); |
| 79 | +var currentVersion = new SemanticVersion(stardew.Name.Version); |
| 80 | +HashSet<string> missingAssets = new(); |
| 81 | +foreach (string file in Directory.GetFiles("outputs")) |
| 82 | +{ |
| 83 | + if (file.EndsWith(stardew.Name.Version + ".json")) continue; |
| 84 | + var otherVersion = new SemanticVersion(Path.GetFileNameWithoutExtension(file), true); |
| 85 | + if (otherVersion.IsNewerThan(currentVersion)) continue; |
| 86 | + try |
| 87 | + { |
| 88 | + var versionInfo = JsonSerializer.Deserialize<VersionModel>(File.ReadAllText(file)); |
| 89 | + foreach (var (asset, type) in versionInfo.AssetMap) |
| 90 | + { |
| 91 | + if (!AssetMap.ContainsKey(asset)) |
| 92 | + { |
| 93 | + missingAssets.Add(asset); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + catch { } |
| 98 | +} |
| 99 | +string jsonOutput = JsonSerializer.Serialize(new VersionModel(AssetMap, missingAssets), new JsonSerializerOptions |
| 100 | +{ |
| 101 | + WriteIndented = true, |
| 102 | +}); |
| 103 | +File.WriteAllText($"outputs/{stardew.Name.Version}.json", jsonOutput); |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments