Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Helpers.cs « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb6e8ac25f0b9d51fde2e2454b8e0cc507645ce2 (plain)
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
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */

using BinarySerialization;
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.Json;
using UVtools.Core.Extensions;

namespace UVtools.Core;

/// <summary>
/// A helper class with utilities
/// </summary>
public static class Helpers
{
    /// <summary>
    /// Gets the <see cref="BinarySerializer"/> instance
    /// </summary>
    public static BinarySerializer Serializer { get; } = new() {Endianness = Endianness.Little };

    public static MemoryStream Serialize(object value)
    {
        MemoryStream stream = new();
        Serializer.Serialize(stream, value);
        return stream;
    }

    public static T Deserialize<T>(Stream stream)
    {
        return Serializer.Deserialize<T>(stream);
    }

    public static uint SerializeWriteFileStream(FileStream fs, object value, int offset = 0)
    {
        using var stream = Serialize(value);
        return fs.WriteStream(stream, offset);
    }

    public static void SwapVariables<T>(ref T var1, ref T var2)
    {
        (var1, var2) = (var2, var1);
    }

    public static float BrightnessToPercent(byte brightness, byte roundPlates = 2) => (float)Math.Round(brightness * 100 / 255f, roundPlates);
}