-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathUserConfig.cs
More file actions
88 lines (87 loc) · 2.96 KB
/
UserConfig.cs
File metadata and controls
88 lines (87 loc) · 2.96 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using BepInEx;
using UnityEngine;
namespace QuickerStack
{
public class UserConfig
{
public UserConfig(long uid)
{
this._configPath = Path.Combine(Paths.ConfigPath, string.Format("QuickerStack_player_{0}.dat", uid));
this._uid = uid;
this.Load();
}
private void Save()
{
using (Stream stream = File.Open(this._configPath, FileMode.Create))
{
UserConfig._bf.Serialize(stream, this._markedItems);
UserConfig._bf.Serialize(stream, this._markedCategories);
}
}
private static object TryDeserialize(Stream stream)
{
object result;
try
{
result = UserConfig._bf.Deserialize(stream);
}
catch (SerializationException)
{
result = null;
}
return result;
}
private static void LoadProperty<T>(Stream file, out T property) where T : new()
{
object obj = UserConfig.TryDeserialize(file);
if (obj is T)
{
T t = (T)((object)obj);
property = t;
return;
}
property = Activator.CreateInstance<T>();
}
private void Load()
{
using (Stream stream = File.Open(this._configPath, FileMode.OpenOrCreate))
{
stream.Seek(0L, SeekOrigin.Begin);
UserConfig.LoadProperty<List<Tuple<int, int>>>(stream, out this._markedItems);
UserConfig.LoadProperty<List<string>>(stream, out this._markedCategories);
}
}
public bool Toggle(Vector2i position)
{
Tuple<int, int> item = new Tuple<int, int>(position.x, position.y);
bool result = this._markedItems.XAdd(item);
this.Save();
return result;
}
public bool IsMarked(Vector2i position)
{
Tuple<int, int> item = new Tuple<int, int>(position.x, position.y);
return this._markedItems.Contains(item);
}
public bool Toggle(ItemDrop.ItemData.SharedData item)
{
bool result = this._markedCategories.XAdd(item.m_name);
this.Save();
return result;
}
public bool IsMarked(ItemDrop.ItemData.SharedData item)
{
return this._markedCategories.Contains(item.m_name);
}
private string _configPath = string.Empty;
private List<Tuple<int, int>> _markedItems;
private List<string> _markedCategories;
private long _uid;
private static BinaryFormatter _bf = new BinaryFormatter();
}
}