Skip to content

Commit

Permalink
Asset load events (#23)
Browse files Browse the repository at this point in the history
* events for when custom assets are loaded
  • Loading branch information
amazingalek authored Dec 26, 2019
1 parent 21ff061 commit 841019b
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 80 deletions.
13 changes: 0 additions & 13 deletions OWML.Assets/DontDestroyOnLoad.cs

This file was deleted.

108 changes: 69 additions & 39 deletions OWML.Assets/ModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,103 @@ public ModAssets(IModConsole console)
_objImporter = new ObjImporter();
}

public GameObject Load3DObject(ModBehaviour modBehaviour, string objectFilename, string imageFilename)
public ObjectAsset Load3DObject(ModBehaviour modBehaviour, string objectFilename, string imageFilename)
{
var objectPath = modBehaviour.ModManifest.FolderPath + objectFilename;
var imagePath = modBehaviour.ModManifest.FolderPath + imageFilename;
_console.WriteLine("Loading object from " + objectPath);

var go = new GameObject();
go.AddComponent<DontDestroyOnLoad>();
var meshFilter = go.AddComponent<MeshFilter>();
var meshRenderer = go.AddComponent<MeshRenderer>();
var modAsset = go.AddComponent<ObjectAsset>();

modBehaviour.StartCoroutine(LoadMesh(meshFilter, objectPath));
modBehaviour.StartCoroutine(LoadTexture(meshRenderer, imagePath));
modBehaviour.StartCoroutine(LoadMesh(modAsset, objectPath));
modBehaviour.StartCoroutine(LoadTexture(modAsset, imagePath));

return go;
return modAsset;
}

public MeshFilter LoadMesh(ModBehaviour modBehaviour, string objectFilename)
public MeshAsset LoadMesh(ModBehaviour modBehaviour, string objectFilename)
{
var objectPath = modBehaviour.ModManifest.FolderPath + objectFilename;
_console.WriteLine("Loading mesh from " + objectPath);

var go = new GameObject();
go.AddComponent<DontDestroyOnLoad>();
var meshFilter = go.AddComponent<MeshFilter>();
var modAsset = go.AddComponent<MeshAsset>();

modBehaviour.StartCoroutine(LoadMesh(meshFilter, objectPath));
modBehaviour.StartCoroutine(LoadMesh(modAsset, objectPath));

return meshFilter;
return modAsset;
}

public MeshRenderer LoadTexture(ModBehaviour modBehaviour, string imageFilename)
public TextureAsset LoadTexture(ModBehaviour modBehaviour, string imageFilename)
{
var imagePath = modBehaviour.ModManifest.FolderPath + imageFilename;
_console.WriteLine("Loading texture from " + imagePath);

var go = new GameObject();
go.AddComponent<DontDestroyOnLoad>();
var meshRenderer = go.AddComponent<MeshRenderer>();
var modAsset = go.AddComponent<TextureAsset>();

modBehaviour.StartCoroutine(LoadTexture(meshRenderer, imagePath));
modBehaviour.StartCoroutine(LoadTexture(modAsset, imagePath));

return meshRenderer;
return modAsset;
}

public AudioSource LoadAudio(ModBehaviour modBehaviour, string audioFilename)
public AudioAsset LoadAudio(ModBehaviour modBehaviour, string audioFilename)
{
var audioPath = modBehaviour.ModManifest.FolderPath + audioFilename;
_console.WriteLine("Loading audio from " + audioPath);

var go = new GameObject();
go.AddComponent<DontDestroyOnLoad>();
var audioSource = go.AddComponent<AudioSource>();
var modAsset = go.AddComponent<AudioAsset>();

var loadAudioFrom = audioFilename.EndsWith(".mp3")
? LoadAudioFromMp3(audioSource, audioPath)
: LoadAudioFromWav(audioSource, audioPath);
? LoadAudioFromMp3(modAsset, audioPath)
: LoadAudioFromWav(modAsset, audioPath);
modBehaviour.StartCoroutine(loadAudioFrom);
return audioSource;

return modAsset;
}

private IEnumerator LoadMesh(ObjectAsset modAsset, string objectPath)
{
var mesh = _objImporter.ImportFile(objectPath);
var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(modAsset.gameObject);
}

private IEnumerator LoadTexture(ObjectAsset modAsset, string imagePath)
{
var texture = new Texture2D(4, 4, TextureFormat.DXT1, false);
var url = "file://" + imagePath;
using (var www = new WWW(url))
{
yield return www;
www.LoadImageIntoTexture(texture);
}
if (texture == null)
{
_console.WriteLine("Texture is null");
}
var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = texture;
}

private IEnumerator LoadMesh(MeshFilter meshFilter, string objectPath)
private IEnumerator LoadMesh(MeshAsset modAsset, string objectPath)
{
var mesh = _objImporter.ImportFile(objectPath);
if (mesh == null)
{
_console.WriteLine("Mesh is null");
}
var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
yield return null;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(meshFilter);
}

private IEnumerator LoadTexture(MeshRenderer meshRenderer, string imagePath)
private IEnumerator LoadTexture(TextureAsset modAsset, string imagePath)
{
var texture = new Texture2D(4, 4, TextureFormat.DXT1, false);
var url = "file://" + imagePath;
Expand All @@ -101,39 +125,45 @@ private IEnumerator LoadTexture(MeshRenderer meshRenderer, string imagePath)
{
_console.WriteLine("Texture is null");
}
var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = texture;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(meshRenderer);
}

