Beispiel #1
0
        private static string Parse(ICodeStruct script)
        {
            StringBuilder text = new StringBuilder();

            text.AppendLine(script.Parse());
            return(text.ToString());
        }
Beispiel #2
0
        private void setCode(ICodeStruct internalScript)
        {
            CodeBlocks.Clear();
            EditorScript = internalScript == null? "" : internalScript.Parse(null, 0);
            List <AssignationModel> listBlock = ModelsToScriptHelper.
                                                TransformScriptToModels(managedScript, new RelayCommand <object>(DeleteNode));

            foreach (AssignationModel item in listBlock)
            {
                CodeBlocks.Add(item);
            }
        }
Beispiel #3
0
        public ICodeStruct Extract(string TagToFind)
        {
            if (Assignee == TagToFind)
            {
                return(this);
            }
            //If value cannot be ran through, return unfound
            if (!(Value is CodeBlock))
            {
                return(null);
            }
            ICodeStruct found = Value.Extract(TagToFind);

            //Return what was found, cannot extract from an assignation,
            //should extract in parent container
            return(found);
        }
 public ICodeStruct Extract(string TagToFind)
 {
     foreach (ICodeStruct item in Code)
     {
         ICodeStruct found = item.Extract(TagToFind);
         //If nothing was found, return null
         if (found == null)
         {
             continue;
         }
         //Otherwise, extract
         if (Code.Contains(found))
         {
             Code.Remove(found);
         }
         return(found);
     }
     return(null);
 }
        public string TryParse(ICodeStruct block, string tag,
                               Dictionary <int, string> comments = null, bool isMandatory = true)
        {
            Assignation found = block.FindAssignation(tag);

            if (found != null)
            {
                try
                {
                    return(found.Value.Parse(comments));
                }
                catch (Exception)
                {
                    Logger.Errors.Add(new SyntaxError(tag, found.Line, null,
                                                      new UnparsableTagException(tag)));
                }
            }
            if (isMandatory)
            {
                Logger.Errors.Add(new SyntaxError(tag, null, null,
                                                  new MandatoryTagException(tag)));
            }
            return(null);
        }
