126 lines
4 KiB
C#
126 lines
4 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using TeaBox.Macros;
|
|
using JD.Core;
|
|
using JD.Core.ProjectManaging;
|
|
|
|
namespace JDSongConverter.Tasks
|
|
{
|
|
public class ReplaceEntry
|
|
{
|
|
public ReplaceEntry()
|
|
{
|
|
RegexOptions = RegexOptions.IgnoreCase;
|
|
}
|
|
public string Pattern { get; set; }
|
|
public string Replacement { get; set; }
|
|
public RegexOptions RegexOptions { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Makes a copy of a file, and also replace in specific files path from input directory with path to output directory.
|
|
/// It is also possible to add some other stuff to replace.
|
|
/// </summary>
|
|
class CopyFileTask : Task
|
|
{
|
|
private string _name = "Copy file";
|
|
private string InputFile { get; set; }
|
|
private string OutputFile { get; set; }
|
|
|
|
readonly string[] _txtExtensions =
|
|
{
|
|
".asc",".skl",".pbk",".anm", ".tpl", ".fcg", ".mcg",
|
|
".ilu", ".lua", ".tbl", ".tbll", ".isc", ".act",
|
|
".tape", ".dtape", ".ktape", ".btape", ".stape"
|
|
};
|
|
|
|
private List<ReplaceEntry> _replaceEntries = new List<ReplaceEntry>();
|
|
|
|
public List<ReplaceEntry> ReplaceEntries
|
|
{
|
|
get { return _replaceEntries; }
|
|
set { _replaceEntries = value; }
|
|
}
|
|
|
|
|
|
public override string Name
|
|
{
|
|
get { return _name; }
|
|
set { _name = value; }
|
|
}
|
|
|
|
public override string ProgressText
|
|
{
|
|
get { return string.Format("Copying file {0}", InputFile); }
|
|
}
|
|
|
|
public CopyFileTask(string inputFile, string outputFile)
|
|
{
|
|
Init(inputFile, outputFile);
|
|
}
|
|
|
|
public CopyFileTask()
|
|
{
|
|
}
|
|
|
|
public void InitWithoutReplaceEntries(string inputFile, string outputFile)
|
|
{
|
|
InputFile = inputFile;
|
|
OutputFile = outputFile;
|
|
}
|
|
|
|
public void Init(string inputFile, string outputFile)
|
|
{
|
|
InputFile = inputFile;
|
|
OutputFile = outputFile;
|
|
ReplaceEntries.Clear();
|
|
|
|
string inputFileRelative;
|
|
var inputProject = ProjectManager.Instance.GetProjectFromPath(InputFile);
|
|
if (inputProject == null)
|
|
{
|
|
inputFileRelative = Path.GetDirectoryName(InputFile);
|
|
}
|
|
else
|
|
{
|
|
DataPath.ExtractRelativePath(inputProject.DataPath, Path.GetDirectoryName(InputFile), out inputFileRelative);
|
|
}
|
|
|
|
string outputFileRelative;
|
|
DataRoot.ExtractRelativePath(Path.GetDirectoryName(OutputFile), out outputFileRelative);
|
|
|
|
ReplaceEntries.Add(new ReplaceEntry { Pattern = inputFileRelative, Replacement = outputFileRelative, RegexOptions = RegexOptions.IgnoreCase });
|
|
}
|
|
|
|
public override TaskResult Process(IExecutionContext executionContext)
|
|
{
|
|
if (!System.IO.File.Exists(InputFile))
|
|
return TaskResult.FileMissing;
|
|
|
|
var fileExtension = Path.GetExtension(InputFile) ?? "";
|
|
fileExtension = fileExtension.ToLower();
|
|
|
|
if (System.IO.File.Exists(OutputFile))
|
|
{
|
|
System.IO.File.SetAttributes(OutputFile, FileAttributes.Normal);
|
|
}
|
|
System.IO.File.Copy(InputFile, OutputFile, true);
|
|
|
|
System.IO.File.SetAttributes(OutputFile, FileAttributes.Normal);
|
|
|
|
if (_txtExtensions.Contains(fileExtension))
|
|
{
|
|
string text = System.IO.File.ReadAllText(InputFile);
|
|
foreach (var replaceEntry in ReplaceEntries)
|
|
{
|
|
text = Regex.Replace(text, replaceEntry.Pattern, replaceEntry.Replacement, replaceEntry.RegexOptions);
|
|
}
|
|
|
|
System.IO.File.WriteAllText(OutputFile, text);
|
|
}
|
|
return TaskResult.Ok;
|
|
}
|
|
}
|
|
}
|