コード例 #1
0
        private SheetsAction GetActionFromCell(string cell)
        {
            if (cell != "")
            {
                if (cell != "")
                {
                    List <string> tempA, tempB;
                    string        command, param, value, content, source;
                    bool          append = false;
                    if (cell [0] == '$')
                    {
                        tempA   = new List <string> (cell.Split(':'));
                        command = tempA [0].ToLower();
                        tempA.RemoveAt(0);
                        content = source = String.Join(":", tempA.ToArray());
                    }
                    else
                    {
                        command = "$speak";
                        content = source = cell;
                    }

                    if (command != "$sequence")
                    {
                        Regex contentMatch   = new Regex(@"[^+@=]*");
                        Match contentResults = contentMatch.Match(content);
                        if (contentResults.Success)
                        {
                            content = contentResults.Value;
                        }
                        if (content.Length > 0)
                        {
                            if (content[0] == '&')
                            {
                                append  = true;
                                content = content.Substring(1);
                            }
                        }
                    }

                    float delay        = Single.NaN;
                    Regex delayMatch   = new Regex(@"\+([\d])*(.[\d]*)(?![^+@=])");
                    Match delayResults = delayMatch.Match(source);
                    if (delayResults.Success)
                    {
                        delay = float.Parse(delayResults.Value);
                    }

                    string tone        = null;
                    Regex  toneMatch   = new Regex(@"@[^+@=]+");
                    Match  toneResults = toneMatch.Match(source);
                    if (toneResults.Success)
                    {
                        tone = toneResults.Value.Substring(1);
                    }

                    string duration        = null;
                    Regex  durationmatch   = new Regex(@"=[^@+=]+");
                    Match  durationResults = durationmatch.Match(source);
                    if (durationResults.Success)
                    {
                        duration = durationResults.Value.Substring(1);
                    }

                    XmlElement   script = null;
                    SheetsAction action;
                    int          n;
                    switch (command)
                    {
                    case "$sequence":
                        tempA  = new List <string> (content.Split(','));
                        action = new SheetsAction("config");
                        action.config ["shuffle"] = false;
                        n = tempA.Count;
                        for (int i = 0; i < n; i++)
                        {
                            tempB = new List <string> (tempA [i].Trim().Split(':'));
                            param = tempB [0];
                            tempB.RemoveAt(0);
                            value = String.Join(":", tempB.ToArray());
                            switch (param)
                            {
                            case "shuffle":
                                action.config ["shuffle"] = true;
                                break;

                            case "hidden":
                                action.config ["visible"] = false;
                                break;

                            case "repeat":
                                action.config ["repeat"] = value.Trim();
                                if (action.config ["repeat"] == "forever")
                                {
                                    action.config ["repeat"] = "+";
                                }
                                break;

                            case "grouping":
                                action.config ["grouping"] = value.Trim();
                                break;

                            case "id":
                                action.config ["id"] = value.Trim();
                                break;
                            }
                        }
                        break;

                    case "$sample":
                    case "$reset":
                        script          = _script.CreateElement(command.Substring(1));
                        script.InnerXml = content;
                        action          = new SheetsAction("command");
                        break;

                    case "$setbackcolor":
                    case "$setdate":
                    case "$setforecolor":
                    case "$setmidcolor":
                    case "$settime":
                    case "$setweather":
                        script = _script.CreateElement(command.Substring(1));
                        action = new SheetsAction("command");
                        break;

                    case "$option":
                        tempA                         = new List <string> (content.Split(','));
                        content                       = tempA [0];
                        script                        = _script.CreateElement(command.Substring(1));
                        script.InnerXml               = content;
                        action                        = new SheetsAction("command");
                        action.config ["type"]        = null;
                        action.config ["destination"] = null;
                        n = tempA.Count;
                        for (int i = 0; i < n; i++)
                        {
                            tempB = new List <string> (tempA [i].Trim().Split(':'));
                            param = tempB [0];
                            tempB.RemoveAt(0);
                            value = String.Join(":", tempB.ToArray());
                            switch (param)
                            {
                            case "type":
                                action.config ["type"] = value;
                                break;

                            case "destination":
                                action.config ["destination"] = value;
                                break;
                            }
                        }
                        if (action.config ["type"] != null)
                        {
                            script.SetAttribute("type", action.config ["type"]);
                        }
                        if (action.config ["destination"] != null)
                        {
                            script.SetAttribute("destination", action.config ["destination"]);
                        }
                        break;

                    case "$setsequence":
                        tempA                       = new List <string> (content.Split(','));
                        content                     = tempA [0];
                        script                      = _script.CreateElement(command.Substring(1));
                        script.InnerXml             = content;
                        action                      = new SheetsAction("command");
                        action.config ["atdate"]    = null;
                        action.config ["autostart"] = false;
                        n = tempA.Count;
                        for (int i = 0; i < n; i++)
                        {
                            tempB = new List <string> (tempA [i].Trim().Split(':'));
                            param = tempB [0];
                            tempB.RemoveAt(0);
                            value = String.Join(":", tempB.ToArray());
                            switch (param)
                            {
                            case "atdate":
                                action.config ["atdate"] = value;
                                break;

                            case "autostart":
                                action.config ["autostart"] = true;
                                break;
                            }
                        }
                        if (action.config ["autostart"])
                        {
                            script.SetAttribute("autostart", action.config ["autostart"]);
                        }
                        if (action.config ["atdate"] != null)
                        {
                            script.SetAttribute("atdate", action.config ["atdate"]);
                        }
                        break;

                    case "$sing":
                    case "$speak":
                    case "$nothing":
                        script = _script.CreateElement(command.Substring(1));
                        action = new SheetsAction("utterance");
                        break;

                    default:
                        script = _script.CreateElement(command.Substring(1));
                        action = new SheetsAction("command");
                        break;
                    }
                    if (script != null)
                    {
                        script.InnerXml = content;
                        if (append)
                        {
                            script.SetAttribute("append", "true");
                        }
                        if (!Single.IsNaN(delay))
                        {
                            script.SetAttribute("delay", delay.ToString());
                        }
                        if (tone != null)
                        {
                            tone = ParseTone(tone);
                            script.SetAttribute("tone", tone);
                        }
                        if (duration != null)
                        {
                            script.SetAttribute("duration", duration);
                        }
                        action.payload = script;
                    }
                    return(action);
                }
            }
            return(null);
        }
