Esempio n. 1
0
        public void RemoveAllVariablesFromFile(RefFilename filename)
        {
            if (unsortedScopedVariablesByFilename.ContainsKey(filename))
            {
                var variables = unsortedScopedVariablesByFilename[filename];

                for (var index = 0; index < variables.Count; index++)
                {
                    var variable = variables[index];
                    unsortedScopedVariables[variable.Name].Remove(variable);
                    variables.Remove(variable);
                    index--;
                }
            }
            if (globalVariablesByFilename.ContainsKey(filename))
            {
                var variables = globalVariablesByFilename[filename];

                for (var index = 0; index < variables.Count; index++)
                {
                    var variable = variables[index];
                    globalVariables.Remove(variable.Name);
                    variables.Remove(variable);
                    index--;
                }
            }
        }
Esempio n. 2
0
        public ScriptFile EnsureFile(RefFilename filename, ScriptContext context)
        {
            if (FileMap.ContainsKey(filename))
            {
                return(FileMap[filename]);
            }

            var file = new ScriptFile();

            file.Filename     = filename;
            file.Context      = context;
            FileMap[filename] = file;

            if (!file.IsBase && Parent != null && Parent.FileMap.ContainsKey(filename))
            {
                var f = Parent.FileMap[filename];
                f.Overridden = true;
                foreach (var mapValue in f.Map.Values)
                {
                    mapValue.Overridden = true;
                }
            }

            return(file);
        }
Esempio n. 3
0
        public void UpdateFile(RefFilename filename, string text)
        {
            VariableStore.Instance.RemoveAllVariablesFromFile(filename);
            Instance.DeferedPostInitializationListNext.Clear();

            ModCK3Library.ClearFile(filename);

            LoadingCK3Library = ModCK3Library;

            var c = GetContextFromDirectory(filename);

            try
            {
                var results = FileTokenizer.Instance.LoadText(text, filename, c);

                ModCK3Library.Add(results, c);
            }
            catch (Exception e)
            {
            }

            try
            {
                for (var i = 0; i < Instance.DeferedPostInitializationListNext.Count; i++)
                {
                    var scriptObject = Instance.DeferedPostInitializationListNext[i];
                    scriptObject.Initialize();
                }
            }
            catch (Exception e)
            {
            }

            try
            {
                LoadingCK3Library.RecalculateGroups();

                do
                {
                    Instance.DeferedPostInitializationList     = Instance.DeferedPostInitializationListNext;
                    Instance.DeferedPostInitializationListNext = new List <ScriptObject>();
                    for (var i = 0; i < Instance.DeferedPostInitializationList.Count; i++)
                    {
                        var scriptObject = Instance.DeferedPostInitializationList[i];
                        scriptObject.PostInitialize(null, null);
                    }

                    Instance.DeferedPostInitializationList.Clear();
                } while (Instance.DeferedPostInitializationListNext.Count > 0);
            }
            catch (Exception e)
            {
            }


            ScriptObject.ClearCachedScriptedEffects();
        }
Esempio n. 4
0
        public ScriptFile GetFile(RefFilename file, bool isBaseFile)
        {
            if (isBaseFile)
            {
                return(BaseCK3Library.GetFile(file));
            }

            return(ModCK3Library.GetFile(file));
        }
