pluginfilehandler.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. class PluginFileHandler extends PluginBase
  2. {
  3. static bool FileDuplicate(string source_name, string dest_name)
  4. {
  5. return CopyFile(source_name, dest_name);
  6. }
  7. static bool FileDelete(string file)
  8. {
  9. return DeleteFile(file);
  10. }
  11. static void FileRename(string source_name, string dest_name)
  12. {
  13. FileDuplicate(source_name, dest_name);
  14. FileDelete(source_name);
  15. }
  16. bool m_ReadOnly;
  17. int m_LineLimit;
  18. ref TStringArray m_FileContent;
  19. void PluginFileHandler()
  20. {
  21. m_FileContent = new TStringArray;
  22. m_LineLimit = -1;
  23. LoadFile();
  24. }
  25. void ~PluginFileHandler()
  26. {
  27. ClearFileNoSave();
  28. }
  29. override void OnInit()
  30. {
  31. super.OnInit();
  32. }
  33. string GetFileName()
  34. {
  35. return string.Empty;
  36. }
  37. string GetSubFolderName()
  38. {
  39. return string.Empty;
  40. }
  41. void ClearFileNoSave()
  42. {
  43. m_FileContent.Clear();
  44. }
  45. bool LoadFile()
  46. {
  47. ClearFileNoSave();
  48. FileHandle file_index = OpenFile(GetFileName(), FileMode.READ);
  49. if ( file_index == 0 )
  50. {
  51. return false;
  52. }
  53. string line_content = "";
  54. int char_count = FGets( file_index, line_content );
  55. while ( char_count != -1 )
  56. {
  57. m_FileContent.Insert(line_content);
  58. char_count = FGets( file_index, line_content );
  59. }
  60. CloseFile(file_index);
  61. return true;
  62. }
  63. bool SaveFile()
  64. {
  65. if ( m_ReadOnly )
  66. {
  67. return false;
  68. }
  69. if (GetSubFolderName() && !FileExist(GetSubFolderName()))
  70. {
  71. MakeDirectory(GetSubFolderName());
  72. }
  73. //Log("SaveFile -> Opening File: "+GetFileName());
  74. FileHandle file_index = OpenFile(GetFileName(), FileMode.WRITE);
  75. if ( file_index == 0 )
  76. {
  77. return false;
  78. }
  79. for ( int i = 0; i < m_FileContent.Count(); ++i)
  80. {
  81. //Log("SaveFile -> Writing: "+m_FileContent.Get(i));
  82. FPrintln(file_index, m_FileContent.Get(i));
  83. }
  84. CloseFile(file_index);
  85. return true;
  86. }
  87. bool IsReadOnly()
  88. {
  89. return m_ReadOnly;
  90. }
  91. void AddText(string text)
  92. {
  93. AddNewLineNoSave(text);
  94. SaveFile();
  95. }
  96. void AddNewLineNoSave(string text)
  97. {
  98. if ( m_LineLimit > -1 )
  99. {
  100. if ( m_LineLimit == 0 )
  101. {
  102. return;
  103. }
  104. int lines_count = m_FileContent.Count();
  105. if ( lines_count > 0 && lines_count >= m_LineLimit )
  106. {
  107. int remove_indexes = lines_count - m_LineLimit;
  108. for ( int i = 0; i <= remove_indexes; ++i )
  109. {
  110. m_FileContent.RemoveOrdered(0);
  111. }
  112. }
  113. }
  114. m_FileContent.Insert(text);
  115. }
  116. TStringArray GetFileContent()
  117. {
  118. return m_FileContent;
  119. }
  120. }