Beispiel #6
0
        public void GoodScriptTest()
        {
            //Arrange
            Script script = new Script();

            //Act
            script.Analyse(File.ReadAllText("usa.txt"));
            //Test code value
            CodeValue value       = script.FindValue("tag");
            string    valueString = value.Parse();
            //Test Assignation
            Assignation assign       = script.FindAssignation("modifier");
            string      assignString = assign.Parse();
            //Test Extraction
            ICodeStruct extracted       = script.Extract("default");
            string      extractedString = extracted.Parse();
            //Test find all focuses
            List <ICodeStruct> assigns = script.FindAllValuesOfType <CodeBlock>("focus");
            //Test try parse
            string tag = script.TryParse(script, "tag");

            //Assert
            Assert.IsTrue(script.Code.Any());
            Assert.IsFalse(script.Logger.hasErrors());
            Assert.IsTrue(valueString == "USA");
            Assert.IsTrue(!string.IsNullOrEmpty(assignString));
            Assert.IsTrue(extractedString.Contains("yes"));
            //Should not find a tag after extraction
            Assert.IsNull(script.FindValue("default"));
            Assert.IsTrue(assigns.Any());
            Assert.IsTrue(tag == "USA");
            //Test broken parse
            Assert.IsNull(script.TryParse(script, "default", null, false));
            script.TryParse(script, "default");
            Assert.IsTrue(script.Logger.hasErrors());
        }
        public static FociGridContainer CreateTreeFromScript(string fileName, Script script)
        {
            Dictionary <string, PrerequisitesSet> waitingList = new Dictionary <string, PrerequisitesSet>();
            FociGridContainer container = new FociGridContainer(Path.GetFileNameWithoutExtension(fileName));

            container.TAG = script.FindValue("tag") != null?script.FindValue("tag").Parse() : "";

            foreach (CodeBlock block in script.FindAllValuesOfType <CodeBlock>("focus"))
            {
                Focus newFocus = new Focus();
                newFocus.UniqueName = block.FindValue("id").Parse();
                newFocus.Image      = block.FindValue("icon").Parse().Replace("GFX_", "");
                newFocus.X          = int.Parse(block.FindValue("x").Parse());
                newFocus.Y          = int.Parse(block.FindValue("y").Parse());
                newFocus.Cost       = int.Parse(block.FindValue("cost").Parse());
                foreach (ICodeStruct exclusives in block.FindAllValuesOfType <ICodeStruct>("mutually_exclusive"))
                {
                    foreach (ICodeStruct focuses in exclusives.FindAllValuesOfType <ICodeStruct>("focus"))
                    {
                        //Check if focus exists in list
                        Focus found = container.FociList.FirstOrDefault((f) =>
                                                                        f.UniqueName == focuses.Parse());
                        if (found != null)
                        {
                            MutuallyExclusiveSet set = new MutuallyExclusiveSet(newFocus, found);
                            newFocus.MutualyExclusive.Add(set);
                            found.MutualyExclusive.Add(set);
                        }
                    }
                }
                foreach (ICodeStruct prerequistes in block.FindAllValuesOfType <ICodeStruct>("prerequisite"))
                {
                    if (!prerequistes.FindAllValuesOfType <ICodeStruct>("focus").Any())
                    {
                        break;
                    }
                    PrerequisitesSet set = new PrerequisitesSet(newFocus);
                    foreach (ICodeStruct focuses in prerequistes.FindAllValuesOfType <ICodeStruct>("focus"))
                    {
                        //Add the focus as a prerequisites in the current existing focuses
                        //or put into wait
                        Focus search = container.FociList.FirstOrDefault((f) =>
                                                                         f.UniqueName == focuses.Parse());
                        if (search != null)
                        {
                            set.FociList.Add(search);
                        }
                        else
                        {
                            waitingList[focuses.Parse()] = set;
                        }
                    }
                    //If any prerequisite was added (Poland has empty prerequisite blocks...)
                    if (set.FociList.Any())
                    {
                        newFocus.Prerequisite.Add(set);
                    }
                }
                //Get all core scripting elements
                Script InternalFocusScript = new Script();
                for (int i = 0; i < CORE_FOCUS_SCRIPTS_ELEMENTS.Length; i++)
                {
                    ICodeStruct found = block.FindAssignation(CORE_FOCUS_SCRIPTS_ELEMENTS[i]);
                    if (found != null)
                    {
                        InternalFocusScript.Code.Add(found);
                    }
                }
                newFocus.InternalScript = InternalFocusScript;
                container.FociList.Add(newFocus);
            }
            //Repair lost sets, shouldn't happen, but in case
            foreach (KeyValuePair <string, PrerequisitesSet> item in waitingList)
            {
                Focus search = container.FociList.FirstOrDefault((f) =>
                                                                 f.UniqueName == item.Key);
                if (search != null)
                {
                    item.Value.FociList.Add(search);
                }
            }
            return(container);
        }
        private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
        {
            //Should never loop inside something other than an assignation, check type
            Assignation assignation = currentBlock as Assignation;

            if (assignation != null)
            {
                Assignation block = assignation;
                //Check if simply empty, consider an empty block
                if (block.Value == null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel
                    {
                        BackgroundColor = BLOCKS_COLORS[0],
                        BorderColor = BLOCKS_COLORS[1],
                        Color = BLOCKS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = true,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " "
                               + block.Operator + " {}",
                        LocalizationKey = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    });
                }
                //check if a code value
                CodeValue value = block.Value as CodeValue;
                if (value != null)
                {
                    //Simple assignation, return corresponding model
                    return(new AssignationModel()
                    {
                        BackgroundColor = ASSIGNATIONS_COLORS[0],
                        BorderColor = ASSIGNATIONS_COLORS[1],
                        Color = ASSIGNATIONS_COLORS[2],
                        IsNotEditable = false,
                        CanHaveChild = false,
                        Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                               + " " + Regex.Replace(value.Value, @"\t|\n|\r|\s", ""),
                        LocalizationKey = "Scripter_Assignation_Custom",
                        DeleteNodeCommand = deleteCommand
                    });
                    //Ignore childs even if there is some
                }
                //Otherwise, the child is a codeblock
                CodeBlock codeBlock = block.Value as CodeBlock;
                if (codeBlock != null)
                {
                    AssignationModel newAssignation = new AssignationModel()
                    {
                        BackgroundColor = getColorArray(block.Assignee)[0],
                        BorderColor     = getColorArray(block.Assignee)[1],
                        Color           = getColorArray(block.Assignee)[2],
                        IsNotEditable   = false,
                        CanHaveChild    = true,
                        Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                          " " + block.Operator + " {}",
                        LocalizationKey   = getAssignationName(block.Assignee),
                        DeleteNodeCommand = deleteCommand
                    };
                    foreach (ICodeStruct code in codeBlock.Code)
                    {
                        newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
                    }
                    return(newAssignation);
                }
                //Error, return nothing.
                return(null);
            }
            //If the current block is a code Value (Corrsponding to block = { Value }
            if (currentBlock is CodeValue)
            {
                //Print the value
                return(new AssignationModel()
                {
                    BackgroundColor = ASSIGNATIONS_COLORS[0],
                    BorderColor = ASSIGNATIONS_COLORS[1],
                    Color = ASSIGNATIONS_COLORS[2],
                    IsNotEditable = false,
                    CanHaveChild = false,
                    Code = Regex.Replace(currentBlock.Parse(), @"\t|\n|\r|\s", ""),
                    LocalizationKey = "Scripter_Assignation_Custom",
                    DeleteNodeCommand = deleteCommand
                });
            }
            return(null);
        }
 private static AssignationModel BlockToModel(ICodeStruct currentBlock, RelayCommand <object> deleteCommand)
 {
     //Should never loop inside something other than an assignation, check type
     if (currentBlock is Assignation)
     {
         Assignation block = currentBlock as Assignation;
         //Check if simply empty, consider an empty block
         if (block.Value == null)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = BLOCKS_COLORS[0],
                 BorderColor = BLOCKS_COLORS[1],
                 Color = BLOCKS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = true,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             });
         }
         //check if a code value
         if (block.Value is CodeValue)
         {
             ResourceDictionary resourceLocalization = new ResourceDictionary();
             resourceLocalization.Source = new Uri(Configurator.getLanguageFile(), UriKind.Relative);
             //Simple assignation, return corresponding model
             return(new AssignationModel()
             {
                 BackgroundColor = ASSIGNATIONS_COLORS[0],
                 BorderColor = ASSIGNATIONS_COLORS[1],
                 Color = ASSIGNATIONS_COLORS[2],
                 IsNotEditable = false,
                 CanHaveChild = false,
                 Code = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") + " " + block.Operator
                        + " " + Regex.Replace(((CodeValue)block.Value).Value, @"\t|\n|\r|\s", ""),
                 Text = resourceLocalization["Scripter_Assignation_Custom"] as string,
                 DeleteNodeCommand = deleteCommand
             });
             //Ignore childs even if there is some
         }
         //Otherwise, the child is a codeblock
         else if (block.Value is CodeBlock)
         {
             AssignationModel newAssignation = new AssignationModel()
             {
                 BackgroundColor = getColorArray(block.Assignee)[0],
                 BorderColor     = getColorArray(block.Assignee)[1],
                 Color           = getColorArray(block.Assignee)[2],
                 IsNotEditable   = false,
                 CanHaveChild    = true,
                 Code            = Regex.Replace(block.Assignee, @"\t|\n|\r|\s", "") +
                                   " " + block.Operator + " {}",
                 Text = getAssignationName(block.Assignee),
                 DeleteNodeCommand = deleteCommand
             };
             foreach (ICodeStruct code in ((CodeBlock)block.Value).Code)
             {
                 newAssignation.Childrens.Add(BlockToModel(code, deleteCommand));
             }
             return(newAssignation);
         }
         else
         {
             //An error in the code, log it
             RecursiveCodeException e = new RecursiveCodeException();
             throw e.AddToRecursiveChain("Invalid assignation", block.Assignee, block.Line.ToString());
         }
     }
     return(null);
 }