Skip to content

Commit

Permalink
add proper music file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzyhau committed Jan 22, 2023
1 parent 2231b51 commit bffbf5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Docs/createmods.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ As an example, here's an instruction on how to change Gomez's house background p
7. In your mod's `Assets` directory, create `background planes` directory and put your XNB file there.
8. From now on Gomez's house should have your modified texture.

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`.

## Creating custom logic mod

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.
Expand Down
29 changes: 26 additions & 3 deletions Source/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,37 @@ private void TryConvertAsset()
{
// TODO: special conversion handling for different types, like images or animation

if(Extension == ".ogg" && AssetPath.StartsWith("music\\"))
{
IsMusicFile = true;
AssetPath = AssetPath.Substring("music\\".Length);
}

Converted = false;
}

public void Inject()
{
var cachedAssetsField = typeof(MemoryContentManager).GetField("cachedAssets", BindingFlags.NonPublic | BindingFlags.Static);
var cachedAssets = cachedAssetsField.GetValue(null) as Dictionary<string, byte[]>;
cachedAssets[AssetPath] = Data;
if (IsMusicFile)
{
// music files are loaded and stored separately in SoundManager service
var soundManager = ServiceHelper.Get<ISoundManager>();

// make sure music assets are already initialized. it can initialize only once so we don't have to worry
soundManager.InitializeLibrary();

var musicCacheField = typeof(SoundManager).GetField("MusicCache", BindingFlags.NonPublic | BindingFlags.Instance);
var musicCache = musicCacheField.GetValue(soundManager) as Dictionary<string, byte[]>;

musicCache[AssetPath] = Data;
}
else
{
// everything else uses static MemoryContentManager cache array
var cachedAssetsField = typeof(MemoryContentManager).GetField("cachedAssets", BindingFlags.NonPublic | BindingFlags.Static);
var cachedAssets = cachedAssetsField.GetValue(null) as Dictionary<string, byte[]>;
cachedAssets[AssetPath] = Data;
}
}

public static List<Asset> LoadDirectory(string directoryPath)
Expand Down

0 comments on commit bffbf5c

Please sign in to comment.