Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var loader = new Ommel();

            loader.InterpretArgs(args);
            loader.Start(args);
            return;
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            loader.RegisterModifiedFile(TargetFile);

            loader.DeleteFile(TargetFile);
            loader.CopyFile(mod, SourceFile, TargetFile);

            DoReplaceIfRequested(mod, loader, TargetFile);
        }
Exemplo n.º 4
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            if (loader.FileExists(TargetFile))
            {
                throw new Exception($"Can't copy as file '{TargetFile}', because it already exists at that path");
            }

            loader.RegisterNewFile(TargetFile);

            loader.DeleteFile(TargetFile);
            loader.CopyFile(mod, SourceFile, TargetFile);

            DoReplaceIfRequested(mod, loader, TargetFile);
        }
Exemplo n.º 5
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            if (Flag != null)
            {
                if (loader.HasFlag(Flag))
                {
                    Logger.Debug($"Flag set - avoiding message!");
                    return;
                }
            }

            MessageBox.Show(Content, mod.Metadata.Name, MessageBoxButtons.OK, MessageBoxIcon.Information);

            if (Flag != null)
            {
                loader.Flags.Add(Flag);
            }
        }
Exemplo n.º 6
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            var source_file_list = GetSourceFileList(mod);
            var target_file_list = GetTargetFileList(loader);

            if (Mode == BulkMode.OneToOne)
            {
                if (source_file_list.Count != target_file_list.Count && (target_file_list.Count != 1 || target_file_list[0] != "{datapath}"))
                {
                    throw new Exception("In OneToOne mode, every source file must have a corresponding target file");
                }
            }

            Logger.Debug($"=== Begin Bulk ===");
            for (var i = 0; i < Operations.Count; i++)
            {
                var original_op = Operations[i];

                for (var j = 0; j < source_file_list.Count; j++)
                {
                    var source_file = source_file_list[j];
                    for (var k = 0; k < target_file_list.Count; k++)
                    {
                        if (Mode == BulkMode.OneToOne && k != j)
                        {
                            continue;
                        }

                        var target_file = target_file_list[k];
                        var new_op      = FileOperation.Copy(original_op);

                        new_op.SourceFile = source_file;
                        new_op.TargetFile = ExpandTargetPathVars(target_file, source_file);

                        Logger.Debug($"Source: {source_file}");
                        Logger.Debug($"Target: {new_op.TargetFile}");

                        new_op.Execute(loader, mod);
                    }
                }
            }
            Logger.Debug($"=== End Bulk ===");
        }
Exemplo n.º 7
0
        public void Execute(Ommel loader, Mod mod)
        {
            Logger.Debug($"Executing");

            if (SourceFileMustExist && SourceFile == null)
            {
                Logger.Error($"No source file ('file') specified");
                return;
            }

            if (SourceFileMustExist && !System.IO.File.Exists(mod.GetFile(SourceFile)))
            {
                Logger.Error($"Specified source file doesn't exist");
                return;
            }
            if (TargetFileMustExist && !System.IO.File.Exists(loader.GetNoitaAssetPath(TargetFile)))
            {
                Logger.Error($"Specified target file doesn't exist");
                return;
            }

            if (TargetFile != null && WhitelistedFileTypes.Count > 0)
            {
                var target_ftype = loader.GetFileTypeFromExtension(TargetFile);
                if (!WhitelistedFileTypes.Contains(target_ftype))
                {
                    Logger.Error($"File type '{target_ftype}' cannot be used in this operation");
                    return;
                }
            }
            else if (TargetFile != null)
            {
                var target_ftype = loader.GetFileTypeFromExtension(TargetFile);
                if (BlacklistedFileTypes.Contains(target_ftype))
                {
                    Logger.Error($"File type '{target_ftype}' cannot be used in this operation");
                    return;
                }
            }

            OnExecute(loader, mod);
        }
Exemplo n.º 8
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            var source_path = mod.GetFile(SourceFile);

            loader.RegisterModifiedFile(TargetFile);

            XMLMerger merger = null;

            using (var source_file = new StreamReader(File.OpenRead(source_path))) {
                using (var target_file = new StreamReader(File.OpenRead(loader.ExpandTargetPathDefaulted(TargetFile)))) {
                    merger = new XMLMerger(source_file, target_file); // patch, target
                }
            }

            var doc = merger.CreateMergedDocument();

            loader.DeleteFile(TargetFile);
            using (var target_file = new StreamWriter(loader.OpenWriteTarget(TargetFile))) {
                XMLMerger.WriteDocumentToFile(doc, target_file);
            }

            DoReplaceIfRequested(mod, loader, TargetFile);
        }
