Skip to content

Commit 3d43322

Browse files
author
Matthew Bate
committed
Import of MPL files from BMTv3.5 Remote and Server Side
1 parent 05d565e commit 3d43322

File tree

3 files changed

+211
-45
lines changed

3 files changed

+211
-45
lines changed

BHD-ServerManager/Classes/InstanceManagers/mapInstanceManager.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using HawkSyncShared;
2-
using HawkSyncShared.SupportClasses;
3-
using BHD_ServerManager.Classes.GameManagement;
4-
using HawkSyncShared.Instances;
5-
using HawkSyncShared.ObjectClasses;
1+
using BHD_ServerManager.Classes.GameManagement;
62
using BHD_ServerManager.Classes.SupportClasses;
73
using BHD_ServerManager.Forms;
4+
using HawkSyncShared;
5+
using HawkSyncShared.Instances;
6+
using HawkSyncShared.ObjectClasses;
7+
using HawkSyncShared.SupportClasses;
8+
using System.Collections.Generic;
89
using System.Text;
910
using System.Text.Json;
1011
using Windows.Gaming.Input;
@@ -65,9 +66,15 @@ public static AvailableMapsResult LoadAvailableMaps()
6566
{
6667
try
6768
{
69+
mapInstance.DefaultMaps.Clear();
70+
mapInstance.CustomMaps.Clear();
71+
6872
var defaultMaps = DatabaseManager.GetDefaultMaps();
6973
var customMapsResult = ScanCustomMaps(defaultMaps.Count);
7074

75+
mapInstance.DefaultMaps.AddRange(defaultMaps);
76+
mapInstance.CustomMaps.AddRange(customMapsResult.Maps);
77+
7178
var allMaps = new List<mapFileInfo>();
7279
allMaps.AddRange(defaultMaps.Where(m => m.ModType == 0));
7380
allMaps.AddRange(customMapsResult.Maps);

BHD-ServerManager/Forms/Panels/tabMaps.cs

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -643,52 +643,121 @@ public void actionClick_importPlaylist(object sender, EventArgs e)
643643
{
644644
OpenFileDialog openDialog = new OpenFileDialog
645645
{
646-
Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*",
646+
Filter = "JSON or MPL Files (*.json;*.mpl)|*.json;*.mpl|JSON Files (*.json)|*.json|MPL Files (*.mpl)|*.mpl|All Files (*.*)|*.*",
647647
DefaultExt = "json"
648648
};
649649

650-
if (openDialog.ShowDialog() == DialogResult.OK)
650+
if (openDialog.ShowDialog() != DialogResult.OK)
651+
return;
652+
653+
string filePath = openDialog.FileName;
654+
string extension = Path.GetExtension(filePath).ToLowerInvariant();
655+
656+
if (extension == ".mpl")
651657
{
652-
var (success, maps, importedCount, skippedCount, errorMessage) =
653-
mapInstanceManager.ImportPlaylistFromJson(openDialog.FileName);
658+
ImportMplPlaylist(filePath);
659+
return;
660+
}
654661

655-
if (!success)
656-
{
657-
MessageBox.Show(errorMessage, "Import Error",
658-
MessageBoxButtons.OK, MessageBoxIcon.Error);
659-
return;
660-
}
662+
// Default: JSON import
663+
var (success, maps, importedCount, skippedCount, errorMessage) =
664+
mapInstanceManager.ImportPlaylistFromJson(filePath);
665+
666+
if (!success)
667+
{
668+
MessageBox.Show(errorMessage, "Import Error",
669+
MessageBoxButtons.OK, MessageBoxIcon.Error);
670+
return;
671+
}
672+
673+
dataGridView_currentMaps.Rows.Clear();
674+
675+
foreach (var map in maps)
676+
{
677+
int rowIndex = dataGridView_currentMaps.Rows.Add(
678+
dataGridView_currentMaps.Rows.Count + 1,
679+
map.MapName,
680+
map.MapFile,
681+
map.ModType,
682+
map.MapType,
683+
objectGameTypes.GetShortName(map.MapType)
684+
);
685+
686+
DataGridViewRow newRow = dataGridView_currentMaps.Rows[rowIndex];
687+
newRow.Cells[1].ToolTipText = $"Map File: {map.MapFile}";
688+
}
689+
690+
string message = $"Import complete.\n\nImported: {importedCount} maps";
691+
if (skippedCount > 0)
692+
{
693+
message += $"\nSkipped: {skippedCount} unavailable maps";
694+
}
695+
message += "\n\nRemember to save the playlist to apply changes.";
661696

662-
// Clear current playlist
663-
dataGridView_currentMaps.Rows.Clear();
697+
MessageBox.Show(message, "Import Complete",
698+
MessageBoxButtons.OK, MessageBoxIcon.Information);
699+
}
700+
701+
// Helper for MPL import
702+
private void ImportMplPlaylist(string filePath)
703+
{
704+
var lines = File.ReadAllLines(filePath, Encoding.GetEncoding(1252));
705+
var DefaultMaps = mapInstance.DefaultMaps;
706+
var CustomMaps = mapInstance.CustomMaps;
707+
int importedCount = 0, skippedCount = 0;
664708

665-
// Add imported maps to grid
666-
foreach (var map in maps)
709+
dataGridView_currentMaps.Rows.Clear();
710+
711+
foreach (var line in lines)
712+
{
713+
if (string.IsNullOrWhiteSpace(line)) continue;
714+
715+
var parts = line.Split(new[] { "[-]" }, StringSplitOptions.None);
716+
if (parts.Length != 3)
667717
{
668-
int rowIndex = dataGridView_currentMaps.Rows.Add(
669-
dataGridView_currentMaps.Rows.Count + 1,
670-
map.MapName,
671-
map.MapFile,
672-
map.ModType,
673-
map.MapType,
674-
objectGameTypes.GetShortName(map.MapType)
675-
);
676-
677-
DataGridViewRow newRow = dataGridView_currentMaps.Rows[rowIndex];
678-
newRow.Cells[1].ToolTipText = $"Map File: {map.MapFile}";
718+
skippedCount++;
719+
continue;
679720
}
680721

681-
// Show results
682-
string message = $"Import complete.\n\nImported: {importedCount} maps";
683-
if (skippedCount > 0)
722+
if (!int.TryParse(parts[0], out int mapType)) { skippedCount++; continue; }
723+
if (!int.TryParse(parts[1], out int modType)) { skippedCount++; continue; }
724+
string fileName = parts[2].Trim();
725+
726+
var map1 = DefaultMaps.FirstOrDefault(m =>
727+
m.MapType == mapType &&
728+
string.Equals(m.MapFile, fileName, StringComparison.OrdinalIgnoreCase));
729+
730+
var map2 = CustomMaps.FirstOrDefault(m =>
731+
m.MapType == mapType &&
732+
string.Equals(m.MapFile, fileName, StringComparison.OrdinalIgnoreCase));
733+
734+
if (map1 == null && map2 == null)
684735
{
685-
message += $"\nSkipped: {skippedCount} unavailable maps";
736+
skippedCount++;
737+
continue;
686738
}
687-
message += "\n\nRemember to save the playlist to apply changes.";
688739

689-
MessageBox.Show(message, "Import Complete",
690-
MessageBoxButtons.OK, MessageBoxIcon.Information);
740+
var map = map1 ?? map2!;
741+
742+
int rowIndex = dataGridView_currentMaps.Rows.Add(
743+
dataGridView_currentMaps.Rows.Count + 1,
744+
map.MapName,
745+
map.MapFile,
746+
map.ModType,
747+
map.MapType,
748+
objectGameTypes.GetShortName(map.MapType)
749+
);
750+
751+
dataGridView_currentMaps.Rows[rowIndex].Cells[1].ToolTipText = $"Map File: {map.MapFile}";
752+
importedCount++;
691753
}
754+
755+
string message = $"MPL Import complete.\n\nImported: {importedCount} maps";
756+
if (skippedCount > 0)
757+
message += $"\nSkipped: {skippedCount} unavailable or invalid maps";
758+
message += "\n\nRemember to save the playlist to apply changes.";
759+
760+
MessageBox.Show(message, "MPL Import Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
692761
}
693762

694763
/// <summary>

RemoteClient/Forms/Panels/tabMaps.cs

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using HawkSyncShared;
22
using HawkSyncShared.DTOs;
33
using HawkSyncShared.Instances;
4+
using HawkSyncShared.ObjectClasses;
45
using HawkSyncShared.SupportClasses;
56
using RemoteClient.Core;
7+
using System.Text;
68

79
namespace RemoteClient.Forms.Panels
810
{
@@ -367,29 +369,117 @@ private async Task ExportPlaylist()
367369

368370
private async Task ImportPlaylist()
369371
{
370-
using var openDialog = new OpenFileDialog
372+
OpenFileDialog openDialog = new OpenFileDialog
371373
{
372-
Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*"
374+
Filter = "JSON or MPL Files (*.json;*.mpl)|*.json;*.mpl|JSON Files (*.json)|*.json|MPL Files (*.mpl)|*.mpl|All Files (*.*)|*.*",
375+
DefaultExt = "json"
373376
};
377+
374378
if (openDialog.ShowDialog() == DialogResult.OK)
375379
{
380+
string filePath = openDialog.FileName;
381+
string extension = Path.GetExtension(filePath).ToLowerInvariant();
382+
383+
if (extension == ".mpl")
384+
{
385+
// --- MPL Import Logic ---
386+
var lines = File.ReadAllLines(filePath, Encoding.GetEncoding(1252));
387+
var DefaultMaps = mapInstance.DefaultMaps;
388+
var CustomMaps = mapInstance.CustomMaps;
389+
var mapList = new List<MapDTO>();
390+
int importedCount = 0, skippedCount = 0;
391+
392+
int mapId = 1;
393+
foreach (var line in lines)
394+
{
395+
if (string.IsNullOrWhiteSpace(line)) continue;
396+
397+
var parts = line.Split(new[] { "[-]" }, StringSplitOptions.None);
398+
if (parts.Length != 3)
399+
{
400+
skippedCount++;
401+
continue;
402+
}
403+
404+
if (!int.TryParse(parts[0], out int mapType)) { skippedCount++; continue; }
405+
if (!int.TryParse(parts[1], out int modType)) { skippedCount++; continue; }
406+
string fileName = parts[2].Trim();
407+
408+
// Try to find in default or custom maps
409+
var map1 = DefaultMaps.FirstOrDefault(m =>
410+
m.MapType == mapType &&
411+
string.Equals(m.MapFile, fileName, StringComparison.OrdinalIgnoreCase));
412+
413+
var map2 = CustomMaps.FirstOrDefault(m =>
414+
m.MapType == mapType &&
415+
string.Equals(m.MapFile, fileName, StringComparison.OrdinalIgnoreCase));
416+
417+
var map = map1 ?? map2;
418+
if (map == null)
419+
{
420+
skippedCount++;
421+
continue;
422+
}
423+
424+
mapList.Add(new MapDTO
425+
{
426+
MapID = mapId++,
427+
MapName = map.MapName,
428+
MapFile = map.MapFile,
429+
ModType = map.ModType,
430+
MapType = map.MapType
431+
});
432+
importedCount++;
433+
}
434+
435+
if (mapList.Count == 0)
436+
{
437+
MessageBox.Show("No valid maps found in MPL file.", "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
438+
return;
439+
}
440+
441+
var playlist1 = new PlaylistDTO
442+
{
443+
PlaylistID = _selectedPlaylist,
444+
Maps = mapList
445+
};
446+
447+
var result = await ApiCore.ApiClient!.ImportPlaylistAsync(playlist1);
448+
if (result.Success)
449+
{
450+
await LoadPlaylist(_selectedPlaylist);
451+
string msg = $"MPL Import complete.\n\nImported: {importedCount} maps";
452+
if (skippedCount > 0)
453+
msg += $"\nSkipped: {skippedCount} unavailable or invalid maps";
454+
msg += "\n\nRemember to save to apply changes.";
455+
MessageBox.Show(msg, "Import Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
456+
}
457+
else
458+
{
459+
MessageBox.Show(result.Message, "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
460+
}
461+
return;
462+
}
463+
464+
// --- JSON Import Logic ---
376465
var json = await File.ReadAllTextAsync(openDialog.FileName);
377-
var playlist = System.Text.Json.JsonSerializer.Deserialize<PlaylistDTO>(json);
378-
if (playlist == null)
466+
var playlist2 = System.Text.Json.JsonSerializer.Deserialize<PlaylistDTO>(json);
467+
if (playlist2 == null)
379468
{
380469
MessageBox.Show("Invalid playlist file.", "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
381470
return;
382471
}
383-
playlist.PlaylistID = _selectedPlaylist;
384-
var result = await ApiCore.ApiClient!.ImportPlaylistAsync(playlist);
385-
if (result.Success)
472+
473+
playlist2.PlaylistID = _selectedPlaylist;
474+
var jsonResult = await ApiCore.ApiClient!.ImportPlaylistAsync(playlist2);
475+
if (jsonResult.Success)
386476
{
387477
await LoadPlaylist(_selectedPlaylist);
388478
MessageBox.Show("Playlist imported and loaded. Remember to save to apply changes.", "Import Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
389479
}
390480
else
391481
{
392-
MessageBox.Show(result.Message, "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
482+
MessageBox.Show(jsonResult.Message, "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
393483
}
394484
}
395485
}

0 commit comments

Comments
 (0)