2020-04-26 13:13:10 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2019-11-20 00:15:46 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public class StatusFileStream : Stream
|
|
|
|
|
{
|
|
|
|
|
private string _message;
|
2019-11-22 05:19:42 +00:00
|
|
|
|
private Stream _inner;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
private WorkQueue? _queue;
|
2020-04-26 13:13:10 +00:00
|
|
|
|
private DateTime _lastUpdate;
|
2020-06-02 02:18:32 +00:00
|
|
|
|
private TimeSpan _span;
|
2019-11-20 00:15:46 +00:00
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public StatusFileStream(Stream fs, string message, WorkQueue? queue = null)
|
2019-11-20 00:15:46 +00:00
|
|
|
|
{
|
2020-01-18 22:09:32 +00:00
|
|
|
|
_queue = queue;
|
2019-11-20 00:15:46 +00:00
|
|
|
|
_inner = fs;
|
|
|
|
|
_message = message;
|
2020-04-26 13:13:10 +00:00
|
|
|
|
_lastUpdate = DateTime.UnixEpoch;
|
2020-09-06 03:19:05 +00:00
|
|
|
|
_span = TimeSpan.FromMilliseconds(100);
|
2019-11-20 00:15:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Flush()
|
|
|
|
|
{
|
|
|
|
|
_inner.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
return _inner.Seek(offset, origin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
{
|
|
|
|
|
_inner.SetLength(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
UpdateStatus();
|
|
|
|
|
return _inner.Read(buffer, offset, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateStatus()
|
|
|
|
|
{
|
2020-06-02 02:18:32 +00:00
|
|
|
|
if (DateTime.Now - _lastUpdate < _span)
|
2020-04-26 13:13:10 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_lastUpdate = DateTime.Now;
|
|
|
|
|
|
2020-01-18 22:09:32 +00:00
|
|
|
|
if (_inner.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_queue != null)
|
2020-02-08 04:35:08 +00:00
|
|
|
|
_queue.Report(_message, Percent.FactoryPutInRange(_inner.Position, _inner.Length));
|
2020-01-18 22:09:32 +00:00
|
|
|
|
else
|
2020-02-08 04:35:08 +00:00
|
|
|
|
Utils.Status(_message, Percent.FactoryPutInRange(_inner.Position, _inner.Length));
|
2019-11-20 00:15:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
UpdateStatus();
|
|
|
|
|
_inner.Write(buffer, offset, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanRead => _inner.CanRead;
|
|
|
|
|
public override bool CanSeek => _inner.CanSeek;
|
|
|
|
|
public override bool CanWrite => _inner.CanWrite;
|
|
|
|
|
public override long Length => _inner.Length;
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => _inner.Position;
|
|
|
|
|
set => _inner.Position = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|