Skip to content

Commit

Permalink
Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
hmqgg committed Nov 30, 2020
0 parents commit 27ed4b7
Show file tree
Hide file tree
Showing 14 changed files with 835 additions and 0 deletions.
442 changes: 442 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Mitch Hwang <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 25 additions & 0 deletions Milil.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30608.117
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Milil", "Milil\Milil.csproj", "{D5FDCD8C-6E13-4D96-9943-67ABD79E921C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D5FDCD8C-6E13-4D96-9943-67ABD79E921C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5FDCD8C-6E13-4D96-9943-67ABD79E921C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5FDCD8C-6E13-4D96-9943-67ABD79E921C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5FDCD8C-6E13-4D96-9943-67ABD79E921C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCA9A0B7-ABDE-4A9E-B962-63217F3BF9C1}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions Milil/Milil.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dahomey.Json" Version="1.10.2" />
<PackageReference Include="shortid" Version="3.0.0" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Milil/Models/BaseObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class BaseObject
{
[JsonPropertyName("_id")]
public string Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("flags")]
public Flags Flags { get; set; } = new Flags();
}
}
7 changes: 7 additions & 0 deletions Milil/Models/Flags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Milil.Models
{
public class Flags
{
// Empty object as for now.
}
}
41 changes: 41 additions & 0 deletions Milil/Models/Module.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class Module
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("author")]
public string Author { get; set; } = "";

[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; } = string.Empty;

[JsonPropertyName("version")]
public string Version { get; set; } = "1.0.0";

[JsonPropertyName("minimumCoreVersion")]
public string MinimumCoreVersion { get; set; } = "0.5.0";

[JsonPropertyName("compatibleCoreVersion")]
public string CompatibleCoreVersion { get; set; } = "1.0.0";

[JsonPropertyName("packs")]
public List<PackFile> Packs { get; set; } = new List<PackFile>();

[JsonPropertyName("url")]
public string Url { get; set; } = string.Empty;

[JsonPropertyName("manifest")]
public string Manifest { get; set; } = string.Empty;

