File size: 2,436 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
	}
}