Skip to content

Commit bffbf5c

Browse files
committed
add proper music file handling
1 parent 2231b51 commit bffbf5c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Docs/createmods.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ As an example, here's an instruction on how to change Gomez's house background p
4444
7. In your mod's `Assets` directory, create `background planes` directory and put your XNB file there.
4545
8. From now on Gomez's house should have your modified texture.
4646

47+
A small note regarding music files: since they're normally stored in a separate `.pak` archive (`Music.pak`) and handled by a separate subsystem, music files are organized in a root directory. It is **not** the case for HAT mods, and instead it looks for OGG files (audio format used by music in this game) in `[Your mod]/Assets/Music` directory, then uses a path relative to this directory to identify the music file. For example, in order to replace `villageville\bed` music file, your new music file needs to be located at `[Your mod]/Assets/Music/villageville/bed.ogg`.
48+
4749
## Creating custom logic mod
4850

4951
Mod loader loads library file given in metadata as an assembly, then attempts to create instances of every public class inheriting from game's `IGameComponent` interface before initialization (before any services are created). After the game has been initialized (that is, as soon as all necessary services are initiated), it adds created instances into the list of game's components and initializes them, allowing their `Update` and `Draw` (use `DrawableGameComponent`) to be properly executed within the game's loop.

Source/Asset.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,37 @@ private void TryConvertAsset()
3131
{
3232
// TODO: special conversion handling for different types, like images or animation
3333

34+
if(Extension == ".ogg" && AssetPath.StartsWith("music\\"))
35+
{
36+
IsMusicFile = true;
37+
AssetPath = AssetPath.Substring("music\\".Length);
38+
}
39+
3440
Converted = false;
3541
}
3642

3743
public void Inject()
3844
{
39-
var cachedAssetsField = typeof(MemoryContentManager).GetField("cachedAssets", BindingFlags.NonPublic | BindingFlags.Static);
40-
var cachedAssets = cachedAssetsField.GetValue(null) as Dictionary<string, byte[]>;
41-
cachedAssets[AssetPath] = Data;
45+
if (IsMusicFile)
46+
{
47+
// music files are loaded and stored separately in SoundManager service
48+
var soundManager = ServiceHelper.Get<ISoundManager>();
49+
50+
// make sure music assets are already initialized. it can initialize only once so we don't have to worry
51+
soundManager.InitializeLibrary();
52+
53+
var musicCacheField = typeof(SoundManager).GetField("MusicCache", BindingFlags.NonPublic | BindingFlags.Instance);
54+
var musicCache = musicCacheField.GetValue(soundManager) as Dictionary<string, byte[]>;
55+
56+
musicCache[AssetPath] = Data;
57+
}
58+
else
59+
{
60+
// everything else uses static MemoryContentManager cache array
61+
var cachedAssetsField = typeof(MemoryContentManager).GetField("cachedAssets", BindingFlags.NonPublic | BindingFlags.Static);
62+
var cachedAssets = cachedAssetsField.GetValue(null) as Dictionary<string, byte[]>;
63+
cachedAssets[AssetPath] = Data;
64+
}
4265
}
4366

4467
public static List<Asset> LoadDirectory(string directoryPath)

0 commit comments

Comments
 (0)