Exemplo n.º 1
0
        public static void Debug(object obj, int level, int line, string caller, string path)
        {
            var msg = $"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} {path.Split('\\').Last()} {caller}:{line} {obj}";

            switch (level)
            {
            case 0:
                log.LogMessage(msg);
                break;

            case 1:
                log.LogWarning(msg);
                break;

            case 2:
                log.LogError(msg);
                break;

            default:
                log.LogFatal(msg);
                break;
            }
        }
        public static void Finish()
        {
            if (mmhLocation == null)
            {
                return;//Nothing to patch.
            }
            using (AssemblyDefinition mmHook = AssemblyDefinition.ReadAssembly(mmhLocation + ".backup"))
            {
                Logger.LogDebug("Stripping types.");

                Func <TypeDefinition, string> FullNameSelector = new Func <TypeDefinition, string>(td => td.FullName);
                var mTypes = mmHook.MainModule.Types;
                List <TypeDefinition> types = mTypes.ToList();
                types = types.OrderBy(FullNameSelector).ToList();

                int index = 0; TypeDefinition currentType;

                var debugTypes = neededTypes.ToArray();

                while (neededTypes.Count > 0 && index < types.Count)
                {
                    currentType = types[index];

                    if (!(currentType.FullName.StartsWith("On") || currentType.FullName.StartsWith("IL")))
                    {
                        Logger.LogDebug("Skip trimming '" + currentType.FullName + "' as it's not a Vanilla type");
                        index++;
                    }


                    if (currentType.FullName != neededTypes[0])
                    {
                        types.RemoveAt(index);
                        if (currentType.IsNested && currentType.BaseType.Name != nameof(MulticastDelegate))
                        {
                            currentType.DeclaringType.NestedTypes.Remove(currentType);
                        }
                        else
                        {
                            mTypes.Remove(currentType);
                        }
                        continue;
                    }
                    else
                    {
                        if (currentType.HasNestedTypes)//expand nested types.
                        {
                            types.InsertRange(index + 1, currentType.NestedTypes.ToList().OrderBy(FullNameSelector));
                        }

                        index++;
                        neededTypes.RemoveAt(0);
                    }
                }

                if (neededTypes.Count > 0)
                {
                    Logger.LogFatal("Couldn't find all needed types!");
                    Logger.LogMessage("Please report this! As a workaround, consider removing LighterPatcher!");
                    Logger.LogMessage("Using old backup mmHook");
                    File.Copy(mmhLocation + ".backup", mmhLocation);
                    mmhLocation += ".failed";
                    File.Delete(mmhLocation);
                    Logger.LogInfo($"Writing failed build to {mmhLocation}");
                    string        tracefile = Path.Combine(Path.GetDirectoryName(mmhLocation), "LighterPatcherTrace.txt");
                    StringBuilder trace     = new StringBuilder();
                    trace.AppendLine("Couldn't find all needed types!");
                    trace.AppendLine($"(First) missing type: {neededTypes[0]}");
                    trace.AppendLine("All needed types:");
                    trace.Append('\t');
                    trace.Append(string.Join("\n\t", debugTypes));
                    Logger.LogInfo($"Writing a trace to {tracefile}");
                    File.Delete(tracefile);
                    File.WriteAllText(tracefile, trace.ToString());
                    trace = null;
                }

                MarkAssembly(mmHook, hash);
                mmHook.Write(mmhLocation);
                debugTypes = null;
            }
        }