全UIテキストをハードコードされた日本語文字列からTranslateクラス経由の多言語対応リソースへ完全移行しました。
YukkuriMovieMaker.Generatorをgitサブモジュールとして追加しソリューションファイルに組み込み、ソースジェネレータによるローカライズクラス自動生成の基盤を整えました。
ZeroDiskProxy/Localization/ディレクトリを新設し、[AutoGenLocalizer]属性を持つpartialクラスTranslateをTexts.csに定義しました。
.gitmodulesにYukkuriMovieMaker.GeneratorのGitHubリポジトリURLを登録し、サブモジュールとして管理します。
ZeroDiskProxy.slnxにYukkuriMovieMaker.Generatorプロジェクトを追加しました。
ProxyCacheManagerのプロキシ生成完了・キャンセル・失敗時のステータスメッセージをそれぞれTranslate.ProxyGenerationStatusCompleted・Translate.ProxyGenerationStatusCanceled・Translate.ProxyGenerationStatusFailedPrefixに置換しました。
ProxyGenerationItemの初期ステータス文字列をTranslate.ProxyGenerationStatusGeneratingに、格納先表示をTranslate.StorageMemory・Translate.StorageDiskに置換しました。
ZeroDiskProxySettings.NameプロパティをリテラルからTranslate.SettingsNameに変更しました。
SettingsViewModelの初期キャッシュ概要テキストをTranslate.CacheSummaryLoadingに変更しました。
キャッシュ削除ダイアログのメッセージをstring.Concatからstring.Format(Translate.CacheNoEntriesToDelete)およびstring.Format(Translate.CacheDeleteSummaryFormat, ...)に変更しました。
ダイアログタイトルをリテラルからTranslate.AppNameに変更しました。
プラグイン未初期化メッセージ・キャッシュ件数表示・メモリおよびディスク使用量表示・割当済メモリ表示をいずれもstring.FormatとTranslateキーに置き換えました。
CacheEntryViewModelのストレージ種別表示をTranslate.StorageMemory・Translate.StorageDiskに置換しました。
GenerationPopupView.xamlにxmlns:local名前空間宣言を追加し、ウィンドウタイトル・ヘッダ文言・空状態メッセージをすべて{x:Static local:Translate.XXX}バインディングに変更しました。
ZeroDiskProxySettingsView.xamlにxmlns:local名前空間宣言を追加し、設定カード説明文・全Expanderヘッダ・全ラベル・全ツールチップ・全ボタン文言・キャッシュ一覧ヘッダおよび空状態メッセージをすべて{x:Static local:Translate.XXX}バインディングに変更しました。
146 行
4.3 KiB
C#
146 行
4.3 KiB
C#
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace ZeroDiskProxy.Memory;
|
|
|
|
internal sealed class FallbackStream : Stream
|
|
{
|
|
private Stream _inner;
|
|
private readonly MemoryBudget _budget;
|
|
private readonly string _fallbackDir;
|
|
private string? _fallbackPath;
|
|
private volatile bool _isFallenBack;
|
|
private int _disposed;
|
|
private readonly object _switchLock = new();
|
|
|
|
internal bool IsFallenBack => _isFallenBack;
|
|
|
|
internal FallbackStream(MemoryBudget budget, string fallbackDirectory, int initialCapacity = 4096)
|
|
{
|
|
_budget = budget;
|
|
_fallbackDir = fallbackDirectory;
|
|
_inner = new MemoryStream(initialCapacity);
|
|
}
|
|
|
|
public override bool CanRead => _inner.CanRead;
|
|
public override bool CanSeek => _inner.CanSeek;
|
|
public override bool CanWrite => !IsDisposed && _inner.CanWrite;
|
|
public override long Length => _inner.Length;
|
|
|
|
public override long Position
|
|
{
|
|
get => _inner.Position;
|
|
set => _inner.Position = value;
|
|
}
|
|
|
|
private bool IsDisposed => Volatile.Read(ref _disposed) != 0;
|
|
|
|
public override void Flush() => _inner.Flush();
|
|
public override int Read(byte[] buffer, int offset, int count) => _inner.Read(buffer, offset, count);
|
|
public override long Seek(long offset, SeekOrigin origin) => _inner.Seek(offset, origin);
|
|
public override void SetLength(long value) => _inner.SetLength(value);
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
if (!_isFallenBack && !_budget.CanAllocateInMemory(_inner.Position + count))
|
|
SwitchToDisk();
|
|
_inner.Write(buffer, offset, count);
|
|
}
|
|
|
|
internal byte[] ToArray()
|
|
{
|
|
lock (_switchLock)
|
|
{
|
|
if (_inner is MemoryStream ms)
|
|
return ms.ToArray();
|
|
|
|
var pos = _inner.Position;
|
|
_inner.Position = 0;
|
|
var result = new byte[_inner.Length];
|
|
int totalRead = 0;
|
|
while (totalRead < result.Length)
|
|
{
|
|
int read = _inner.Read(result, totalRead, result.Length - totalRead);
|
|
if (read == 0)
|
|
break;
|
|
totalRead += read;
|
|
}
|
|
_inner.Position = pos;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
internal string? TransferDiskOwnership()
|
|
{
|
|
lock (_switchLock)
|
|
{
|
|
if (!_isFallenBack || _fallbackPath is null)
|
|
return null;
|
|
|
|
var path = _fallbackPath;
|
|
_fallbackPath = null;
|
|
return path;
|
|
}
|
|
}
|
|
|
|
private void SwitchToDisk()
|
|
{
|
|
lock (_switchLock)
|
|
{
|
|
if (_isFallenBack)
|
|
return;
|
|
|
|
try
|
|
{
|
|
Directory.CreateDirectory(_fallbackDir);
|
|
var path = Path.Combine(_fallbackDir, string.Concat("zdp_", Guid.NewGuid().ToString("N"), ".tmp"));
|
|
var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 65536);
|
|
|
|
var oldStream = _inner;
|
|
var position = oldStream.Position;
|
|
oldStream.Position = 0;
|
|
oldStream.CopyTo(fileStream);
|
|
fileStream.Position = position;
|
|
|
|
var memLength = oldStream.Length;
|
|
_inner = fileStream;
|
|
_fallbackPath = path;
|
|
_isFallenBack = true;
|
|
|
|
oldStream.Dispose();
|
|
_budget.RecordDeallocation(memLength);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine(string.Concat("[FallbackStream] Disk fallback failed: ", ex.Message));
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
|
return;
|
|
|
|
if (disposing)
|
|
{
|
|
lock (_switchLock)
|
|
{
|
|
var length = _inner.Length;
|
|
_inner.Dispose();
|
|
|
|
if (!_isFallenBack)
|
|
_budget.RecordDeallocation(length);
|
|
|
|
if (_fallbackPath is not null)
|
|
{
|
|
try { File.Delete(_fallbackPath); }
|
|
catch { }
|
|
_fallbackPath = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
base.Dispose(disposing);
|
|
}
|
|
} |