forked from TehCheat/FullRareSetManager
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStashData.cs
More file actions
137 lines (125 loc) · 4.25 KB
/
StashData.cs
File metadata and controls
137 lines (125 loc) · 4.25 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
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using ExileCore;
using Newtonsoft.Json;
namespace FullRareSetManager
{
public class StashData
{
private const string StashDataFile = "StashData.json";
public StashTabData PlayerInventory = new StashTabData();
[JsonIgnore]
public Dictionary<(int, int?), StashTabData> StashTabs { get; private set; } = new();
[JsonProperty]
private IReadOnlyDictionary<string, StashTabData> StoredStashTabs
{
get { return StashTabs.ToImmutableDictionary(x => $"{x.Key.Item1}/{x.Key.Item2}", x => x.Value); }
set
{
StashTabs = value.ToDictionary(x => x.Key.Split('/') switch
{
{ Length: 2 } a => (int.Parse(a[0]), string.IsNullOrEmpty(a[1]) ? (int?)null : int.Parse(a[1]))
},
x => x.Value);
}
}
public static StashData Load(FullRareSetManagerCore plugin)
{
try
{
var dataFileFullPath = Path.Join(plugin.ConfigDirectory, StashDataFile);
if (!File.Exists(dataFileFullPath))
{
var result = new StashData();
Save(plugin, result);
return result;
}
var json = File.ReadAllText(dataFileFullPath);
return JsonConvert.DeserializeObject<StashData>(json);
}
catch (Exception ex)
{
DebugWindow.LogError(
$"RareSetManager: Can't load cached items from file StashData.json. Creating new config. " +
$"Open stash tabs for updating info. Tell the developer if this happen often enough. {ex}",
10);
return new StashData();
}
}
public static void Save(FullRareSetManagerCore plugin, StashData data)
{
try
{
if (data == null) return;
var dataFileFullPath = Path.Join(plugin.ConfigDirectory, StashDataFile);
var settingsDirName = Path.GetDirectoryName(dataFileFullPath);
Directory.CreateDirectory(settingsDirName);
using var stream = new StreamWriter(File.Create(dataFileFullPath));
var json = JsonConvert.SerializeObject(data, Formatting.Indented);
stream.Write(json);
}
catch (Exception ex)
{
DebugWindow.LogError($"Unable to save settings: {ex}");
}
}
public static bool Reset(FullRareSetManagerCore plugin)
{
try
{
var dataFileFullPath = Path.Join(plugin.ConfigDirectory, StashDataFile);
if (!File.Exists(dataFileFullPath))
{
return true;
}
using var stream = new StreamWriter(File.Create(dataFileFullPath));
var json = JsonConvert.SerializeObject(new StashData(), Formatting.Indented);
stream.Write(json);
return true;
}
catch (Exception ex)
{
DebugWindow.LogError(
$"Cannot reset StashData:\n {ex}",
10);
return false;
}
}
}
public class StashTabData
{
public List<StashItem> StashTabItems = new List<StashItem>();
}
public class StashItem
{
public int InventPosX;
public int InventPosY;
public string ItemClass;
public string ItemName;
public StashItemType ItemType;
public int Width;
public int Height;
public bool LowLvl;
public string StashName;
public int StashIndex;
public int? NestedStashIndex;
public bool BInPlayerInventory { get; set; }
}
public enum StashItemType
{
Undefined = -1,
Weapon = 0,
Helmet = 1,
Body = 2,
Gloves = 3,
Boots = 4,
Belt = 5,
Amulet = 6,
Ring = 7,
TwoHanded = 8,
OneHanded = 9,
}
}