Example #1
0
        private OffsetString AddLable(uint pos, string valid)
        {
            OffsetString off = new OffsetString(pos, 0, valid);

            Lables.Add(off);
            return(off);
        }
Example #2
0
        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();
        }
Example #3
0
        // generate valid lables from a specific rule and position
        private bool ObtainValidLable(string lable, string lastlable, uint position, out string valid, out OffsetString lab)
        {
            valid = null;
            lab   = null;
            if (lable == "")
            {
                return(false);
            }

            // check if any lable exists with same address
            foreach (OffsetString o in Lables)
            {
                if (o.offset == position)
                {
                    valid = o.line;
                    return(true);
                }
            }

            // get num of lables that match rule, and return if no wildcard
            int num = GetLablesRule(lable, lastlable).Count;

            if (num != 0 && !lable.Contains("?"))
            {
                return(false);
            }

            // build lable, add it to pool, and return
            valid = lable.Replace("£", baselable).Replace("%", lastlable).Replace("?", LableRule.GetNextRandom(num));
            lab   = AddLable(position, valid);
            return(true);
        }
Example #4
0
        private void Convert(OffsetString o, GenericScriptItem[] LUT, List <GenericScriptItem>[] run, bool str, bool single, out string text)
        {
            // empty list to be ref'd later (idk =/ )
            string[] args = null;
            text = null;

            // check and set pointer for this lable
            if (o.offset == null)
            {
                error("Pointer to the lable is null! This seems like a programming error. Report to developers.");
            }

            if (!IsValidLocation(o))
            {
                return;
            }

            pos = (uint)(o.offset - offset);
            if (o.line != null)
            {
                Console.WriteLine("Parsing " + o.line + " at " + toHexString((double)o.offset, 4) + "...");
            }
            if (debug)
            {
                Debug("--: " + o.line + " " + toHexString((double)o.offset, 4) + " LUT=" + (LUT != null) + " run=" + (run != null));
            }

            // if no LUT, only run sequentially
            if (LUT == null)
            {
                followlables = false;
                foreach (List <GenericScriptItem> en in run)
                {
                    ConvertRun(en, ref args, o.line, out bool stop, out string c);
                    if (stop)
                    {
                        return;
                    }
                }
                return;
            }

            // else do LUT thing
            if (!str)
            {
                followlables = true;
                StoredLUT    = LUT;
                StoredRun    = run;
            }

            do
            {
                if (ProcessItem(LUT, str, o.line, out bool stop, out text))
                {
                    if (stop)
                    {
                        break;
                    }
                }
                else if (run != null && run.Length > 0)
                {
                    foreach (List <GenericScriptItem> en in run)
                    {
                        ConvertRun(en, ref args, o.line, out bool stop2, out string c);
                        if (stop2)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    if (str)
                    {
                        text = toHexString(Read(pos), 2);
                    }
                    else
                    {
                        AddLine(pos, 1, Read(pos));
                    }
                    SkipByte(pos++);                        // skip over current byte

                    if (stop)
                    {
                        break;
                    }
                }
            } while (!single);