Skip to content

Commit

Permalink
Merge pull request #36 from Govorunb/cds-nuts
Browse files Browse the repository at this point in the history
Custom jukebox tracks (+ unlockable disks)
  • Loading branch information
Alexejhero authored Oct 19, 2023
2 parents 76f2ec2 + a081df6 commit 89f27b1
Show file tree
Hide file tree
Showing 59 changed files with 1,220 additions and 175 deletions.
6 changes: 1 addition & 5 deletions SCHIZO/BelowZero.props → BelowZero.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
<Compile Remove="**/*.Subnautica.cs" />

<Compile Update="**/*.BelowZero.cs">
<DependentUpon>$([System.Text.RegularExpressions.Regex]::Replace('%(FileName)', '^(.*)\.(.*)$', '$1')).cs</DependentUpon>
<!-- <DependentUpon>$([System.Text.RegularExpressions.Regex]::Replace('%(FileName)', '^(.*)\.(.*)$', '$1')).cs</DependentUpon>-->
</Compile>
</ItemGroup>

<ItemGroup>
<None Remove="Subnautica.props" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions SCHIZO.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Non-project Files", "Non-pr
nuget.config = nuget.config
README.md = README.md
Directory.Build.props = Directory.Build.props
BelowZero.props = BelowZero.props
Subnautica.props = Subnautica.props
EndProjectSection
EndProject
Global
Expand Down
4 changes: 2 additions & 2 deletions SCHIZO/Helpers/MaterialHelpers.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections;
using System.Collections;
using System.Runtime.CompilerServices;
using SCHIZO.Attributes;
using SCHIZO.Attributes.Loading;
using UnityEngine;
using UWE;

Expand Down
4 changes: 2 additions & 2 deletions SCHIZO/Interop/NaughtyAttributes/NaughtyAttributesInterop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable UnusedParameter.Local
// ReSharper disable UnusedParameter.Local
// ReSharper disable UnusedType.Global

#pragma warning disable CS9113 // Parameter is unread.
Expand Down Expand Up @@ -56,7 +56,7 @@ internal class EnumFlagsAttribute : Attribute;
internal class ValidateInputAttribute(string callbackName, string message = null) : Attribute;

[Conditional("NEVER")]
internal class RequiredAttribute : Attribute;
internal class RequiredAttribute(string message = null) : Attribute;

[Conditional("NEVER")]
internal class DropdownAttribute(string valuesName) : Attribute;
Expand Down
8 changes: 8 additions & 0 deletions SCHIZO/Interop/Subnautica/_JukeboxDisk.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SCHIZO.Interop.Subnautica;

partial class _JukeboxDisk :
#if BELOWZERO
JukeboxDisk;
#else
UnityEngine.MonoBehaviour;
#endif
4 changes: 2 additions & 2 deletions SCHIZO/Registering/CompoundModRegistryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ protected override void Register()
{
foreach (ModRegistryItem item in registryItems)
{
item.InvokeRegister();
if (item) item.InvokeRegister();
}
}

protected override void PostRegister()
{
foreach (ModRegistryItem item in registryItems)
{
item.InvokePostRegister();
if (item) item.InvokePostRegister();
}
}
}
2 changes: 1 addition & 1 deletion SCHIZO/Resources/AssetBundles/Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SCHIZO.Resources;

