@@ -44,17 +44,34 @@ public static Result<Unit, Error> ValidateConfig(this InstanceConfig config)
4444 return ResultExt . Err < Unit > ( "Could not parse mc_version" ) ;
4545 }
4646
47- if (
48- config . TargetType is TargetType . Jar
49- && config . JavaPath . ToLower ( ) != "java"
50- && ! File . Exists ( config . JavaPath )
51- ) return ResultExt . Err < Unit > ( "Could not found java in java_path" ) ;
47+ var javaPathValidation = config . ValidateJavaPathExists ( ) ;
48+ if ( javaPathValidation . IsErr ( out var javaPathError ) && javaPathError is not null )
49+ return ResultExt . Err < Unit > ( javaPathError ) ;
5250
5351 if ( config . Uuid == Guid . Empty ) return ResultExt . Err < Unit > ( "uuid should not be empty" ) ;
5452
5553 return ResultExt . Ok ( Unit . Default ) ;
5654 }
5755
56+ private static Result < Unit , Error > ValidateJavaPathExists ( this InstanceConfig config )
57+ {
58+ if (
59+ config . TargetType is not TargetType . Jar
60+ || config . JavaPath . Equals ( "java" , StringComparison . OrdinalIgnoreCase )
61+ ) return ResultExt . Ok ( Unit . Default ) ;
62+
63+ try
64+ {
65+ return File . Exists ( config . JavaPath )
66+ ? ResultExt . Ok ( Unit . Default )
67+ : ResultExt . Err < Unit > ( "Could not found java in java_path" ) ;
68+ }
69+ catch ( Exception ex )
70+ {
71+ return ResultExt . Err < Unit > ( new Error ( "Failed to inspect java_path" ) . CauseBy ( ex ) ) ;
72+ }
73+ }
74+
5875 public static bool IsMinecraftJavaInstance ( InstanceType type )
5976 {
6077 return type . IsMinecraftJavaRuntimeType ( ) ;
@@ -172,14 +189,35 @@ private static string[] GenerateEula()
172189 public static async Task < Result < Unit , Error > > FixEula ( this InstanceConfig config )
173190 {
174191 var eulaPath = Path . Combine ( config . GetWorkingDirectory ( ) , "eula.txt" ) ;
175- return await ResultExt . TryAsync ( async Task ( ) =>
192+
193+ var eula = await ReadOrGenerateEula ( eulaPath ) ;
194+ if ( eula . IsErr ( out var readError ) && readError is not null ) return ResultExt . Err < Unit > ( readError ) ;
195+
196+ return await WriteEula ( eulaPath , eula . Unwrap ( ) ) ;
197+ }
198+
199+ private static async Task < Result < string [ ] , Error > > ReadOrGenerateEula ( string eulaPath )
200+ {
201+ var result = await ResultExt . TryAsync ( async path =>
176202 {
177- var text = File . Exists ( eulaPath )
178- ? ( await File . ReadAllLinesAsync ( eulaPath ) )
203+ return File . Exists ( path )
204+ ? ( await File . ReadAllLinesAsync ( path ) )
179205 . Select ( x => x . Trim ( ) . StartsWith ( "eula" ) ? "eula=true" : x )
180206 . ToArray ( )
181207 : GenerateEula ( ) ;
182- await File . WriteAllLinesAsync ( eulaPath , text , Encoding . UTF8 ) ;
183- } ) . MapTask ( result => result . MapErr ( ex => new Error ( ) . CauseBy ( ex ) ) ) ;
208+ } , eulaPath ) ;
209+
210+ return result . MapErr ( ex => new Error ( "Failed to read EULA" ) . CauseBy ( ex ) ) ;
211+ }
212+
213+ private static async Task < Result < Unit , Error > > WriteEula ( string eulaPath , string [ ] text )
214+ {
215+ var result = await ResultExt . TryAsync ( async state =>
216+ {
217+ var ( path , lines ) = state ;
218+ await File . WriteAllLinesAsync ( path , lines , Encoding . UTF8 ) ;
219+ } , ( eulaPath , text ) ) ;
220+
221+ return result . MapErr ( ex => new Error ( "Failed to write EULA" ) . CauseBy ( ex ) ) ;
184222 }
185223}
0 commit comments