private bool GenerateScripts(IndentedTextWriter output, bool showInfo) { if (_scripts.Scripts is null || _scripts.Scripts.Count == 0) { return(false); } WriteComment("SCRIPTS", output); output.WriteLine(); for (int counter = 0; counter < _scripts.Scripts.Count; counter++) { var script = _scripts.Scripts[counter]; // filter out branch scripts which were generated by the compiler var split = script.Name.Split(new string[] { "_to_" }, StringSplitOptions.RemoveEmptyEntries); if (split.Length > 1 && _scripts.Scripts.Exists(s => s.Name == split[0])) { continue; } if (showInfo) { WriteComment(string.Format("Index: {0}, Expression Index: {1}", counter.ToString(), script.RootExpressionIndex.Index.ToString()), output); } output.Write("(script {0} {1} {2}", _opcodes.GetScriptTypeName((ushort)script.ExecutionType), _opcodes.GetTypeInfo((ushort)script.ReturnType).Name, script.Name); if (script.Parameters.Count > 0) { output.Write(" ("); bool firstParam = true; foreach (ScriptParameter param in script.Parameters) { if (!firstParam) { output.Write(", "); } output.Write("{1} {0}", param.Name, _opcodes.GetTypeInfo(param.Type).Name); firstParam = false; } output.Write(")"); } output.Indent++; output.WriteLine(); WriteExpression(script.RootExpressionIndex, output, _buildInfo.HeaderSize == 0x1E000); output.Indent--; output.WriteLine(); output.WriteLine(")"); output.WriteLine(); output.Indent = 0; counter++; } return(true); }
/// <summary> /// Writes scripts to the output. /// </summary> /// <param name="output">The outout.</param> /// <param name="showInfo">If set to true, writes additional information to the decompiled text.</param> /// <returns>Returns true if scripts were written to the output.</returns> private bool WriteScripts(IndentedTextWriter output, bool showInfo) { // return if the map doesn't contain scripts. if (_scripts.Scripts is null || _scripts.Scripts.Count == 0) { return(false); } WriteComment(output, "SCRIPTS"); output.WriteLine(); for (int i = 0; i < _scripts.Scripts.Count; i++) { Script scr = _scripts.Scripts[i]; // Filter out branch scripts which were generated by the compiler. var split = scr.Name.Split(new string[] { "_to_" }, StringSplitOptions.RemoveEmptyEntries); if (split.Length == 2) { if (_scripts.Scripts.Exists(s => s.Name == split[0]) && _scripts.Scripts.Exists(s => s.Name == split[split.Length - 1])) { continue; } } else if (split.Length == 3) { string firstTwo = string.Join("_to_", split, 0, 2); string lastTwo = string.Join("_to_", split, 1, 2); if ((_scripts.Scripts.Exists(s => s.Name == firstTwo) && _scripts.Scripts.Exists(s => s.Name == split[2])) || (_scripts.Scripts.Exists(s => s.Name == lastTwo) && _scripts.Scripts.Exists(s => s.Name == split[0]))) { continue; } } else if (split.Length == 4) { string firstHalf = string.Join("_to_", split, 0, 2); string secondHalf = string.Join("_to_", split, 2, 2); if (_scripts.Scripts.Exists(s => s.Name == firstHalf) && _scripts.Scripts.Exists(s => s.Name == secondHalf)) { continue; } } // Write additional information. if (showInfo) { WriteComment(output, $"Index: {i}, Expression Index: {scr.RootExpressionIndex.Index.ToString()}"); } // Write script declaration. output.Write("(script {0} {1} {2}", _opcodes.GetScriptTypeName((ushort)scr.ExecutionType), _opcodes.GetTypeInfo((ushort)scr.ReturnType).Name, scr.Name); // Write script parameter declarations. if (scr.Parameters?.Count > 0) { output.Write(" ("); bool firstParam = true; foreach (ScriptParameter param in scr.Parameters) { if (!firstParam) { output.Write(", "); } output.Write("{1} {0}", param.Name, _opcodes.GetTypeInfo((ushort)param.Type).Name); firstParam = false; } output.WriteLine(")"); } else { output.WriteLine(); } output.Indent++; // Write code. FollowRootIndex(output, scr.RootExpressionIndex, true, BranchType.InitialBegin); // Close parenthesis output.Indent = 0; output.WriteLine(")"); output.WriteLine(); } return(true); }