-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaExporter.cs
More file actions
142 lines (131 loc) · 4.98 KB
/
MediaExporter.cs
File metadata and controls
142 lines (131 loc) · 4.98 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
130
131
132
133
134
135
136
137
138
139
140
141
142
using Cysharp.Threading.Tasks;
using HarmonyLib;
using ngov3;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace CustomStreamLoader
{
internal class MediaExporter
{
public static Dictionary<string, AssetBundle> LoadedAssetBundlePaths = new Dictionary<string, AssetBundle>();
public static List<string> LoadedAddressables = new List<string>();
public static List<string> AddressablePaths = new List<string>();
public static List<string> CatalogPaths = new List<string>();
public static Dictionary<string, AnimationClip> AnimClipLoader = new Dictionary<string, AnimationClip>();
public static Sprite LoadImageFromFile(string path)
{
string fileName = Path.GetFileName(path);
byte[] file = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(2, 2);
ImageConversion.LoadImage(tex, file, false);
tex.filterMode = FilterMode.Point;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100f);
sprite.name = fileName;
return sprite;
}
public static async UniTask AddExternalCatalogs()
{
foreach (var bundle in AddressablePaths)
{
await AddAddressBundle(bundle);
}
foreach (var catalog in CatalogPaths)
{
await AddExternalCatalog(catalog);
}
}
public static async UniTask AddExternalCatalog(string path)
{
try
{
AsyncOperationHandle<IResourceLocator> handle = Addressables.LoadContentCatalogAsync(path, true);
await UniTask.WaitUntil(() => handle.IsDone);
Debug.Log("Catalog loaded.");
}
catch { }
}
public static async UniTask AddAddressBundle(string path)
{
string fileName = Path.GetFileName(path);
string targetStrAssetPath = Path.Combine(Addressables.RuntimePath, "StandaloneWindows64", fileName);
if (File.Exists(targetStrAssetPath))
return;
var bundleData = File.ReadAllBytes(path);
using FileStream targetStream = File.Create(targetStrAssetPath);
await targetStream.WriteAsync(bundleData, 0, bundleData.Length);
LoadedAddressables.Add(targetStrAssetPath);
}
public static void LoadAssetBundles(string clipName, string path)
{
AssetBundle bundle;
if (LoadedAssetBundlePaths.ContainsKey(path))
bundle = LoadedAssetBundlePaths[path];
else
{
bundle = AssetBundle.LoadFromFile(path);
LoadedAssetBundlePaths.Add(path, bundle);
}
if (!AnimClipLoader.ContainsKey(clipName))
{
var clip = bundle.LoadAsset<AnimationClip>(clipName);
AnimClipLoader.Add(clipName, clip);
}
}
internal static void DeleteAddressBundlesFromPath()
{
if (LoadedAddressables.Count == 0) { return; }
foreach (string bundlePath in LoadedAddressables)
{
File.Delete(bundlePath);
}
}
}
[HarmonyPatch]
internal class AnimationPatcher
{
[HarmonyPostfix]
[HarmonyPatch(typeof(LoadWebcamData), "LoadAnimation")]
[HarmonyPatch(typeof(LoadLiveViewData), "LoadAnimation")]
static async UniTask<AnimationClip> SetCustomAnim(UniTask<AnimationClip> value, string address)
{
string customId = address.Replace(".anim", "");
if (MediaExporter.AnimClipLoader.Count == 0)
{
return await value;
}
try
{
AnimationClip customClip = MediaExporter.AnimClipLoader[customId];
if (customClip == null || customClip.ToString() == "")
{
return await value;
}
if (ThatOneLongList.list.Exists(x => x == customClip.name))
{
return await value;
}
if (StreamLoader.customAssets.Exists(x => x.fileName == customClip.name && x.customAssetFileType == CustomAssetFileType.AddressableBundle))
{
return await value;
}
return customClip;
}
catch
{
}
return await value;
}
[HarmonyFinalizer]
[HarmonyPatch(typeof(LoadWebcamData), "LoadAnimation")]
[HarmonyPatch(typeof(LoadLiveViewData), "LoadAnimation")]
static Exception SuppressException(Exception __exception)
{
return null;
}
}
}