コード例 #2
0
        private Dictionary <string, List <SheetsAction> > GetActionsFromEntry(JSONNode entry)
        {
            SheetsAction action;
            string       id;
            XmlElement   element;
            XmlNodeList  nodes;
            Dictionary <string, List <SheetsAction> > actionsByCharacter = new Dictionary <string, List <SheetsAction> > ();

            foreach (var prop in entry.Keys)
            {
                if (PropertyIsCharacterId(prop))
                {
                    id = GetCharacterIdFromProperty(prop);
                    actionsByCharacter [id] = new List <SheetsAction> ();
                    if (entry[prop] != null)
                    {
                        if (entry[prop]["$t"] == " ")
                        {
                            action         = new SheetsAction("command");
                            action.payload = _script.CreateElement("nothing");
                            action.payload.SetAttribute("character", id);
                            action.payload.SetAttribute("explicit", "true");
                            actionsByCharacter [id].Add(action);
                        }
                        else if (entry[prop]["$t"] == "")
                        {
                            action         = new SheetsAction("command");
                            action.payload = _script.CreateElement("nothing");
                            action.payload.SetAttribute("character", id);
                            actionsByCharacter [id].Add(action);
                        }
                        else
                        {
                            string cell = entry [prop] ["$t"].ToString();
                            if (cell != "")
                            {
                                cell = cell.Substring(1, Mathf.Max(0, cell.Length - 2));                                   // remove outer quotes
                                cell = cell.Replace("\\\\n", "\n");
                                string [] subActions = cell.Split(new [] { "\\n" }, StringSplitOptions.None);
                                int       n          = subActions.Length;
                                for (int i = 0; i < n; i++)
                                {
                                    action = GetActionFromCell(subActions [i]);
                                    if (action != null)
                                    {
                                        switch (action.type)
                                        {
                                        case "config":
                                            foreach (var index in _sequencesByCharacter.Keys)
                                            {
                                                nodes = _sequencesByCharacter [index].GetElementsByTagName("nothing");
                                                foreach (XmlElement node in nodes)
                                                {
                                                    node.SetAttribute("explicit", "true");
                                                }
                                            }
                                            element = _script.CreateElement("sequence");
                                            _script.DocumentElement.AppendChild(element);
                                            _sequencesByCharacter [id] = element;
                                            if (action.config ["shuffle"].AsBool)
                                            {
                                                element.SetAttribute("shuffle", "true");
                                            }
                                            if (action.config ["visible"] != null)
                                            {
                                                element = _script.SelectSingleNode("//*[@id = '" + id + "']") as XmlElement;
                                                element.SetAttribute("visible", action.config ["visible"] ? "true" : "false");
                                            }
                                            if (action.config ["repeat"] != null)
                                            {
                                                element.SetAttribute("repeat", action.config ["repeat"]);
                                            }
                                            if (action.config ["grouping"] != null)
                                            {
                                                element.SetAttribute("grouping", action.config ["grouping"]);
                                            }
                                            if (action.config ["id"] != null)
                                            {
                                                element.SetAttribute("id", action.config ["id"]);
                                            }
                                            break;

                                        case "command":
                                        case "utterance":
                                            action.payload.SetAttribute("character", id);
                                            actionsByCharacter [id].Add(action);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(actionsByCharacter);
        }