public ConvertSMPS(string filebin, string fileasm, string lable) { context = this; // set files filein = filebin; fileout = fileasm; // check input file exists if (!File.Exists(filein)) { error("Input file '" + filein + "' does not exist!"); } // set base lable baselable = lable; }
public static void DoIt(ConvertSMPS cvt) { if (debug) { Debug("--; Prepare output to " + cvt.fileout); } // if file exists already if (File.Exists(cvt.fileout)) { File.Delete(cvt.fileout); } // create new writer StreamWriter writer = new StreamWriter(cvt.fileout); // get ordered list of lables and lines. OffsetString[] la = cvt.Lables.OrderBy(o => o.offset).ToArray(); OffsetString[] li = cvt.Lines.OrderBy(o => o.offset).ToArray(); int lai = 0, lii = 0; // next byte to check for unused uint check = 0; // Debug(li, la, cvt); // used for checking if line already used OffsetString last = null; // used for nicely formatting dc.b's string line = "\t" + DataMacro + " "; int bytes = 0; bool unused = false; // if last was unused bool lastlable = true; // if last line was a lable. Essentially just omits extra newline if (debug) { Debug(new string('-', 80)); } for (uint i = cvt.offset; i <= cvt.offset + cvt.data.Length; i++) { // write lables for this byte while (lai < la.Length && la[lai].offset <= i) { if (la[lai].offset < i) { lai++; } else { // if already unused bytes on the line, save them if (bytes > 0) { lastlable = false; writer.WriteLine(line.Substring(0, line.Length - 2) + (unused ? "\t; Unused" : "")); if (debug) { Debug(i, line.Substring(0, line.Length - 2) + (unused ? "\t; Unused" : "")); } line = "\t" + DataMacro + " "; bytes = 0; } if (debug) { Debug(i, la[lai].line + ":"); } writer.WriteLine((lastlable ? "" : "\n") + la[lai].line + ":"); lai++; lastlable = true; } } if (check <= i) { last = null; } bool found = false; while (lii < li.Length && li[lii].offset <= i) { if (li[lii].offset < i) { lii++; } else { lastlable = false; found = true; if (li[lii].line.ElementAt(0) == '\b') { // if 'db xx', it is a byte value if (last != null && last.length > 0 && li[lii].length > 0 && i <= check) { if (last.line != li[lii].line) { if (debug) { Debug("--% " + toHexString((double)last.offset, 4) + " '" + last.line.Replace("\b", "db") + "' <> '" + li[lii].line.Replace("\b", "db") + "'"); } Console.WriteLine("WARNING! Line '" + last.line.Replace("\b", "db") + "' conflicts with line '" + li[lii].line.Replace("\b", "db") + "' at " + toHexString((double)last.offset, 4) + "!"); goto nowrite; } else { goto nowrite; } } // if already unused bytes on the line, save them if (unused) { if (bytes > 0) { writer.WriteLine(line.Substring(0, line.Length - 2) + "\t; Unused"); } if (debug) { Debug(i, line.Substring(0, line.Length - 2) + "\t; Unused"); } line = "\t" + DataMacro + " "; bytes = 0; } // add actual data in last = li[lii]; line += last.line.Substring(2) + ", "; if (debug) { Debug("--& " + last.line.Substring(2)); } bytes++; unused = false; // if enough data, save it if (bytes >= 8) { writer.WriteLine(line.Substring(0, line.Length - 2)); if (debug) { Debug(i, line.Substring(0, line.Length - 2)); } line = "\t" + DataMacro + " "; bytes = 0; } nowrite :; } else { // else direct string if (last != null && last.length > 0 && li[lii].length > 0 && i <= check) { if (last.line != li[lii].line) { // writer.WriteLine(li[lii].line); if (debug) { Debug("--% " + toHexString((double)last.offset, 4) + " '" + last.line.Replace("\b", "db") + "' <> '" + li[lii].line.Replace("\b", "db") + "'"); } Console.WriteLine("WARNING! Line '" + last.line.Replace("\b", "db") + "' conflicts with line '" + li[lii].line.Replace("\b", "db") + "' at " + toHexString((double)last.offset, 4) + "!"); } } else { // if already unused bytes on the line, save them if (bytes > 0) { writer.WriteLine(line.Substring(0, line.Length - 2) + (unused ? "\t; Unused" : "")); if (debug) { Debug(i, line.Substring(0, line.Length - 2) + (unused ? "\t; Unused" : "")); } line = "\t" + DataMacro + " "; bytes = 0; } // write it if (li[lii].length > 0) { last = li[lii]; } writer.WriteLine(li[lii].line); if (debug) { Debug(i, li[lii].line); } } } lii++; } } // check if we need to do unused bytes if (found && last != null) { check = last.length + (uint)last.offset; } else if (!cvt.skipped[i - cvt.offset]) { lastlable = false; // if already used bytes on the line, save them if (!unused) { if (bytes > 0) { writer.WriteLine(line.Substring(0, line.Length - 2)); } if (debug) { Debug(i, line.Substring(0, line.Length - 2)); } line = "\t" + DataMacro + " "; bytes = 0; } // add actual data in line += toHexString(cvt.Read(i - cvt.offset), 2) + ", "; if (debug) { Debug("--= " + toHexString(cvt.Read(i - cvt.offset), 2)); } bytes++; unused = true; // if enough data, save it if (bytes >= 8) { writer.WriteLine(line.Substring(0, line.Length - 2) + "\t; Unused"); if (debug) { Debug(i, line.Substring(0, line.Length - 2) + "\t; Unused"); } line = "\t" + DataMacro + " "; bytes = 0; } } } // check if any unused bytes weren't written if (bytes > 0) { writer.WriteLine(line.Substring(0, line.Length - 2) + "\t; Unused"); if (debug) { Debug((uint)(cvt.offset + cvt.data.Length), line.Substring(0, line.Length - 2) + "\t; Unused"); } bytes = 0; } writer.Flush(); }
static void Main(string[] args) { Console.Title = "SMPS2ASM/NAT Built: " + new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime.ToShortDateString() + " " + new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime.ToShortTimeString(); // args[input file with ext, Sound driver name, label, extra: may be used by script] // get the exe folder string[] a = args; //check if we have a debug option opcheck: if (args.Length > 0 && args[0] == "-d") { args = args.Skip(1).ToArray(); debug = true; goto opcheck; } //check if we have a pause option if (args.Length > 0 && args[0] == "-p") { args = args.Skip(1).ToArray(); pause = true; goto opcheck; } //check if we have a type option if (args.Length > 1 && args[0] == "-t") { type = args[1]; args = args.Skip(2).ToArray(); goto opcheck; } //check if a script file was dragged in if (args.Length > 0) { if (File.Exists(args[0]) && args[0].EndsWith(".smpss")) { folder = Environment.CurrentDirectory; string script = args[0]; args = args.Skip(1).ToArray(); // check if all arguments are gotten if (args.Length < 2) { pause = true; args = ConsoleArguments.Get(args, new ArgHandler[] { new ArgHandler("Music file name with extension:", chkfilext2), new ArgHandler("Project name:", chkname), }, new ButtonHandler[] { new ButtonHandler(ConsoleKey.Escape, "Quit the program", quitprg, quitcl), new ButtonHandler(ConsoleKey.F1, "Pause program at the end", pauseprg, pausecl), new ButtonHandler(ConsoleKey.F2, "Print debug info", debugprg, debugcl), }); } else { args[0] = chkfilext2(args[0], true); args[1] = chkname(args[1], true); } string[] ax = new string[1 + args.Length]; ax[0] = args[0]; ax[2] = args[1]; ax[1] = script; for (int i = 3; i < ax.Length; i++) { ax[i] = args[i - 1]; } args = ax; goto oops; } } // check if all arguments are gotten if (args.Length < 3) { folder = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), @"")); pause = true; args = ConsoleArguments.Get(args, new ArgHandler[] { new ArgHandler("Music file name with extension:", chkfilext), new ArgHandler("Sound driver folder name:", chkfolext), new ArgHandler("Project name:", chkname), }, new ButtonHandler[] { new ButtonHandler(ConsoleKey.Escape, "Quit the program", quitprg, quitcl), new ButtonHandler(ConsoleKey.F1, "Pause program at the end", pauseprg, pausecl), new ButtonHandler(ConsoleKey.F2, "Print debug info", debugprg, debugcl), }); } else { folder = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.CurrentDirectory), @"")); args[0] = chkfilext(args[0], true); args[1] = chkfolext(args[1], true); args[2] = chkname(args[2], true); } // time how long this will take oops: timer = new Stopwatch(); timer.Start(); // remove bin folder from path // if (folder.EndsWith("\\bin") || folder.EndsWith("\\bin\\")) folder = folder.Substring(0, folder.LastIndexOf("\\")); // removes the extension of input file and adds .asm as the extension of output file string fileout; if (args[0].IndexOf(".", args[0].LastIndexOf("\\")) > 0) { fileout = args[0].Substring(0, args[0].LastIndexOf(".")) + ".asm"; } else { fileout = args[0] + ".asm"; } // init debugwriter and put in debug info if (debug) { string db; if (args[0].IndexOf(".", args[0].LastIndexOf("\\")) > 0) { db = args[0].Substring(0, args[0].LastIndexOf(".")) + ".smpsd"; } else { db = args[0] + ".smpsd"; } //init stream dbWr = new StreamWriter(db); // write info about args Debug("--; args=[" + string.Join(", ", a) + "]"); Debug("--; filein=" + args[0]); Debug("--; fileout=" + fileout); Debug("--; folder=" + folder); Debug("--; script=" + args[1]); Debug("--; lable=" + args[2]); Debug("--; type=" + type); } // get new SMPS object ConvertSMPS cvt = new ConvertSMPS(args[0], fileout, args[2]); // get the file for smps2asm script S2AScript scr = new S2AScript(args[1], args.Skip(3).ToArray(), type); // print timer info long tra = timer.ElapsedMilliseconds; Console.WriteLine("Script translated! Took " + tra + " ms!"); // restart timer timer.Reset(); timer.Start(); // do teh conversion cvt.Convert(scr); // print timer info long con = timer.ElapsedMilliseconds; Console.WriteLine("File converted! Took " + con + " ms!"); // restart timer timer.Reset(); timer.Start(); // write teh file Output.DoIt(cvt); // print timer info long pot = timer.ElapsedMilliseconds; Console.WriteLine("File saved to disk! Took " + pot + " ms!"); Console.WriteLine("Conversion done! Took " + (pot + tra + con) + " ms!"); if (debug) { Debug(new string('-', 80)); Debug("--; Time for Script " + tra + " ms"); Debug("--; Time for Convert " + con + " ms"); Debug("--; Time for Save " + pot + " ms"); Debug("--; Time for Total " + (pot + tra + con) + " ms"); dbWr.Flush(); } if (pause) { Console.ReadKey(); } }