Skip to content

Commit

Permalink
Merge pull request #331 from amazingalek/asset-overhaul
Browse files Browse the repository at this point in the history
Assets overhaul
  • Loading branch information
amazingalek authored Dec 22, 2020
2 parents 0c33d48 + db43fe3 commit 1b5121b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 140 deletions.
24 changes: 19 additions & 5 deletions src/OWML.Common/Interfaces/IModAssets.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
using UnityEngine;
using System;
using UnityEngine;

namespace OWML.Common
{
public interface IModAssets
{
AssetBundle LoadBundle(string filename);

IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename);
GameObject Get3DObject(string objectFilename, string audioFilename);

Mesh GetMesh(string filename);

Texture2D GetTexture(string filename);

IModAsset<MeshFilter> LoadMesh(string objectFilename);

IModAsset<MeshRenderer> LoadTexture(string imageFilename);
AudioClip GetAudio(string filename);

[Obsolete("Use Get3DObject instead.")]
IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename);

[Obsolete("Use GetMesh instead.")]
IModAsset<MeshFilter> LoadMesh(string filename);

[Obsolete("Use GetTexture instead.")]
IModAsset<MeshRenderer> LoadTexture(string filename);

IModAsset<AudioSource> LoadAudio(string audioFilename);
[Obsolete("Use GetAudio instead.")]
IModAsset<AudioSource> LoadAudio(string filename);
}
}
4 changes: 2 additions & 2 deletions src/OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
Expand All @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Json.Net.Unity3D" Version="9.0.1" />
<PackageReference Include="OW.Unity.Dlls" Version="1.0.8">
<PackageReference Include="OW.Unity.Dlls" Version="1.0.9">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/OWML.Launcher/OWML.Manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Alek",
"name": "OWML",
"uniqueName": "Alek.OWML",
"version": "1.1.3",
"version": "1.1.4",
"description": "The mod loader and mod framework for Outer Wilds",
"minGameVersion": "1.0.7.0",
"maxGameVersion": "1.0.7.481"
Expand Down
169 changes: 50 additions & 119 deletions src/OWML.ModHelper.Assets/ModAssets.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.IO;
using NAudio.Wave;
using OWML.Common;
using UnityEngine;
Expand Down Expand Up @@ -32,149 +32,80 @@ public AssetBundle LoadBundle(string filename)

public IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename)
{
var objectPath = _manifest.ModFolderPath + objectFilename;
var imagePath = _manifest.ModFolderPath + imageFilename;
_console.WriteLine($"Loading object from {objectPath}");

var go = new GameObject();
var modAsset = go.AddComponent<ObjectAsset>();

modAsset.StartCoroutine(LoadMesh(modAsset, objectPath));
modAsset.StartCoroutine(LoadTexture(modAsset, imagePath));

var modAsset = new GameObject().AddComponent<ObjectAsset>();

var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = GetMesh(objectFilename);
modAsset.SetMeshFilter(meshFilter);

var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = GetTexture(imageFilename);
modAsset.SetMeshRenderer(meshRenderer);
return modAsset;
}

public IModAsset<MeshFilter> LoadMesh(string objectFilename)
public IModAsset<MeshFilter> LoadMesh(string filename)
{
var objectPath = _manifest.ModFolderPath + objectFilename;
_console.WriteLine($"Loading mesh from {objectPath}");

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

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

var modAsset = new GameObject().AddComponent<MeshAsset>();
var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = GetMesh(filename);
modAsset.SetAsset(meshFilter);
return modAsset;
}

public IModAsset<MeshRenderer> LoadTexture(string imageFilename)
public IModAsset<MeshRenderer> LoadTexture(string filename)
{
var imagePath = _manifest.ModFolderPath + imageFilename;
_console.WriteLine($"Loading texture from {imagePath}");

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

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

var modAsset = new GameObject().AddComponent<TextureAsset>();
var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = GetTexture(filename);
modAsset.SetAsset(meshRenderer);
return modAsset;
}

public IModAsset<AudioSource> LoadAudio(string audioFilename)
{
var audioPath = _manifest.ModFolderPath + audioFilename;
_console.WriteLine($"Loading audio from {audioPath}");

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

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

var modAsset = new GameObject().AddComponent<AudioAsset>();
var audioSource = modAsset.AddComponent<AudioSource>();
audioSource.clip = GetAudio(audioFilename);
modAsset.SetAsset(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.SetMeshFilter(meshFilter);
}

private IEnumerator LoadTexture(ObjectAsset modAsset, string imagePath)
public Mesh GetMesh(string filename)
{
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);
}

var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = texture;
modAsset.SetMeshRenderer(meshRenderer);
}

private IEnumerator LoadMesh(MeshAsset modAsset, string objectPath)
{
var mesh = _objImporter.ImportFile(objectPath);
if (mesh == null)
{
_console.WriteLine("Error - Mesh is null.", MessageType.Error);
}
var meshFilter = modAsset.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(meshFilter);
var path = _manifest.ModFolderPath + filename;
_console.WriteLine($"Loading mesh from {path}");
return _objImporter.ImportFile(path);
}

