Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1.  
  2. static readonly CommandDescriptor CdRules = new CommandDescriptor {
  3.     Name = "rules",
  4.     Category = CommandCategory.Info,
  5.     IsConsoleSafe = true,
  6.     Help = "Shows a list of rules defined by server operator(s).",
  7.     Handler = Rules
  8. };
  9.  
  10. const string DefaultRules = "Rules: Use common sense!";
  11.  
  12. static string[] GetRuleSectionList() {
  13.     if( Directory.Exists( Paths.RulesDirectory ) ) {
  14.         string[] sections = Directory.GetFiles( Paths.RulesDirectory, "*.txt", SearchOption.TopDirectoryOnly )
  15.                             .Select( name => Path.GetFileNameWithoutExtension( name ) )
  16.                             .ToArray();
  17.         if( sections.Length != 0 ) {
  18.             return sections;
  19.         }
  20.     }
  21.     return null;
  22. }
  23.  
  24. static void PrintRuleFile( Player player, FileInfo ruleFile ) {
  25.     try {
  26.         foreach( string ruleLine in File.ReadAllLines( ruleFile.FullName ) ) {
  27.             if( ruleLine.Trim().Length > 0 ) {
  28.                 player.Message( "&R{0}", ruleLine );
  29.             }
  30.         }
  31.     } catch( Exception ex ) {
  32.         Logger.Log( "InfoCommands.PrintRuleFile: An error occured while trying to read {0}: {1}", LogType.Error,
  33.                     ruleFile.FullName, ex );
  34.         player.Message( "&WError reading the rule file. Error details logged." );
  35.     }
  36. }
  37.  
  38. internal static void Rules( Player player, Command cmd ) {
  39.     string sectionName = cmd.Next();
  40.  
  41.     // if no section name is given
  42.     if( sectionName == null ) {
  43.         FileInfo ruleFile = new FileInfo( Paths.RulesFileName );
  44.  
  45.         if( ruleFile.Exists ) {
  46.             PrintRuleFile( player, ruleFile );
  47.         } else {
  48.             player.Message( DefaultRules );
  49.         }
  50.  
  51.         // print a list of available sections
  52.         string[] sections = GetRuleSectionList();
  53.         if( sections != null ) {
  54.             player.Message( "Rule sections: {0}. Type &H/rules SectionName&S to read.", sections.JoinToString() );
  55.         }
  56.         return;
  57.     }
  58.  
  59.     // if a section name is given, but no section files exist
  60.     if( !Directory.Exists( Paths.RulesDirectory ) ) {
  61.         player.Message( "There are no rule sections defined.", sectionName );
  62.         return;
  63.     }
  64.  
  65.     string ruleFileName = null;
  66.     string[] sectionFiles = Directory.GetFiles( Paths.RulesDirectory, "*.txt", SearchOption.TopDirectoryOnly );
  67.  
  68.     for( int i = 0; i < sectionFiles.Length; i++ ) {
  69.         string sectionFullName = Path.GetFileNameWithoutExtension( sectionFiles[i] );
  70.         if( sectionFullName.StartsWith( sectionName, StringComparison.OrdinalIgnoreCase ) ) {
  71.             if( sectionFullName.Equals( sectionName, StringComparison.OrdinalIgnoreCase ) ) {
  72.                 // if there is an exact match, break out of the loop early
  73.                 ruleFileName = sectionFiles[i];
  74.                 break;
  75.  
  76.             } else if( ruleFileName == null ) {
  77.                 // if there is a partial match, keep going to check for multiple matches
  78.                 ruleFileName = sectionFiles[i];
  79.  
  80.             } else {
  81.                 // if there are multiple matches, print a list
  82.                 player.Message( "Multiple rule sections matched \"{0}\": {1}",
  83.                                 sectionFiles.Select( f => Path.GetFileNameWithoutExtension( f )
  84.                                                               .StartsWith( sectionName ) )
  85.                                             .JoinToString() );
  86.             }
  87.         }
  88.     }
  89.  
  90.     if( ruleFileName == null ) {
  91.         player.Message( "No rule section defined for \"{0}\". Available sections: {1}",
  92.                         sectionName, GetRuleSectionList().JoinToString() );
  93.     } else {
  94.         player.Message( "Rule section \"{0}\":",
  95.                         Path.GetFileNameWithoutExtension( ruleFileName ) );
  96.         PrintRuleFile( player, new FileInfo( ruleFileName ) );
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement