Exemplo n.º 1
0
        public void Run()
        {
            List <string> result  = new List <string>();
            List <string> defines = new List <string>();
            List <string> code    = new List <string>();


            List <string> globalVariables = new List <string>();
            SortedDictionary <int, string> addressDict = new SortedDictionary <int, string>();

            foreach (string patchFile in patchFiles)
            {
                this.currentFilename = Path.GetFileName(patchFile);
                SSLCode ssl = new SSLCode(protossl);

                var lines = SafeReadAllLines(patchFile);
                if (lines == null)
                {
                    Console.Error.WriteLine($"Skipping {patchFile}");
                }

                try
                {
                    var patch = new Patch(lines, runMode, ssl, memoryArgs, (line) => this.currentLine = line);
                    code.AddRange(patch.Run());
                    this.parseEvents.AddRange(patch.parseEvents);
                }
                catch (Exception ex)
                {
                    Error.Fatal($"Unhandled exception: {ex.Message}\r\n{ex.StackTrace}", ErrorCodes.UnhandledException);
                }

                if (!MultiPatch)
                {
                    break;
                }

                foreach (KeyValuePair <int, int> group in ssl.GetWriteGroups())
                {
                    if (memoryArgs.IsDefined(group.Key))
                    {
                        string memoryArgName = memoryArgs[group.Key];
                        string defineName    = $"{ssl.GetName()}__{memoryArgName}";

                        addressDict[-memoryArgs[memoryArgName]] = defineName; // store as negative for cheap reversed order
                        // addressDictMax = Math.Max(addressDictMax, defineName.Length);

                        memoryArgs[memoryArgName] = group.Value;
                    }
                }

                if (patchFile != patchFiles.Last())
                {
                    code.Add("");
                }
            }

            var allDefines = new Dictionary <string, string>();

            foreach (KeyValuePair <int, string> define in addressDict)
            {
                allDefines.Add(define.Value, $"0x{(-define.Key).ToString("x")}");
            }

            int i = 1;

            foreach (var define in mallocVariables)
            {
                allDefines.Add("VOODOO_ID_" + define, (i++).ToString());
            }

            var defineLength = 0;

            if (allDefines.Any())
            {
                defineLength = allDefines?.Max(x => x.Key.Length) ?? 0;
                foreach (var define in allDefines)
                {
                    defines.Add($"#define {define.Key.PadRight(defineLength)}  {define.Value}");
                }
            }

            var variables = new List <string>();

            foreach (var var in globalVariables)
            {
                variables.Add($"variable ${var};");
            }

            if (updateFile.IsSet)
            {
                updateFile.SetTag(nameof(variables), variables);
                updateFile.SetTag(nameof(defines), defines);
                updateFile.SetTag(nameof(code), code);
            }
            else
            {
                if (defines.Count > 0)
                {
                    defines.Add("");
                }

                result.AddRange(defines);
                result.AddRange(variables);
                result.AddRange(code);
            }

            if (updateFile.IsSet)
            {
                File.WriteAllLines(updateFile.Name, updateFile.Lines);
            }
            else
            {
                result.ForEach(line => Console.WriteLine(line));
            }
        }