Exemplo n.º 1
0
        /// <summary>
        /// Create a sequence from text.
        /// </summary>
        /// <param name="text">The text.</param>
        public void FromText(List <string> text)
        {
            //Success by default.
            WritingCommandSuccess = true;

            //Reset labels.
            PublicLabels = new Dictionary <string, int>();
            OtherLabels  = new List <int>();
            Dictionary <string, int> privateLabels = new Dictionary <string, int>();
            List <int> labelLines = new List <int>();

            //Format text.
            List <string> t      = text.ToList();
            int           comNum = 0;

            for (int i = t.Count - 1; i >= 0; i--)
            {
                t[i] = t[i].Replace("\t", " ").Replace("\r", "").Replace("  ", " ").Replace("  ", " ").Replace("  ", " ").Replace("  ", " ").Replace("  ", " ");
                try { t[i] = t[i].Split(';')[0]; } catch { }
                if (t[i].Replace(" ", "").Length == 0)
                {
                    t.RemoveAt(i); continue;
                }
                for (int j = 0; j < t[i].Length; j++)
                {
                    if (t[i][j].Equals(' '))
                    {
                        t[i] = t[i].Substring(j + 1);
                        j--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //Fetch labels.
            for (int i = 0; i < t.Count; i++)
            {
                //If it's a label.
                if (t[i].EndsWith(":"))
                {
                    labelLines.Add(i);
                    if (t[i].StartsWith("_"))
                    {
                        privateLabels.Add(t[i].Replace(":", ""), comNum);
                        OtherLabels.Add(comNum);
                    }
                    else
                    {
                        PublicLabels.Add(t[i].Replace(":", ""), comNum);
                    }
                }
                else
                {
                    comNum++;
                }
            }

            //Sort labels.
            PublicLabels = PublicLabels.OrderBy(obj => new NullTerminatedString(obj.Key)).ToDictionary(obj => obj.Key, obj => obj.Value);

            //Get commands.
            Commands = new List <SequenceCommand>();
            for (int i = 0; i < t.Count; i++)
            {
                if (labelLines.Contains(i))
                {
                    continue;
                }
                SequenceCommand seq = new SequenceCommand();
                try { seq.FromString(t[i], PublicLabels, privateLabels); } catch (Exception e) { WritingCommandSuccess = false; throw new Exception("Command " + i + ": \"" + t[i] + "\" is invalid.", e); }
                Commands.Add(seq);
            }

            //Set reference commands.
            for (int i = 0; i < Commands.Count; i++)
            {
                SequenceCommands trueType = Playback.Player.GetTrueCommandType(Commands[i]);
                switch (trueType)
                {
                case SequenceCommands.Call:
                case SequenceCommands.Jump:
                case SequenceCommands.OpenTrack:
                    SetReferenceCommand(Commands[i]);
                    break;
                }
            }

            //Fin.
            Commands.Add(new SequenceCommand()
            {
                CommandType = SequenceCommands.Fin
            });
            WriteCommandData();
        }
Exemplo n.º 2
0
        /// <summary>
        /// From text.
        /// </summary>
        /// <param name="text">The text to parse.</param>
        /// <param name="a">Sound archive.</param>
        public void FromText(List <string> text, SoundArchive a)
        {
            //Success by default.
            WritingCommandSuccess = true;

            //Get platform.
            var p = Platform();

            //Reset labels.
            PublicLabels = new Dictionary <string, int>();
            OtherLabels  = new List <int>();
            Dictionary <string, int> privateLabels = new Dictionary <string, int>();
            List <int> labelLines = new List <int>();

            //Format text.
            List <string> t      = text.ToList();
            int           comNum = 0;

            for (int i = t.Count - 1; i >= 0; i--)
            {
                t[i] = t[i].Replace("\t", "").Replace("\r", "");
                try { t[i] = t[i].Split(';')[0]; } catch { }
                if (t[i].Replace(" ", "").Length == 0)
                {
                    t.RemoveAt(i); continue;
                }
                for (int j = 0; j < t[i].Length; j++)
                {
                    if (t[i][j].Equals(' '))
                    {
                        t[i] = t[i].Substring(j + 1);
                        j--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //Sequence id to label name.
            Dictionary <int, string> seqId2Label = new Dictionary <int, string>();

            //Get sequences.
            Sequences = new List <SequenceArchiveSequence>();
            int currSeqId = 0;

            for (int i = t.IndexOf("@SEQ_TABLE") + 1; i < t.IndexOf("@SEQ_DATA"); i++)
            {
                //New sequence.
                SequenceArchiveSequence s = new SequenceArchiveSequence();

                //Get sequence data.
                string[] seqData = t[i].Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "").Split(',');

                //Get sequence.
                string label       = seqData[0].Split(':')[1];
                string seqNameData = seqData[0].Split(':')[0];
                if (seqNameData.Contains("="))
                {
                    currSeqId = int.Parse(seqNameData.Split('=')[1]);
                    s.Name    = seqNameData.Split('=')[0];
                }
                else
                {
                    s.Name = seqNameData;
                }
                s.Index = currSeqId;
                seqId2Label.Add(currSeqId++, label);
                s.LabelName = label;

                //Bank.
                string bnk = seqData[1];
                if (ushort.TryParse(bnk, out _))
                {
                    s.ReadingBankId = ushort.Parse(bnk);
                    if (a != null)
                    {
                        s.Bank = a.Banks.Where(x => x.Index == s.ReadingBankId).FirstOrDefault();
                    }
                }
                else if (a != null)
                {
                    var bnkReal = a.Banks.Where(x => x.Name.Equals(bnk)).FirstOrDefault();
                    if (bnkReal == null)
                    {
                        throw new Exception("Bank " + bnk + " does not exist!");
                    }
                    s.ReadingBankId = (ushort)bnkReal.Index;
                    s.Bank          = bnkReal;
                }
                else
                {
                    throw new Exception("Can't use a name when there is no sound archive open!");
                }

                //Data.
                s.Volume          = byte.Parse(seqData[2]);
                s.ChannelPriority = byte.Parse(seqData[3]);
                s.PlayerPriority  = byte.Parse(seqData[4]);

                //Player
                string ply = seqData[5];
                if (ushort.TryParse(ply, out _))
                {
                    s.ReadingPlayerId = byte.Parse(ply);
                    if (a != null)
                    {
                        s.Player = a.Players.Where(x => x.Index == s.ReadingPlayerId).FirstOrDefault();
                    }
                }
                else if (a != null)
                {
                    var plyReal = a.Players.Where(x => x.Name.Equals(ply)).FirstOrDefault();
                    if (plyReal == null)
                    {
                        throw new Exception("Player " + ply + " does not exist!");
                    }
                    s.ReadingPlayerId = (byte)plyReal.Index;
                    s.Player          = plyReal;
                }
                else
                {
                    throw new Exception("Can't use a name when there is no sound archive open!");
                }

                //Add sequence.
                Sequences.Add(s);
            }

            //Fetch labels.
            int strt = t.IndexOf("@SEQ_DATA") + 1;

            for (int i = strt; i < t.Count; i++)
            {
                //If it's a label.
                if (t[i].EndsWith(":"))
                {
                    labelLines.Add(i);
                    string lbl = t[i].Replace(":", "");
                    if (!seqId2Label.ContainsValue(lbl))
                    {
                        privateLabels.Add(lbl, comNum);
                        OtherLabels.Add(comNum);
                    }
                    else
                    {
                        PublicLabels.Add(lbl, comNum);
                    }
                }
                else
                {
                    comNum++;
                }
            }

            //Get commands.
            Commands = new List <SequenceCommand>();
            for (int i = t.IndexOf("@SEQ_DATA") + 1; i < t.Count; i++)
            {
                if (labelLines.Contains(i))
                {
                    continue;
                }
                SequenceCommand seq = new SequenceCommand();
                try { seq.FromString(t[i], PublicLabels, privateLabels); } catch { WritingCommandSuccess = false; throw new Exception("Command " + i + ": \"" + t[i] + "\" is invalid."); }
                Commands.Add(seq);
            }

            //Fin.
            Commands.Add(new SequenceCommand()
            {
                CommandType = SequenceCommands.Fin
            });

            //Backup labels.
            var bakLabels = PublicLabels;

            PublicLabels = new Dictionary <string, int>();
            foreach (var seq in Sequences)
            {
                PublicLabels.Add(seq.Name, bakLabels[seq.LabelName]);
            }
        }