Example #1
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            loader.RegisterModifiedFile(TargetFile);
            var source_path = mod.GetFile(SourceFile);

            var parsed_script = LuaParser.ParseFile(loader.ExpandTargetPathDefaulted(TargetFile));

            if (parsed_script == null)
            {
                throw new Exception($"Failed parsing '{TargetFile}'");
            }
            var ast_search = new ASTSearchWalker();

            ast_search.Match(LuaInsertTarget, parsed_script);

            if (!ast_search.HasSelection)
            {
                throw new Exception($"AST search failed");
            }

            var offset = ast_search.SelectedSpan.Value.Location.Position;

            string s;

            using (var target_file = new StreamReader(File.OpenRead(loader.ExpandTargetPathDefaulted(TargetFile)))) {
                using (var source_file = new StreamReader(File.OpenRead(source_path))) {
                    s = InsertAtPos(target_file, offset, source_file, Trim);
                }
            }

            loader.DeleteFile(TargetFile);

            using (var output = new StreamWriter(loader.OpenWriteTarget(TargetFile))) {
                output.Write(loader.ProcessNewlines(s.ToString()));
            }

            if (loader.ExtraChecks)
            {
                Logger.Debug($"Checking result to see if it's still valid");
                var parsed_new_script = LuaParser.ParseFile(loader.ExpandTargetPath(TargetFile));
                if (parsed_new_script == null)
                {
                    Logger.Warn($"Insertion broke Lua script");
                }
                else
                {
                    var test_ast_search = new ASTSearchWalker();
                    test_ast_search.Match(LuaInsertTarget, parsed_new_script);

                    if (!test_ast_search.HasSelection)
                    {
                        Logger.Warn($"AST query no longer resolves after insertion");
                    }
                }
            }

            DoReplaceIfRequested(mod, loader, TargetFile);
        }
Example #2
0
        protected void DoReplaceIfRequested(Mod mod, Ommel loader, string target_path)
        {
            if (Placeholders == null)
            {
                return;
            }
            if (Placeholders != null && PlaceholderFile == null)
            {
                Logger.Warn($"File operation requested to use placeholders, but did not specify where the placeholder file is");
                return;
            }
            var string_map = mod.LoadReplacementMap(PlaceholderFile);

            if (string_map == null && Placeholders != null)
            {
                Logger.Warn($"File operation requested to use placeholders, but there is no string map file");
                return;
            }

            if (Placeholders != null)
            {
                mod.ReplaceStringsInFile(string_map, Placeholders.ToArray(), mod.Ommeldata.PlaceholderPrefix, mod.Ommeldata.PlaceholderSuffix, loader.ExpandTargetPath(target_path));
            }
        }