This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignManager.cs
97 lines (82 loc) · 3.09 KB
/
SignManager.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
96
97
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using TShockAPI;
using Terraria;
namespace SignControl
{
internal class SignManager
{
private static readonly Sign[] Signs = new Sign[Terraria.Sign.maxSigns];
private static readonly string ControlDirectory = Path.Combine(TShock.SavePath, "signcontrol");
private static readonly string SavePath = Path.Combine(ControlDirectory, Main.worldID + ".txt");
//TODO: add signs to db
public static Sign GetSign(int id)
{
return Signs[id];
}
public static void AddSign(Sign sign)
{
SetSign(sign.GetID(), sign);
}
public static void SetSign(int id, Sign sign)
{
if (Signs.Length > id)
Signs[id] = sign;
}
public static void Load()
{
if (!Directory.Exists(ControlDirectory))
Directory.CreateDirectory(ControlDirectory);
if (!File.Exists(SavePath))
File.Create(SavePath).Close();
var error = false;
foreach (var args in File.ReadAllLines(SavePath).Select(line => line.Split('|')).Where(args => args.Length >= 4))
{
try
{
var sign = new Sign();
sign.SetID(int.Parse(args[0]));
sign.SetPosition(new Vector2(int.Parse(args[1]), int.Parse(args[2])));
sign.SetPassword(args[3], args[3] != "");
if (args.Length == 5)
sign.SetWarp(args[4]);
//check if sign still exists in world
var id = Terraria.Sign.ReadSign((int)sign.GetPosition().X, (int)sign.GetPosition().Y);
if (id != 1)
{
//the id of sign changed
if (id != sign.GetID())
sign.SetID(id);
//add to array
AddSign(sign);
}
}
catch
{
error = true;
}
}
//TODO: lazy loading of signs ( null checks )
//fill empty slots
for (var i = 0; i < Signs.Length; i++)
{
if (Signs[i] == null)
{
Signs[i] = new Sign();
Signs[i].SetID(i);
}
}
if (error)
Console.WriteLine("Failed to load some sign data, corresponding signs will be left unprotected.");
}
public static void Save()
{
File.WriteAllLines(SavePath, (from sign in Signs
where sign != null
where sign.IsLocked() || sign.IsWarping()
select string.Format("{0}|{1}|{2}|{3}|{4}", sign.GetID(), sign.GetPosition().X, sign.GetPosition().Y, sign.GetPassword(), sign.GetWarp())).ToArray());
}
}
}