Repository navigation
Expand file tree
/
Copy pathUniPyLogStream.cs
More file actions
72 lines (58 loc) · 2.21 KB
/
Copy pathUniPyLogStream.cs
File metadata and controls
72 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.IO;
using System.Text;
using UnityEngine;
namespace Exodrifter.UnityPython
{
public class UniPyLogStream : Stream
{
private static void ThrowNotSupport() => throw new NotSupportedException("Do not support read operation.");
private Action<string> m_logger;
private Byte[] m_buf_swap = Array.Empty<Byte>();
public UniPyLogStream(Action<string> logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
m_logger = logger;
}
public override void Flush() {}
public override int Read(byte[] buffer, int offset, int count)
{
ThrowNotSupport();
return -1;
}
public override long Seek(long offset, SeekOrigin origin)
{
ThrowNotSupport();
return -1;
}
public override void SetLength(long value)
{
ThrowNotSupport();
}
public override void Write(byte[] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer));
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), "offset should not be negative");
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), "count should not be negative");
if (offset >= buffer.Length)
throw new ArgumentException("offset is greater than length of buffer");
if (offset + count > buffer.Length)
throw new ArgumentException("offset + count is greater than length of buffer");
Array.Resize(ref m_buf_swap, count);
Array.Copy(buffer, offset, m_buf_swap, 0, count);
var str = Encoding.UTF8.GetString(m_buf_swap);
m_logger.Invoke(str);
}
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length { get; }
public override long Position { get => -1; set => ThrowNotSupport(); }
}
}