Exemplo n.º 9
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));
            }
        }
Exemplo n.º 10
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            TargetFile = TRANSLATION_CSV_FILE;
            loader.RegisterModifiedFile(TargetFile);

            var    available_lang_keys = new Dictionary <string, int>();
            var    total_entries       = 0;
            var    lines_by_key        = new Dictionary <string, string[]>();
            var    all_keys            = new List <string>();
            string header = null;

            using (var reader = new StreamReader(File.OpenRead(loader.ExpandTargetPathDefaulted(TRANSLATION_CSV_FILE)))) {
                var keys_line = reader.ReadLine();
                header = keys_line;

                var keys = keys_line.Split(',');
                total_entries = keys.Length;
                for (var i = 0; i < keys.Length; i++)
                {
                    var key = keys[i];
                    if (key.Length == 0 || key.StartsWith("NOTES", StringComparison.InvariantCulture))
                    {
                        continue;
                    }

                    available_lang_keys[key] = i;
                }


                while (!reader.EndOfStream)
                {
                    var line  = reader.ReadLine();
                    var elems = new string[total_entries];

                    CSVParseLine(line, elems);
                    lines_by_key[elems[0]] = elems;
                    all_keys.Add(elems[0]);
                }
            }

            using (var reader = XmlReader.Create(File.OpenRead(mod.GetFile(SourceFile)))) {
                var doc = new XmlDocument();
                doc.Load(reader);

                if (doc.ChildNodes.Count == 0)
                {
                    return;
                }

                var root = doc.ChildNodes[0];
                if (root.Name != "Localization")
                {
                    throw new Exception("Root element for a localization file must be of type Localization");
                }

                for (var i = 0; i < root.ChildNodes.Count; i++)
                {
                    var child = root.ChildNodes[i];
                    if (!(child is XmlElement))
                    {
                        continue;
                    }

                    if (child.Name != "Text" || !((XmlElement)child).HasAttribute("key"))
                    {
                        throw new Exception("Children of a Localization element must be Text elements with a key attribute");
                    }
                    var str_key = ((XmlElement)child).GetAttribute("key");

                    string[] csv_line;
                    if (!lines_by_key.TryGetValue(str_key, out csv_line))
                    {
                        lines_by_key[str_key] = csv_line = new string[total_entries];
                        csv_line[0]           = CSVEscape(str_key);
                        all_keys.Add(str_key);
                    }

                    for (var j = 0; j < child.ChildNodes.Count; j++)
                    {
                        var innerchild = child.ChildNodes[j];
                        if (!(innerchild is XmlElement))
                        {
                            continue;
                        }

                        if (innerchild.Name != "Language" || !((XmlElement)innerchild).HasAttribute("lang") || !((XmlElement)innerchild).HasAttribute("value"))
                        {
                            throw new Exception("Children of a Text element must be Language elements with lang and value attributes");
                        }

                        var lang_key = ((XmlElement)innerchild).GetAttribute("lang");
                        var lang_idx = 0;
                        if (!available_lang_keys.TryGetValue(lang_key, out lang_idx))
                        {
                            throw new Exception($"Invalid Language key: '{lang_key}'");
                        }

                        var lang_value = ((XmlElement)innerchild).GetAttribute("value");

                        csv_line[lang_idx] = lang_value;
                    }
                }
            }

            using (var w = new StreamWriter(loader.OpenWriteTarget(TargetFile))) {
                w.NewLine = "\r\n";
                w.WriteLine(header);
                for (var i = 0; i < all_keys.Count; i++)
                {
                    var csv_line = lines_by_key[all_keys[i]];
                    for (var j = 0; j < csv_line.Length; j++)
                    {
                        var entry = csv_line[j];
                        if (entry != null)
                        {
                            w.Write(CSVEscape(entry));
                        }
                        if (j < csv_line.Length - 1)
                        {
                            w.Write(",");
                        }
                    }
                    w.WriteLine();
                }
            }

            DoReplaceIfRequested(mod, loader, TargetFile);
        }
