@@ -562,7 +562,20 @@ public static bool TrySaveYamlFile(string filePath, object data, ISerializer? se
562562 /// otherwise, the default value for type T.</param>
563563 /// <returns>true if the YAML file was found and successfully deserialized; otherwise, false.</returns>
564564 public static bool TryLoadYamlFile < T > ( string directory , string name , out T result )
565- => TryLoadYamlFile ( directory , name , null , out result ) ;
565+ => TryLoadYamlFile ( directory , name , default ( IDeserializer ) , out result ) ;
566+
567+ /// <summary>
568+ /// Attempts to load a YAML file from the specified directory and deserialize its contents into an object of
569+ /// type T.
570+ /// </summary>
571+ /// <typeparam name="T">The type into which the YAML file contents will be deserialized.</typeparam>
572+ /// <param name="directory">The path to the directory containing the YAML file. Cannot be null or empty.</param>
573+ /// <param name="name">The name of the YAML file to load, without extension. Cannot be null or empty.</param>
574+ /// <param name="result">When this method returns, contains the deserialized object if the file was loaded and parsed successfully;
575+ /// otherwise, the default value for type T.</param>
576+ /// <returns>true if the YAML file was found and successfully deserialized; otherwise, false.</returns>
577+ public static bool TryLoadYamlFile < T > ( string directory , string name , Type type , out T result )
578+ => TryLoadYamlFile ( directory , name , type , null , out result ) ;
566579
567580 /// <summary>
568581 /// Attempts to load and deserialize a YAML file into an object of type <typeparamref name="T"/>.
@@ -574,6 +587,17 @@ public static bool TryLoadYamlFile<T>(string directory, string name, out T resul
574587 /// <returns>true if the YAML file was successfully loaded and deserialized; otherwise, false.</returns>
575588 public static bool TryLoadYamlFile < T > ( string filePath , out T result )
576589 => TryLoadYamlFile ( filePath , default ( IDeserializer ) , out result ) ;
590+
591+ /// <summary>
592+ /// Attempts to load and deserialize a YAML file into an object of type <typeparamref name="T"/>.
593+ /// </summary>
594+ /// <typeparam name="T">The type into which the YAML file will be deserialized.</typeparam>
595+ /// <param name="filePath">The path to the YAML file to load. Cannot be null or empty.</param>
596+ /// <param name="result">When this method returns, contains the deserialized object if the operation succeeds; otherwise, the default
597+ /// value for type <typeparamref name="T"/>.</param>
598+ /// <returns>true if the YAML file was successfully loaded and deserialized; otherwise, false.</returns>
599+ public static bool TryLoadYamlFile < T > ( string filePath , Type type , out T result )
600+ => TryLoadYamlFile ( filePath , type , default ( IDeserializer ) , out result ) ;
577601
578602 /// <summary>
579603 /// Attempts to load and deserialize a YAML file from the specified directory and file name into an object of
@@ -602,6 +626,34 @@ public static bool TryLoadYamlFile<T>(string directory, string name, IDeserializ
602626 var filePath = Path . Combine ( directory , name ) ;
603627 return TryLoadYamlFile ( filePath , deserializer , out result ) ;
604628 }
629+
630+ /// <summary>
631+ /// Attempts to load and deserialize a YAML file from the specified directory and file name into an object of
632+ /// type <typeparamref name="T"/>.
633+ /// </summary>
634+ /// <remarks>This method does not throw exceptions for missing or invalid files; instead, it
635+ /// returns false if the file cannot be loaded or deserialized. The file path is constructed by combining the
636+ /// specified directory and file name.</remarks>
637+ /// <typeparam name="T">The type into which the YAML file will be deserialized.</typeparam>
638+ /// <param name="directory">The path to the directory containing the YAML file. Cannot be null or empty.</param>
639+ /// <param name="name">The name of the YAML file to load. Cannot be null or empty.</param>
640+ /// <param name="deserializer">An optional YAML deserializer to use for parsing the file. If null, a default deserializer may be used.</param>
641+ /// <param name="result">When this method returns, contains the deserialized object if the operation succeeds; otherwise, the default
642+ /// value for type <typeparamref name="T"/>.</param>
643+ /// <returns>true if the YAML file was successfully loaded and deserialized; otherwise, false.</returns>
644+ public static bool TryLoadYamlFile < T > ( string directory , string name , Type type , IDeserializer ? deserializer , out T result )
645+ {
646+ result = default ! ;
647+
648+ if ( string . IsNullOrEmpty ( directory ) )
649+ return false ;
650+
651+ if ( string . IsNullOrEmpty ( name ) )
652+ return false ;
653+
654+ var filePath = Path . Combine ( directory , name ) ;
655+ return TryLoadYamlFile ( filePath , type , deserializer , out result ) ;
656+ }
605657
606658 /// <summary>
607659 /// Attempts to load and deserialize a YAML file into an object of type <typeparamref name="T"/>.
@@ -640,6 +692,84 @@ public static bool TryLoadYamlFile<T>(string filePath, IDeserializer? deserializ
640692 catch ( Exception ex )
641693 {
642694 ApiLog . Error ( "LabExtended" , $ "Caught an exception while loading file &3{ Path . GetFileName ( filePath ) } &r (&6{ typeof ( T ) } &r):\n { ex } ") ;
695+
696+ try
697+ {
698+ var movePath = Path . Combine (
699+ Path . GetDirectoryName ( filePath ) ,
700+ string . Concat (
701+ Path . GetFileNameWithoutExtension ( filePath ) ,
702+ "_error_" ,
703+ DateTime . Now . Ticks . ToString ( ) ,
704+ Path . GetExtension ( filePath ) ) ) ;
705+
706+ File . Move ( filePath , movePath ) ;
707+ }
708+ catch
709+ {
710+ // ignored
711+ }
712+
713+ return false ;
714+ }
715+ }
716+
717+ /// <summary>
718+ /// Attempts to load and deserialize a YAML file into an object of type <typeparamref name="T"/>.
719+ /// </summary>
720+ /// <remarks>This method returns false if the file does not exist, is empty, or if deserialization
721+ /// fails due to invalid content or an exception. The default deserializer is used if none is
722+ /// provided.</remarks>
723+ /// <typeparam name="T">The type into which the YAML file content will be deserialized.</typeparam>
724+ /// <param name="filePath">The path to the YAML file to load. Cannot be null or empty.</param>
725+ /// <param name="type">The type to deserialize to.</param>
726+ /// <param name="deserializer">The YAML deserializer to use. If null, a default deserializer is used.</param>
727+ /// <param name="result">When this method returns, contains the deserialized object if the operation succeeds; otherwise, the default
728+ /// value for type <typeparamref name="T"/>.</param>
729+ /// <returns>true if the file was successfully loaded and deserialized; otherwise, false.</returns>
730+ public static bool TryLoadYamlFile < T > ( string filePath , Type type , IDeserializer ? deserializer , out T result )
731+ {
732+ result = default ! ;
733+
734+ if ( string . IsNullOrEmpty ( filePath ) )
735+ return false ;
736+
737+ deserializer ??= YamlConfigParser . Deserializer ;
738+
739+ try
740+ {
741+ if ( ! File . Exists ( filePath ) )
742+ return false ;
743+
744+ var content = File . ReadAllText ( filePath ) ;
745+
746+ if ( string . IsNullOrEmpty ( content ) )
747+ return false ;
748+
749+ result = ( T ) deserializer . Deserialize ( content , type ) ! ;
750+ return result != null ;
751+ }
752+ catch ( Exception ex )
753+ {
754+ ApiLog . Error ( "LabExtended" , $ "Caught an exception while loading file &3{ Path . GetFileName ( filePath ) } &r (&6{ typeof ( T ) } &r):\n { ex } ") ;
755+
756+ try
757+ {
758+ var movePath = Path . Combine (
759+ Path . GetDirectoryName ( filePath ) ,
760+ string . Concat (
761+ Path . GetFileNameWithoutExtension ( filePath ) ,
762+ "_error_" ,
763+ DateTime . Now . Ticks . ToString ( ) ,
764+ Path . GetExtension ( filePath ) ) ) ;
765+
766+ File . Move ( filePath , movePath ) ;
767+ }
768+ catch
769+ {
770+ // ignored
771+ }
772+
643773 return false ;
644774 }
645775 }
0 commit comments