[JsonPropertyName("download")]
public string Download { get; set; } = string.Empty;
}
}
11 changes: 11 additions & 0 deletions Milil/Models/Pack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class Pack : BaseObject
{
[JsonPropertyName("permission")]
public Dictionary<string, int> Permission { get; set; }
}
}
19 changes: 19 additions & 0 deletions Milil/Models/PackFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class PackFile
{
[JsonPropertyName("name")]
public string Name { get; set; } = "playlists";

[JsonPropertyName("label")]
public string Label { get; set; }

[JsonPropertyName("path")]
public string Path { get; set; }

[JsonPropertyName("entity")]
public string Entity { get; set; } = "Playlist";
}
}
19 changes: 19 additions & 0 deletions Milil/Models/Sound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class Sound : BaseObject
{
[JsonPropertyName("path")]
public string Path { get; set; }

[JsonPropertyName("repeat")]
public bool Repeat { get; set; }

[JsonPropertyName("volume")]
public double Volume { get; set; }

[JsonPropertyName("playing")]
public bool Playing { get; set; }
}
}
17 changes: 17 additions & 0 deletions Milil/Models/SoundPack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Milil.Models
{
public class SoundPack : Pack
{
[JsonPropertyName("mode")]
public long Mode { get; set; }

[JsonPropertyName("playing")]
public bool Playing { get; set; }

[JsonPropertyName("sounds")]
public List<Sound> Sounds { get; set; }
}
}
146 changes: 146 additions & 0 deletions Milil/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using Dahomey.Json;
using Milil.Models;
using shortid;
using shortid.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Milil
{
public sealed class Program
{
private static readonly string[] AvailableExtensions = { ".ogg", ".mp3", ".flac", ".wav", ".webm" };

private const string PREFIX = "modules";

private const string DB_FILE = "playlists.db";

private const string MODULE_FILE = "module.json";

private static readonly byte[] NewLineBytes = Encoding.UTF8.GetBytes("\n");

private static readonly GenerationOptions IdRules = new GenerationOptions
{
Length = 16,
UseNumbers = true,
UseSpecialCharacters = false
};

private static readonly JsonSerializerOptions ModuleSerializationRules = new JsonSerializerOptions
{
WriteIndented = true,
}.SetupExtensions();

private static string GetId() => ShortId.Generate(IdRules);

public static async Task Main()
{
var root = new DirectoryInfo(".");

var subs = root.EnumerateDirectories().Where(x =>
x.EnumerateFiles().Any(f =>
AvailableExtensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase))).ToList();

if (!subs.Any())
{
// No sounds, return.
await Console.Error.WriteLineAsync("No sounds found in top directories.");
return;
}

var rootName = Path.Combine(PREFIX, root.Name);
var permission = new Dictionary<string, int>
{
{ "default", 0 },
{ GetId(), 3 }
};

// Playlists.db
await using var dbFile = File.Open(DB_FILE, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

foreach (var sub in subs)
{
var sounds = sub.EnumerateFiles()
.Where(f => AvailableExtensions.Contains(f.Extension, StringComparer.OrdinalIgnoreCase))
.Select(s => new Sound
{
Id = GetId(),
Name = Path.GetFileNameWithoutExtension(s.Name),
Path = Path.Combine(rootName, sub.Name, s.Name).Replace("#", "%23"),
Playing = false,
Repeat = false,
Volume = 0.50
});

var pack = new SoundPack
{
Id = GetId(),
Name = sub.Name,
Mode = -1,
Permission = permission,
Playing = false,
Sounds = sounds.ToList()
};

var success = false;

try
{
await JsonSerializer.SerializeAsync(dbFile, pack);
await dbFile.WriteAsync(NewLineBytes);

success = true;
}
catch (JsonException jEx)
{
await Console.Error.WriteLineAsync(jEx.Message);
}
catch (IOException ioE)
{
await Console.Error.WriteLineAsync(ioE.Message);
}

if (success)
{
Console.WriteLine($"SUC: {sub.Name}");
}
else
{
await Console.Error.WriteLineAsync($"ERR: {sub.Name}");
}
}

var flushPackTask = dbFile.FlushAsync().ConfigureAwait(false);

// module.json
var module = new Module
{
Name = root.Name,
Title = root.Name,
Packs = new List<PackFile>
{
new PackFile
{
Name = Path.GetFileNameWithoutExtension(DB_FILE),
Label = root.Name,
Path = string.Concat("/", DB_FILE),
Entity = "Playlist"
}
}
};

await using var moduleFile = File.Open(MODULE_FILE, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
await JsonSerializer.SerializeAsync(moduleFile, module, ModuleSerializationRules);
var flushModuleTask = moduleFile.FlushAsync().ConfigureAwait(false);

await flushPackTask;
await flushModuleTask;
Console.WriteLine("Completed!");
}
}
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Milil
A simple tool to turn your music collections into Foundry VTT Module with playlists.

[English README](README.md) | [中文说明](README_CN.md)

## Installation
Milil itself does not need installation, it's a Click-to-Run program.

However, you need `.NET 5.0` Runtime to run it or download a self-contained Milil instead. *Milil size <1M, but self-contained Milil size >50M.*

## Usage
1. Ensure your music collection directory structure to be organized as below.
```
📦My_Music
┣ 📂1st Playlist
┃ ┣ 🎵Daredevil (Extended).ogg
┃ ┗ 🎵The Unsung War.ogg
┣ 📂2nd Playlist
┃ ┣ 🎵Aksis_Theme.ogg
┃ ┗ 🎵Journey (feat. Kronos Quartet).ogg
┗ 🤖Milil.exe
```
2. Run `Milil.exe` and it will generate `module.json` and `playlists.db`.
3. Modify the generated files and delete `Milil.exe`.
3. Move the whole root directory under `Data/modules/` that Foundry VTT configured.

## Limitation
- Currently, Milil will ignore all 2nd level and deeper directories and their files/musics.
- The root directory name must be a valid Foundry VTT module name (e.g. `My_Music`).
- A valid filename might be URL unsafe and not handled by Milil and Foundry VTT itself. If so, it'd be unplayable.
Loading

0 comments on commit 27ed4b7

Please sign in to comment.