Advertisement
RiseoftheEast

Untitled

Aug 6th, 2020
1,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.35 KB | None | 0 0
  1. using ClientCore;
  2. using Microsoft.Xna.Framework;
  3. using System;
  4. using System.Collections.Generic;
  5. using DTAClient.Domain;
  6. using System.IO;
  7. using ClientGUI;
  8. using Rampastring.XNAUI.XNAControls;
  9. using Rampastring.XNAUI;
  10. using Rampastring.Tools;
  11. using Updater;
  12.  
  13. namespace DTAClient.DXGUI.Generic
  14. {
  15.     public class CampaignSelectorYR : XNAWindow
  16.     {
  17.         private const int DEFAULT_WIDTH = 650;
  18.         private const int DEFAULT_HEIGHT = 600;
  19.  
  20.         private static string[] DifficultyNames = new string[] { "Easy", "Medium", "Hard" };
  21.  
  22.         private static string[] DifficultyIniPaths = new string[]
  23.         {
  24.             "INI\\Map Code\\Difficulty Easy.ini",
  25.             "INI\\Map Code\\Difficulty Medium.ini",
  26.             "INI\\Map Code\\Difficulty Hard.ini"
  27.         };
  28.  
  29.         public CampaignSelectorYR(WindowManager windowManager, DiscordHandler discordHandler) : base(windowManager)
  30.         {
  31.             this.discordHandler = discordHandler;
  32.         }
  33.  
  34.         private DiscordHandler discordHandler;
  35.  
  36.         private List<Mission> Missions = new List<Mission>();
  37.         private XNAListBox lbCampaignList;
  38.         private XNAClientButton btnLaunch;
  39.         private XNATextBlock tbMissionDescription;
  40.         private XNATrackbar trbDifficultySelector;
  41.  
  42.         private CheaterWindow cheaterWindow;
  43.  
  44.         private string[] filesToCheck = new string[]
  45.         {
  46.             "INI\\AI.ini",
  47.             "INI\\AIE.ini",
  48.             "INI\\Art.ini",
  49.             "INI\\ArtE.ini",
  50.             "INI\\Enhance.ini",
  51.             "INI\\Rules.ini",
  52.             "INI\\Map Code\\Difficulty Hard.ini",
  53.             "INI\\Map Code\\Difficulty Medium.ini",
  54.             "INI\\Map Code\\Difficulty Easy.ini"
  55.         };
  56.  
  57.         private Mission missionToLaunch;
  58.  
  59.         public override void Initialize()
  60.         {
  61.             BackgroundTexture = AssetLoader.LoadTexture("missionselectorbg.png");
  62.             ClientRectangle = new Rectangle(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  63.             BorderColor = UISettings.ActiveSettings.PanelBorderColor;
  64.  
  65.             Name = "CampaignSelectorYR";
  66.  
  67.             var lblSelectCampaign = new XNALabel(WindowManager);
  68.             lblSelectCampaign.Name = "lblSelectCampaign";
  69.             lblSelectCampaign.FontIndex = 1;
  70.             lblSelectCampaign.ClientRectangle = new Rectangle(12, 12, 0, 0);
  71.             lblSelectCampaign.Text = "MISSIONS:";
  72.  
  73.             lbCampaignList = new XNAListBox(WindowManager);
  74.             lbCampaignList.Name = "lbCampaignList";
  75.             lbCampaignList.BackgroundTexture = AssetLoader.CreateTexture(new Color(0, 0, 0, 128), 2, 2);
  76.             lbCampaignList.PanelBackgroundDrawMode = PanelBackgroundImageDrawMode.STRETCHED;
  77.             lbCampaignList.ClientRectangle = new Rectangle(12,
  78.                 lblSelectCampaign.Bottom + 6, 300, 516);
  79.             lbCampaignList.SelectedIndexChanged += LbCampaignList_SelectedIndexChanged;
  80.  
  81.             var lblMissionDescriptionHeader = new XNALabel(WindowManager);
  82.             lblMissionDescriptionHeader.Name = "lblMissionDescriptionHeader";
  83.             lblMissionDescriptionHeader.FontIndex = 1;
  84.             lblMissionDescriptionHeader.ClientRectangle = new Rectangle(
  85.                 lbCampaignList.Right + 12,
  86.                 lblSelectCampaign.Y, 0, 0);
  87.             lblMissionDescriptionHeader.Text = "MISSION DESCRIPTION:";
  88.  
  89.             tbMissionDescription = new XNATextBlock(WindowManager);
  90.             tbMissionDescription.Name = "tbMissionDescription";
  91.             tbMissionDescription.ClientRectangle = new Rectangle(
  92.                 lblMissionDescriptionHeader.X,
  93.                 lblMissionDescriptionHeader.Bottom + 6,
  94.                 Width - 24 - lbCampaignList.Right, 430);
  95.             tbMissionDescription.PanelBackgroundDrawMode = PanelBackgroundImageDrawMode.STRETCHED;
  96.             tbMissionDescription.Alpha = 1.0f;
  97.  
  98.             tbMissionDescription.BackgroundTexture = AssetLoader.CreateTexture(AssetLoader.GetColorFromString(ClientConfiguration.Instance.AltUIBackgroundColor),
  99.                 tbMissionDescription.Width, tbMissionDescription.Height);
  100.  
  101.             var lblDifficultyLevel = new XNALabel(WindowManager);
  102.             lblDifficultyLevel.Name = "lblDifficultyLevel";
  103.             lblDifficultyLevel.Text = "DIFFICULTY LEVEL";
  104.             lblDifficultyLevel.FontIndex = 1;
  105.             Vector2 textSize = Renderer.GetTextDimensions(lblDifficultyLevel.Text, lblDifficultyLevel.FontIndex);
  106.             lblDifficultyLevel.ClientRectangle = new Rectangle(
  107.                 tbMissionDescription.X + (tbMissionDescription.Width - (int)textSize.X) / 2,
  108.                 tbMissionDescription.Bottom + 12, (int)textSize.X, (int)textSize.Y);
  109.  
  110.             trbDifficultySelector = new XNATrackbar(WindowManager);
  111.             trbDifficultySelector.Name = "trbDifficultySelector";
  112.             trbDifficultySelector.ClientRectangle = new Rectangle(
  113.                 tbMissionDescription.X, lblDifficultyLevel.Bottom + 6,
  114.                 tbMissionDescription.Width, 30);
  115.             trbDifficultySelector.MinValue = 0;
  116.             trbDifficultySelector.MaxValue = 2;
  117.             trbDifficultySelector.BackgroundTexture = AssetLoader.CreateTexture(
  118.                 new Color(0, 0, 0, 128), 2, 2);
  119.             trbDifficultySelector.ButtonTexture = AssetLoader.LoadTextureUncached(
  120.                 "trackbarButton_difficulty.png");
  121.  
  122.             var lblEasy = new XNALabel(WindowManager);
  123.             lblEasy.Name = "lblEasy";
  124.             lblEasy.FontIndex = 1;
  125.             lblEasy.Text = "EASY";
  126.             lblEasy.ClientRectangle = new Rectangle(trbDifficultySelector.X,
  127.                 trbDifficultySelector.Bottom + 6, 1, 1);
  128.  
  129.             var lblNormal = new XNALabel(WindowManager);
  130.             lblNormal.Name = "lblNormal";
  131.             lblNormal.FontIndex = 1;
  132.             lblNormal.Text = "NORMAL";
  133.             textSize = Renderer.GetTextDimensions(lblNormal.Text, lblNormal.FontIndex);
  134.             lblNormal.ClientRectangle = new Rectangle(
  135.                 tbMissionDescription.X + (tbMissionDescription.Width - (int)textSize.X) / 2,
  136.                 lblEasy.Y, (int)textSize.X, (int)textSize.Y);
  137.  
  138.             var lblHard = new XNALabel(WindowManager);
  139.             lblHard.Name = "lblHard";
  140.             lblHard.FontIndex = 1;
  141.             lblHard.Text = "HARD";
  142.             lblHard.ClientRectangle = new Rectangle(
  143.                 tbMissionDescription.Right - lblHard.Width,
  144.                 lblEasy.Y, 1, 1);
  145.  
  146.             btnLaunch = new XNAClientButton(WindowManager);
  147.             btnLaunch.Name = "btnLaunch";
  148.             btnLaunch.ClientRectangle = new Rectangle(12, Height - 35, 133, 23);
  149.             btnLaunch.Text = "Launch";
  150.             btnLaunch.AllowClick = false;
  151.             btnLaunch.LeftClick += BtnLaunch_LeftClick;
  152.  
  153.             var btnCancel = new XNAClientButton(WindowManager);
  154.             btnCancel.Name = "btnCancel";
  155.             btnCancel.ClientRectangle = new Rectangle(Width - 145,
  156.                 btnLaunch.Y, 133, 23);
  157.             btnCancel.Text = "Cancel";
  158.             btnCancel.LeftClick += BtnCancel_LeftClick;
  159.  
  160.             AddChild(lblSelectCampaign);
  161.             AddChild(lblMissionDescriptionHeader);
  162.             AddChild(lbCampaignList);
  163.             AddChild(tbMissionDescription);
  164.             AddChild(lblDifficultyLevel);
  165.             AddChild(btnLaunch);
  166.             AddChild(btnCancel);
  167.             AddChild(trbDifficultySelector);
  168.             AddChild(lblEasy);
  169.             AddChild(lblNormal);
  170.             AddChild(lblHard);
  171.  
  172.             // Set control attributes from INI file
  173.             base.Initialize();
  174.  
  175.             // Center on screen
  176.             CenterOnParent();
  177.  
  178.             trbDifficultySelector.Value = UserINISettings.Instance.Difficulty;
  179.  
  180.             ParseBattleIni("INI\\BattleYR.ini");
  181.             ParseBattleIni("INI\\" + ClientConfiguration.Instance.BattleYRFileName);
  182.  
  183.             cheaterWindow = new CheaterWindow(WindowManager);
  184.             DarkeningPanel dp = new DarkeningPanel(WindowManager);
  185.             dp.AddChild(cheaterWindow);
  186.             AddChild(dp);
  187.             dp.CenterOnParent();
  188.             cheaterWindow.CenterOnParent();
  189.             cheaterWindow.YesClicked += CheaterWindow_YesClicked;
  190.             cheaterWindow.Disable();
  191.         }
  192.  
  193.         private void LbCampaignList_SelectedIndexChanged(object sender, EventArgs e)
  194.         {
  195.             if (lbCampaignList.SelectedIndex == -1)
  196.             {
  197.                 tbMissionDescription.Text = string.Empty;
  198.                 btnLaunch.AllowClick = false;
  199.                 return;
  200.             }
  201.  
  202.             Mission mission = Missions[lbCampaignList.SelectedIndex];
  203.  
  204.             if (string.IsNullOrEmpty(mission.Scenario))
  205.             {
  206.                 tbMissionDescription.Text = string.Empty;
  207.                 btnLaunch.AllowClick = false;
  208.                 return;
  209.             }
  210.  
  211.             tbMissionDescription.Text = mission.GUIDescription;
  212.  
  213.             if (!mission.Enabled)
  214.             {
  215.                 btnLaunch.AllowClick = false;
  216.                 return;
  217.             }
  218.  
  219.             btnLaunch.AllowClick = true;
  220.         }
  221.  
  222.         private void BtnCancel_LeftClick(object sender, EventArgs e)
  223.         {
  224.             Enabled = false;
  225.         }
  226.  
  227.         private void BtnLaunch_LeftClick(object sender, EventArgs e)
  228.         {
  229.             int selectedMissionId = lbCampaignList.SelectedIndex;
  230.  
  231.             Mission mission = Missions[selectedMissionId];
  232.  
  233.             if (!ClientConfiguration.Instance.ModMode &&
  234.                 (!CUpdater.IsFileNonexistantOrOriginal(mission.Scenario) || AreFilesModified()))
  235.             {
  236.                 // Confront the user by showing the cheater screen
  237.                 missionToLaunch = mission;
  238.                 cheaterWindow.Enable();
  239.                 return;
  240.             }
  241.  
  242.             LaunchMission(mission);
  243.         }
  244.  
  245.         private bool AreFilesModified()
  246.         {
  247.             foreach (string filePath in filesToCheck)
  248.             {
  249.                 if (!CUpdater.IsFileNonexistantOrOriginal(filePath))
  250.                     return true;
  251.             }
  252.  
  253.             return false;
  254.         }
  255.  
  256.         /// <summary>
  257.         /// Called when the user wants to proceed to the mission despite having
  258.         /// being called a cheater.
  259.         /// </summary>
  260.         private void CheaterWindow_YesClicked(object sender, EventArgs e)
  261.         {
  262.             LaunchMission(missionToLaunch);
  263.         }
  264.  
  265.         /// <summary>
  266.         /// Starts a singleplayer mission.
  267.         /// </summary>
  268.         /// <param name="scenario">The internal name of the scenario.</param>
  269.         /// <param name="requiresAddon">True if the mission is for Firestorm / Enhanced Mode.</param>
  270.         private void LaunchMission(Mission mission)
  271.         {
  272.             bool copyMapsToSpawnmapINI = ClientConfiguration.Instance.CopyMissionsToSpawnmapINI;
  273.  
  274.             Logger.Log("About to write spawn.ini.");
  275.             StreamWriter swriter = new StreamWriter(ProgramConstants.GamePath + "spawn.ini");
  276.             swriter.WriteLine("; Generated by DTA Client");
  277.             swriter.WriteLine("[Settings]");
  278.             if (copyMapsToSpawnmapINI)
  279.                 swriter.WriteLine("Scenario=spawnmap.ini");
  280.             else
  281.                 swriter.WriteLine("Scenario=" + mission.Scenario);
  282.  
  283.             // No one wants to play missions on Fastest, so we'll change it to Faster
  284.             if (UserINISettings.Instance.GameSpeed == 0)
  285.                 UserINISettings.Instance.GameSpeed.Value = 1;
  286.  
  287.             swriter.WriteLine("GameSpeed=" + UserINISettings.Instance.GameSpeed);
  288.             swriter.WriteLine("Firestorm=" + mission.RequiredAddon);
  289.             swriter.WriteLine("CustomLoadScreen=" + LoadingScreenController.GetLoadScreenName(mission.Side));
  290.             swriter.WriteLine("IsSinglePlayer=Yes");
  291.             swriter.WriteLine("SidebarHack=" + ClientConfiguration.Instance.SidebarHack);
  292.             swriter.WriteLine("Side=" + mission.Side);
  293.             swriter.WriteLine("BuildOffAlly=" + mission.BuildOffAlly);
  294.  
  295.             UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;
  296.  
  297.             swriter.WriteLine("DifficultyModeHuman=" + (mission.PlayerAlwaysOnNormalDifficulty ? "1" : trbDifficultySelector.Value.ToString()));
  298.             swriter.WriteLine("DifficultyModeComputer=" + GetComputerDifficulty());
  299.  
  300.             IniFile difficultyIni = new IniFile(ProgramConstants.GamePath + DifficultyIniPaths[trbDifficultySelector.Value]);
  301.             string difficultyName = DifficultyNames[trbDifficultySelector.Value];
  302.  
  303.             swriter.WriteLine();
  304.             swriter.WriteLine();
  305.             swriter.WriteLine();
  306.             swriter.Close();
  307.  
  308.             if (copyMapsToSpawnmapINI)
  309.             {
  310.                 IniFile mapIni = new IniFile(ProgramConstants.GamePath + mission.Scenario);
  311.                 IniFile.ConsolidateIniFiles(mapIni, difficultyIni);
  312.                 mapIni.WriteIniFile(ProgramConstants.GamePath + "spawnmap.ini");
  313.             }
  314.  
  315.             UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;
  316.             UserINISettings.Instance.SaveSettings();
  317.  
  318.             ((MainMenuDarkeningPanel)Parent).Hide();
  319.  
  320.             discordHandler?.UpdatePresence(mission.GUIName, difficultyName, mission.IconPath, true);
  321.             GameProcessLogic.GameProcessExited += GameProcessExited_Callback;
  322.  
  323.             GameProcessLogic.StartGameProcess();
  324.         }
  325.  
  326.         private int GetComputerDifficulty() =>
  327.             Math.Abs(trbDifficultySelector.Value - 2);
  328.  
  329.         private void GameProcessExited_Callback()
  330.         {
  331.             WindowManager.AddCallback(new Action(GameProcessExited), null);
  332.         }
  333.  
  334.         protected virtual void GameProcessExited()
  335.         {
  336.             GameProcessLogic.GameProcessExited -= GameProcessExited_Callback;
  337.             // Logger.Log("GameProcessExited: Updating Discord Presence.");
  338.             discordHandler?.UpdatePresence();
  339.         }
  340.  
  341.         /// <summary>
  342.         /// Parses a Battle(E).ini file. Returns true if succesful (file found), otherwise false.
  343.         /// </summary>
  344.         /// <param name="path">The path of the file, relative to the game directory.</param>
  345.         /// <returns>True if succesful, otherwise false.</returns>
  346.         private bool ParseBattleIni(string path)
  347.         {
  348.             Logger.Log("Attempting to parse " + path + " to populate mission list.");
  349.  
  350.             string battleIniPath = ProgramConstants.GamePath + path;
  351.             if (!File.Exists(battleIniPath))
  352.             {
  353.                 Logger.Log("File " + path + " not found. Ignoring.");
  354.                 return false;
  355.             }
  356.  
  357.             IniFile battleIni = new IniFile(battleIniPath);
  358.  
  359.             List<string> battleKeys = battleIni.GetSectionKeys("Battles");
  360.  
  361.             if (battleKeys == null)
  362.                 return false; // File exists but [Battles] doesn't
  363.  
  364.             foreach (string battleEntry in battleKeys)
  365.             {
  366.                 string battleSection = battleIni.GetStringValue("Battles", battleEntry, "NOT FOUND");
  367.  
  368.                 if (!battleIni.SectionExists(battleSection))
  369.                     continue;
  370.  
  371.                 var mission = new Mission(battleIni, battleSection);
  372.  
  373.                 Missions.Add(mission);
  374.  
  375.                 XNAListBoxItem item = new XNAListBoxItem();
  376.                 item.Text = mission.GUIName;
  377.                 if (!mission.Enabled)
  378.                 {
  379.                     item.TextColor = UISettings.ActiveSettings.DisabledItemColor;
  380.                 }
  381.                 else if (string.IsNullOrEmpty(mission.Scenario))
  382.                 {
  383.                     item.TextColor = AssetLoader.GetColorFromString(
  384.                         ClientConfiguration.Instance.ListBoxHeaderColor);
  385.                     item.IsHeader = true;
  386.                     item.Selectable = false;
  387.                 }
  388.                 else
  389.                 {
  390.                     item.TextColor = lbCampaignList.DefaultItemColor;
  391.                 }
  392.  
  393.                 if (!string.IsNullOrEmpty(mission.IconPath))
  394.                     item.Texture = AssetLoader.LoadTexture(mission.IconPath + "icon.png");
  395.  
  396.                 lbCampaignList.AddItem(item);
  397.             }
  398.  
  399.             Logger.Log("Finished parsing " + path + ".");
  400.             return true;
  401.         }
  402.  
  403.         public override void Draw(GameTime gameTime)
  404.         {
  405.             base.Draw(gameTime);
  406.         }
  407.     }
  408. }
  409.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement