예제 #1
0
        public static void PlaceLine(STSCFile file, List <string> strings, List <int> lines, int address, string s)
        {
            int index = file.FindIndex(address);

            strings.Insert(lines[index], s);
            for (int i = index; i < lines.Count; ++i)
            {
                ++lines[i];
            }
        }
예제 #2
0
        public static void ConvertSingleInstructionToText(STSCFile file, int instructionPointer,
                                                          Dictionary <string, int> labels, List <int> scopeEnds, List <int> lines, List <string> strings, ref int address, ref int currentIndent)
        {
            var instruction = file.Instructions[instructionPointer];

            if (scopeEnds.Count != 0 && scopeEnds.Last() == address)
            {
                --currentIndent;
                strings.Add($"{new string(' ', currentIndent * 4)}}}");
                scopeEnds.RemoveAt(scopeEnds.Count - 1);
            }
            // TODO
            int address2 = address;

            if (labels.Any(t => t.Value == address2))
            {
                strings.Add($"#label {labels.FirstOrDefault(t => t.Value == address2).Key}");
            }
            if (instruction.Name == "if")
            {
                lines.Add(strings.Count);
                var      ifInstruction = instruction as STSCInstructions.InstructionIf;
                string[] comps         = ifInstruction.Comparisons
                                         .Select((comparison, index) => ConvertComparisonToString(comparison)).ToArray();
                strings.Add($"{new string(' ', currentIndent * 4)}if ({string.Join(" && ", comps)})");
                strings.Add($"{new string(' ', currentIndent * 4)}{{");
                ++currentIndent;
                scopeEnds.Add(ifInstruction.GetArgument <int>(0));
            }
            else
            {
                string[] argString = instruction.Arguments.Select((arg, index) =>
                {
                    if (instruction.ArgTypes[index] == STSCInstructions.ArgumentType.AT_CodePointer)
                    {
                        int jumpAddress  = instruction.GetArgument <int>(index);
                        string labelName = $"LABEL_{jumpAddress:X4}";
                        // Change Label name if its been used as a function
                        if (instruction.Name == STSCInstructions.DALRRInstructions[0x1A].Name)
                        {
                            labelName = $"SUB_{jumpAddress:X4}";
                        }

                        if (!labels.ContainsKey(labelName))
                        {
                            labels.Add(labelName, jumpAddress);
                            if (file.FindIndex(jumpAddress) < instructionPointer)
                            {
                                PlaceLine(file, strings, lines, jumpAddress, $"#label {labelName}");
                            }
                        }
                        return(labelName);
                    }
                    return(ConvertArgumentToString(instruction, index));
                }).ToArray();
                // Macros
                if (instruction.Name == STSCInstructions.DALRRInstructions[0x52].Name)
                {
                    argString[0] = STSCMacros.DALRRCharacterNames[int.Parse(argString[0])] ?? argString[0];
                }
                lines.Add(strings.Count);
                strings.Add($"{new string(' ', currentIndent * 4)}{instruction.Name}({string.Join(", ", argString)})");
            }
        }