This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDatafile.cs
95 lines (85 loc) · 2.4 KB
/
Datafile.cs
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
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
namespace Oxide
{
/// <summary>
/// Represents a data file than can store and recall plain text
/// </summary>
public class Datafile
{
private string text;
private bool changed;
private string filename;
public Datafile(string name)
{
filename = Main.GetPath("data/" + name + ".txt");
Reload();
}
/// <summary>
/// Lists all partialname*.txt files in the ./data/ folder
/// </summary>
public static string[] List(string partialname)
{
// Iterate all physical plugins
var files1 = Directory.GetFiles(Main.GetPath("data/"), partialname + "*.txt").Select(f => Path.GetFileNameWithoutExtension(f));
return files1.ToArray();
}
/// <summary>
/// Removes a fullname.txt file from the ./data/ folder
/// </summary>
public static bool Remove(string fullname)
{
// Iterate all physical plugins
string fname = Main.GetPath("data/" + fullname + ".txt");
if (!File.Exists(fname))
return false;
try
{
File.Delete(fname);
return true;
}
catch (IOException deleteError)
{
return false;
}
}
/// <summary>
/// Reloads this datafile
/// </summary>
public void Reload()
{
if (File.Exists(filename))
text = File.ReadAllText(filename);
else
text = "";
}
/// <summary>
/// Gets the plaintext stored in this datafile
/// </summary>
/// <returns></returns>
public string GetText()
{
return text;
}
/// <summary>
/// Sets the plaintext stored in this datafile
/// </summary>
/// <param name="txt"></param>
public void SetText(string txt)
{
text = txt;
changed = true;
}
/// <summary>
/// Saves this datafile if changes have been made
/// </summary>
public void Save()
{
if (!changed) return;
changed = false;
File.WriteAllText(filename, text);
}
}
}