Fix up black images

This commit is contained in:
Nick 2022-09-13 19:53:58 -04:00
parent 13b714ac88
commit 7c183d7333
2 changed files with 37 additions and 58 deletions

View File

@ -2,8 +2,10 @@ using NewHorizons.External.Modules;
using NewHorizons.Handlers;
using NewHorizons.Utility;
using OWML.Common;
using OWML.Common.Menus;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using static NewHorizons.External.Modules.PropModule;
using Logger = NewHorizons.Utility.Logger;
@ -89,19 +91,8 @@ namespace NewHorizons.Builder.Props
// The base game ones only have 15 slides max
var textures = new Texture2D[slidesCount >= 15 ? 15 : slidesCount];
var imageLoader = slideReelObj.AddComponent<ImageUtilities.AsyncImageLoader>();
for (int i = 0; i < slidesCount; i++)
{
var slide = new Slide();
var slideInfo = info.slides[i];
var imageLoader = AddAsyncLoader(slideReelObj, mod, info.slides, ref slideCollection);
imageLoader.PathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath);
AddModules(slideInfo, ref slide, mod);
slideCollection.slides[i] = slide;
}
// this variable just lets us track how many of the first 15 slides have been loaded.
// this way as soon as the last one is loaded (due to async loading, this may be
// slide 7, or slide 3, or whatever), we can build the slide reel texture. This allows us
@ -191,18 +182,7 @@ namespace NewHorizons.Builder.Props
int slidesCount = info.slides.Length;
var slideCollection = new SlideCollection(slidesCount);
var imageLoader = projectorObj.AddComponent<ImageUtilities.AsyncImageLoader>();
for (int i = 0; i < slidesCount; i++)
{
var slide = new Slide();
var slideInfo = info.slides[i];
imageLoader.PathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath);
AddModules(slideInfo, ref slide, mod);
slideCollection.slides[i] = slide;
}
var imageLoader = AddAsyncLoader(projectorObj, mod, info.slides, ref slideCollection);
imageLoader.imageLoadedEvent.AddListener((Texture2D tex, int index) => { slideCollection.slides[index]._image = ImageUtilities.Invert(tex); });
slideCollectionContainer.slideCollection = slideCollection;
@ -256,19 +236,7 @@ namespace NewHorizons.Builder.Props
var slidesCount = slides.Length;
var slideCollection = new SlideCollection(slidesCount);
var imageLoader = g.AddComponent<ImageUtilities.AsyncImageLoader>();
for (int i = 0; i < slidesCount; i++)
{
var slide = new Slide();
var slideInfo = slides[i];
imageLoader.PathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath);
AddModules(slideInfo, ref slide, mod);
slideCollection.slides[i] = slide;
}
var imageLoader = AddAsyncLoader(g, mod, info.slides, ref slideCollection);
imageLoader.imageLoadedEvent.AddListener((Texture2D tex, int index) => { slideCollection.slides[index]._image = tex; });
// attach a component to store all the data for the slides that play when a vision torch scans this target
@ -330,19 +298,8 @@ namespace NewHorizons.Builder.Props
var slidesCount = slides.Length;
var slideCollection = new SlideCollection(slidesCount);
var imageLoader = standingTorch.AddComponent<ImageUtilities.AsyncImageLoader>();
for (int i = 0; i < slidesCount; i++)
{
var slide = new Slide();
var slideInfo = slides[i];
var imageLoader = AddAsyncLoader(standingTorch, mod, slides, ref slideCollection);
imageLoader.PathsToLoad.Add(mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath);
AddModules(slideInfo, ref slide, mod);
slideCollection.slides[i] = slide;
}
// This variable just lets us track how many of the slides have been loaded.
// This way as soon as the last one is loaded (due to async loading, this may be
// slide 7, or slide 3, or whatever), we can enable the vision torch. This allows us
@ -378,6 +335,32 @@ namespace NewHorizons.Builder.Props
return standingTorch;
}
private static ImageUtilities.AsyncImageLoader AddAsyncLoader(GameObject gameObject, IModBehaviour mod, SlideInfo[] slides, ref SlideCollection slideCollection)
{
var imageLoader = gameObject.AddComponent<ImageUtilities.AsyncImageLoader>();
for (int i = 0; i < slides.Length; i++)
{
var slide = new Slide();
var slideInfo = slides[i];
if (string.IsNullOrEmpty(slideInfo.imagePath))
{
imageLoader.imageLoadedEvent?.Invoke(Texture2D.blackTexture, i);
}
else
{
// Don't use Path.Combine here else you break the Vision
imageLoader.PathsToLoad.Add((i, mod.ModHelper.Manifest.ModFolderPath + slideInfo.imagePath));
}
AddModules(slideInfo, ref slide, mod);
slideCollection.slides[i] = slide;
}
return imageLoader;
}
private static void AddModules(PropModule.SlideInfo slideInfo, ref Slide slide, IModBehaviour mod)
{
var modules = new List<SlideFunctionModule>();

View File

@ -1,9 +1,11 @@
using OWML.Common;
using OWML.ModHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
@ -364,7 +366,7 @@ namespace NewHorizons.Utility
// Modified from https://stackoverflow.com/a/69141085/9643841
public class AsyncImageLoader : MonoBehaviour
{
public List<string> PathsToLoad { get; private set; } = new ();
public List<(int index, string path)> PathsToLoad { get; private set; } = new ();
public class ImageLoadedEvent : UnityEvent<Texture2D, int> { }
public ImageLoadedEvent imageLoadedEvent = new ();
@ -381,9 +383,9 @@ namespace NewHorizons.Utility
void Start()
{
imageLoadedEvent.AddListener(OnImageLoaded);
for (int i = 0; i < PathsToLoad.Count; i++)
foreach (var (index, path) in PathsToLoad)
{
StartCoroutine(DownloadTexture(PathsToLoad[i], i));
StartCoroutine(DownloadTexture(path, index));
}
}
@ -403,12 +405,6 @@ namespace NewHorizons.Utility
IEnumerator DownloadTexture(string url, int index)
{
if (string.IsNullOrEmpty(url))
{
imageLoadedEvent?.Invoke(Texture2D.blackTexture, index);
yield break;
}
lock(_loadedTextures)
{
if (_loadedTextures.ContainsKey(url))