Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DesigntimeLicenseContextSerializer.cs
Go to the documentation of this file.
2using System.IO;
5using System.Text;
6
8
10{
11 private class StreamWrapper : Stream
12 {
13 private Stream _stream;
14
15 private bool _readFirstByte;
16
17 internal byte _firstByte;
18
19 public override bool CanRead => _stream.CanRead;
20
21 public override bool CanSeek => _stream.CanSeek;
22
23 public override bool CanWrite => _stream.CanWrite;
24
25 public override long Length => _stream.Length;
26
27 public override long Position
28 {
29 get
30 {
31 return _stream.Position;
32 }
33 set
34 {
36 }
37 }
38
40 {
42 _readFirstByte = false;
43 _firstByte = 0;
44 }
45
46 public override void Flush()
47 {
48 _stream.Flush();
49 }
50
51 public override int Read(byte[] buffer, int offset, int count)
52 {
53 if (_stream.Position == 1)
54 {
56 return _stream.Read(buffer, offset + 1, count - 1) + 1;
57 }
58 return _stream.Read(buffer, offset, count);
59 }
60
61 public override long Seek(long offset, SeekOrigin origin)
62 {
63 return _stream.Seek(offset, origin);
64 }
65
66 public override void SetLength(long value)
67 {
69 }
70
71 public override void Write(byte[] buffer, int offset, int count)
72 {
74 }
75
76 public override int ReadByte()
77 {
78 byte result = (_firstByte = (byte)_stream.ReadByte());
79 _readFirstByte = true;
80 return result;
81 }
82 }
83
84 private static bool EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization { get; } = AppContext.TryGetSwitch("System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization", out var isEnabled) && isEnabled;
85
86
87 public static void Serialize(Stream o, string cryptoKey, DesigntimeLicenseContext context)
88 {
90 {
92 return;
93 }
95 binaryWriter.Write(byte.MaxValue);
98 foreach (DictionaryEntry savedLicenseKey in context._savedLicenseKeys)
99 {
100 binaryWriter.Write(savedLicenseKey.Key.ToString());
101 binaryWriter.Write(savedLicenseKey.Value.ToString());
102 }
103 }
104
106 {
108 formatter.Serialize(o, new object[2] { cryptoKey, context._savedLicenseKeys });
109 }
110
112 {
113 if (stream.ReadByte() != 0)
114 {
115 return false;
116 }
117 return true;
118 }
119
121 {
123 {
125 object obj = formatter.Deserialize(wrappedStream);
126 if (obj is object[] array && array[0] is string && (string)array[0] == cryptoKey)
127 {
129 }
130 return;
131 }
133 }
134
135 internal static void Deserialize(Stream o, string cryptoKey, RuntimeLicenseContext context)
136 {
139 {
141 return;
142 }
144 byte firstByte = streamWrapper._firstByte;
145 string text = binaryReader.ReadString();
146 int num = binaryReader.ReadInt32();
147 if (text == cryptoKey)
148 {
149 context._savedLicenseKeys.Clear();
150 for (int i = 0; i < num; i++)
151 {
152 string key = binaryReader.ReadString();
153 string value = binaryReader.ReadString();
154 context._savedLicenseKeys.Add(key, value);
155 }
156 }
157 }
158}
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static void SerializeWithBinaryFormatter(Stream o, string cryptoKey, DesigntimeLicenseContext context)
static void Deserialize(Stream o, string cryptoKey, RuntimeLicenseContext context)
static void DeserializeUsingBinaryFormatter(StreamWrapper wrappedStream, string cryptoKey, RuntimeLicenseContext context)
static void Serialize(Stream o, string cryptoKey, DesigntimeLicenseContext context)
virtual string ReadString()
virtual void Write(bool value)
virtual int ReadByte()
Definition Stream.cs:994
void SetLength(long value)
long Seek(long offset, SeekOrigin origin)
int Read(byte[] buffer, int offset, int count)
void Write(byte[] buffer, int offset, int count)
static string BinaryFormatterMessage
Definition SR.cs:132
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526