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: 3ac95cb00d0bca163a6dd51b14a957f1d427a32e (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
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
/*
 *                     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 bool SetPropertyValue(PropertyInfo attribute, object obj, string value)
    {
        var name = attribute.PropertyType.Name.ToLower();
        switch (name)
        {
            case "string":
                attribute.SetValue(obj, value.Convert<string>());
                return true;
            case "boolean":
                if(char.IsDigit(value[0])) attribute.SetValue(obj, !value.Equals("0"));
                else attribute.SetValue(obj, value.Equals("True", StringComparison.OrdinalIgnoreCase));
                return true;
            case "byte":
                if (value.Equals("true", StringComparison.OrdinalIgnoreCase)) attribute.SetValue(obj, (byte)1);
                else if (value.Equals("false", StringComparison.OrdinalIgnoreCase)) attribute.SetValue(obj, byte.MinValue);
                else attribute.SetValue(obj, value.Convert<byte>());
                return true;
            case "sbyte":
                attribute.SetValue(obj, value.Convert<sbyte>());
                return true;
            case "uint16":
                attribute.SetValue(obj, value.Convert<ushort>());
                return true;
            case "int16":
                attribute.SetValue(obj, value.Convert<short>());
                return true;
            case "uint32":
                attribute.SetValue(obj, value.Convert<uint>());
                return true;
            case "int32":
                attribute.SetValue(obj, value.Convert<int>());
                return true;
            case "uint64":
                attribute.SetValue(obj, value.Convert<ulong>());
                return true;
            case "int64":
                attribute.SetValue(obj, value.Convert<long>());
                return true;
            case "single":
                attribute.SetValue(obj, (float)Math.Round(float.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
                return true;
            case "double":
                attribute.SetValue(obj, Math.Round(double.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
                return true;
            case "decimal":
                attribute.SetValue(obj, Math.Round(decimal.Parse(value, CultureInfo.InvariantCulture.NumberFormat), 3));
                return true;
            default:
                throw new Exception($"Data type '{name}' not recognized, contact developer.");
        }
    }

    public static int GetPixelPosition(int width, int x, int y)
    {
        return width * y + x;
    }

    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);
}