/// <summary> /// Executes the binary plugin edit and changes the file at the specified offset. /// </summary> /// <param name="extractionFolder">The extraction folder, this should be <see cref="ScriptReturnData.PluginsFolder"/></param> /// <param name="outputPath">The output path.</param> /// <exception cref="Exception">Offset is greater than length of file</exception> public void ExecuteEdit(string extractionFolder, string outputPath) { var filePath = PluginFile.CopyToOutput(extractionFolder, outputPath); using var fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); using var bw = new BinaryWriter(fs, Encoding.UTF8); if (Offset > bw.BaseStream.Length) { throw new Exception("Offset is greater than length of file stream!"); } bw.BaseStream.Position = Offset; if (ValueType == typeof(byte)) { bw.Write((byte)Value); } else if (ValueType == typeof(short)) { bw.Write((short)Value); } else if (ValueType == typeof(int)) { bw.Write((int)Value); } else if (ValueType == typeof(long)) { bw.Write((long)Value); } else if (ValueType == typeof(float)) { bw.Write((float)Value); } }
/// <summary> /// Executes the edit and changes the XML file. This function will copy the file to the output folder and then /// either replace <see cref="Find"/> with <see cref="Replace"/> if <see cref="IsReplace"/> or change line /// <see cref="Line"/> with <see cref="Value"/> if <see cref="IsEditLine"/>. /// </summary> /// <param name="extractionFolder">Path to the extraction folder. This should be <see cref="ScriptReturnData.DataFolder"/>.</param> /// <param name="outputFolder">Path to the output folder.</param> /// <exception cref="OBMMScriptHandlerException">Line <see cref="Line"/> does not exist</exception> public void ExecuteEdit(string extractionFolder, string outputFolder) { var path = File.CopyToOutput(extractionFolder, outputFolder); if (IsReplace) { var contents = System.IO.File.ReadAllText(path, Encoding.UTF8); contents = contents.Replace(Find, Replace); System.IO.File.WriteAllText(path, contents, Encoding.UTF8); } else { var lines = System.IO.File.ReadAllLines(path, Encoding.UTF8); if (Line < 0 || Line >= lines.Length) { throw new OBMMScriptHandlerException($"Unable to edit XML {path}, line {Line} does not exist!"); } lines[Line] = Value; System.IO.File.WriteAllLines(path, lines, Encoding.UTF8); } }