public static class Assets
{
private const int _rnd = -1122530483;
private const int _rnd = 884876943;

private static readonly UnityEngine.AssetBundle _a = ResourceManager.GetAssetBundle("assets");

Expand Down
Binary file modified SCHIZO/Resources/AssetBundles/assets
Binary file not shown.
14 changes: 7 additions & 7 deletions SCHIZO/SCHIZO.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configurations>Subnautica;BelowZero</Configurations>
<NoWarn>$(NoWarn);CS0436;CS0649;Publicizer001;</NoWarn>
</PropertyGroup>

<Import Project="Subnautica.props" Condition=" '$(Configuration)' == 'Subnautica' " />
<Import Project="BelowZero.props" Condition=" '$(Configuration)' == 'BelowZero' " />
<Import Project="$(SolutionDir)/Subnautica.props" Condition=" '$(Configuration)' == 'Subnautica' " />
<Import Project="$(SolutionDir)/BelowZero.props" Condition=" '$(Configuration)' == 'BelowZero' " />

<ItemGroup>
<PackageReference Include="BepInEx.Core" Version="5.4.21" />
Expand All @@ -17,27 +17,27 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(SolutionDir)/Unity/Assets/Scripts/SCHIZO/**/*.cs" LinkBase=".">
<Compile Include="$(SolutionDir)/Unity/Assets/Scripts/SCHIZO/**/*.cs">
<DependentUpon>%(FileName).cs</DependentUpon>
</Compile>
<Compile Remove="$(SolutionDir)/Unity/Assets/Scripts/SCHIZO/**/*.Unity.cs" />

<Compile Remove="$(SolutionDir)/Unity/Assets/Scripts/SCHIZO/Creatures/_old/**/*.cs" />
<Compile Remove="$(SolutionDir)/Unity/Assets/Scripts/SCHIZO/Materials/**/*.cs" />

<Folder Include="Enums/BelowZero" />
<Folder Include="Enums/Subnautica" />
<Folder Include="Utilities" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources/**" />
<EmbeddedResource Remove="Resources/**/*.cs" />
<EmbeddedResource Remove="Resources/AssetBundles/AssetBundles" />
<EmbeddedResource Remove="Resources/AssetBundles/*.manifest" />
<None Remove="Resources/AssetBundles/AssetBundles" />
<None Remove="Resources/AssetBundles/AssetBundles.manifest" />

<None Update="Resources/AssetBundles/*.manifest">
<DependentUpon>%(FileName)</DependentUpon>
</None>
Expand Down
35 changes: 35 additions & 0 deletions SCHIZO/Sounds/Jukebox/CustomJukeboxDisk.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text;
using Nautilus.Handlers;
using Nautilus.Utility;
using UnityEngine;

namespace SCHIZO.Sounds.Jukebox;

partial class CustomJukeboxDisk
{
private CustomJukeboxTrack _track;
public AudioClip unlockSound;

public new void Start()
{
if (_track) track = _track;
if (track == default) LOGGER.LogWarning($"Jukebox disk {name} at {transform.position} was not assigned a track");

// the lore is that when you pick up a disk, AL-AN plays a snippet of it in your head
// if (unlockSound && Story.StoryGoalManager.main!?.GetAlanActor() == Actor.AlAn)
if (unlockSound) // temp for development/testing // Alex's PR comment: uuh
{
int soundLenHash = unlockSound.samples.GetHashCode();
int soundNameHash = unlockSound.name.GetHashCode();
string guid = new StringBuilder(32).Insert(0, $"{soundLenHash:x08}{soundNameHash:x08}", 2).ToString();

if (!CustomSoundHandler.TryGetCustomSound(guid, out _))
{
CustomSoundHandler.RegisterCustomSound(guid, unlockSound, "bus:/master/SFX_for_pause/PDA_pause/jukebox", AudioUtils.StandardSoundModes_2D);
}

acquireSound = AudioUtils.GetFmodAsset(guid);
}
base.Start();
}
}
118 changes: 118 additions & 0 deletions SCHIZO/Sounds/Jukebox/CustomJukeboxTrack.BelowZero.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using FMOD;
using Nautilus.Handlers;
using Nautilus.Utility;
using UnityEngine;
using BZJukebox = Jukebox;

namespace SCHIZO.Sounds.Jukebox;

public sealed partial class CustomJukeboxTrack
{
public partial struct TrackLabel
{
public override string ToString() => string.IsNullOrEmpty(artist) ? title : $"{artist} - {title}";
public static implicit operator string(TrackLabel trackLabel) => trackLabel.ToString();
}

public uint Length => audioClip ? (uint) audioClip.length * 1000 : 0;

internal Sound sound;

public bool IsSoundValid() => sound.hasHandle() && sound.getMode(out _) != RESULT.ERR_INVALID_HANDLE;

public static implicit operator BZJukebox.TrackInfo(CustomJukeboxTrack track)
{
string label = track.trackLabel;
if (label.Length == 0) label = track.identifier;
return new BZJukebox.TrackInfo { label = label, length = track.Length };
}

public static implicit operator BZJukebox.UnlockableTrack(CustomJukeboxTrack track)
=> EnumHandler.TryGetValue(track.identifier, out BZJukebox.UnlockableTrack id) ? id
: throw new ArgumentException("Track is not registered, cannot convert to Jukebox.UnlockableTrack", nameof(track));

protected override void Register()
{
LOGGER.LogDebug($"Registering custom jukebox track {identifier}");

if (EnumHandler.TryGetValue(identifier, out BZJukebox.UnlockableTrack trackId))
{
LOGGER.LogWarning($"Someone else has already registered unlockable track {identifier}! ({trackId} at {(int) trackId})");
return;
}

if (EnumHandler.TryAddEntry(identifier, out EnumBuilder<BZJukebox.UnlockableTrack> registered))
{
trackId = registered.Value;
}
else
{
LOGGER.LogError($"Could not add Jukebox.UnlockableTrack entry for {identifier}");
return;
}
CustomJukeboxTrackPatches.customTracks[trackId] = this;

if (!Player.main) return;

// if we get here, we're registering during a game
// less than ideal but let's roll with it
LOGGER.LogWarning($"Setting up unlock for track '{identifier}' during a game! This might not behave how you expect it to.");
SetupUnlock();
}

public static bool TryGetCustomTrack(string identifier, out CustomJukeboxTrack track)
{
track = null;

return identifier is not null
&& EnumHandler.TryGetValue(identifier, out BZJukebox.UnlockableTrack trackId)
&& TryGetCustomTrack(trackId, out track);
}

public static bool TryGetCustomTrack(BZJukebox.UnlockableTrack trackId, out CustomJukeboxTrack track)
=> CustomJukeboxTrackPatches.customTracks.TryGetValue(trackId, out track);

internal void SetupUnlock(BZJukebox.UnlockableTrack trackId = BZJukebox.UnlockableTrack.None)
{
if (trackId == default)
trackId = this;

if (!Player.main || !GameModeManager.HaveGameOptionsSet)
{
LOGGER.LogError($"Can't set up unlock for {trackId} with no {(!Player.main ? "player" : "game options")}!");
return;
}

if (unlockedOnStart || !GameModeManager.GetOption<bool>(GameOption.Story))
{
BZJukebox.Unlock(trackId, false);
BZJukebox.main.SetInfo(identifier, this);
}
else
{
SpawnDisk(trackId, diskSpawnLocation);
}
}

internal GameObject SpawnDisk(BZJukebox.UnlockableTrack trackId, Vector3 position)
{
bool isDefault = !diskPrefab;
GameObject prefab = !isDefault ? diskPrefab.gameObject : CustomJukeboxTrackPatches.defaultDiskPrefab;

GameObject disk = Instantiate(prefab);
disk.transform.position = position;

if (isDefault) Destroy(disk.GetComponent<JukeboxDisk>());

CustomJukeboxDisk diskComp = disk.EnsureComponent<CustomJukeboxDisk>();
diskComp.track = trackId;
diskComp.unlockSound = unlockSound;

disk.GetComponent<LargeWorldEntity>().enabled = false; // don't save

if (!isDefault) MaterialUtils.ApplySNShaders(disk, 1);

return disk;
}
}
8 changes: 8 additions & 0 deletions SCHIZO/Sounds/Jukebox/CustomJukeboxTrack.Subnautica.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SCHIZO.Sounds.Jukebox;

partial class CustomJukeboxTrack
{
protected override void Register()
{
}
}
Loading

0 comments on commit 89f27b1

Please sign in to comment.