1
0
mirror of https://github.com/SirusDoma/VoxCharger.git synced 2024-11-28 01:10:49 +01:00

Fix Music ID Handling

Also, Music ID field is now disabled
This commit is contained in:
SirusDoma 2020-04-19 15:02:55 +07:00
parent e3564e690d
commit 2d6eb38dff
5 changed files with 418 additions and 286 deletions

View File

@ -12,6 +12,8 @@ namespace VoxCharger
{ {
#region --- Properties --- #region --- Properties ---
private static List<string> MixList = new List<string>(); private static List<string> MixList = new List<string>();
private static MusicDb InternalHeaders = null;
private static int LastOriginalID = 0;
public static string MixName { get; private set; } public static string MixName { get; private set; }
@ -32,13 +34,6 @@ namespace VoxCharger
if (!File.Exists(dbFilename)) if (!File.Exists(dbFilename))
throw new FormatException("Invalid Game Directory"); throw new FormatException("Invalid Game Directory");
// Validate whether cache exists, perform full load when cache is not available
string cacheFilename = Path.Combine(gamePath, @"data_mods\_cache\others\music_db.xml");
// Load original headers data
Headers = new MusicDb();
Headers.Load(File.Exists(cacheFilename) ? cacheFilename : dbFilename);
// Look for other mixes // Look for other mixes
MixList.Clear(); MixList.Clear();
string modsPath = Path.Combine(gamePath, @"data_mods\"); string modsPath = Path.Combine(gamePath, @"data_mods\");
@ -59,10 +54,10 @@ namespace VoxCharger
continue; continue;
// Confirmed mod path, append into music db, ignore cache to avoid uncached mix being excluded // Confirmed mod path, append into music db, ignore cache to avoid uncached mix being excluded
Headers.Load(dbFilename, true);
MixList.Add(modName); MixList.Add(modName);
} }
LastOriginalID = 0;
GamePath = gamePath; GamePath = gamePath;
} }
@ -77,6 +72,9 @@ namespace VoxCharger
Directory.CreateDirectory(Path.Combine(mixPath, @"music\")); Directory.CreateDirectory(Path.Combine(mixPath, @"music\"));
Directory.CreateDirectory(Path.Combine(mixPath, @"others\")); Directory.CreateDirectory(Path.Combine(mixPath, @"others\"));
// Load Existing song DB to avoid duplicate id
LoadInternalDb(mixName);
// Create empty db // Create empty db
MdbFilename = Path.Combine(mixPath, @"others\music_db.merged.xml"); MdbFilename = Path.Combine(mixPath, @"others\music_db.merged.xml");
File.WriteAllText(MdbFilename, "<?xml version=\"1.0\" encoding=\"Shift_JIS\"?><mdb></mdb>"); File.WriteAllText(MdbFilename, "<?xml version=\"1.0\" encoding=\"Shift_JIS\"?><mdb></mdb>");
@ -93,7 +91,8 @@ namespace VoxCharger
if (!Directory.Exists(mixPath)) if (!Directory.Exists(mixPath))
throw new DirectoryNotFoundException("Mix directory missing"); throw new DirectoryNotFoundException("Mix directory missing");
// No way it happen since combo box is dropdownlist, but well.. :v // Load Existing song DB to avoid duplicate id
LoadInternalDb(mixName);
if (!string.IsNullOrEmpty(mixName) && !MixList.Contains(mixName)) if (!string.IsNullOrEmpty(mixName) && !MixList.Contains(mixName))
MixList.Add(mixName); MixList.Add(mixName);
@ -110,6 +109,38 @@ namespace VoxCharger
return MixList.ToArray(); return MixList.ToArray();
} }
private static void LoadInternalDb(string mixName)
{
// First, we need to populate available ids outside selected mix
// This will prevent duplicate ID not only with originals but with other mixes too
// Load original music, ignore cache to avoid uncached mix being excluded or included
string dbFilename = Path.Combine(GamePath, @"data\others\music_db.xml");
string modsPath = Path.Combine(GamePath, @"data_mods\");
// Load original headers data
InternalHeaders = new MusicDb();
InternalHeaders.Load(dbFilename);
LastOriginalID = InternalHeaders.LastID;
// Load other music db
foreach (var modDir in Directory.GetDirectories(modsPath))
{
// Get directory name, exclude selected mix
string modName = new DirectoryInfo(modDir).Name;
if (modName == "_cache" || modName == mixName)
continue;
// Validate whether the mod is a mix mod
// Do not skip unsupported mods, just read the db and ignore the rest of assets
dbFilename = Path.Combine(modDir, @"others\music_db.merged.xml");
if (!File.Exists(dbFilename))
continue;
// Confirmed mod path, append into music db
InternalHeaders.Load(dbFilename, true);
}
}
#endregion #endregion
#region --- Asset Management --- #region --- Asset Management ---
@ -217,6 +248,23 @@ namespace VoxCharger
#endregion #endregion
#region --- Asset Identifier --- #region --- Asset Identifier ---
public static int GetNextMusicID()
{
// TODO: This probably inefficient in scenario where one or more mix has gap between each ids
// This could be waste to those gaps, and eat up our precious limited id
// However, these gaps may indicate deleted song, which should be taken by omnimix
int id = InternalHeaders.LastID + 1;
while (InternalHeaders.Contains(id) || Headers.Contains(id)) // Contains is O(1) so its should be fine
id++;
return id;
}
public static bool ValidateMusicID(int id)
{
return !InternalHeaders.Contains(id);
}
public static string GetDifficultyCodes(VoxHeader header, Difficulty difficulty) public static string GetDifficultyCodes(VoxHeader header, Difficulty difficulty)
{ {

View File

@ -39,6 +39,7 @@ namespace VoxCharger
public VoxHeader Header { get; private set; } = null; public VoxHeader Header { get; private set; } = null;
public Action Action { get; private set; } = null; public Action Action { get; private set; } = null;
public Ksh.ParseOption Options { get; private set; } = new Ksh.ParseOption();
public ConverterForm(string path, bool asConverter = false) public ConverterForm(string path, bool asConverter = false)
{ {
@ -110,30 +111,12 @@ namespace VoxCharger
MessageBoxIcon.Error MessageBoxIcon.Error
); );
CancelButton.PerformClick(); Close();
return;
} }
// Try to locate another difficulty // Try to locate another difficulty
string dir = Path.GetDirectoryName(target); charts = GetCharts(target, main);
foreach (string fn in Directory.GetFiles(dir, "*.ksh"))
{
try
{
var chart = new Ksh();
chart.Parse(fn);
// Different chart
if (chart.Title != main.Title)
continue;
charts[chart.Difficulty] = new ChartInfo(chart, ToLevelHeader(chart), fn);
}
catch (Exception ex)
{
Debug.WriteLine("Failed attempt to parse ksh file: {0} ({1})", fn, ex.Message);
}
}
UpdateUI(); UpdateUI();
} }
@ -207,8 +190,7 @@ namespace VoxCharger
private void OnProcessConvertButtonClick(object sender, EventArgs e) private void OnProcessConvertButtonClick(object sender, EventArgs e)
{ {
bool warned = false; Options = new Ksh.ParseOption()
var options = new Ksh.ParseOption()
{ {
RealignOffset = RealignOffsetCheckBox.Checked, RealignOffset = RealignOffsetCheckBox.Checked,
EnableChipFx = ChipFxCheckBox.Checked, EnableChipFx = ChipFxCheckBox.Checked,
@ -221,48 +203,49 @@ namespace VoxCharger
// Act as converter // Act as converter
if (converter) if (converter)
Convert();
else
Process();
}
private VoxHeader ToHeader(Ksh chart)
{
return new VoxHeader()
{
ID = AssetManager.GetNextMusicID(),
Title = chart.Title,
Artist = chart.Artist,
BpmMin = chart.BpmMin,
BpmMax = chart.BpmMax,
Volume = chart.Volume > 0 ? (short)chart.Volume : (short)91,
DistributionDate = DateTime.Now,
BackgroundId = 63,
GenreId = 16,
};
}
private VoxLevelHeader ToLevelHeader(Ksh chart)
{
return new VoxLevelHeader
{
Difficulty = chart.Difficulty,
Illustrator = chart.Illustrator,
Effector = chart.Effector,
Level = chart.Level
};
}
private void Process()
{ {
try try
{ {
if (File.Exists(target) || Directory.Exists(target)) bool warned = false;
{
if (File.Exists(target))
SingleConvert(options);
else if (Directory.Exists(target))
BulkConvert(options);
}
else
{
MessageBox.Show(
"Target path not found",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
Close();
}
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to convert ksh chart.\n{ex.Message}",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
return;
}
// Again, stupid input get stupid output
foreach (var header in AssetManager.Headers) foreach (var header in AssetManager.Headers)
{ {
if (Header.Ascii == header.Ascii) if (Header.Ascii == header.Ascii)
{ {
MessageBox.Show( MessageBox.Show(
$"Music Code is already exists.\n{AssetManager.GetMusicPath(header)}", $"Music Code is already exists.\n{AssetManager.GetMusicPath(header).Replace(AssetManager.GamePath, "")}",
"Error", "Error",
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Error MessageBoxIcon.Error
@ -280,7 +263,6 @@ namespace VoxCharger
Header.GenreId = 16; Header.GenreId = 16;
Header.Levels = new Dictionary<Difficulty, VoxLevelHeader>(); Header.Levels = new Dictionary<Difficulty, VoxLevelHeader>();
// Again, stupid input get stupid output
if (Directory.Exists(AssetManager.GetMusicPath(Header))) if (Directory.Exists(AssetManager.GetMusicPath(Header)))
{ {
MessageBox.Show( MessageBox.Show(
@ -320,10 +302,10 @@ namespace VoxCharger
// If you happen to read the source, this is probably what you're looking for // If you happen to read the source, this is probably what you're looking for
var ksh = new Ksh(); var ksh = new Ksh();
ksh.Parse(info.FileName, options); ksh.Parse(info.FileName, Options);
var bpmCount = ksh.Events.Count(ev => ev is Event.BPM); var bpmCount = ksh.Events.Count(ev => ev is Event.BPM);
if (!warned && bpmCount > 1 && ksh.MusicOffset % 48 != 0 && options.RealignOffset) if (!warned && bpmCount > 1 && ksh.MusicOffset % 48 != 0 && Options.RealignOffset)
{ {
// You've been warned! // You've been warned!
var prompt = MessageBox.Show( var prompt = MessageBox.Show(
@ -340,6 +322,7 @@ namespace VoxCharger
return; return;
} }
// Conversion is actually boring because its already "pre-converted"
var vox = new VoxChart(); var vox = new VoxChart();
vox.Import(ksh); vox.Import(ksh);
@ -441,35 +424,88 @@ namespace VoxCharger
} }
}); });
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to import ksh chart.\n{ex.Message}",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
private void Convert()
{
// Only serve as options dialog
if (string.IsNullOrEmpty(target))
{
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
Close(); Close();
} }
private VoxHeader ToHeader(Ksh chart) try
{ {
return new VoxHeader() if (File.Exists(target) || Directory.Exists(target))
{ {
ID = AssetManager.Headers.LastID + 1, if (File.Exists(target))
Title = chart.Title, SingleConvert(Options);
Artist = chart.Artist, else if (Directory.Exists(target))
BpmMin = chart.BpmMin, BulkConvert(Options);
BpmMax = chart.BpmMax, }
Volume = chart.Volume > 0 ? (short)chart.Volume : (short)91, else
DistributionDate = DateTime.Now, {
BackgroundId = 63, MessageBox.Show(
GenreId = 16, "Target path not found",
}; "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
DialogResult = DialogResult.Cancel;
Close();
}
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to convert ksh chart.\n{ex.Message}",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
} }
private VoxLevelHeader ToLevelHeader(Ksh chart) private Dictionary<Difficulty, ChartInfo> GetCharts(string target, Ksh main)
{ {
return new VoxLevelHeader // Try to locate another difficulty
string dir = Path.GetDirectoryName(target);
var charts = new Dictionary<Difficulty, ChartInfo>();
foreach (string fn in Directory.GetFiles(dir, "*.ksh"))
{ {
Difficulty = chart.Difficulty, try
Illustrator = chart.Illustrator, {
Effector = chart.Effector, var chart = new Ksh();
Level = chart.Level chart.Parse(fn);
};
// Different chart
if (chart.Title != main.Title)
continue;
charts[chart.Difficulty] = new ChartInfo(chart, ToLevelHeader(chart), fn);
}
catch (Exception ex)
{
Debug.WriteLine("Failed attempt to parse ksh file: {0} ({1})", fn, ex.Message);
}
}
return charts;
} }
private void LoadJacket(ChartInfo info) private void LoadJacket(ChartInfo info)
@ -608,6 +644,7 @@ namespace VoxCharger
MessageBoxIcon.Information MessageBoxIcon.Information
); );
DialogResult = DialogResult.OK;
Close(); Close();
} }
} }
@ -692,6 +729,7 @@ namespace VoxCharger
MessageBoxIcon.Information MessageBoxIcon.Information
); );
DialogResult = DialogResult.OK;
Close(); Close();
} }
} }

View File

@ -858,9 +858,9 @@
this.IdTextBox.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.IdTextBox.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.IdTextBox.Location = new System.Drawing.Point(83, 23); this.IdTextBox.Location = new System.Drawing.Point(83, 23);
this.IdTextBox.Name = "IdTextBox"; this.IdTextBox.Name = "IdTextBox";
this.IdTextBox.ReadOnly = true;
this.IdTextBox.Size = new System.Drawing.Size(383, 21); this.IdTextBox.Size = new System.Drawing.Size(383, 21);
this.IdTextBox.TabIndex = 1; this.IdTextBox.TabIndex = 1;
this.IdTextBox.TextChanged += new System.EventHandler(this.OnMetadataChanged);
// //
// IdLabel // IdLabel
// //

View File

@ -107,7 +107,8 @@ namespace VoxCharger
{ {
try try
{ {
Save(AssetManager.MdbFilename); if (Save(AssetManager.MdbFilename))
{
MessageBox.Show( MessageBox.Show(
"Mix has been saved successfully", "Mix has been saved successfully",
"Information", "Information",
@ -115,6 +116,7 @@ namespace VoxCharger
MessageBoxIcon.Information MessageBoxIcon.Information
); );
} }
}
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show( MessageBox.Show(
@ -139,7 +141,8 @@ namespace VoxCharger
if (exporter.ShowDialog() != DialogResult.OK) if (exporter.ShowDialog() != DialogResult.OK)
return; return;
Save(exporter.FileName); if (Save(exporter.FileName))
{
MessageBox.Show( MessageBox.Show(
"Mix has been saved successfully", "Mix has been saved successfully",
"Information", "Information",
@ -148,6 +151,7 @@ namespace VoxCharger
); );
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show( MessageBox.Show(
@ -491,7 +495,16 @@ namespace VoxCharger
return; return;
if (int.TryParse(IdTextBox.Text, out int id)) if (int.TryParse(IdTextBox.Text, out int id))
{
// Validate ID
if (!AssetManager.ValidateMusicID(id))
{
IdTextBox.Text = header.ID.ToString();
MessageBox.Show("Music ID is already taken", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
header.ID = id; header.ID = id;
}
else else
IdTextBox.Text = header.ID.ToString(); IdTextBox.Text = header.ID.ToString();
@ -703,19 +716,21 @@ namespace VoxCharger
} }
} }
private void Save(string dbFilename) private bool Save(string dbFilename)
{ {
var errors = new List<string>();
using (var loader = new LoadingForm()) using (var loader = new LoadingForm())
{ {
var proc = new Action(() => var proc = new Action(() =>
{ {
int max = actions.Count + 1; int max = actions.Count + 1;
foreach (var queue in actions.Values) foreach (var action in actions)
{ {
float progress = ((float)(max - actions.Count) / max) * 100f; float progress = ((float)(max - actions.Count) / max) * 100f;
loader.SetStatus($"[{progress:00}%] - Processing assets.."); loader.SetStatus($"[{progress:00}%] - Processing assets..");
loader.SetProgress(progress); loader.SetProgress(progress);
var queue = action.Value;
while (queue.Count > 0) while (queue.Count > 0)
{ {
try try
@ -724,6 +739,7 @@ namespace VoxCharger
} }
catch (Exception ex) catch (Exception ex)
{ {
errors.Add($"{action.Key}: {ex.Message}");
Debug.WriteLine(ex.Message); Debug.WriteLine(ex.Message);
} }
} }
@ -740,8 +756,19 @@ namespace VoxCharger
loader.ShowDialog(); loader.ShowDialog();
} }
if (errors.Count > 0)
{
string message = "Error occured when processing following assets:\n";
foreach (var err in errors)
message += $"\n{err}";
MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
actions.Clear(); actions.Clear();
Pristine = true; Pristine = true;
return errors.Count == 0;
} }
private void Import2DX(bool preview = false) private void Import2DX(bool preview = false)
@ -880,6 +907,8 @@ namespace VoxCharger
control.Enabled = safe; control.Enabled = safe;
} }
// Modifying this could lead into disaster, must be left untouched
IdTextBox.ReadOnly = true;
LevelGroupBox.Enabled = true; LevelGroupBox.Enabled = true;
foreach (Control control in LevelGroupBox.Controls) foreach (Control control in LevelGroupBox.Controls)
{ {

View File

@ -15,11 +15,12 @@ namespace VoxCharger
private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("Shift_JIS"); private static readonly Encoding DefaultEncoding = Encoding.GetEncoding("Shift_JIS");
private Dictionary<int, VoxHeader> headers; private Dictionary<int, VoxHeader> headers;
private int max = 0;
public int LastID => headers.Count > 0 ? headers.Values.Max(h => h.ID) : 0;
public int Count => headers.Count; public int Count => headers.Count;
public int LastID => headers.Values.Max(h => h.ID);
public MusicDb() public MusicDb()
{ {
headers = new Dictionary<int, VoxHeader>(); headers = new Dictionary<int, VoxHeader>();
@ -178,17 +179,33 @@ namespace VoxCharger
public void Add(VoxHeader header) public void Add(VoxHeader header)
{ {
if (max < header.ID)
max = header.ID;
headers[header.ID] = header; headers[header.ID] = header;
} }
public void Remove(int id) public void Remove(int id)
{ {
if (max == id)
max = 0;
headers.Remove(id); headers.Remove(id);
} }
public void Remove(VoxHeader header) public void Remove(VoxHeader header)
{ {
headers.Remove(header.ID); Remove(header.ID);
}
public bool Contains(int id)
{
return headers.ContainsKey(id);
}
public bool Contains(VoxHeader header)
{
return Contains(header.ID);
} }
public VoxHeader GetHeader(int id) public VoxHeader GetHeader(int id)