forked from DatZach/CharacterEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormLoadCharacter.cs
More file actions
121 lines (101 loc) · 3.43 KB
/
FormLoadCharacter.cs
File metadata and controls
121 lines (101 loc) · 3.43 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace CharacterEditor
{
public partial class FormLoadCharacter : Form
{
// TODO Exception form
// TODO Backup database just in case
public List<CharacterData> Characters { get; private set; }
public CharacterData SelectedCharacter { get; private set; }
private readonly Database database;
public FormLoadCharacter(Database database)
{
Characters = new List<CharacterData>();
this.database = database;
SelectedCharacter = null;
InitializeComponent();
}
private void FormLoadCharacterLoad(object sender, EventArgs e)
{
string databasePath = FindCubeWorldDirectory();
if (!String.IsNullOrEmpty(databasePath) && File.Exists(databasePath + @"\characters.db"))
DatabaseLoad(databasePath + @"\characters.db");
}
private void ButtonLoadDatabaseClick(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Filter = "Database|characters.db",
CheckFileExists = true,
CheckPathExists = true
};
DialogResult result = dialog.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
DatabaseLoad(dialog.FileName);
}
private void ListBoxCharactersSelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxCharacters.SelectedIndex < 0 || listBoxCharacters.SelectedIndex >= Characters.Count)
return;
SelectedCharacter = Characters[listBoxCharacters.SelectedIndex];
DialogResult = DialogResult.OK;
Close();
}
private void DatabaseLoad(string filename)
{
try
{
database.Load(filename);
}
catch (Exception)
{
MessageBox.Show(this, "Database appears to be corrupted!", "Character Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Characters.Clear();
listBoxCharacters.Items.Clear();
try
{
int characterCount = database.ReadBlobByKey("num")[0];
for (int i = 0; i < characterCount; ++i)
{
CharacterData character = new CharacterData(i);
character.Load(database);
Characters.Add(character);
listBoxCharacters.Items.Add(character.Name);
}
}
catch (Exception exception)
{
StringBuilder message = new StringBuilder();
message.AppendLine("Database appears to be corrupted!");
message.AppendLine(exception.Message);
message.AppendLine(exception.Source);
message.AppendLine(exception.StackTrace);
MessageBox.Show(this, message.ToString(), "Character Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
Characters.Clear();
listBoxCharacters.Items.Clear();
}
}
private string FindCubeWorldDirectory()
{
const string cubeWorldSaveDirectory = @"\Cube World\Save";
// Check if it exists in Program Files (32bit)
string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (Directory.Exists(programFiles + cubeWorldSaveDirectory))
return programFiles + cubeWorldSaveDirectory;
// Check if it exists on the Desktop
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
if (Directory.Exists(desktop + cubeWorldSaveDirectory))
return desktop + cubeWorldSaveDirectory;
// Can't find it
return String.Empty;
}
}
}