Commit 1d9aa1d8 authored by ElderLich's avatar ElderLich

Implemented Pre-Release IDs Replacement

Solves a long problem with the "UNKNOWN" cards after the cards where out of "Pre-Release" and got assigned the official id.
parent 644a2a6b
......@@ -111,8 +111,12 @@ namespace MDPro3.Duel.YGOSharp
{
continue;
}
if (code > 100)
{
// Replace pre-release ids with official ids (if mapping exists).
code = YdkIdHelper.ToOfficial(code);
switch (flag)
{
case 1:
......@@ -152,7 +156,7 @@ namespace MDPro3.Duel.YGOSharp
}
}
if(Pickup.Count < 3)
if (Pickup.Count < 3)
Pickup.AddRange(Enumerable.Repeat(0, 3 - Pickup.Count));
}
......@@ -168,6 +172,9 @@ namespace MDPro3.Duel.YGOSharp
Grave = 1100001;
Stand = 1110001;
Mate = 1000001;
// Normalize any pre-release ids that may be in the provided lists.
YdkIdHelper.NormalizeDeck(this);
}
public int Check(Banlist ban, bool ocg, bool tcg)
......
using MDPro3.Duel.YGOSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace MDPro3
{
/// <summary>
/// Maps "pre-release" card ids to their official ids.
/// File format (tab/space separated): <preReleaseId>\t<officialId>
/// Location: Data/YDK_Helper.txt
/// </summary>
public static class YdkIdHelper
{
private const string FileName = "YDK_Helper.txt";
private static readonly object _lock = new();
private static bool _loaded;
private static Dictionary<int, int> _map;
// Matches an ID line (optionally with # and whitespace), e.g. "12345678", "#12345678", " 12345678 "
private static readonly Regex _ydkIdLine =
new Regex(@"^(\s*#?\s*)(\d+)(\s*)$", RegexOptions.Compiled);
public static void EnsureLoaded()
{
if (_loaded) return;
lock (_lock)
{
if (_loaded) return;
Load();
_loaded = true;
}
}
private static void Load()
{
_map = new Dictionary<int, int>();
try
{
var path = Path.Combine(Program.PATH_DATA, FileName);
if (!File.Exists(path))
return;
foreach (var raw in File.ReadAllLines(path))
{
var line = raw?.Trim();
if (string.IsNullOrEmpty(line)) continue;
if (line.StartsWith("#")) continue;
// Accept tab or whitespace separation.
var parts = line.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) continue;
if (!int.TryParse(parts[0], out var pre)) continue;
if (!int.TryParse(parts[1], out var official)) continue;
if (pre <= 0 || official <= 0) continue;
if (pre == official) continue;
_map[pre] = official;
}
Debug.Log($"[YDK_Helper] Loaded {_map.Count} mappings from '{path}'");
}
catch (Exception e)
{
Debug.LogWarning($"[YDK_Helper] Failed to load mappings: {e.Message}");
_map?.Clear();
}
}
public static int ToOfficial(int id)
{
EnsureLoaded();
return (_map != null && _map.TryGetValue(id, out var mapped)) ? mapped : id;
}
public static int NormalizeList(List<int> list)
{
if (list == null || list.Count == 0) return 0;
EnsureLoaded();
if (_map == null || _map.Count == 0) return 0;
var changed = 0;
for (var i = 0; i < list.Count; i++)
{
var oldId = list[i];
if (_map.TryGetValue(oldId, out var newId) && newId != oldId)
{
list[i] = newId;
changed++;
}
}
return changed;
}
public static int NormalizeDeck(Deck deck)
{
if (deck == null) return 0;
var changed = 0;
changed += NormalizeList(deck.Main);
changed += NormalizeList(deck.Extra);
changed += NormalizeList(deck.Side);
changed += NormalizeList(deck.Pickup);
return changed;
}
/// <summary>
/// Reads a .ydk file and rewrites it on disk, replacing pre-release ids with official ids.
/// Returns the number of lines changed.
/// </summary>
public static int NormalizeYdkFile(string path)
{
EnsureLoaded();
if (_map == null || _map.Count == 0) return 0;
if (string.IsNullOrEmpty(path) || !File.Exists(path)) return 0;
string original;
try
{
original = File.ReadAllText(path, Encoding.UTF8);
}
catch
{
original = File.ReadAllText(path);
}
// Preserve original newline style.
var newline = original.Contains("\r\n") ? "\r\n" : "\n";
// Preserve empty lines and trailing newline (do NOT RemoveEmptyEntries).
var lines = original.Replace("\r\n", "\n").Split('\n');
var changed = 0;
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
var m = _ydkIdLine.Match(line);
if (!m.Success) continue;
if (!int.TryParse(m.Groups[2].Value, out var id)) continue;
if (id <= 100) continue;
var mapped = ToOfficial(id);
if (mapped == id) continue;
// Rebuild line, preserving prefix and trailing whitespace.
lines[i] = m.Groups[1].Value + mapped.ToString() + m.Groups[3].Value;
changed++;
}
if (changed <= 0) return 0;
var sb = new StringBuilder(original.Length);
for (int i = 0; i < lines.Length; i++)
{
sb.Append(lines[i]);
if (i < lines.Length - 1) sb.Append(newline);
}
try
{
File.WriteAllText(path, sb.ToString(), Encoding.UTF8);
}
catch
{
File.WriteAllText(path, sb.ToString());
}
return changed;
}
}
}
fileFormatVersion: 2
guid: 046cafdedb37a614e82fa0a9579fe7c6
\ No newline at end of file
......@@ -15,7 +15,7 @@ namespace MDPro3
ydkeString = ydkeString.Replace(ydkeHeader, string.Empty);
var sections = ydkeString.Split('!');
if(sections.Length < 3)
if (sections.Length < 3)
{
//throw new ArgumentException("Invalid YDKE format");
return null;
......@@ -27,6 +27,10 @@ namespace MDPro3
Extra = DecodeSection(sections[1]),
Side = DecodeSection(sections[2])
};
// Replace pre-release ids with official ids (if mapping exists).
YdkIdHelper.NormalizeDeck(result);
return result;
}
......
......@@ -49,10 +49,17 @@ namespace MDPro3.Servant
case Condition.EditDeck:
returnServant = Program.instance.deckSelector;
DeckName = Config.GetConfigDeckName();
Deck = new Deck(Program.PATH_DECK + DeckName + Program.EXPANSION_YDK);
var deckPath = Program.PATH_DECK + DeckName + Program.EXPANSION_YDK;
// Auto-fix the file on disk (pre-release ids -> official ids).
YdkIdHelper.NormalizeYdkFile(deckPath);
Deck = new Deck(deckPath);
DeckIsFromLocal = true;
historyCards = new();
break;
case Condition.OnlineDeck:
returnServant = Program.instance.onlineDeckViewer;
DeckName = deckName;
......@@ -60,6 +67,7 @@ namespace MDPro3.Servant
DeckIsFromLocal = false;
historyCards = new();
break;
case Condition.ReplayDeck:
returnServant = Program.instance.replay;
DeckName = deckName;
......@@ -67,6 +75,7 @@ namespace MDPro3.Servant
DeckIsFromLocal = false;
historyCards = new();
break;
case Condition.ChangeSide:
DeckName = Config.GetConfigDeckName();
Deck = TcpHelper.deck;
......
This diff is collapsed.
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