mirror of
https://github.com/JDM170/SteamAchievementManager
synced 2025-12-10 05:37:18 +07:00
116 lines
3.6 KiB
C#
116 lines
3.6 KiB
C#
/* Copyright (c) 2019 Rick (rick 'at' gibbed 'dot' us)
|
|
*
|
|
* This software is provided 'as-is', without any express or implied
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
* arising from the use of this software.
|
|
*
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
* including commercial applications, and to alter it and redistribute it
|
|
* freely, subject to the following restrictions:
|
|
*
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
* claim that you wrote the original software. If you use this software
|
|
* in a product, an acknowledgment in the product documentation would
|
|
* be appreciated but is not required.
|
|
*
|
|
* 2. Altered source versions must be plainly marked as such, and must not
|
|
* be misrepresented as being the original software.
|
|
*
|
|
* 3. This notice may not be removed or altered from any source
|
|
* distribution.
|
|
*/
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace SAM.Game
|
|
{
|
|
internal static class StreamHelpers
|
|
{
|
|
public static byte ReadValueU8(this Stream stream)
|
|
{
|
|
return (byte)stream.ReadByte();
|
|
}
|
|
|
|
public static int ReadValueS32(this Stream stream)
|
|
{
|
|
var data = new byte[4];
|
|
int read = stream.Read(data, 0, 4);
|
|
Debug.Assert(read == 4);
|
|
return BitConverter.ToInt32(data, 0);
|
|
}
|
|
|
|
public static uint ReadValueU32(this Stream stream)
|
|
{
|
|
var data = new byte[4];
|
|
int read = stream.Read(data, 0, 4);
|
|
Debug.Assert(read == 4);
|
|
return BitConverter.ToUInt32(data, 0);
|
|
}
|
|
|
|
public static ulong ReadValueU64(this Stream stream)
|
|
{
|
|
var data = new byte[8];
|
|
int read = stream.Read(data, 0, 8);
|
|
Debug.Assert(read == 8);
|
|
return BitConverter.ToUInt64(data, 0);
|
|
}
|
|
|
|
public static float ReadValueF32(this Stream stream)
|
|
{
|
|
var data = new byte[4];
|
|
int read = stream.Read(data, 0, 4);
|
|
Debug.Assert(read == 4);
|
|
return BitConverter.ToSingle(data, 0);
|
|
}
|
|
|
|
internal static string ReadStringInternalDynamic(this Stream stream, Encoding encoding, char end)
|
|
{
|
|
int characterSize = encoding.GetByteCount("e");
|
|
Debug.Assert(characterSize == 1 || characterSize == 2 || characterSize == 4);
|
|
string characterEnd = end.ToString(CultureInfo.InvariantCulture);
|
|
|
|
int i = 0;
|
|
var data = new byte[128 * characterSize];
|
|
|
|
while (true)
|
|
{
|
|
if (i + characterSize > data.Length)
|
|
{
|
|
Array.Resize(ref data, data.Length + (128 * characterSize));
|
|
}
|
|
|
|
int read = stream.Read(data, i, characterSize);
|
|
Debug.Assert(read == characterSize);
|
|
|
|
if (encoding.GetString(data, i, characterSize) == characterEnd)
|
|
{
|
|
break;
|
|
}
|
|
|
|
i += characterSize;
|
|
}
|
|
|
|
if (i == 0)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
return encoding.GetString(data, 0, i);
|
|
}
|
|
|
|
public static string ReadStringAscii(this Stream stream)
|
|
{
|
|
return stream.ReadStringInternalDynamic(Encoding.ASCII, '\0');
|
|
}
|
|
|
|
public static string ReadStringUnicode(this Stream stream)
|
|
{
|
|
return stream.ReadStringInternalDynamic(Encoding.UTF8, '\0');
|
|
}
|
|
}
|
|
}
|