Exemplo n.º 11
0
 public abstract void OnExecute(Ommel loader, Mod mod);
Exemplo n.º 12
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            if (!ValidEvents.Contains(Event))
            {
                throw new Exception($"Invalid event '{Event}'");
            }

            if (Event == "init")
            {
                TargetFile = "data/scripts/perks/perk_list.lua";                              // lua entry point
            }
            if (!loader.FileExists(TargetFile))
            {
                throw new Exception("Target file doesn't exist");
            }

            var event_files     = loader.TryGetLuaModEvent(Event, TargetFile);
            var mod_source_file = mod.GetFile(SourceFile);

            if (event_files == null)
            {
                loader.RegisterModifiedFile(TargetFile);
                event_files = loader.RegisterLuaModEvent(Event, TargetFile);
                int offset = 0;

                if (Event == "enter")
                {
                    var lua_parser = new NetLua.Parser();
                    var block      = lua_parser.ParseFile(loader.ExpandTargetPathDefaulted(TargetFile));
                    NetLua.Ast.IStatement first_non_dofile = null;

                    for (var i = 0; i < block.Statements.Count; i++)
                    {
                        var statement = block.Statements[i];

                        if (statement is NetLua.Ast.FunctionCall)
                        {
                            var call = (NetLua.Ast.FunctionCall)statement;
                            if (call.Function is NetLua.Ast.Variable)
                            {
                                var func = (NetLua.Ast.Variable)call.Function;

                                if (func.Name == "dofile")
                                {
                                    continue;
                                }
                            }
                        }

                        first_non_dofile = statement;
                        Logger.Debug($"Matched AST");
                        break;
                    }

                    if (first_non_dofile == null)
                    {
                        Logger.Warn("Failed to match AST - empty file? Inserting call at the very beginning");
                    }
                    else
                    {
                        offset = first_non_dofile.Span.Location.Position;
                    }
                }

                var event_caller = new StringBuilder();
                event_caller.AppendLine("local ommelrt = loadfile(\"data/ommel/ommelrt.lua\")()");
                event_caller.Append("ommelrt.run_event(\"");
                event_caller.Append(Event);
                event_caller.AppendLine($"\", \"{TargetFile}\")");

                string new_target_content = null;

                using (var target_file = new StreamReader(loader.ExpandTargetPathDefaulted(TargetFile))) {
                    using (var source_file = new StringReader(event_caller.ToString())) {
                        if (Event == "enter" || Event == "top" || Event == "init")
                        {
                            new_target_content = InsertAtPos(target_file, offset, source_file, false);
                        }
                        else if (Event == "leave")
                        {
                            new_target_content  = target_file.ReadToEnd();
                            new_target_content += "\r\n";
                            new_target_content += source_file.ReadToEnd();
                        }
                    }
                }

                loader.DeleteFile(TargetFile);
                using (var f = new StreamWriter(loader.OpenWriteTarget(TargetFile))) {
                    f.Write(new_target_content);
                }
            }

            var target_source_file = loader.ConvertOmmeldataPath(SourceFile);

            event_files.Add(target_source_file);

            loader.RegisterNewFile(target_source_file);
            loader.DeleteFile(target_source_file);
            loader.CopyFile(mod, SourceFile, target_source_file);

            DoReplaceIfRequested(mod, loader, target_source_file);
        }
Exemplo n.º 13
0
 public override void OnExecute(Ommel loader, Mod mod)
 {
     Logger.Debug($"Stubbed.");
 }
