-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBGDataStorage.cs
More file actions
66 lines (54 loc) · 1.46 KB
/
BGDataStorage.cs
File metadata and controls
66 lines (54 loc) · 1.46 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace BrickGameEmulator
{
public class BGDataStorage
{
private readonly string _path;
private Dictionary<string, int> _data;
public BGDataStorage(string name)
{
_path = name;
_init();
}
private void _init()
{
_checkFile();
_data = new Dictionary<string, int>();
var lines = File.ReadAllLines(_path);
foreach (var line in lines)
{
var splitLine = line.Split();
_data[splitLine[0]] = int.Parse(splitLine[1]);
}
}
private void _checkFile()
{
if (File.Exists(_path)) return;
using (var fs = File.Create(_path))
{
fs.Close();
}
}
public int GetInt(string key, int def)
{
return _data.ContainsKey(key) ? _data[key] : def;
}
public void PutInt(string key, int value)
{
_data[key] = value;
}
public void Remove(string key)
{
_data.Remove(key);
}
public void Commit()
{
File.Delete(_path);
var keyList = _data.Select(x => x.Key).ToArray();
File.WriteAllLines(_path, keyList.Select(key => key + " " + _data[key]).ToArray());
}
}
}