Repository navigation
Expand file tree
/
Copy pathUtils.cs
More file actions
129 lines (120 loc) · 5.21 KB
/
Copy pathUtils.cs
File metadata and controls
129 lines (120 loc) · 5.21 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
128
129
using JetBrains.Annotations;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
using WorldBoxModLoader.ModAPI;
using Object = UnityEngine.Object;
namespace WorldBoxModLoader
{
internal static class UtilsInternal
{
private static Sprite modIcon;
public static void Provide(string modDirectory, out ModConstants modConstantsObject)
{
Debug.Log("Providing " + modDirectory + "mod.json");
string jsonFile = File.ReadAllText(modDirectory + "mod.json");
modConstantsObject = JsonConvert.DeserializeObject<ModConstants>(jsonFile);
}
public static void UpdateModJson(string modDirectory, ModConstants modConstants)
{
File.WriteAllText(modDirectory + "mod.json", JsonConvert.SerializeObject(modConstants, Formatting.Indented));
}
public static T FindResource<T>(string name) where T : Object
{
T[] first_search = Resources.FindObjectsOfTypeAll<T>();
T result = null;
foreach (var obj in first_search)
{
if (obj.name.ToLower() == name.ToLower())
{
result = obj;
}
}
return result;
}
private static byte[] LoadManifestBytes(string path_under_resources)
{
var s = Assembly.GetExecutingAssembly().GetManifestResourceStream($"WorldBoxModLoader.resources.{path_under_resources}");
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
return buffer;
}
public static Texture2D LoadManifestTexture(string path_under_resources)
{
var s = Assembly.GetExecutingAssembly()
.GetManifestResourceStream($"WorldBoxModLoader.resources.{path_under_resources}");
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, buffer.Length);
Texture2D texture = new Texture2D(0, 0);
texture.filterMode = FilterMode.Point;
texture.LoadImage(buffer);
return texture;
}
public static Sprite LoadManifestSprite(string path_under_resources, int pixelsPerUnit = 1)
{
Texture2D texture = LoadManifestTexture(path_under_resources);
Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), 0.5f * Vector2.one, pixelsPerUnit);
return sprite;
}
public static Sprite GetIcon()
{
if (modIcon != null)
return modIcon;
SpriteTextureLoader.addSprite("ui/icons/worldboxmodloader", LoadManifestBytes("Icon.png"));
var icon = SpriteTextureLoader.getSprite("ui/icons/worldboxmodloader");
modIcon = icon;
return icon;
}
public static PowerButton CreateModPowerButton([NotNull] string buttonId, UnityAction<ModConstants> buttonAction, Sprite buttonIcon, ModConstants modConstants, string description = "")
{
PowerButton prefab = UtilsInternal.FindResource<PowerButton>("worldlaws");
PowerButton button = Object.Instantiate(prefab);
ModPowerButton mbutton = button.gameObject.AddComponent<ModPowerButton>();
TipButton tipButton = button.gameObject.GetOrAddComponent<TipButton>();
LocalizedTextManager.add(description, description);
tipButton.textOnClick = buttonId;
tipButton.textOnClickDescription = description;
mbutton.ModConstants = modConstants;
button.name = buttonId;
button.icon.sprite = buttonIcon;
button.type = PowerButtonType.Library;
button.gameObject.SetActive(true);
if (buttonAction != null) mbutton.onClick += buttonAction;
return button;
}
public static Sprite LoadSprite(string fullPath, FilterMode filterMode = FilterMode.Point, int pixelsPerUnit = 20)
{
Texture2D texture = LoadTexture(fullPath, filterMode);
return texture == null ? null : Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), 0.5f * Vector2.one, pixelsPerUnit);
}
public static Texture2D LoadTexture(string fullPath, FilterMode filterMode = FilterMode.Point)
{
byte[] data;
try
{
data = File.ReadAllBytes(Path.Combine(fullPath));
}
catch (Exception exception)
{
Debug.LogWarning(exception);
return null;
}
Texture2D texture = new Texture2D(0, 0);
if (!texture.LoadImage(data))
throw new InvalidDataException("Cannot load texture");
texture.filterMode = filterMode;
texture.wrapMode = TextureWrapMode.Clamp;
return texture;
}
public static ModConstants RecognizeMod(string directory)
{
if (!File.Exists(Path.Combine(directory, "mod.json")))
Debug.LogWarning("No mod.json in " + directory);
Provide(directory, out ModConstants mod);
return mod;
}
}
}