private IEnumerator LoadTexture(TextureAsset modAsset, string imagePath)
public Texture2D GetTexture(string filename)
{
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);
}

var meshRenderer = modAsset.AddComponent<MeshRenderer>();
meshRenderer.material.mainTexture = texture;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(meshRenderer);
var path = _manifest.ModFolderPath + filename;
_console.WriteLine($"Loading texture from {path}");
var data = File.ReadAllBytes(path);
var texture = new Texture2D(2, 2);
texture.LoadImage(data);
return texture;
}

private IEnumerator LoadAudioFromMp3(AudioAsset modAsset, string audioPath)
public GameObject Get3DObject(string objectFilename, string imageFilename)
{
AudioClip clip;
using (var reader = new AudioFileReader(audioPath))
{
var outputBytes = new float[reader.Length];
reader.Read(outputBytes, 0, (int)reader.Length);
clip = AudioClip.Create(audioPath, (int)reader.Length, reader.WaveFormat.Channels, reader.WaveFormat.SampleRate, false);
clip.SetData(outputBytes, 0);
}

var audioSource = modAsset.AddComponent<AudioSource>();
audioSource.clip = clip;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(audioSource);
var go = new GameObject();
go.AddComponent<MeshFilter>().mesh = GetMesh(objectFilename);
go.AddComponent<MeshRenderer>().material.mainTexture = GetTexture(imageFilename);
return go;
}

private IEnumerator LoadAudioFromWav(AudioAsset modAsset, string audioPath)
public AudioClip GetAudio(string filename)
{
AudioClip clip;
var url = "file://" + audioPath;
using (var www = new WWW(url))
{
yield return www;
clip = www.GetAudioClip(true);
}
if (clip == null)
{
_console.WriteLine("Error - Audio is null.", MessageType.Error);
}

var audioSource = modAsset.AddComponent<AudioSource>();
audioSource.clip = clip;
yield return new WaitForEndOfFrame();
modAsset.SetAsset(audioSource);
var path = _manifest.ModFolderPath + filename;
_console.WriteLine($"Loading audio from {path}");
using var reader = new AudioFileReader(path);
var outputBytes = new float[reader.Length];
reader.Read(outputBytes, 0, (int)reader.Length);
var clip = AudioClip.Create(path, (int)reader.Length, reader.WaveFormat.Channels, reader.WaveFormat.SampleRate, false);
clip.SetData(outputBytes, 0);
return clip;
}
}
}
}
27 changes: 14 additions & 13 deletions src/SampleMods/OWML.LoadCustomAssets/LoadCustomAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ public void Start()

var assetBundle = ModHelper.Assets.LoadBundle("cubebundle");
_cube = assetBundle.LoadAsset<GameObject>("Cube");

var gunSoundAsset = ModHelper.Assets.LoadAudio("blaster-firing.wav");
gunSoundAsset.Loaded += OnGunSoundLoaded;
var duckAsset = ModHelper.Assets.Load3DObject("duck.obj", "duck.png");
duckAsset.Loaded += OnDuckLoaded;
var musicAsset = ModHelper.Assets.LoadAudio("spiral-mountain.mp3");
ModHelper.Events.Unity.RunWhen(() => musicAsset.Asset != null, () => OnMusicLoaded(musicAsset.Asset));


ModHelper.Events.Player.OnPlayerAwake += OnPlayerAwake;
ModHelper.Events.Scenes.OnStartSceneChange += OnStartSceneChange;
ModHelper.Events.Scenes.OnCompleteSceneChange += OnCompleteSceneChange;
Expand Down Expand Up @@ -77,21 +70,25 @@ public void TestLogging()
ModHelper.Console.WriteLine("Test Debug", MessageType.Debug);
}

private void OnMusicLoaded(AudioSource audio)
private void LoadMusic()
{
_music = audio;
_music = new GameObject().AddComponent<AudioSource>();
_music.clip = ModHelper.Assets.GetAudio("spiral-mountain.mp3");
ModHelper.Console.WriteLine("Music loaded!");
}

private void OnGunSoundLoaded(AudioSource audio)
private void LoadGunSound()
{
_shootSound = audio;
_shootSound = new GameObject().AddComponent<AudioSource>();
_shootSound.clip = ModHelper.Assets.GetAudio("blaster-firing.wav");
ModHelper.Console.WriteLine("Gun sound loaded!");
}

private void OnDuckLoaded(GameObject duck)
private void LoadDuck()
{
var duck = ModHelper.Assets.Get3DObject("duck.obj", "duck.png");
ModHelper.Console.WriteLine("Duck loaded!");

duck.AddComponent<SphereCollider>();
duck.AddComponent<Rigidbody>();
_duckBody = duck.AddComponent<OWRigidbody>();
Expand All @@ -102,6 +99,10 @@ private void OnPlayerAwake(PlayerBody playerBody)
{
_playerBody = playerBody;
_playerTransform = playerBody.transform;

LoadDuck();
LoadGunSound();
LoadMusic();
}

private void OnStartSceneChange(OWScene oldScene, OWScene newScene)
Expand Down

0 comments on commit 1b5121b

Please sign in to comment.