forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamAppPropertyHelper.cs
More file actions
127 lines (119 loc) · 4.99 KB
/
SteamAppPropertyHelper.cs
File metadata and controls
127 lines (119 loc) · 4.99 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Application.Models;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
namespace System.Application
{
public static class SteamAppPropertyHelper
{
internal static string ReadAppInfoString(this BinaryReader reader)
{
byte[] buffer;
int count;
using (MemoryStream memoryStream = new MemoryStream())
{
byte value;
while ((value = reader.ReadByte()) != 0)
{
memoryStream.WriteByte(value);
}
buffer = memoryStream.GetBuffer();
count = (int)memoryStream.Length;
}
return Encoding.UTF8.GetString(buffer, 0, count);
}
internal static string ReadAppInfoWideString(this BinaryReader reader)
{
StringBuilder stringBuilder = new StringBuilder();
for (char c = (char)reader.ReadUInt16(); c != 0; c = (char)reader.ReadUInt16())
{
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
internal static void WriteAppInfoString(this BinaryWriter writer, string str)
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
writer.Write(bytes);
writer.Write((byte)0);
}
internal static void WriteAppInfoWideString(this BinaryWriter writer, string str)
{
byte[] bytes = Encoding.Unicode.GetBytes(str);
writer.Write(bytes);
writer.Write((byte)0);
}
internal static Color ReadColor(this BinaryReader reader)
{
byte red = reader.ReadByte();
byte green = reader.ReadByte();
byte blue = reader.ReadByte();
return Color.FromArgb(red, green, blue);
}
internal static void Write(this BinaryWriter writer, Color color)
{
writer.Write(color.R);
writer.Write(color.G);
writer.Write(color.B);
}
internal static void Write(this BinaryWriter writer, SteamAppPropertyTable table)
{
foreach (SteamAppProperty property in table.Properties)
{
writer.Write((byte)property.PropertyType);
writer.WriteAppInfoString(property.Name);
switch (property.PropertyType)
{
case SteamAppPropertyType.Table:
writer.Write((SteamAppPropertyTable)property.Value);
break;
case SteamAppPropertyType.String:
writer.WriteAppInfoString((string)property.Value);
break;
case SteamAppPropertyType.WString:
writer.WriteAppInfoWideString((string)property.Value);
break;
case SteamAppPropertyType.Int32:
writer.Write((int)property.Value);
break;
case SteamAppPropertyType.Uint64:
writer.Write((ulong)property.Value);
break;
case SteamAppPropertyType.Float:
writer.Write((float)property.Value);
break;
case SteamAppPropertyType.Color:
writer.Write((Color)property.Value);
break;
default:
throw new NotImplementedException("The value type " + property.PropertyType.ToString() + " has not been implemented.");
}
}
writer.Write((byte)8);
}
internal static SteamAppPropertyTable ReadPropertyTable(this BinaryReader reader)
{
SteamAppPropertyTable propertyTable = new SteamAppPropertyTable();
SteamAppPropertyType propertyType;
while ((propertyType = (SteamAppPropertyType)reader.ReadByte()) != SteamAppPropertyType._EndOfTable_)
{
string name = reader.ReadAppInfoString();
object obj = null;
propertyTable.AddPropertyValue(value: propertyType switch
{
SteamAppPropertyType.Table => reader.ReadPropertyTable(),
SteamAppPropertyType.String => reader.ReadAppInfoString(),
SteamAppPropertyType.WString => reader.ReadAppInfoWideString(),
SteamAppPropertyType.Int32 => reader.ReadInt32(),
SteamAppPropertyType.Uint64 => reader.ReadUInt64(),
SteamAppPropertyType.Float => reader.ReadSingle(),
SteamAppPropertyType.Color => reader.ReadColor(),
_ => throw new NotImplementedException("The property type " + propertyType.ToString() + " has not been implemented."),
}, name: name, type: propertyType);
}
return propertyTable;
}
}
}