源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

66 行
1.8 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. namespace ConsoleWindows
  7. {
  8. public class ConsoleWindow
  9. {
  10. TextWriter oldOutput;
  11. public void Initialize()
  12. {
  13. if ( !AttachConsole( 0x0ffffffff ) )
  14. {
  15. AllocConsole();
  16. }
  17. oldOutput = Console.Out;
  18. try
  19. {
  20. IntPtr stdHandle = GetStdHandle( STD_OUTPUT_HANDLE );
  21. Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle( stdHandle, true );
  22. FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
  23. System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(936);
  24. StreamWriter standardOutput = new StreamWriter( fileStream, encoding);
  25. standardOutput.AutoFlush = true;
  26. Console.SetOut( standardOutput );
  27. }
  28. catch ( System.Exception e )
  29. {
  30. //Debug.Log( "Couldn't redirect output: " + e.Message );
  31. }
  32. }
  33. public void Shutdown()
  34. {
  35. Console.SetOut( oldOutput );
  36. FreeConsole();
  37. }
  38. public void SetTitle( string strName )
  39. {
  40. SetConsoleTitle( strName );
  41. }
  42. private const int STD_OUTPUT_HANDLE = -11;
  43. [DllImport( "kernel32.dll", SetLastError = true )]
  44. static extern bool AttachConsole( uint dwProcessId );
  45. [DllImport( "kernel32.dll", SetLastError = true )]
  46. static extern bool AllocConsole();
  47. [DllImport( "kernel32.dll", SetLastError = true )]
  48. static extern bool FreeConsole();
  49. [DllImport( "kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall )]
  50. private static extern IntPtr GetStdHandle( int nStdHandle );
  51. [DllImport( "kernel32.dll" )]
  52. static extern bool SetConsoleTitle( string lpConsoleTitle );
  53. }
  54. }