forked from aldelaro5/BugFables-AssetsRedirector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetsRedirection.cs
145 lines (135 loc) · 5.07 KB
/
AssetsRedirection.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using BepInEx;
using HarmonyLib;
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using XUnity.ResourceRedirector;
namespace BugFables.AssetsRedirector
{
[BepInPlugin("com.aldelaro5.BugFables.plugins.AssetsRedirector", "Assets Redirector", "1.0.0")]
[BepInProcess("Bug Fables.exe")]
public class AssetsRedirection : BaseUnityPlugin
{
public void Awake()
{
Common.Plugin = this;
var harmony = new Harmony("com.aldelaro5.BugFables.plugins.AssetsRedirector");
harmony.PatchAll(typeof(EntitiesSpritesRedirector));
harmony.PatchAll(typeof(PlayMusicRedirector));
ResourceRedirection.EnableSyncOverAsyncAssetLoads();
ResourceRedirection.RegisterResourceLoadedHook(HookBehaviour.OneCallbackPerResourceLoaded, AseetLoad);
}
private void AseetLoad(ResourceLoadedContext context)
{
// Only redirect sound effects, music are redirected via Harmony patch
if (context.Asset is AudioClip && context.Parameters.Path.StartsWith("Audio/Sounds"))
RedirectSoundEffect(context);
else if (context.Asset is Sprite)
RedirectSprites(context);
else if (context.Asset is TextAsset)
RedirectTextAsset(context);
else if (context.Asset is Material)
RedirectMaterialTexture(context);
}
private void RedirectMaterialTexture(ResourceLoadedContext context)
{
Material mat = (Material)context.Asset;
if (mat?.mainTexture?.name == "main1")
{
string path = Path.Combine(Path.GetDirectoryName(base.Info.Location), "Sprites\\Misc\\main1");
if (File.Exists(path + ".png"))
{
ImageConversion.LoadImage((Texture2D)mat.mainTexture, File.ReadAllBytes(path + ".png"));
context.Complete();
}
}
if (mat?.mainTexture?.name == "main2")
{
string path = Path.Combine(Path.GetDirectoryName(base.Info.Location), "Sprites\\Misc\\main2");
if (File.Exists(path + ".png"))
{
ImageConversion.LoadImage((Texture2D)mat.mainTexture, File.ReadAllBytes(path + ".png"));
context.Complete();
}
}
}
private void RedirectTextAsset(ResourceLoadedContext context)
{
string path = Path.Combine(Path.GetDirectoryName(base.Info.Location), context.Parameters.Path);
string resolved = ResolveTextFilePath(path);
if (resolved != null)
{
context.Asset = new TextAsset(File.ReadAllText(resolved));
context.Complete();
}
else if ((resolved = ResolveTextFilePath(path + ".template")) != null)
{
string processed;
string[] bfAsset = Resources.Load<TextAsset>(context.Parameters.Path).ToString().Replace("\r\n", "\n").Split('\n');
// process template file
string[] templateText = File.ReadAllLines(resolved);
foreach (string line in templateText)
{
string beforeSpace, afterSpace;
int firstSpaceInd, lineNum;
firstSpaceInd = line.IndexOf(' ');
if (firstSpaceInd < 0)
continue;
beforeSpace = line.Substring(0, firstSpaceInd);
afterSpace = line.Substring(firstSpaceInd + 1);
if (int.TryParse(beforeSpace, out lineNum))
bfAsset[lineNum] = afterSpace;
}
// cache modified file
processed = string.Join("\n", bfAsset);
File.WriteAllText(path, processed);
context.Asset = new TextAsset(processed);
context.Complete();
}
}
private string ResolveTextFilePath(string path)
{
if (File.Exists(path))
return path;
else if (File.Exists(path + ".txt"))
return path + ".txt";
else if (File.Exists(path + ".bytes"))
return path + ".bytes";
return null;
}
private void RedirectSprites(ResourceLoadedContext context)
{
string path = Path.Combine(Path.GetDirectoryName(base.Info.Location), context.Parameters.Path);
if (File.Exists(path + ".png"))
{
Sprite ogSprite = (Sprite)context.Asset;
ImageConversion.LoadImage(ogSprite.texture, File.ReadAllBytes(path + ".png"));
Vector2 standardisedPivot = new Vector2(ogSprite.pivot.x / ogSprite.rect.width, ogSprite.pivot.y / ogSprite.rect.height);
Sprite newSprite = Sprite.Create(ogSprite.texture, ogSprite.rect, standardisedPivot, ogSprite.pixelsPerUnit);
newSprite.name = ogSprite.name;
context.Asset = newSprite;
context.Complete();
}
}
private void RedirectSoundEffect(ResourceLoadedContext context)
{
string path = Path.Combine(Path.GetDirectoryName(base.Info.Location), context.Parameters.Path);
if (File.Exists(path + ".wav"))
{
string name = context.Asset.name;
context.Asset = Common.LoadAudioClip(path + ".wav");
context.Asset.name = name;
context.Complete();
}
else if (File.Exists(path + ".ogg"))
{
string name = context.Asset.name;
context.Asset = Common.LoadAudioClip(path + ".ogg");
context.Asset.name = name;
context.Complete();
}
}
}
}