Skip to content

Commit

Permalink
add integrated BGM support
Browse files Browse the repository at this point in the history
  • Loading branch information
guerro323 committed Jul 31, 2020
1 parent ff63428 commit eade7bd
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 40 deletions.
1 change: 1 addition & 0 deletions PataNext.Export.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static async Task Main(string[] args)
gameBootstrap.GameEntity.Set(typeof(PataNext.Simulation.Client.Module));
gameBootstrap.GameEntity.Set(typeof(Feature.RhythmEngineAudio.CustomModule));
gameBootstrap.GameEntity.Set(typeof(PataNext.Game.Module));
gameBootstrap.GameEntity.Set(typeof(PataNext.Game.Client.Resources.Module));

foreach (var clientData in clientDirectory.GetFiles("*.json", SearchOption.TopDirectoryOnly))
{
Expand Down
5 changes: 3 additions & 2 deletions PataNext.Feature.RhythmEngineAudio/BGM/BgmStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string pattern)
{
var founds = new List<IFile>();

using (var zip = new ZipFile(computedPath))
using (var stream = new MemoryStream(await Source.GetContentAsync()))
using (var zip = new ZipFile(stream))
{
foreach (ZipEntry entry in zip)
if (FileSystemName.MatchesSimpleExpression(pattern, entry.Name))
founds.Add(new ZipEntryFile(computedPath, entry));
founds.Add(new ZipEntryFile(Source, entry));
}

return founds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class BgmDefaultSamplesLoader : BgmSamplesLoaderBase

public BgmDefaultSamplesLoader(BgmStore store) : base(store)
{
Console.WriteLine(store);
Console.WriteLine(store.CurrentPath);
foreach (var file in store.GetFilesAsync("*.*").Result)
Console.WriteLine(file.FullName);

commandsTaskMap = new TaskMap<CharBuffer64, ComboBasedCommand>(async key =>
{
var files = (await Store.GetFilesAsync($"commands/{key}/*.ogg"))
Expand All @@ -37,6 +42,7 @@ public BgmDefaultSamplesLoader(BgmStore store) : base(store)
if (files.Length == 0)
throw new InvalidOperationException($"No files found for soundtrack");
Console.WriteLine(">>> " + files.Length);
return new SlicedSoundTrack(files);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ protected override void OnUpdate()

if (currentLoadedBgm.Span.SequenceEqual(LocalInformation.ActiveBgmId.Span) && isBgmLoaded)
return;


Console.WriteLine(">>>> 0");
currentLoadedBgm = LocalInformation.ActiveBgmId;
if (currentLoadedBgm.GetLength() == 0)
return;

isBgmLoaded = false;
isBgmLoaded = true;
Console.WriteLine(">>>> 1");

// We use a double scheduler strategy.
// - First schedule from the client app (main thread) to get the requested BGM file.
Expand All @@ -49,11 +51,10 @@ protected override void OnUpdate()
where ent.Has<BgmFile>()
select ent.Get<BgmFile>())
.FirstOrDefault(bmgFile => bmgFile.Description.Id.AsSpan().SequenceEqual(currentLoadedBgm.Span));
//Console.WriteLine($"File exist for '{currentLoadedBgm}' ? {file != null}");
if (file == null)
return;
scheduler.Schedule(LoadBgm, file, default);
}, default);
}
Expand All @@ -62,8 +63,7 @@ private void LoadBgm(BgmFile file)
{
// since the description has been already computed, there is no need to await the result
var director = BgmDirector.Create(file).Result;

var entity = World.Mgr.CreateEntity();
var entity = World.Mgr.CreateEntity();
entity.Set(director);

isBgmLoaded = true;
Expand Down
Binary file not shown.
40 changes: 40 additions & 0 deletions PataNext.Game.Client.Resources/Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using DefaultEcs;
using GameHost.Core.Modules;
using GameHost.Injection;
using GameHost.IO;
using GameHost.Simulation.Application;
using GameHost.Threading;
using GameHost.Worlds;
using PataNext.Game.BGM;
using PataNext.Game.Client.Resources;

[assembly: RegisterAvailableModule("PataNext.Game.Client.Resources", "guerro", typeof(Module))]

namespace PataNext.Game.Client.Resources
{
public class Module : GameHostModule
{
public Module(Entity source, Context ctxParent, GameHostModuleDescription description) : base(source, ctxParent, description)
{
Console.WriteLine("Resources module loaded");

var global = new ContextBindingStrategy(ctxParent, true).Resolve<GlobalWorld>();
Storage.Subscribe((_,exteriorStorage) =>
{
var storage = exteriorStorage switch
{
{} => new StorageCollection{exteriorStorage, DllStorage},
null => new StorageCollection {DllStorage}
};
global.Context.BindExisting(new BgmContainerStorage(storage.GetOrCreateDirectoryAsync("Bgm").Result));
foreach (ref readonly var listener in global.World.Get<IListener>())
{
if (listener is SimulationApplication simulationApplication)
simulationApplication.Data.Context.BindExisting(new BgmContainerStorage(storage.GetOrCreateDirectoryAsync("Bgm").Result));
}
}, true);
}
}
}
2 changes: 1 addition & 1 deletion PataNext.Game/BGM/BgmContainerStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PataNext.Game.BGM
/// </summary>
public class BgmContainerStorage : IStorage
{
private IStorage parent;
public readonly IStorage parent;

public BgmContainerStorage(IStorage parent)
{
Expand Down
5 changes: 3 additions & 2 deletions PataNext.Game/BGM/BgmFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public async Task ComputeDescription()

private async Task FromZip()
{
using var archive = ZipFile.OpenRead(FullName);
var descEntry = archive.GetEntry("description.json");
await using var memoryStream = new MemoryStream(await GetContentAsync());
using var archive = new ZipArchive(memoryStream, ZipArchiveMode.Read);
var descEntry = archive.GetEntry("description.json");

// TODO: file errors should be more explicit to the end user
if (descEntry == null)
Expand Down
13 changes: 0 additions & 13 deletions PataNext.Game/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ public Module(Entity source, Context ctxParent, GameHostModuleDescription descri
var global = new ContextBindingStrategy(ctxParent, true).Resolve<GlobalWorld>();
var systems = new List<Type>();
AppSystemResolver.ResolveFor<SimulationApplication>(GetType().Assembly, systems);

Storage.Subscribe((_, storage) =>
{
if (storage == null)
return;
global.Context.BindExisting(new BgmContainerStorage(storage.GetOrCreateDirectoryAsync("Bgm").Result));
foreach (ref readonly var listener in global.World.Get<IListener>())
{
if (listener is SimulationApplication simulationApplication)
simulationApplication.Data.Context.BindExisting(new BgmContainerStorage(storage.GetOrCreateDirectoryAsync("Bgm").Result));
}
}, true);

foreach (ref readonly var listener in global.World.Get<IListener>())
{
Expand Down
57 changes: 41 additions & 16 deletions PataNext.Game/NotifyBgmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using System.Collections.Generic;
using System.IO;
using GameHost.Core.Ecs;
using GameHost.Core.IO;
using GameHost.IO;
using GameHost.Simulation.Application;
using System.Linq;
using PataNext.Client.Systems;
using PataNext.Game.BGM;

Expand All @@ -18,24 +21,41 @@ public NotifyBgmManager(WorldCollection collection) : base(collection)
DependencyResolver.Add(() => ref bgmStorage);
}

private FileSystemWatcher watcher;
private List<FileSystemWatcher> watchers;

protected override void OnDependenciesResolved(IEnumerable<object> dependencies)
{
base.OnDependenciesResolved(dependencies);
watcher = new FileSystemWatcher(bgmStorage.CurrentPath);
watcher.Filters.Add("*.zip");
watcher.Filters.Add("*.json");
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.CreationTime;

watcher.Created += onChange;
watcher.Changed += onChange;
watcher.Deleted += onChange;
watcher.Renamed += onChange;

watcher.EnableRaisingEvents = true;
watchers = new List<FileSystemWatcher>();
switch (bgmStorage.parent)
{
case LocalStorage localStorage:
watchers.Add(new FileSystemWatcher(localStorage.CurrentPath));
break;
case StorageCollection collection:
{
foreach (var storage in collection)
if (storage is LocalStorage local)
watchers.Add(new FileSystemWatcher(local.CurrentPath));
break;
}
}

foreach (var watcher in watchers)
{
watcher.Filters.Add("*.zip");
watcher.Filters.Add("*.json");
watcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.CreationTime;

watcher.Created += onChange;
watcher.Changed += onChange;
watcher.Deleted += onChange;
watcher.Renamed += onChange;

watcher.EnableRaisingEvents = true;
}
}

private void onChange(object source, FileSystemEventArgs e)
Expand All @@ -49,8 +69,13 @@ public override void Dispose()
{
base.Dispose();

watcher.EnableRaisingEvents = false;
watcher.Dispose();
foreach (var watcher in watchers)
{
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}

watchers.Clear();
}
}
}

0 comments on commit eade7bd

Please sign in to comment.