Beispiel #1
0
    /**
     * <summary>
     * Parses the <see cref="CommandList"/> with the specified file name to an object
     * </summary>
     */
    public static CommandList Parse(string name = "base")
    {
        Dictionary <string, object> jsonData;

        // Check if the files Directory exists, if not, create it
        string dataPath = Application.persistentDataPath + "/commandlists";

        if (!Directory.Exists(dataPath))
        {
            Directory.CreateDirectory(dataPath);
        }

        // Read from the file, handle cases the specified file, or even the standard file doesnt exist
        if (File.Exists(dataPath + "/" + name + ".json"))
        {
            using (StreamReader sr = new StreamReader(dataPath + "/" + name + ".json"))
            {
                jsonData = JsonConvert.DeserializeObject <Dictionary <string, object> >(sr.ReadLine());
            }
        }
        else if (File.Exists(dataPath + "/base.json"))
        {
            using (StreamReader sr = new StreamReader(dataPath + "/base.json"))
            {
                jsonData = JsonConvert.DeserializeObject <Dictionary <string, object> >(sr.ReadLine());
            }
            PlayerPrefs.SetString("list", "base");
        }
        else
        {
            jsonData = new Dictionary <string, object>();
        }

        // Parse the string to an object
        CommandList newCommandList;

        if (jsonData.Count > 0)
        {
            // Try get the name of the list
            if (jsonData.TryGetValue("name", out object dataName))
            {
                newCommandList = new CommandList(name, (string)dataName);
            }
            else
            {
                newCommandList = new CommandList(name, "Some List");
            }

            // Try get the content of the list
            if (jsonData.TryGetValue("parts", out object partlistobj))
            {
                // Iterate over each PartCommandList in the JSON data
                foreach (List <Dictionary <string, object> > part in ((Newtonsoft.Json.Linq.JArray)partlistobj).ToObject <List <List <Dictionary <string, object> > > >())
                {
                    PartCommandList newPart = new PartCommandList();
                    // Iterate over each Command in that list in the JSON data
                    foreach (Dictionary <string, object> cmd in part)
                    {
                        if (cmd.TryGetValue("commandTitle", out object commandTitle))
                        {
                            // Incase there is a commandTitle specified in JSON, try and get meaning from it
                            Command newCmd = null;
                            switch (commandTitle)
                            {
                            // Incase the title means something, try and get the values for that specific Command
                            case "rainbow":
                                newCmd = new RainbowCommand();
                                if (cmd.TryGetValue("wait_ms", out object wait_ms))
                                {
                                    ((RainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms);
                                }
                                break;

                            case "rainbowStationary":
                                newCmd = new StatRainbowCommand();
                                if (cmd.TryGetValue("wait_ms", out wait_ms))
                                {
                                    ((StatRainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms);
                                }
                                break;

                            case "colorWipe":
                                newCmd = new ColorwipeCommand();
                                if (cmd.TryGetValue("color", out object color))
                                {
                                    ((ColorwipeCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color));
                                }
                                if (cmd.TryGetValue("wait_ms", out wait_ms))
                                {
                                    ((ColorwipeCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms);
                                }
                                break;

                            case "setColor":
                                newCmd = new SetColorCommand();
                                if (cmd.TryGetValue("color", out color))
                                {
                                    ((SetColorCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color));
                                }
                                break;

                            case "theaterChase":
                                newCmd = new TheaterCommand();
                                if (cmd.TryGetValue("color", out color))
                                {
                                    ((TheaterCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color));
                                }
                                if (cmd.TryGetValue("wait_ms", out wait_ms))
                                {
                                    ((TheaterCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms);
                                }
                                if (cmd.TryGetValue("iterations", out object iterations))
                                {
                                    ((TheaterCommand)newCmd).iterations = Convert.ToInt32(iterations);
                                }
                                break;

                            case "theaterChaseRainbow":
                                newCmd = new TheaterRainbowCommand();
                                if (cmd.TryGetValue("wait_ms", out wait_ms))
                                {
                                    ((TheaterRainbowCommand)newCmd).wait_ms = Convert.ToInt32(wait_ms);
                                }
                                break;

                            case "interpolate":
                                newCmd = new InterpolateCommand();
                                if (cmd.TryGetValue("color", out color))
                                {
                                    ((InterpolateCommand)newCmd).color = Helper.ToColor(Convert.ToInt32(color));
                                }
                                if (cmd.TryGetValue("color2", out object color2))
                                {
                                    ((InterpolateCommand)newCmd).color2 = Helper.ToColor(Convert.ToInt32(color2));
                                }
                                if (cmd.TryGetValue("duration_ms", out object duration_ms))
                                {
                                    ((InterpolateCommand)newCmd).duration_ms = Convert.ToInt32(duration_ms);
                                }
                                if (cmd.TryGetValue("goback", out object goback))
                                {
                                    ((InterpolateCommand)newCmd).goback = (bool)goback;
                                }
                                break;

                            case "wait":
                                newCmd = new WaitCommand();
                                if (cmd.TryGetValue("duration_ms", out duration_ms))
                                {
                                    ((WaitCommand)newCmd).duration_ms = Convert.ToInt32(duration_ms);
                                }
                                break;

                            case "split":
                                newCmd = new SplitCommand();
                                if (cmd.TryGetValue("newList", out object newList))
                                {
                                    ((SplitCommand)newCmd).newList = Convert.ToInt32(newList);
                                }
                                break;

                            case "join":
                                newCmd = new JoinCommand();
                                if (cmd.TryGetValue("listtojoin", out object listtojoin))
                                {
                                    ((JoinCommand)newCmd).listtojoin = Convert.ToInt32(listtojoin);
                                }
                                if (cmd.TryGetValue("waitlist", out object waitlist))
                                {
                                    ((JoinCommand)newCmd).waitlist = (bool)waitlist;
                                }
                                break;
                            }
                            // Only add to list if the title meant sth
                            if (newCmd != null)
                            {
                                if (cmd.TryGetValue("title", out object title))
                                {
                                    newCmd.title = (string)title;
                                }
                                if (cmd.TryGetValue("lamps", out object lamps))
                                {
                                    newCmd.lamps = ((Newtonsoft.Json.Linq.JArray)lamps).ToObject <List <int> >();
                                }
                                newPart.AddHidden(newCmd);
                            }
                        }
                    }
                    newCommandList.lists.Add(newPart);
                }
            }
            return(newCommandList);
        }
        return(new CommandList());
    }
Beispiel #2
0
    /**
     * <summary>
     * Changes the <see cref="Command"/> object to the new dynamic type specified by newSetting
     * </summary>
     */
    public void ChangeCommand(Command cmd, string newSetting)
    {
        // Initializing the subtype
        Command newCommand;

        if (newSetting == "rainbow")
        {
            newCommand = new RainbowCommand();
        }
        else if (newSetting == "rainbowStationary")
        {
            newCommand = new StatRainbowCommand();
        }
        else if (newSetting == "theaterChaseRainbow")
        {
            newCommand = new TheaterRainbowCommand();
        }
        else if (newSetting == "theaterChase")
        {
            newCommand = new TheaterCommand();
        }
        else if (newSetting == "colorWipe")
        {
            newCommand = new ColorwipeCommand();
        }
        else if (newSetting == "interpolate")
        {
            newCommand = new InterpolateCommand();
        }
        else if (newSetting == "setColor")
        {
            newCommand = new SetColorCommand();
        }
        else if (newSetting == "wait")
        {
            newCommand = new WaitCommand();
        }
        else if (newSetting == "split")
        {
            newCommand = new SplitCommand();
        }
        else if (newSetting == "join")
        {
            newCommand = new JoinCommand();
        }
        else
        {
            newCommand = new RainbowCommand();
        }

        // Saves all settings from the old command that the new Command has
        newCommand.SaveSettings(cmd.GetSettings());

        // Replaces the old Gameobject with a fresh one and initializes it
        int idx = 0;
        int i   = 0;

        foreach (PartCommandList list in commands.lists)
        {
            if (list.list.Contains(cmd))
            {
                idx = list.Remove(cmd);
                list.list.Insert(idx, newCommand);

                i = commands.lists.IndexOf(list);
            }
        }
        GameObject obj = Instantiate(commandPrefab, commands.lists[i].listObject.transform);

        newCommand.commandObject = obj;
        newCommand.Init();

        obj.transform.SetSiblingIndex(idx);

        // Deletes the old Gameobject to be picked up by garbage collector
        GameObject.Destroy(cmd.commandObject);
    }