Esempio n. 5
0
        public bool HasFile(RefFilename file)
        {
            var f = new RefFilename(file.ToRelativeFilename(), this == Core.Instance.BaseCK3Library);

            if (FileMap.ContainsKey(f))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        public List <ScriptObject> LoadFile(RefFilename filename, ScriptContext context, bool save)
        {
            if (!filename.Exists)
            {
                return(new List <ScriptObject>());
            }

            var text = File.ReadAllText(filename.ToFullFilename());

            return(LoadText(text, filename, context, save));
        }
Esempio n. 7
0
        public ScriptContext GetContextFromDirectory(RefFilename dir)
        {
            var res = BaseCK3Library.ContextData
                      .Where(a => a.Value.Directory != null && dir.ToRelativeFilename().StartsWith(a.Value.Directory))
                      .ToList();

            if (res.Any())
            {
                return(res[0].Key);
            }

            return(ScriptContext.Event);
        }
Esempio n. 8
0
        public void ClearFile(RefFilename filename)
        {
            if (FileMap.ContainsKey(filename))
            {
                var file = FileMap[filename];

                foreach (var keyValuePair in file.Map)
                {
                    Remove(keyValuePair.Key, keyValuePair.Value.Context);
                }
                file.Map.Clear();
            }
        }
Esempio n. 9
0
        public void LoadCK3BaseFile(RefFilename filename)
        {
            if (BaseCK3Library.HasFile(filename))
            {
                return;
            }

            LoadingCK3Library = BaseCK3Library;

            var context = GetContextFromDirectory(filename);

            var results = FileTokenizer.Instance.LoadFile(filename, context, true);

            BaseCK3Library.Add(results, context);
        }
Esempio n. 10
0
        public ScriptFile GetFile(RefFilename file)
        {
            var f = new RefFilename(file.ToRelativeFilename(), this == Core.Instance.BaseCK3Library);

            if (FileMap.ContainsKey(f))
            {
                return(FileMap[f]);
            }

            if (Parent != null)
            {
                return(Parent.GetFile(file));
            }

            return(null);
        }
Esempio n. 11
0
        public bool AddFile(RefFilename file)
        {
            var f = new ScriptFile();

            f.Filename = new RefFilename(file.ToRelativeFilename(), this == Core.Instance.BaseCK3Library);
            var l = Core.Instance.BaseCK3Library.ContextData.Where(a =>
                                                                   a.Value.Directory != null && f.Filename.ToRelativeDirectoryOfFile().Contains(a.Value.Directory));

            if (l.Count() == 0)
            {
                return(false);
            }
            f.Context     = l.First().Key;
            FileMap[file] = f;
            return(true);
        }
Esempio n. 12
0
        public bool LoadCK3File(RefFilename filename, bool forceBase = false, bool forceReload = false)
        {
            var fromBase = filename.IsBase;

            LoadingCK3Library = fromBase ? BaseCK3Library : ModCK3Library;
            if (!forceReload)
            {
                if (fromBase)
                {
                    if (BaseCK3Library.HasFile(filename))
                    {
                        return(fromBase);
                    }
                }
                else
                {
                    if (ModCK3Library.HasFile(filename))
                    {
                        return(fromBase);
                    }
                }
            }
            else
            {
                LoadingCK3Library.ClearFile(filename);
            }


            // string directory = filename.Substring(0, Math.Max(filename.LastIndexOf("/"), 0));

            var context = GetContextFromDirectory(filename);

            var results = FileTokenizer.Instance.LoadFile(filename, context, true);

            LoadingCK3Library.Add(results, context);

            PostInitialize(null);

            return(fromBase);
        }
Esempio n. 13
0
        public List <ScriptObject> LoadDirectory(RefFilename directory, ScriptContext context, bool save, bool load)
        {
            var results = new List <ScriptObject>();

            if (!directory.Exists)
            {
                return(results);
            }

            var directories = directory.GetDirectories();

            foreach (var s in directories)
            {
                results.AddRange(LoadDirectory(s, context, save, load));
            }

            var files = directory.GetFiles();

            foreach (var file in files)
            {
                if (file.Extension == ".txt")
                {
                    var binFilename = file.ToRelativeFilename().Replace(".txt", ".bin");
                    binFilename = binFilename.Replace(Globals.CK3Path, "");
                    binFilename = Globals.CK3EdDataPath.Replace("\\", "/") + "CachedCK3Data/" + binFilename;
                    if (File.Exists(binFilename) && load)
                    {
                        ScriptNamespace = "";
                        results.AddRange(LoadParsableFile(binFilename));
                    }
                    else
                    {
                        results.AddRange(LoadFile(file, context, save));
                    }
                }
            }

            return(results);
        }
Esempio n. 14
0
 public ScriptFile GetFile(RefFilename file)
 {
     return(ModCK3Library.GetFile(file));
 }
Esempio n. 15
0
        public List <ScriptParsedSegment> ParseTokens(List <string> tokens, List <int> lineNumbers, RefFilename filename)
        {
            var parsables            = new List <ScriptParsedSegment>();
            var lineNumbersForObject = new List <int>();
            var tokensForObject      = new List <string>();
            var token = 0;

            while (tokens.Count > 0)
            {
                if (tokens.Count == 1)
                {
                    tokensForObject.Clear();
                    tokensForObject.AddRange(tokens);
                    var lineNumChild = new List <int>();
                    lineNumChild.AddRange(lineNumbers);
                    var parsable = new ScriptParsedSegment();
                    parsable.lineNumbers = lineNumChild;
                    parsable.filename    = filename;
                    parsable.Parse(tokensForObject);
                    parsables.Add(parsable);
                    tokens.Clear();
                    // lineNumChild.Clear();
                    token += 1;
                    continue;
                }

                if (tokens.Count == 2)
                {
                    tokensForObject.Clear();
                    tokensForObject.Add(tokens[0]);
                    var lineNumChild = new List <int>();
                    lineNumChild.Add(lineNumbers[0]);
                    var parsable = new ScriptParsedSegment();
                    parsable.lineNumbers = lineNumChild;
                    parsable.filename    = filename;
                    parsable.Parse(tokensForObject);
                    parsables.Add(parsable);
                    tokens.RemoveAt(0);
                    //  lineNumChild.Clear();
                    lineNumbers.RemoveAt(0);
                    token += 1;
                    continue;
                }

                var first  = tokens[0];
                var second = tokens[1];
                var third  = tokens[2];

                var extraTokens = 0;
                if (first == "NOT" && lineNumbers[0] == 431)
                {
                }

                if (third == "hsv")
                {
                    third = string.Join("", tokens.GetRange(2, tokens.Count - 2));
                    lineNumbers.RemoveRange(2, tokens.Count - 3);
                    tokens.RemoveAt(2);
                    tokens.Insert(2, third);
                    tokens.RemoveRange(3, tokens.Count - 3);
                    token += 3;
                }

                if (first == "scripted_trigger" || first == "scripted_effect")
                {
                    first  = first + " " + second;
                    second = third;
                    third  = tokens[3];
                    tokens.RemoveAt(1);
                    lineNumbers.RemoveAt(1);
                    tokens[0] = first;
                }

                // could be a list...
                if (first == "{")
                {
                    first  = "listEntry";
                    second = "=";
                    third  = tokens[0];
                    tokens.Insert(0, "listEntry");
                    tokens.Insert(1, "=");
                    lineNumbers.Insert(0, lineNumbers[0]);
                    lineNumbers.Insert(1, lineNumbers[0]);
                }

                if (!(second == "=" || second == ">" || second == "<" || second == ">=" || second == "<=" ||
                      second == "!=" || second == "==") && second != "{")
                {
                    tokensForObject.Clear();
                    tokensForObject.Add(tokens[0]);
                    var lineNumChild = new List <int>();
                    lineNumChild.Add(lineNumbers[0]);
                    var parsable = new ScriptParsedSegment();
                    parsable.lineNumbers = lineNumChild;
                    parsable.filename    = filename;
                    parsable.Parse(tokensForObject);
                    parsables.Add(parsable);
                    tokens.RemoveAt(0);
                    //  lineNumChild.Clear();
                    lineNumbers.RemoveAt(0);
                    token += 1;
                    continue;
                }

                if (second == "=" || second == ">" || second == "<" || second == ">=" || second == "<=" ||
                    second == "!=" || second == "==")
                {
                }
                else
                {
                    // something weird going on - insert an = sign

                    {
                        // whoopsie, missed =...
                        tokens.Insert(1, "=");
                        first  = tokens[0];
                        second = tokens[1];
                        third  = tokens[2];
                        lineNumbers.Insert(1, lineNumbers[0]);
                    }
                }

                var isBlock = false;
                if (second == "=" || second == ">" || second == "<" || second == ">=" || second == "<=" ||
                    second == "!=" || second == "==")
                {
                    if (third == "{")
                    {
                        // this is a complex bracketed object... find the equivelent closing one...

                        var range = GetRangeOfClosedBracket(tokens, extraTokens);

                        if (range == -1)
                        {
                            tokens.Add("}");
                            lineNumbers.Add(lineNumbers.Last() + 1);
                            range = tokens.Count - 1;
                        }

                        isBlock = true;

                        tokensForObject = tokens.GetRange(0, range + 1);

                        lineNumbersForObject = lineNumbers.GetRange(0, range + 1);
                        lineNumbers.RemoveRange(0, range + 1);
                        tokens.RemoveRange(0, range + 1);
                    }
                    else if (third.StartsWith("@["))
                    {
                        // weird inliney calculations in scriptvalues
                        var index = tokens.IndexOf("]");
                        tokensForObject = tokens.GetRange(0, index + 1);

                        lineNumbersForObject = lineNumbers.GetRange(0, index + 1);
                        lineNumbers.RemoveRange(0, index + 1);


                        tokens.RemoveRange(0, index + 1);
                        token += index + 1;
                    }
                    else
                    {
                        var n = 3 + extraTokens;

                        tokensForObject = tokens.GetRange(0, n);

                        lineNumbersForObject = lineNumbers.GetRange(0, n);
                        lineNumbers.RemoveRange(0, n);

                        tokens.RemoveRange(0, n);
                        token += n;
                    }
                }

                {
                    var parsable = new ScriptParsedSegment();
                    parsable.lineNumbers = lineNumbersForObject;
                    parsable.filename    = filename;
                    parsable.isBlock     = isBlock;
                    if (parsable.lineNumbers[0] == 125 && filename.Equals("common/on_action/childhood_on_actions.txt"))
                    {
                    }

                    parsable.Parse(tokensForObject);
                    parsables.Add(parsable);
                }
            }

            return(parsables);
        }
Esempio n. 16
0
        public List <ScriptObject> LoadText(string text, RefFilename filename, ScriptContext context, bool save = false)
        {
            ScriptNamespace = "";
            var results = new List <ScriptObject>();


            file = Core.Instance.LoadingCK3Library.EnsureFile(filename, context);

            var lines       = text.Split('\n');
            var lineNumbers = new List <int>();
            var tokens      = new List <string>();
            var index       = 0;

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                line = line.Trim();
                index++;

                if (line.Contains("debug_log"))
                {
                    continue;
                }
                if (line.Trim().StartsWith("#"))
                {
                    continue;
                }
                var line2 = line;
                if (line.Length == 0)
                {
                    continue;
                }
                if (line2 == "name = \"Abdallah\" #ibn Muhammad\"")
                {
                }

                if (line2.Contains("\"") && (line2.IndexOf("\"") < line2.IndexOf("#") || line2.IndexOf("#") == -1))
                {
                    var ll    = line2;
                    var after = "";
                    while (ll.Contains("\""))
                    {
                        if (ll.Contains("\"") &&
                            (ll.IndexOf("\"") < ll.IndexOf("#") || ll.IndexOf("#") == -1))
                        {
                            var s      = ll.IndexOf("\"");
                            var e      = ll.IndexOf("\"", s + 1);
                            var l      = e - s;
                            var before = ll.Substring(0, s);
                            var str    = ll.Substring(s + 1, l - 1);
                            after = ll.Substring(e + 1);

                            TokenizeLine(before.Trim(), tokens);
                            tokens.Add("\"" + str + "\"");
                            lineNumbers.Add(index);
                            ll = after;
                        }
                        else
                        {
                            ll = ll.Substring(0, ll.IndexOf("#")).Trim();
                        }
                    }

                    TokenizeLine(after.Trim(), tokens);


                    while (lineNumbers.Count < tokens.Count)
                    {
                        lineNumbers.Add(index);
                    }
                }
                else
                {
                    TokenizeLine(line2, tokens);
                    while (lineNumbers.Count < tokens.Count)
                    {
                        lineNumbers.Add(index);
                    }
                }
            }

            if (tokens.Count == 0)
            {
                return(results);
            }

            var parsableResults = ParseTokens(tokens, lineNumbers, filename);

            if (save)
            {
                SaveBinary(parsableResults, context, filename.ToRelativeFilename());
            }

            ParseResults(null, parsableResults, context, results);

            return(results);
        }