Task12Acceptance.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;using System.IO;using System.Linq;using System.Reflection;using System.Collections.Generic;using DevilLink2.UI;using DevilLink2.Gameplay;using DevilLink2.Core;using DevilLink2.Persistence;using DevilLink2.Presentation;using UnityEditor;using UnityEngine;using UnityEngine.UI;
  2. namespace DevilLink2.Editor {
  3. internal static class Task12Acceptance {
  4. const BindingFlags F=BindingFlags.Instance|BindingFlags.Static|BindingFlags.Public|BindingFlags.NonPublic;
  5. static int cell,phase;static double wait,soak;static long firstMemory;static int firstObjects;static EditorWindow game,sim;static object group;static int originalIndex,customIndex=-1;static bool prepared;
  6. static readonly int[] W={360,360,390,412,430,480},H={640,800,844,915,932,1040};
  7. static readonly string[] Pages={"Start","Lobby","Gameplay","Pause","Victory","Defeat","Shop","Daily","Wheel","Profile","Settings","Help"};
  8. static readonly List<string> rows=new List<string>();
  9. static SavedPlayer Player(UIManager ui)=>(SavedPlayer)typeof(UIManager).GetField("player",F).GetValue(ui);
  10. static Type GV=>typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GameView");
  11. static object Get(object o,string name)=>o.GetType().GetProperty(name,F).GetValue(o,null);
  12. static void Set(object o,string name,object value)=>o.GetType().GetProperty(name,F).SetValue(o,value,null);
  13. static void Call(object o,string name,params object[] args)=>o.GetType().GetMethod(name,F).Invoke(o,args);
  14. public static void Reset(){cell=phase=0;wait=soak=0;prepared=false;rows.Clear();customIndex=-1;}
  15. static void Resize(int index){
  16. var st=Type.GetType("UnityEditor.DeviceSimulation.SimulatorWindow, UnityEditor.DeviceSimulatorModule");
  17. if(index<6){foreach(var v in Resources.FindObjectsOfTypeAll(st).Cast<EditorWindow>())v.Close();
  18. if(!game){game=EditorWindow.GetWindow(GV);originalIndex=(int)Get(game,"selectedSizeIndex");var t=typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GameViewSizes");var sizes=t.BaseType.GetProperty("instance",F).GetValue(null,null);group=Get(sizes,"currentGroup");}
  19. var sizeType=typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GameViewSize");var kind=Enum.Parse(typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GameViewSizeType"),"FixedResolution");
  20. if(customIndex<0){customIndex=(int)group.GetType().GetMethod("GetTotalCount").Invoke(group,null);var size=Activator.CreateInstance(sizeType,F,null,new object[]{kind,W[index],H[index],"DL2Task12"},null);Call(group,"AddCustomSize",size);}
  21. var item=group.GetType().GetMethod("GetGameViewSize").Invoke(group,new object[]{customIndex});Set(item,"width",W[index]);Set(item,"height",H[index]);Set(game,"selectedSizeIndex",customIndex);game.Focus();game.Repaint();
  22. }else{
  23. sim=EditorWindow.GetWindow(st);var main=Get(sim,"main");var devices=(Array)Get(main,"devices");int chosen=-1;
  24. for(int i=0;i<devices.Length;i++){var raw=JsonUtility.ToJson(devices.GetValue(i));if(raw.Contains(index==6?"iOS Notch Device (1170x2532)":"Punch Hole Center (1440x3088)")){chosen=i;break;}}
  25. if(chosen<0)throw new Exception("Requested Simulator profile unavailable");Set(main,"deviceIndex",chosen);sim.Focus();sim.Repaint();
  26. }
  27. }
  28. public static void Restore(){if(sim)sim.Close();if(game){Set(game,"selectedSizeIndex",originalIndex);if(group!=null&&customIndex>=0)Call(group,"RemoveCustomSize",customIndex);game.Repaint();}customIndex=-1;}
  29. static void Clear(UIManager ui){var flow=UnityEngine.Object.FindFirstObjectByType<BoardDemoBootstrap>().Flow;int max=flow.Runtime.Session.Board.RemainingTileCount/2;for(int i=0;i<max;i++){if(!new BoardMoveFinder().TryFindMove(flow.Runtime.Session.Board,out var pair))throw new Exception("No pair after automatic recovery");flow.Click(pair.First.Row,pair.First.Column);flow.Click(pair.Second.Row,pair.Second.Column);flow.Runtime.Interaction.Advance(.24);}if(flow.State!=GameFlowState.Victory)throw new Exception("Clear did not settle Victory");}
  30. static void Show(UIManager ui,BoardDemoBootstrap b,int page){
  31. if(page==0){ui.ShowLobby();ui.Lobby.SetVisible(false);ui.StartScreen.SetVisible(true);}
  32. if(page==1)ui.ShowLobby();
  33. if(page==2){ui.SelectMode(GameMode.Classic);}
  34. if(page==3)ui.Hud.PauseButton.onClick.Invoke();
  35. if(page==4){ui.Hud.Pause.ResumeButton.onClick.Invoke();Clear(ui);}
  36. if(page==5){ui.Hud.Result.RetryButton.onClick.Invoke();b.Flow.Runtime.Timer.Advance(10000);}
  37. if(page==6){ui.Hud.Result.LobbyButton.onClick.Invoke();ui.OpenShop();}
  38. if(page==7){ui.CloseShop();ui.OpenDaily();}
  39. if(page==8){ui.CloseDaily();ui.OpenWheel();}
  40. if(page==9){ui.CloseWheel();ui.OpenProfile();}
  41. if(page==10){ui.CloseProfile();ui.OpenSettings();}
  42. if(page==11){ui.CloseSettings();ui.OpenHelp();}
  43. }
  44. static void CheckLayout(UIManager ui,BoardDemoBootstrap b,int size,int page,Action<bool,string> check){
  45. if(size<6)check(Screen.width==W[size]&&Screen.height==H[size],"Actual resolution "+W[size]+"x"+H[size]);
  46. var adapter=b.GetComponentInChildren<SafeAreaAdapter>();var r=(RectTransform)adapter.transform;
  47. check(Vector2.Distance(r.anchorMin,new Vector2(Screen.safeArea.xMin/Screen.width,Screen.safeArea.yMin/Screen.height))<.001f,"Actual SafeArea minimum anchors");
  48. check(Vector2.Distance(r.anchorMax,new Vector2(Screen.safeArea.xMax/Screen.width,Screen.safeArea.yMax/Screen.height))<.001f,"Actual SafeArea maximum anchors");
  49. if(page>=2&&page<=5){var v=b.GetComponentInChildren<BoardView>(true);var rect=(RectTransform)v.transform;check(Vector2.Distance(rect.rect.size,GameplayStageView.BoardArea)<.01f&&rect.anchoredPosition==new Vector2(6,-226),"Responsive Gameplay board region");}
  50. int buttons=0;foreach(var bt in ui.transform.parent.GetComponentsInChildren<Button>(false)){
  51. if(bt.GetComponentInParent<ScrollRect>())continue;
  52. var cg=bt.GetComponentsInParent<CanvasGroup>();if(cg.Any(g=>g.alpha<.95f))continue;
  53. var corners=new Vector3[4];((RectTransform)bt.transform).GetWorldCorners(corners);foreach(var c in corners){var p=RectTransformUtility.WorldToScreenPoint(null,c);if(p.x<Screen.safeArea.xMin-2||p.x>Screen.safeArea.xMax+2||p.y<Screen.safeArea.yMin-2||p.y>Screen.safeArea.yMax+2)throw new Exception("Button beyond SafeArea: "+bt.name+" "+p);}buttons++;
  54. }
  55. var shot=ScreenCapture.CaptureScreenshotAsTexture();try{Directory.CreateDirectory("Temp/DevilLink2Task12/Shots");File.WriteAllBytes("Temp/DevilLink2Task12/Shots/"+size+"_"+Pages[page]+".png",shot.EncodeToPNG());}finally{UnityEngine.Object.Destroy(shot);}
  56. rows.Add(JsonUtility.ToJson(new Row{size=size,page=Pages[page],width=Screen.width,height=Screen.height,safe=Screen.safeArea.ToString(),buttons=buttons}));File.WriteAllLines("Temp/DevilLink2Task12/Layout.jsonl",rows);
  57. foreach(var scroll in ui.GetComponentsInChildren<ScrollRect>(false)){scroll.verticalNormalizedPosition=0;Canvas.ForceUpdateCanvases();scroll.verticalNormalizedPosition=1;}
  58. }
  59. [Serializable]class Row{public int size,width,height,buttons;public string page,safe;}
  60. public static bool Tick(UIManager ui,BoardDemoBootstrap b,Action<bool,string> check){
  61. if(SessionState.GetBool("DL2Task12.Reopen",false)){
  62. var p=Player(ui);check(p.Profile.Nickname=="Task12Hero"&&p.Profile.Avatar=="moon","Actual scene restart restores profile");
  63. check(p.Economy.Coins.ToString()==SessionState.GetString("DL2Task12.Coins",""),"Actual restart restores economy");check(p.Economy.SpinTickets.ToString()==SessionState.GetString("DL2Task12.Tickets",""),"Actual restart restores tickets");
  64. foreach(GameMode m in Enum.GetValues(typeof(GameMode)))check(p.Progress.GetHighestCleared(m)==20,"Restart progress "+m);
  65. var audio=DevilLink2.Presentation.AudioManager.Instance;check(audio.MusicEnabled&&audio.SfxEnabled&&Mathf.Approximately(audio.MusicVolume,.3f)&&Mathf.Approximately(audio.SfxVolume,.5f),"Audio settings reset by existing runtime-only lifecycle (not persisted)");
  66. check(p.Economy.Inventory.GetCount(ItemType.Hint)==SessionState.GetInt("DL2Task12.Hint",-1)&&p.Economy.Inventory.GetCount(ItemType.Shuffle)==SessionState.GetInt("DL2Task12.Shuffle",-1)&&p.Economy.Inventory.GetCount(ItemType.Time)==SessionState.GetInt("DL2Task12.Time",-1),"Actual restart inventory preserved");
  67. check(b.GetComponentsInChildren<AudioSource>(true).Length==8,"Scene restart recreates one bounded audio pool");
  68. check(b.Session==null,"No unfinished board restored");return true;
  69. }
  70. if(cell<96){int size=cell/12,page=cell%12;
  71. if(!prepared){if(page==0){ui.ShowLobby();Resize(size);}Show(ui,b,page);wait=EditorApplication.timeSinceStartup+.5;prepared=true;return false;}
  72. if(EditorApplication.timeSinceStartup<wait)return false;CheckLayout(ui,b,size,page,check);if(page==11)ui.CloseHelp();cell++;prepared=false;return false;
  73. }
  74. if(phase==0){
  75. Restore();ui.ShowLobby();
  76. foreach(GameMode m in Enum.GetValues(typeof(GameMode))){ui.SelectMode(m);check(b.Session.Definition.Level==20,"Mode20 "+m);if(m==GameMode.TimeAttack)check(b.Session.Board.RemainingTileCount==48&&b.Flow.Runtime.Timer.InitialTime==44,"TimeAttack20 parameters");if(m==GameMode.Zen)check(!b.Flow.Runtime.Timer.HasTimer,"Zen no timer");Clear(ui);long coins=Player(ui).Economy.Coins;long reward=b.Flow.Result.CoinReward;ui.Hud.Result.NextButton.onClick.Invoke();Clear(ui);check(Player(ui).Economy.Coins==coins+reward,"Repeated20 full reward "+m);ui.Hud.Result.LobbyButton.onClick.Invoke();}
  77. var p=Player(ui);p.UpdateProfile("Task12Hero","moon");p.Shop.TryBuy(ItemType.Hint,Guid.NewGuid());p.CheckIn.TryClaimDailyReward(Guid.NewGuid());p.Wheel.TrySpin(Guid.NewGuid());var before=p.CaptureSnapshot();
  78. using(var again=new SavedPlayer(new LocalSaveService(new FileSaveStorage(SessionState.GetString("DevilLink2.Task08A.Play.Directory",""))))){var after=again.CaptureSnapshot();check(before.Coins==after.Coins&&before.SpinTickets==after.SpinTickets&&before.HintCount==after.HintCount&&before.ShuffleCount==after.ShuffleCount&&before.TimeCount==after.TimeCount,"Economy inventory disk roundtrip");check(before.LastCheckInDateUtc==after.LastCheckInDateUtc&&before.LastFreeSpinDateUtc==after.LastFreeSpinDateUtc&&before.RewardOperations.Count==after.RewardOperations.Count,"Daily wheel disk roundtrip");}
  79. SessionState.SetString("DL2Task12.Coins",p.Economy.Coins.ToString());SessionState.SetString("DL2Task12.Tickets",p.Economy.SpinTickets.ToString());
  80. ui.OpenSettings();ui.Settings.SetMusicVolume(.13f);ui.Settings.SetSfxVolume(.27f);ui.Settings.ToggleMusic();ui.Settings.ToggleSfx();ui.CloseSettings();
  81. soak=EditorApplication.timeSinceStartup;firstMemory=GC.GetTotalMemory(true);firstObjects=UnityEngine.Object.FindObjectsByType<Transform>(FindObjectsInactive.Include,FindObjectsSortMode.None).Length;phase=1;
  82. }
  83. if(phase==1){
  84. ui.OpenSettings();ui.CloseSettings();ui.OpenHelp();ui.CloseHelp();
  85. if(EditorApplication.timeSinceStartup-soak<180)return false;
  86. int now=UnityEngine.Object.FindObjectsByType<Transform>(FindObjectsInactive.Include,FindObjectsSortMode.None).Length;long memory=GC.GetTotalMemory(true);
  87. File.WriteAllText("Temp/DevilLink2Task12/Soak.json", "{\"seconds\":180,\"objectsBefore\":"+firstObjects+",\"objectsAfter\":"+now+",\"managedBefore\":"+firstMemory+",\"managedAfter\":"+memory+"}");check(now<=firstObjects+4,"Three minute modal churn has bounded objects");return true;
  88. }
  89. return false;
  90. }
  91. }
  92. }