forked from tukasa0001/TownOfHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
69 lines (58 loc) · 2.58 KB
/
Helpers.cs
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
using System;
using System.Reflection;
using UnhollowerBaseLib;
using UnityEngine;
namespace TownOfHost {
// https://github.com/Eisbison/TheOtherRoles/blob/main/TheOtherRoles/Helpers.cs
public static class Helpers {
public static Sprite LoadSpriteFromResources(string path, float pixelsPerUnit) {
try {
var texture = LoadTextureFromResources(path);
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit);
} catch {
Logger.error($"Error loading sprite from path: {path}");
}
return null;
}
public static Texture2D LoadTextureFromResources(string path) {
try {
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, true);
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(path);
var byteTexture = new byte[stream.Length];
_ = stream.Read(byteTexture, 0, (int) stream.Length);
LoadImage(texture, byteTexture, false);
return texture;
} catch {
Logger.error($"Error loading texture from resources: {path}");
}
return null;
}
private delegate bool DelegateLoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
private static DelegateLoadImage _callLoadImage;
private static bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable) {
_callLoadImage ??= IL2CPP.ResolveICall<DelegateLoadImage>("UnityEngine.ImageConversion::LoadImage");
var il2cppArray = (Il2CppStructArray<byte>) data;
return _callLoadImage.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
}
}
public class PlayerVersion
{
public readonly Version version;
public readonly string tag;
public PlayerVersion(int major, int minor, int patch, int revision, string tag_str)
{
version = new Version(major,minor,patch == -1?0:patch,revision == -1?0:revision);
tag = tag_str;
}
public PlayerVersion(Version ver, string tag_str)
{
version = new Version(ver.Major,ver.Minor,ver.Build == -1?0:ver.Build,ver.Revision == -1?0:ver.Revision);
tag = tag_str;
}
public bool isEqual(PlayerVersion pv)
{
return (pv.version == version && pv.tag == tag);
}
}
}