Commit bc6391e9 authored by ElderLich's avatar ElderLich

Bug Fix: Fix LAN host banlist loading from Expansions and .ypk

When creating a local LAN host, MDPro3 now temporarily writes a merged Data/lflist.conf (official + Expansions/lflist.conf + zipped/.ypk lflist.conf) before starting ygoserver, then restores the original file when the server stops. This makes LAN use the same expansion banlists shown in UI without changing non-LAN behavior.
parent 0eefebcf
using Ionic.Zip; using Ionic.Zip;
using MDPro3.Servant; using MDPro3.Servant;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
namespace MDPro3.Duel.YGOSharp namespace MDPro3.Duel.YGOSharp
{ {
...@@ -9,6 +11,9 @@ namespace MDPro3.Duel.YGOSharp ...@@ -9,6 +11,9 @@ namespace MDPro3.Duel.YGOSharp
{ {
public static List<Banlist> Banlists { get; private set; } public static List<Banlist> Banlists { get; private set; }
public static string EmptyBanlistName = "N/A"; public static string EmptyBanlistName = "N/A";
private static bool localServerLflistInjected;
private static string localServerLflistBackupContent;
private const string LfListFileName = "lflist.conf";
public static void Initialize() public static void Initialize()
{ {
...@@ -54,6 +59,110 @@ namespace MDPro3.Duel.YGOSharp ...@@ -54,6 +59,110 @@ namespace MDPro3.Duel.YGOSharp
Banlists.Add(current); Banlists.Add(current);
} }
public static void PrepareLocalServerLflist()
{
if (localServerLflistInjected)
return;
if (!Config.GetBool("Expansions", true))
return;
try
{
if (!File.Exists(Program.PATH_LFLIST))
return;
localServerLflistBackupContent = File.ReadAllText(Program.PATH_LFLIST);
var mergedContent = BuildMergedLflistContent();
if (string.IsNullOrEmpty(mergedContent) || mergedContent == localServerLflistBackupContent)
{
localServerLflistBackupContent = null;
return;
}
File.WriteAllText(Program.PATH_LFLIST, mergedContent);
localServerLflistInjected = true;
}
catch (Exception e)
{
localServerLflistBackupContent = null;
localServerLflistInjected = false;
UnityEngine.Debug.LogError("Failed to prepare LAN lflist.conf.");
UnityEngine.Debug.LogException(e);
}
}
public static void RestoreLocalServerLflist()
{
if (!localServerLflistInjected)
return;
try
{
if (localServerLflistBackupContent != null)
File.WriteAllText(Program.PATH_LFLIST, localServerLflistBackupContent);
}
catch (Exception e)
{
UnityEngine.Debug.LogError("Failed to restore original lflist.conf.");
UnityEngine.Debug.LogException(e);
}
finally
{
localServerLflistBackupContent = null;
localServerLflistInjected = false;
}
}
private static string BuildMergedLflistContent()
{
StringBuilder builder = new StringBuilder();
var confPath = Program.PATH_EXPANSIONS + LfListFileName;
AppendLflistFromFile(builder, confPath);
foreach (var zip in ZipHelper.zips)
{
if (zip.Name.ToLower().EndsWith("script.zip"))
continue;
foreach (var file in zip.EntryFileNames)
{
if (!file.ToLower().EndsWith(LfListFileName))
continue;
var entry = zip[file];
using (var memoryStream = new MemoryStream())
{
entry.Extract(memoryStream);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
AppendLflistText(builder, reader.ReadToEnd());
}
}
}
AppendLflistFromFile(builder, Program.PATH_LFLIST);
return builder.ToString();
}
private static void AppendLflistFromFile(StringBuilder builder, string path)
{
if (!File.Exists(path))
return;
AppendLflistText(builder, File.ReadAllText(path));
}
private static void AppendLflistText(StringBuilder builder, string text)
{
if (string.IsNullOrWhiteSpace(text))
return;
if (builder.Length > 0 && builder[builder.Length - 1] != '\n')
builder.AppendLine();
builder.Append(text);
if (builder.Length > 0 && builder[builder.Length - 1] != '\n')
builder.AppendLine();
}
public static void InitializeFromReader(StreamReader reader) public static void InitializeFromReader(StreamReader reader)
{ {
Banlist current = null; Banlist current = null;
...@@ -149,4 +258,4 @@ namespace MDPro3.Duel.YGOSharp ...@@ -149,4 +258,4 @@ namespace MDPro3.Duel.YGOSharp
return returnValue; return returnValue;
} }
} }
} }
\ No newline at end of file
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using MDPro3.Duel.YGOSharp;
namespace MDPro3.Net namespace MDPro3.Net
{ {
...@@ -21,15 +22,29 @@ namespace MDPro3.Net ...@@ -21,15 +22,29 @@ namespace MDPro3.Net
serverThread = new Thread(() => serverThread = new Thread(() =>
{ {
Dll.start_server(args); try
{
Dll.start_server(args);
}
finally
{
BanlistManager.RestoreLocalServerLflist();
}
}); });
serverThread.Start(); serverThread.Start();
} }
public static void StopServer() public static void StopServer()
{ {
Dll.stop_server(); try
serverThread?.Abort(); {
Dll.stop_server();
}
finally
{
serverThread?.Abort();
BanlistManager.RestoreLocalServerLflist();
}
} }
public static bool ServerRunning() public static bool ServerRunning()
......
...@@ -147,6 +147,7 @@ namespace MDPro3.Servant ...@@ -147,6 +147,7 @@ namespace MDPro3.Servant
RoomServant.FromLocalHost = true; RoomServant.FromLocalHost = true;
RoomServant.FromHandTest = false; RoomServant.FromHandTest = false;
BanlistManager.PrepareLocalServerLflist();
YgoServer.StartServer(args); YgoServer.StartServer(args);
TcpHelper.LinkStart("127.0.0.1", Config.Get("DuelPlayerName0", Config.EMPTY_STRING), port.ToString(), string.Empty, true, null); TcpHelper.LinkStart("127.0.0.1", Config.Get("DuelPlayerName0", Config.EMPTY_STRING), port.ToString(), string.Empty, true, null);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment