/// <summary> /// 棋譜を読み取ります。 /// </summary> /// <param name="inputFile">読み取る棋譜ファイル。</param> /// <param name="outputFile">出力先ファイル。</param> /// <returns>リターン コード。</returns> public static int ConvertAnyFileToRpm(TraceableFile inputFile, TraceableFile outputFile) { ProcessStartInfo info = new ProcessStartInfo(); // 起動する実行ファイルのパスを設定する info.FileName = LocationMaster.Kw29Exe.FullName; info.WorkingDirectory = LocationMaster.Kw29Opt.FullName; // コマンドライン引数を指定する info.Arguments = $@"--input ""{inputFile.FullName.Replace(@"\", "/")}"" --output ""{Path.Combine(Directory.GetParent(outputFile.FullName).FullName, Path.GetFileNameWithoutExtension(outputFile.FullName)).Replace(@"\", "/")}"""; // コンソール・ウィドウを開かない。 info.CreateNoWindow = true; // シェル機能を使用しない info.UseShellExecute = false; Trace.WriteLine($"{LogHelper.Stamp}Go : Process {info.FileName} {info.Arguments}"); var p = Process.Start(info); // タイムアウト時間(秒)。1棋譜に 1分も かからないだろう。 p.WaitForExit(60 * 1000); var returnCode = p.ExitCode; Trace.WriteLine($"{LogHelper.Stamp}Returned: Process returnCode: {returnCode}."); return(returnCode); }
/// <summary> /// Shift-JIS から UTF-8 に変換します。 /// </summary> /// <param name="inputFile">棋譜のテキストファイル。圧縮ファイルではなく。</param> /// <param name="outputDirectory">保存先ディレクトリー。</param> public void ExecuteSjisToU8(TraceableFile inputFile, TraceableDirectory outputDirectory) { // エンコーディング変換後の棋譜の出力先テキストファイル。 var(parentDirectory, stem, extensionWithDot) = PathHelper.DestructFileName(inputFile.FullName); var outputFile = new TraceableFile(PathHelper.Combine(outputDirectory.FullName, string.Concat(stem, "[SJ-U8]", extensionWithDot))); byte[] bytesData; // ファイルをbyte形で全て読み込み using (FileStream fs1 = new FileStream(inputFile.FullName, FileMode.Open)) { byte[] data = new byte[fs1.Length]; fs1.Read(data, 0, data.Length); fs1.Close(); // Shift-JIS -> UTF-8 変換(byte形) string sjisstr = Encoding.GetEncoding("Shift_JIS").GetString(data); bytesData = System.Text.Encoding.UTF8.GetBytes(sjisstr); } // 出力ファイル Trace.WriteLine($"{LogHelper.Stamp}Write : {outputFile.FullName}"); using (FileStream fs2 = new FileStream(outputFile.FullName, FileMode.Create)) { // 書き込み設定(デフォルトはUTF-8) BinaryWriter bw = new BinaryWriter(fs2); // 出力ファイルへ全て書き込み bw.Write(bytesData); bw.Close(); fs2.Close(); } }
/// <summary> /// Initializes a new instance of the <see cref="UnexpectedFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public UnexpectedFile(TraceableFile expansionGoFile) : base(expansionGoFile) { // Trace.WriteLine($"Unexpected file: {this.ExpansionGoFilePath}"); // .exe とか解凍できないやつが入っている☆(^~^)! // Trace.WriteLine($"むり: {this.ExpansionGoFilePath}"); }
/// <summary> /// 名前を変えて、そのまま保存します。 /// </summary> /// <param name="inputFile">棋譜のテキストファイル。圧縮ファイルではなく。</param> /// <param name="outputDirectory">保存先ディレクトリー。</param> public void ExecuteAsIt(TraceableFile inputFile, TraceableDirectory outputDirectory) { // エンコーディング変換後の棋譜の出力先テキストファイル。 var(parentDirectory, stem, extensionWithDot) = PathHelper.DestructFileName(inputFile.FullName); var outputFile = new TraceableFile(PathHelper.Combine(outputDirectory.FullName, string.Concat(stem, "[-]", extensionWithDot))); // 移動ではなくコピー。 inputFile.Copy(outputFile, true); }
/// <summary> /// 親ディレクトリーの名前と、主体のファイル名を $% でくっつける。 /// </summary> /// <param name="file">ファイル名。</param> private static void DeleteParentDirectoryByMerge(TraceableFile file) { // 親ディレクトリーの名前と、主体のファイル名を $% でくっつける。 var joinedName = $"{Directory.GetParent(file.FullName).Name}$%{Path.GetFileName(file.FullName)}"; // 親の親ディレクトリーのフル名。(なければ例外) var parentParentDirectory = Directory.GetParent(Directory.GetParent(file.FullName).FullName).FullName; // 親の親ディレクトリーの下に さっきくっつけた名前。衝突したら Move が例外を投げる。 Trace.WriteLine($"{LogHelper.Stamp}parentParentDirectory: '{parentParentDirectory}', joinedName: '{joinedName}'."); var destination = new TraceableFile(PathHelper.Combine(parentParentDirectory, joinedName)); // 古い名前から、新しい名前へ移動。 file.MoveTo(destination, true); }
/// <summary> /// Initializes a new instance of the <see cref="CsaFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> /// <param name="eatingGoFile">棋譜読取を待っているファイルパス。</param> public CsaFile(TraceableFile expansionGoFile, TraceableFile eatingGoFile) : base(expansionGoFile, eatingGoFile) { /* * // 解凍先ファイル。 * if (!string.IsNullOrWhiteSpace(this.ExpansionGoFile.FullName)) * { * // Trace.WriteLine($"Csa exp: {this.ExpansionGoFilePath}"); * * // そのままコピーすると名前がぶつかってしまう☆(^~^) * var wrappingDir = new TraceableDirectory(PathHelper.Combine(ExpansionOutputDirectory.Instance.FullName, $"copied-{Path.GetFileNameWithoutExtension(this.ExpansionGoFile.FullName)}")); * wrappingDir.Create(); * this.ExpansionOutputFile = new TraceableFile(PathHelper.Combine(wrappingDir.FullName, Path.GetFileName(this.ExpansionGoFile.FullName))); * } */ }
/// <summary> /// エンコーディング変換。 /// </summary> /// <param name="expandedFile">解凍済み棋譜のテキストファイル。</param> /// <returns>エンコーディング変換した。</returns> private static bool EncodingOfTextFile(TraceableFile expandedFile) { Trace.WriteLine($"{LogHelper.Stamp}Encode : {expandedFile.FullName}"); var(parentDirectory, stem, extensionWithDot) = PathHelper.DestructFileName(expandedFile.FullName); // Trace.WriteLine($"{LogHelper.Stamp}ParentDirectory={parentDirectory}, Stem={stem}, ExtensionWithDot={extensionWithDot}."); // 出力先ディレクトリー。 var outputDir = LocationMaster.EncodedDirectory; var encoded = false; switch (extensionWithDot.ToUpperInvariant()) { case ".CSA": case ".KIF": // TODO 両方試したい。 (1)変換なし (2)Shift-JIS -> UTF-8 変換 { // 名前を変えて、そのまま保存します。 new Encords().ExecuteAsIt(expandedFile, outputDir); // Shift-JIS と決めつけて、UTF8に変換する。 new Encords().ExecuteSjisToU8(expandedFile, outputDir); // 終わったファイルは消す。 try { expandedFile.Delete(); } catch (IOException e) { Trace.WriteLine(e); } } encoded = true; break; default: Trace.WriteLine("対象外。"); break; } Trace.WriteLine($"{LogHelper.Stamp}Encode : End."); return(encoded); }
static LocationMaster() { // このアプリケーション.exeと同じディレクトリに置いてある設定ファイル。 ExeConf = new TraceableFile(PathHelper.Combine(ZerothSettings.ExeDirectory, "./exe-config.json")); { var json = DynamicJson.Parse(LocationMaster.ExeConf.ReadAllText()); ExeConfJson = json.Deserialize <ExeConfigJson>(); } // ゲームエンジンの設定ファイル。 Kw29MasterConf = new TraceableFile(LocationMaster.ExeConfJson.kifuwarabe_wcsc29_master_config_path); { var json = DynamicJson.Parse(LocationMaster.Kw29MasterConf.ReadAllText()); Kw29MasterConfJson = json.Deserialize <KifuwarabeWcsc29MasterConfigJson>(); } // 入力ディレクトリー。 InputDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "input")); InputDirectory.Create(); // 解凍済みディレクトリー。 ExpandedDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "expanded")); ExpandedDirectory.Create(); // エンコーディング済みディレクトリー。 EncodedDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "encoded")); EncodedDirectory.Create(); // 変換済みの棋譜ディレクトリー。 ConvertedDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "converted")); ConvertedDirectory.Create(); // 棋譜を1つのファイルに詰め込んだファイルのディレクトリー。 JammedDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "jammed")); JammedDirectory.Create(); // 変換エラー出力ディレクトリー。 ErrorDirectory = new TraceableDirectory(PathHelper.Combine(LocationMaster.Kw29MasterConfJson.converter_var_lib, "error")); ErrorDirectory.Create(); }
/// <summary> /// Initializes a new instance of the <see cref="AbstractArchiveFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public AbstractArchiveFile(TraceableFile expansionGoFile) : base(expansionGoFile) { }
/// <summary> /// Initializes a new instance of the <see cref="TargzFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public TargzFile(TraceableFile expansionGoFile) : base(expansionGoFile) { // Trace.WriteLine($"TarGz: {this.ExpansionGoFilePath}"); }
/// <summary> /// Initializes a new instance of the <see cref="AbstractFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> protected AbstractFile(TraceableFile expansionGoFile) { this.InputFile = expansionGoFile; }
/// <summary> /// Initializes a new instance of the <see cref="ZipArchiveFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public ZipArchiveFile(TraceableFile expansionGoFile) : base(expansionGoFile) { // Trace.WriteLine($"Zip: {this.ExpansionGoFilePath}"); }
/// <summary> /// 少し解凍。 /// </summary> /// <returns>ループが回った回数。</returns> public static int ExpandLittleIt() { Trace.WriteLine($"{LogHelper.Stamp}Expand : Start..."); // 指定ディレクトリ以下のファイルをすべて取得する IEnumerable <string> expansionGoFiles = System.IO.Directory.EnumerateFiles( LocationMaster.InputDirectory.FullName, "*", System.IO.SearchOption.AllDirectories); Rest = 0; // 圧縮ファイルを 3つ 解凍する var count = 0; foreach (string expansionGoFileFullName in expansionGoFiles) { var expansionGoFile = new TraceableFile(expansionGoFileFullName); if (count > 3) { break; } AbstractFile anyFile; switch (Path.GetExtension(expansionGoFile.FullName).ToUpperInvariant()) { case ".7Z": anyFile = new SevenZipFile(expansionGoFile); break; case ".CSA": anyFile = new CsaFile(expansionGoFile, new TraceableFile(string.Empty)); break; case ".KIF": anyFile = new KifFile(expansionGoFile, new TraceableFile(string.Empty)); break; case ".LZH": anyFile = new LzhFile(expansionGoFile); break; case ".TGZ": anyFile = new TargzFile(expansionGoFile); break; case ".ZIP": anyFile = new ZipArchiveFile(expansionGoFile); break; default: anyFile = new UnexpectedFile(expansionGoFile); Rest++; break; } // 解凍する。 if (anyFile.Expand()) { } count++; } // Trace.WriteLine($"{LogHelper.Stamp}むり1: {Rest}"); return(count); }
/// <summary> /// Initializes a new instance of the <see cref="SevenZipFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public SevenZipFile(TraceableFile expansionGoFile) : base(expansionGoFile) { // Trace.WriteLine($"7zip: {this.ExpansionGoFilePath}"); // TODO this.ExpansionGoFilePath = this.ExpansionGoFilePath.Replace(@"\", "/"); }
/// <summary> /// Initializes a new instance of the <see cref="AbstractGameRecordFile"/> class. /// </summary> /// <param name="inputFile">解凍を待っているファイル。</param> /// <param name="encodedFile">棋譜読取を待っているファイルパス。</param> public AbstractGameRecordFile(TraceableFile inputFile, TraceableFile encodedFile) : base(inputFile) { this.EncodedFile = encodedFile; }
/// <summary> /// Initializes a new instance of the <see cref="LzhFile"/> class. /// </summary> /// <param name="expansionGoFile">解凍を待っているファイル。</param> public LzhFile(TraceableFile expansionGoFile) : base(expansionGoFile) { // Trace.WriteLine($"Lzh: {this.ExpansionGoFilePath}"); }