Exemplo n.º 14
0
        private List <string> GetTargetFileList(Ommel loader)
        {
            if (TargetFile == null && TargetFiles == null && TargetPattern == null)
            {
                return(new List <string> {
                    "{datapath}"
                });
            }
            if (TargetFile != null)
            {
                return new List <string> {
                           TargetFile
                }
            }
            ;
            if (TargetFiles != null)
            {
                return(TargetFiles);
            }

            var l = new List <string>();
            var s = new HashSet <string>();

            var regex = new Regex(TargetPattern);

            Logger.Debug($"Target Pattern: {TargetPattern}");

            var target_ents = Directory.GetFiles(loader.NoitaPath, "*.*", SearchOption.AllDirectories);

            for (var i = 0; i < target_ents.Length; i++)
            {
                var start_idx = loader.NoitaPath.Length + 1;
                var data_ent  = target_ents[i].Substring(start_idx).Replace("\\", "/");
                if (!regex.IsMatch(data_ent))
                {
                    continue;
                }
                var skip = false;
                if (TargetExceptionRules != null)
                {
                    for (var j = 0; j < TargetExceptionRules.Count; j++)
                    {
                        if (TargetExceptionRules[j].IsMatch(data_ent))
                        {
                            Logger.Debug($"  Match (target, rejected): {data_ent}");
                            skip = true;
                        }
                    }
                }
                if (skip)
                {
                    continue;
                }
                Logger.Debug($"  Match (target): {data_ent}");
                loader.CheckDataEntry(data_ent);

                l.Add(data_ent);
                s.Add(data_ent);
            }

            var noita_ents = Directory.GetFiles(loader.NoitaPath, "*.*", SearchOption.AllDirectories);

            for (var i = 0; i < noita_ents.Length; i++)
            {
                var start_idx = loader.NoitaPath.Length + 1;
                var data_ent  = noita_ents[i].Substring(start_idx);
                if (!regex.IsMatch(data_ent))
                {
                    continue;
                }

                var skip = false;
                if (TargetExceptionRules != null)
                {
                    for (var j = 0; j < TargetExceptionRules.Count; j++)
                    {
                        if (TargetExceptionRules[j].IsMatch(data_ent))
                        {
                            Logger.Debug($"  Match (Noita, rejected): {data_ent}");
                            skip = true;
                        }
                    }
                }
                if (skip)
                {
                    continue;
                }
                if (s.Contains(data_ent))
                {
                    Logger.Debug($"  Ignored match: {data_ent}");
                    continue;
                }

                Logger.Debug($"  Glob match (Noita): {data_ent}");

                l.Add(data_ent);
                s.Add(data_ent);
            }

            return(l);
        }
Exemplo n.º 15
0
        public override void OnExecute(Ommel loader, Mod mod)
        {
            var param_counter = 0;

            if (Line != null)
            {
                param_counter += 1;
            }
            if (Offset != null)
            {
                param_counter += 1;
            }
            if (param_counter == 0)
            {
                throw new Exception($"Missing location parameter - choose either 'Line' or 'Offset'");
            }
            if (param_counter > 1)
            {
                throw new Exception("Too many parameters given to TextInsert - choose either 'Line' or 'Offset'");
            }

            loader.RegisterModifiedFile(TargetFile);
            var source_path = mod.GetFile(SourceFile);

            StringBuilder s = new StringBuilder();

            string data_to_be_inserted = null;

            using (var source_file = new StreamReader(File.OpenRead(source_path))) {
                data_to_be_inserted = source_file.ReadToEnd();
                if (Trim)
                {
                    data_to_be_inserted = data_to_be_inserted.Trim();
                }
            }


            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;
            }
            else if (string_map != null && Placeholders != null)
            {
                data_to_be_inserted = mod.ReplaceStrings(string_map, Placeholders.ToArray(), mod.Ommeldata.PlaceholderPrefix, mod.Ommeldata.PlaceholderSuffix, data_to_be_inserted);
            }

            if (Offset != null)
            {
                using (var target_file = new StreamReader(File.OpenRead(loader.ExpandTargetPathDefaulted(TargetFile)))) {
                    var target_pre_data = new char[Offset.Value];
                    target_file.Read(target_pre_data, 0, Offset.Value);
                    s.Append(target_pre_data);
                    s.Append(data_to_be_inserted);
                    s.Append(target_file.ReadToEnd());
                }
            }
            else if (Line != null)
            {
                using (var target_file = new StreamReader(File.OpenRead(loader.ExpandTargetPathDefaulted(TargetFile)))) {
                    var line_idx = 1;
                    while (!target_file.EndOfStream)
                    {
                        if (line_idx == Line.Value)
                        {
                            s.Append(data_to_be_inserted);
                        }
                        s.AppendLine(target_file.ReadLine());
                        line_idx += 1;
                    }
                }
            }

            loader.DeleteFile(TargetFile);

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