private IEnumerator LoadAudioFromMp3(AudioSource audioSource, string audioPath)
private IEnumerator LoadAudioFromMp3(AudioAsset modAsset, string audioPath)
{
_console.WriteLine("Loading mp3");
AudioClip audioClip;
AudioClip clip;
using (var reader = new AudioFileReader(audioPath))
{
var outputBytes = new float[reader.Length];
reader.Read(outputBytes, 0, (int)reader.Length);
audioClip = AudioClip.Create(audioPath, (int)reader.Length, reader.WaveFormat.Channels, reader.WaveFormat.SampleRate, false);
audioClip.SetData(outputBytes, 0);
clip = AudioClip.Create(audioPath, (int)reader.Length, reader.WaveFormat.Channels, reader.WaveFormat.SampleRate, false);
clip.SetData(outputBytes, 0);
}
audioSource.clip = audioClip;
yield return null;
var audioSource = modAsset.AddComponent<AudioSource>();
audioSource.clip = clip;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(audioSource);
}

private IEnumerator LoadAudioFromWav(AudioSource audioSource, string audioPath)
private IEnumerator LoadAudioFromWav(AudioAsset modAsset, string audioPath)
{
_console.WriteLine("Loading wav");
AudioClip audio;
AudioClip clip;
var url = "file://" + audioPath;
using (var www = new WWW(url))
{
yield return www;
audio = www.GetAudioClip(true);
clip = www.GetAudioClip(true);
}
if (audio == null)
if (clip == null)
{
_console.WriteLine("Audio is null");
}
audioSource.clip = audio;
var audioSource = modAsset.AddComponent<AudioSource>();
audioSource.clip = clip;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(audioSource);
}

}
Expand Down
1 change: 0 additions & 1 deletion OWML.Assets/OWML.Assets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DontDestroyOnLoad.cs" />
<Compile Include="ModAssets.cs" />
<Compile Include="ObjImporter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
8 changes: 8 additions & 0 deletions OWML.Common/AudioAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;

namespace OWML.Common
{
public class AudioAsset : ModAsset<AudioSource>
{
}
}
1 change: 0 additions & 1 deletion OWML.Common/IHarmonyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using UnityEngine;

namespace OWML.Common
{
Expand Down
12 changes: 5 additions & 7 deletions OWML.Common/IModAssets.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using UnityEngine;

namespace OWML.Common
namespace OWML.Common
{
public interface IModAssets
{
GameObject Load3DObject(ModBehaviour modBehaviour, string objectFilename, string imageFilename);
MeshFilter LoadMesh(ModBehaviour modBehaviour, string objectFilename);
MeshRenderer LoadTexture(ModBehaviour modBehaviour, string imageFilename);
AudioSource LoadAudio(ModBehaviour modBehaviour, string audioFilename);
ObjectAsset Load3DObject(ModBehaviour modBehaviour, string objectFilename, string imageFilename);
MeshAsset LoadMesh(ModBehaviour modBehaviour, string objectFilename);
TextureAsset LoadTexture(ModBehaviour modBehaviour, string imageFilename);
AudioAsset LoadAudio(ModBehaviour modBehaviour, string audioFilename);
}
}
8 changes: 8 additions & 0 deletions OWML.Common/MeshAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;

namespace OWML.Common
{
public class MeshAsset : ModAsset<MeshFilter>
{
}
}
33 changes: 33 additions & 0 deletions OWML.Common/ModAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using UnityEngine;

namespace OWML.Common
{
public class ModAsset<T> : MonoBehaviour
{
public event Action<T> OnLoaded;

public T Asset { get; private set; }

public void SetAsset(T asset)
{
Asset = asset;
if (OnLoaded == null)
{
ModBehaviour.ModHelper.Console.WriteLine("Invoking OnLoaded with no subscribers :(");
}
OnLoaded?.Invoke(asset);
}

private void Start()
{
DontDestroyOnLoad(gameObject);
}

public T1 AddComponent<T1>() where T1 : Component
{
return gameObject.AddComponent<T1>();
}

}
}
5 changes: 5 additions & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@
<Compile Include="IModConsole.cs" />
<Compile Include="IModLogger.cs" />
<Compile Include="IModManifest.cs" />
<Compile Include="ModAsset.cs" />
<Compile Include="ModBehaviour.cs" />
<Compile Include="ModConfig.cs" />
<Compile Include="ModConsole.cs" />
<Compile Include="ModHelper.cs" />
<Compile Include="ModLogger.cs" />
<Compile Include="ModManifest.cs" />
<Compile Include="AudioAsset.cs" />
<Compile Include="MeshAsset.cs" />
<Compile Include="TextureAsset.cs" />
<Compile Include="ObjectAsset.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions OWML.Common/ObjectAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;

namespace OWML.Common
{
public class ObjectAsset : ModAsset<GameObject>
{
}
}
8 changes: 8 additions & 0 deletions OWML.Common/TextureAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;

namespace OWML.Common
{
public class TextureAsset : ModAsset<MeshRenderer>
{
}
}
2 changes: 1 addition & 1 deletion OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OWML.Launcher
{
public class App
{
private const string OWMLVersion = "0.2.2";
private const string OWMLVersion = "0.2.3";

private readonly string[] _filesToCopy = { "UnityEngine.CoreModule.dll", "Assembly-CSharp.dll" };

Expand Down
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.EnableDebugMode/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"name": "EnableDebugMode",
"uniqueName": "Alek.EnableDebugMode",
"version": "0.2",
"owmlVersion": "0.2.2",
"owmlVersion": "0.2.3",
"enabled": false
}
Loading

0 comments on commit 841019b

Please sign in to comment.