2020-06-16 17:22:29 +00:00
using GCMM.Properties ;
using Newtonsoft.Json.Linq ;
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO.Compression ;
using System.IO ;
using System.Linq ;
using System.Net ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace GCMM
{
partial class MainForm
{
public bool? CheckIfPatched ( )
{
2020-08-18 14:02:57 +00:00
string pnp = "Patch && Play" ;
2020-07-03 00:00:08 +00:00
if ( ! File . Exists ( GamePath ( @"\IPA.exe" ) ) )
2020-06-16 17:22:29 +00:00
{
status . Text = "Status: Patcher missing\nClicking Play will install it" ;
2020-07-18 20:25:52 +00:00
playbtn . Text = pnp ;
2020-06-16 17:22:29 +00:00
return null ;
}
2020-08-18 14:02:57 +00:00
string nopatch = "Status: Unpatched\nClicking Play patches it" ;
2020-07-18 20:25:52 +00:00
string gc = GetExe ( ) . Replace ( ".exe" , "" ) ;
string backups = GamePath ( @"\IPA\Backups\" + gc ) ;
2020-07-03 00:00:08 +00:00
if ( ! Directory . Exists ( backups ) )
2020-06-16 17:22:29 +00:00
{
status . Text = nopatch ;
2020-07-18 20:25:52 +00:00
playbtn . Text = pnp ;
2020-06-16 17:22:29 +00:00
return false ;
}
2020-07-03 00:00:08 +00:00
string backup = Directory . EnumerateDirectories ( backups ) . OrderByDescending ( s = > s ) . FirstOrDefault ( ) ;
2020-06-16 17:22:29 +00:00
if ( backup = = null )
{
status . Text = nopatch ;
2020-07-18 20:25:52 +00:00
playbtn . Text = pnp ;
2020-06-16 17:22:29 +00:00
return false ;
}
2020-07-18 20:25:52 +00:00
if ( File . GetLastWriteTime ( GamePath ( $@"\{gc}_Data\Managed\Assembly-CSharp.dll" ) )
2020-06-16 17:22:29 +00:00
> //If the file was updated at least 2 minutes after patching
Directory . GetLastWriteTime ( backup ) . AddMinutes ( 2 ) )
{
status . Text = nopatch ;
2020-07-18 20:25:52 +00:00
playbtn . Text = pnp ;
2020-06-16 17:22:29 +00:00
return false ;
}
2020-08-18 14:02:57 +00:00
status . Text = "Status: " + ( unpatched . Checked ? "Mods disabled" : "Patched" ) ;
playbtn . Text = "Play" + ( unpatched . Checked ? " unmodded" : "" ) ;
2020-06-16 17:22:29 +00:00
return true ;
}
2020-06-18 16:28:07 +00:00
public async Task PatchStartGame ( )
2020-06-16 17:22:29 +00:00
{
2020-06-18 16:28:07 +00:00
if ( ! BeginWork ( ) ) return ;
foreach ( ListViewItem item in modlist . SelectedItems )
item . Selected = false ;
2020-06-16 17:22:29 +00:00
if ( ! CheckIfPatched ( ) . HasValue )
{
2020-06-18 16:28:07 +00:00
if ( MessageBox . Show ( "The patcher (GCIPA) is not found. It's necessary to load the mods. It will be downloaded from https://git.exmods.org/modtainers/GCIPA/releases and ran to patch the game. You can unpatch to run without mods at any time." , "Patcher download needed" , MessageBoxButtons . OKCancel )
2020-06-16 17:22:29 +00:00
= = DialogResult . Cancel )
2020-06-18 16:28:07 +00:00
{
EndWork ( ) ;
2020-06-16 17:22:29 +00:00
return ;
2020-06-18 16:28:07 +00:00
}
2020-06-16 17:22:29 +00:00
string releases = "/api/v1/repos/modtainers/GCIPA/releases" ;
string url ;
this . status . Text = "Status: Patching..." ;
using ( WebClient client = GetClient ( ) )
{
url = JArray . Parse ( await client . DownloadStringTaskAsync ( releases ) ) . First [ "assets" ] . First [ "browser_download_url" ] . ToString ( ) ;
await client . DownloadFileTaskAsync ( url , "IPA.zip" ) ;
ZipFile . ExtractToDirectory ( "IPA.zip" , Settings . Default . GamePath ) ;
}
}
bool? status = CheckIfPatched ( ) ;
if ( ! status . HasValue ) //Make sure it actually worked
2020-06-18 16:28:07 +00:00
{
EndWork ( false ) ;
2020-06-16 17:22:29 +00:00
return ;
2020-06-18 16:28:07 +00:00
}
2020-08-18 14:02:57 +00:00
if ( ! status . Value )
2020-07-03 00:00:08 +00:00
{ //TODO: Wine
2020-08-18 14:02:57 +00:00
var psi = new ProcessStartInfo ( GamePath ( @"\IPA.exe" ) , GetExe ( ) + " --nowait" )
2020-06-17 21:56:18 +00:00
{
UseShellExecute = false ,
RedirectStandardError = true ,
RedirectStandardOutput = true ,
WorkingDirectory = Settings . Default . GamePath ,
CreateNoWindow = true
} ;
2020-06-16 17:22:29 +00:00
var process = Process . Start ( psi ) ;
process . BeginErrorReadLine ( ) ;
process . BeginOutputReadLine ( ) ;
process . EnableRaisingEvents = true ;
modinfobox . Text = "" ;
DataReceivedEventHandler onoutput = ( sender , e ) = >
{
Invoke ( ( Action ) ( ( ) = > modinfobox . Text + = e . Data + Environment . NewLine ) ) ;
} ;
process . OutputDataReceived + = onoutput ;
process . ErrorDataReceived + = onoutput ;
process . Exited + = CheckStartGame ;
}
else
CheckStartGame ( null , null ) ;
}
}
}