Files
SVSimServer/SVSim.BattleEngine/Engine/Cute/Utility.cs
gamer147 2d9a6eea4b engine cleanup passes 4-7 + multi-instancing ambient rip
Squashes 146 commits from battle-engine-extraction. Net: 2,045 files changed,
+11,896 / -158,687 lines. Ships engine passes 4-7 (dead-code cull, view-layer
stub, receive-path shrink) plus the Phase-5 AsyncLocal ambient deletion that
turns concurrent battles into a type-system property rather than a scope
contract.

## What landed

**Passes 4-7 (chunks 1-34):** Extended the Phase-4 const-false collapse into a
cascading cull across the skill graph, view layer, and receive-path periphery.
Six mode flags (IsWatchBattle/IsReplayBattle/IsAdmin/IsAdminWatch/IsPuzzleQuest/
IsAINetwork) became `const false`, every guarded block deleted. Field*.cs
subclass ctors + BackGroundBase + ObjectChecker culled to no-ops. Mulligan
family reworked to take a mgr param through IMulliganMgr.InitMulligan.
Emotion/Recovery/Resource clusters null-stubbed. Prediction/OperationSimulator/
skill filters converted from static ambient reads to per-mgr reads via
SkillPrm.ownerCard.SelfBattlePlayer.BattleMgr / ins.BattleMgr / this.BattleMgr.

**Phase-5 ambient rip (chunks 35-47):** Deleted BattleAmbient / BattleAmbient-
Context / TestBattleScope in full. Every per-battle mutable slot now lives on
the mgr instance itself:
  mgr.InstanceIsForecast / InstanceIsRandomDraw / InstanceRecoveryInfo /
  InstanceViewerId / InstanceNetworkAgent / GameMgr
BattleManagerBase.GetIns() returns null unconditionally; the residual static
flags + 3 façades (Certification.ViewerId, Data.BattleRecoveryInfo,
ToolboxGame.RealTimeNetworkAgent) are null-tolerant defaults kept for the
handful of engine-internal readers that still reference their types. Zero
BattleAmbient references anywhere in engine + node + tests.

Added pre-seeded GameMgr ctor overload threaded through the mgr chain
(BattleManagerBase → SingleBattleMgr / NetworkBattleManagerBase → NetworkStandard-
BattleMgr → HeadlessBattleMgr / HeadlessNetworkBattleMgr). Fixtures build a
GameMgr, seed it via HeadlessEngineEnv.SeedCharaIds/SeedNetUser, and pass it
to the mgr's ctor — no ambient reach.

Node side (SVSim.BattleNode/SessionBattleEngine): _ctx replaced with a plain
GameMgr field; 34 `using var _ambient = BattleAmbient.Enter(_ctx)` scope wraps
ripped from every accessor and mutator; EngineGlobalInit.WirePerSessionGameMgr
takes GameMgr as a param and runs from SessionBattleEngine.SetupInternal
BEFORE mgr construction.

Test side: TestBattleScope deleted; 18 fixture [SetUp]s migrated to
`HeadlessEngineEnv.EnsureProcessGlobals()`; MultiInstanceEngineTests rewritten
around per-mgr construction (GetIns() → null is the pinned invariant).

## Regression fixes

- **chunk-48** (MulliganCtrl): chunk-35's `= null` stubs on card lookups broke
  the live receive-driven Deal path (BattlePlayerBase.DrawCard NRE'd downstream
  of NetworkPlayerMulliganCtrl.StartMulliganVfx). Restored the three lookups
  via `_battlePlayer.BattleMgr.GetBattleCardIdx`. Engine tests were satisfied
  by the WireMulliganPhase seam; unit tests exposed the live-path gap.

## Ship state

- SVSim.BattleEngine.Tests: 56/56 pass, 2 skip
- SVSim.UnitTests: 1554/1554 pass (was 1523/31-fail before chunk 48)
- Solution build: 0 source warnings (40 pre-existing NU1902 MessagePack CVEs
  in SVSim.EmulatedEntrypoint, unrelated)
- Sequential PVP smoke: verified live (two back-to-back battles, no regression
  on cleanup/spinup)
- Concurrent PVP smoke: verified live

Adds tools/engine-port/ClosureAnalyzer/ — the Roslyn transitive-type-closure
analyzer needed to make future cascade cleanup safe (per feedback memory
"Engine cleanup needs closure tool" from the 2026-06-28 pass-3 failure).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-03 19:18:54 -04:00

485 lines
10 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace Cute;
public class Utility
{
private enum CSVParserState
{
BEGIN_FIELD,
PLAIN,
QUOTE,
END_FIELD
}
public class EzCrypt
{
private int numScale;
private int[] table;
private int key;
public EzCrypt(int key)
{
numScale = ".SWK2hm4sVd8fOxZr0tqBncwX6P5k3HTCL_IzGYeMlyQFEbNvDjio9J7paRUAg1u-".Length;
this.key = key;
table = new int[128];
for (int i = 0; i < 128; i++)
{
table[i] = ".SWK2hm4sVd8fOxZr0tqBncwX6P5k3HTCL_IzGYeMlyQFEbNvDjio9J7paRUAg1u-".IndexOf((char)i);
}
}
}
public class LeanSemaphore
{
private int max;
public int value { get; private set; }
public LeanSemaphore(int maxAcquire)
{
max = maxAcquire;
Reset();
}
public bool TryWait()
{
if (value > 0)
{
value--;
return true;
}
return false;
}
public void Post()
{
value++;
}
public void Reset()
{
value = max;
}
public void Reset(int maxAcquire)
{
max = maxAcquire;
Reset();
}
}
private static readonly string LangTypeKorString = Global.LANG_TYPE.Kor.ToString();
public static ArrayList ConvertCSV(string csvText, bool removeTitle = true)
{
if (CustomPreference.GetTextLanguage() == LangTypeKorString)
{
HangulManager.ConvertRule(csvText);
}
int length = csvText.Length;
int i;
for (i = 0; Convert.ToInt32(csvText[i]) == 65279 && i < length; i++)
{
}
ArrayList arrayList = new ArrayList();
ArrayList arrayList2 = new ArrayList();
int num = i;
int num2 = i;
bool flag = removeTitle;
bool flag2 = false;
CSVParserState cSVParserState = CSVParserState.BEGIN_FIELD;
for (int j = i; j < length; j++)
{
switch (csvText[j])
{
case ',':
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
arrayList2.Add("");
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
{
string text = csvText.Substring(num, num2 + 1 - num);
arrayList2.Add(flag2 ? text.Replace("\"\"", "\"") : text);
flag2 = false;
cSVParserState = CSVParserState.BEGIN_FIELD;
break;
}
case CSVParserState.QUOTE:
num2 = j;
break;
}
break;
case '\t':
case ' ':
switch (cSVParserState)
{
case CSVParserState.QUOTE:
num2 = j;
break;
}
break;
case '\r':
if (j < length - 1 && csvText[j + 1] == '\n')
{
j++;
}
goto case '\n';
case '\n':
switch (cSVParserState)
{
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
{
string text2 = csvText.Substring(num, num2 + 1 - num);
arrayList2.Add(flag2 ? text2.Replace("\"\"", "\"") : text2);
flag2 = false;
if (!flag)
{
arrayList2.TrimToSize();
arrayList.Add(arrayList2);
}
else
{
flag = false;
}
arrayList2 = new ArrayList(arrayList2.Count);
cSVParserState = CSVParserState.BEGIN_FIELD;
break;
}
case CSVParserState.BEGIN_FIELD:
if (arrayList2.Count > 0)
{
arrayList2.Add("");
if (!flag)
{
arrayList2.TrimToSize();
arrayList.Add(arrayList2);
}
else
{
flag = false;
}
arrayList2 = new ArrayList(arrayList2.Count);
}
break;
case CSVParserState.QUOTE:
num2 = j;
break;
}
break;
case '"':
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
num = j + 1;
num2 = j;
cSVParserState = CSVParserState.QUOTE;
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
throw new ApplicationException("不正なCSV");
case CSVParserState.QUOTE:
if (j < length - 1)
{
if (csvText[j + 1] == '"')
{
j++;
flag2 = true;
num2 = j;
}
else
{
cSVParserState = CSVParserState.END_FIELD;
}
}
else
{
cSVParserState = CSVParserState.END_FIELD;
}
break;
}
break;
default:
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
num = j;
num2 = j;
cSVParserState = CSVParserState.PLAIN;
break;
case CSVParserState.END_FIELD:
throw new ApplicationException("Could not parse CSV: extra character found outside quotation.");
case CSVParserState.PLAIN:
case CSVParserState.QUOTE:
num2 = j;
break;
}
break;
}
}
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
if (arrayList2.Count > 0 && !flag)
{
arrayList2.Add("");
arrayList2.TrimToSize();
arrayList.Add(arrayList2);
}
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
if (!flag)
{
arrayList2.Add(csvText.Substring(num, num2 + 1 - num));
arrayList2.TrimToSize();
arrayList.Add(arrayList2);
}
break;
case CSVParserState.QUOTE:
throw new ApplicationException("不正なCSV");
}
return arrayList;
}
public static List<string[]> ConvertCSV_Array(string csvText, bool removeTitle = true)
{
if (CustomPreference.GetTextLanguage() == LangTypeKorString)
{
HangulManager.ConvertRule(csvText);
}
int length = csvText.Length;
int i;
for (i = 0; Convert.ToInt32(csvText[i]) == 65279 && i < length; i++)
{
}
int num = 1;
for (int j = i; j < length; j++)
{
switch (csvText[j])
{
case ',':
num++;
continue;
default:
continue;
case '\n':
break;
}
break;
}
List<string[]> list = new List<string[]>();
string[] array = new string[num];
int num2 = 0;
int num3 = i;
int num4 = i;
bool flag = removeTitle;
bool flag2 = false;
CSVParserState cSVParserState = CSVParserState.BEGIN_FIELD;
for (int k = i; k < length; k++)
{
switch (csvText[k])
{
case ',':
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
array[num2++] = "";
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
{
string text = csvText.Substring(num3, num4 + 1 - num3);
array[num2++] = (flag2 ? text.Replace("\"\"", "\"") : text);
flag2 = false;
cSVParserState = CSVParserState.BEGIN_FIELD;
break;
}
case CSVParserState.QUOTE:
num4 = k;
break;
}
break;
case '\t':
case ' ':
switch (cSVParserState)
{
case CSVParserState.QUOTE:
num4 = k;
break;
}
break;
case '\r':
if (k < length - 1 && csvText[k + 1] == '\n')
{
k++;
}
goto case '\n';
case '\n':
switch (cSVParserState)
{
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
{
string text2 = csvText.Substring(num3, num4 + 1 - num3);
array[num2++] = (flag2 ? text2.Replace("\"\"", "\"") : text2);
flag2 = false;
if (!flag)
{
list.Add(array);
}
else
{
flag = false;
}
array = new string[num];
num2 = 0;
cSVParserState = CSVParserState.BEGIN_FIELD;
break;
}
case CSVParserState.BEGIN_FIELD:
if (num2 > 0)
{
array[num2++] = "";
if (!flag)
{
list.Add(array);
}
else
{
flag = false;
}
array = new string[num];
num2 = 0;
}
break;
case CSVParserState.QUOTE:
num4 = k;
break;
}
break;
case '"':
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
num3 = k + 1;
num4 = k;
cSVParserState = CSVParserState.QUOTE;
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
throw new ApplicationException("不正なCSV");
case CSVParserState.QUOTE:
if (k < length - 1)
{
if (csvText[k + 1] == '"')
{
k++;
flag2 = true;
num4 = k;
}
else
{
cSVParserState = CSVParserState.END_FIELD;
}
}
else
{
cSVParserState = CSVParserState.END_FIELD;
}
break;
}
break;
default:
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
num3 = k;
num4 = k;
cSVParserState = CSVParserState.PLAIN;
break;
case CSVParserState.END_FIELD:
throw new ApplicationException("Could not parse CSV: extra character found outside quotation.");
case CSVParserState.PLAIN:
case CSVParserState.QUOTE:
num4 = k;
break;
}
break;
}
}
switch (cSVParserState)
{
case CSVParserState.BEGIN_FIELD:
if (num2 > 0 && !flag)
{
array[num2] = "";
list.Add(array);
}
break;
case CSVParserState.PLAIN:
case CSVParserState.END_FIELD:
if (!flag)
{
array[num2] = csvText.Substring(num3, num4 + 1 - num3);
list.Add(array);
}
break;
case CSVParserState.QUOTE:
throw new ApplicationException("不正なCSV");
}
return list;
}
public static StringBuilder CreateHash(byte[] data)
{
MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] array = mD5CryptoServiceProvider.ComputeHash(data);
mD5CryptoServiceProvider.Clear();
StringBuilder stringBuilder = new StringBuilder();
byte[] array2 = array;
foreach (byte b in array2)
{
stringBuilder.Append(b.ToString("x2"));
}
return stringBuilder;
}
public static StringBuilder CreateHash(string data)
{
return CreateHash(Encoding.UTF8.GetBytes(data));
}
public static string GetRuntimePlatform()
{
string result = "Windows";
if (Application.platform == RuntimePlatform.Android)
{
result = "Android";
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
result = "iOS";
}
else if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXEditor)
{
result = "Mac";
}
return result;
}
}