2020-05-16 09:03:10 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System ;
using System.IO ;
using Newtonsoft.Json ;
using osu.Framework.Allocation ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Framework.Graphics.Shapes ;
using osu.Framework.Logging ;
using osu.Framework.Platform ;
using osu.Game.Tournament.Models ;
using osu.Game.Graphics ;
using osu.Game.Graphics.Sprites ;
using osu.Game.Graphics.UserInterface ;
using osu.Game.Graphics.UserInterfaceV2 ;
using osu.Game.Overlays ;
using osu.Game.Tournament.IPC ;
using osu.Game.Tournament.Components ;
using osuTK ;
namespace osu.Game.Tournament.Screens
{
public class StablePathSelectScreen : TournamentScreen
{
private DirectorySelector directorySelector ;
[Resolved]
private StableInfo stableInfo { get ; set ; }
[Resolved]
private MatchIPCInfo ipc { get ; set ; }
private DialogOverlay overlay ;
[Resolved(canBeNull: true)]
private TournamentSceneManager sceneManager { get ; set ; }
[BackgroundDependencyLoader(true)]
private void load ( Storage storage , OsuColour colours )
{
// begin selection in the parent directory of the current storage location
2020-05-18 04:28:24 +08:00
var initialPath = new DirectoryInfo ( storage . GetFullPath ( string . Empty ) ) . Parent ? . FullName ;
2020-05-16 09:03:10 +08:00
2020-05-18 04:28:24 +08:00
if ( ! string . IsNullOrEmpty ( stableInfo . StablePath . Value ) )
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
// If the original path info for osu! stable is not empty, set it to the parent directory of that location
initialPath = new DirectoryInfo ( stableInfo . StablePath . Value ) . Parent ? . FullName ;
}
AddRangeInternal ( new Drawable [ ]
{
new Container
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
Masking = true ,
CornerRadius = 10 ,
RelativeSizeAxes = Axes . Both ,
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Size = new Vector2 ( 0.5f , 0.8f ) ,
Children = new Drawable [ ]
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
new Box
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
Colour = colours . GreySeafoamDark ,
RelativeSizeAxes = Axes . Both ,
2020-05-16 09:03:10 +08:00
} ,
2020-05-18 04:28:24 +08:00
new GridContainer
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
RelativeSizeAxes = Axes . Both ,
RowDimensions = new [ ]
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
new Dimension ( ) ,
new Dimension ( GridSizeMode . Relative , 0.8f ) ,
new Dimension ( ) ,
2020-05-16 09:03:10 +08:00
} ,
2020-05-18 04:28:24 +08:00
Content = new [ ]
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
new Drawable [ ]
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
new OsuSpriteText
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Text = "Please select a new location" ,
Font = OsuFont . Default . With ( size : 40 )
} ,
} ,
new Drawable [ ]
2020-05-16 09:03:10 +08:00
{
2020-05-18 04:28:24 +08:00
directorySelector = new DirectorySelector ( initialPath )
{
RelativeSizeAxes = Axes . Both ,
}
2020-05-16 09:03:10 +08:00
} ,
2020-05-18 04:28:24 +08:00
new Drawable [ ]
{
new FillFlowContainer
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Direction = FillDirection . Horizontal ,
Spacing = new Vector2 ( 20 ) ,
Children = new Drawable [ ]
{
new TriangleButton
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Width = 300 ,
Text = "Select stable path" ,
Action = ( ) = > changePath ( storage )
} ,
new TriangleButton
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Width = 300 ,
Text = "Auto detect" ,
Action = autoDetect
} ,
}
}
}
2020-05-16 09:03:10 +08:00
}
}
2020-05-18 04:28:24 +08:00
} ,
} ,
new BackButton
{
Anchor = Anchor . BottomLeft ,
Origin = Anchor . BottomLeft ,
State = { Value = Visibility . Visible } ,
Action = ( ) = > sceneManager ? . SetScreen ( typeof ( SetupScreen ) )
2020-05-16 09:03:10 +08:00
}
} ) ;
}
2020-05-18 04:28:24 +08:00
private void changePath ( Storage storage )
2020-05-16 09:03:10 +08:00
{
var target = directorySelector . CurrentDirectory . Value . FullName ;
2020-05-18 06:47:31 +08:00
Logger . Log ( $"Changing Stable CE location to {target}" ) ;
2020-05-16 09:03:10 +08:00
2020-05-18 04:28:24 +08:00
if ( File . Exists ( Path . Combine ( target , "ipc.txt" ) ) )
2020-05-16 09:03:10 +08:00
{
stableInfo . StablePath . Value = target ;
try
{
2020-05-20 22:30:38 +08:00
using ( var stream = storage . GetStream ( StableInfo . STABLE_CONFIG , FileAccess . Write , FileMode . Create ) )
2020-05-16 09:03:10 +08:00
using ( var sw = new StreamWriter ( stream ) )
{
sw . Write ( JsonConvert . SerializeObject ( stableInfo ,
new JsonSerializerSettings
{
Formatting = Formatting . Indented ,
NullValueHandling = NullValueHandling . Ignore ,
DefaultValueHandling = DefaultValueHandling . Ignore ,
} ) ) ;
}
2020-05-18 06:47:31 +08:00
var fileBasedIpc = ipc as FileBasedIPC ;
fileBasedIpc ? . LocateStableStorage ( ) ;
2020-05-16 09:03:10 +08:00
sceneManager ? . SetScreen ( typeof ( SetupScreen ) ) ;
}
catch ( Exception e )
{
Logger . Log ( $"Error during migration: {e.Message}" , level : LogLevel . Error ) ;
}
}
else
{
overlay = new DialogOverlay ( ) ;
2020-05-18 04:46:43 +08:00
overlay . Push ( new IPCErrorDialog ( "This is an invalid IPC Directory" , "Select a directory that contains an osu! stable cutting edge installation and make sure it has an empty ipc.txt file in it." ) ) ;
2020-05-16 09:03:10 +08:00
AddInternal ( overlay ) ;
Logger . Log ( "Folder is not an osu! stable CE directory" ) ;
// Return an error in the picker that the directory does not contain ipc.txt
}
}
2020-05-18 04:28:24 +08:00
private void autoDetect ( )
{
2020-05-18 06:47:31 +08:00
stableInfo . StablePath . Value = string . Empty ; // This forces findStablePath() to look elsewhere.
2020-05-18 04:28:24 +08:00
var fileBasedIpc = ipc as FileBasedIPC ;
fileBasedIpc ? . LocateStableStorage ( ) ;
2020-05-18 04:46:43 +08:00
2020-05-18 04:28:24 +08:00
if ( fileBasedIpc ? . IPCStorage = = null )
{
// Could not auto detect
overlay = new DialogOverlay ( ) ;
2020-05-18 04:46:43 +08:00
overlay . Push ( new IPCErrorDialog ( "Failed to auto detect" , "An osu! stable cutting-edge installation could not be auto detected.\nPlease try and manually point to the directory." ) ) ;
2020-05-18 04:28:24 +08:00
AddInternal ( overlay ) ;
}
else
{
sceneManager ? . SetScreen ( typeof ( SetupScreen ) ) ;
}
}
2020-05-16 09:03:10 +08:00
}
}