123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- class PluginFileHandler extends PluginBase
- {
- static bool FileDuplicate(string source_name, string dest_name)
- {
- return CopyFile(source_name, dest_name);
- }
- static bool FileDelete(string file)
- {
- return DeleteFile(file);
- }
- static void FileRename(string source_name, string dest_name)
- {
- FileDuplicate(source_name, dest_name);
- FileDelete(source_name);
- }
-
- bool m_ReadOnly;
- int m_LineLimit;
-
- ref TStringArray m_FileContent;
-
- void PluginFileHandler()
- {
- m_FileContent = new TStringArray;
- m_LineLimit = -1;
-
- LoadFile();
- }
- void ~PluginFileHandler()
- {
- ClearFileNoSave();
- }
-
- override void OnInit()
- {
- super.OnInit();
- }
-
- string GetFileName()
- {
- return string.Empty;
- }
-
- string GetSubFolderName()
- {
- return string.Empty;
- }
-
- void ClearFileNoSave()
- {
- m_FileContent.Clear();
- }
- bool LoadFile()
- {
- ClearFileNoSave();
- FileHandle file_index = OpenFile(GetFileName(), FileMode.READ);
-
- if ( file_index == 0 )
- {
- return false;
- }
-
- string line_content = "";
- int char_count = FGets( file_index, line_content );
- while ( char_count != -1 )
- {
- m_FileContent.Insert(line_content);
-
- char_count = FGets( file_index, line_content );
- }
-
- CloseFile(file_index);
-
- return true;
- }
- bool SaveFile()
- {
- if ( m_ReadOnly )
- {
- return false;
- }
-
- if (GetSubFolderName() && !FileExist(GetSubFolderName()))
- {
- MakeDirectory(GetSubFolderName());
- }
-
- //Log("SaveFile -> Opening File: "+GetFileName());
-
- FileHandle file_index = OpenFile(GetFileName(), FileMode.WRITE);
-
- if ( file_index == 0 )
- {
- return false;
- }
-
- for ( int i = 0; i < m_FileContent.Count(); ++i)
- {
- //Log("SaveFile -> Writing: "+m_FileContent.Get(i));
- FPrintln(file_index, m_FileContent.Get(i));
- }
-
- CloseFile(file_index);
-
- return true;
- }
- bool IsReadOnly()
- {
- return m_ReadOnly;
- }
- void AddText(string text)
- {
- AddNewLineNoSave(text);
- SaveFile();
- }
- void AddNewLineNoSave(string text)
- {
- if ( m_LineLimit > -1 )
- {
- if ( m_LineLimit == 0 )
- {
- return;
- }
-
- int lines_count = m_FileContent.Count();
-
- if ( lines_count > 0 && lines_count >= m_LineLimit )
- {
- int remove_indexes = lines_count - m_LineLimit;
-
- for ( int i = 0; i <= remove_indexes; ++i )
- {
- m_FileContent.RemoveOrdered(0);
- }
- }
- }
-
- m_FileContent.Insert(text);
- }
- TStringArray GetFileContent()
- {
- return m_FileContent;
- }
- }
|