-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddGameWindow.cs
204 lines (180 loc) · 7.26 KB
/
AddGameWindow.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using SpineGTK_v1;
using System.Net;
using System.Diagnostics;
using Amr;
using System.Drawing;
using System.Drawing.Imaging;
namespace SpineGTK_v1
{
public partial class AddGameWindow : Gtk.Window
{
public AddGameWindow() : base(Gtk.WindowType.Toplevel)
{
this.Build();
}
protected void OnBtnEnter1Clicked(object sender, EventArgs e)
{
string home = Environment.GetEnvironmentVariable("HOME");
if (string.IsNullOrEmpty(home))
{
var passwd = File.ReadAllLines("/etc/passwd");
var entry = passwd.FirstOrDefault(x => x.StartsWith(Environment.UserName + ":"));
if (entry != null)
{
var parts = entry.Split(':');
home = parts[5];
}
}
if (entry1.Text != "" && entry2.Text != "")
{
String path = home + "/SpineGTK/config.xml";
XDocument Xdoc = new XDocument(new XElement("Games"));
if (File.Exists(path))
{
Xdoc = XDocument.Load(path);
}
else
{
Xdoc = new XDocument(new XElement("Games"));
}
var existing = Xdoc.Descendants("Game").FirstOrDefault(m => m.Element("Name")?.Value == entry1.Text);
if (existing != null) //name existed in xml
{
existing.Element("Directory").Value = entry2.Text;
}
else
{
XElement xml = new XElement("Game");
xml.Add(new XElement("Name", entry1.Text));
xml.Add(new XElement("Directory", entry2.Text));
if (!string.IsNullOrEmpty(entry3.Text)) // Check if entry3 (image path) is provided
{
if (!Directory.Exists(home + "/SpineGTK/Icons"))
{
DirectoryInfo di = Directory.CreateDirectory(home + "/SpineGTK/Icons");
}
File.Copy(entry3.Text, home + "/SpineGTK/Icons/" + System.IO.Path.GetFileName(entry3.Text), true); //Overwrite option enabled to avoid errors
using (var newImage = Amr.Resizer.FixedSize(home + "/SpineGTK/Icons/" + System.IO.Path.GetFileName(entry3.Text), 64, 64))
{
newImage.Save(home + "/SpineGTK/Icons/" + System.IO.Path.GetFileName(entry3.Text), ImageFormat.Png);
newImage.Dispose();
}
xml.Add(new XElement("Icon", home + "/SpineGTK/Icons/" + System.IO.Path.GetFileName(entry3.Text)));
}
if (Xdoc.Descendants().Count() > 0)
{
Xdoc.Descendants().First().Add(xml);
}
else
{
Xdoc.Add(xml);
}
}
Xdoc.Save(path);
}
else
{
Console.WriteLine("ERROR: You didn't fill an important text field.");
}
this.Destroy();
MainWindow mainWindow = new MainWindow();
mainWindow.UpdateMainWindow();
}
protected void OnBtnCancel1Clicked(object sender, EventArgs e)
{
this.Destroy();
}
protected void OnBtnInstallSpineClicked(object sender, EventArgs e)
{
string home = Environment.GetEnvironmentVariable("HOME");
if (string.IsNullOrEmpty(home))
{
var passwd = File.ReadAllLines("/etc/passwd");
var entry = passwd.FirstOrDefault(x => x.StartsWith(Environment.UserName + ":"));
if (entry != null)
{
var parts = entry.Split(':');
home = parts[5];
}
}
//File to Download & Download Path
string url = "https://github.com/devofspine/spine/releases/download/20220517/spine-20220517.zip";
string filePath = home + "/SpineGTK/";
//Check if the directory exists
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
Console.WriteLine("Directory Created");
}
//Try to give permissions for the folder
try
{
Process process = new Process();
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"-c \"chmod a+rw {filePath}\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.WaitForExit();
Console.WriteLine("Folder Permissions given successfully");
if (process.ExitCode != 0)
{
Console.WriteLine("Warning: Folder's permissions not set. Try executing this Software as ROOT");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: An exception occurred while trying to set folder's permissions: {0}", ex.Message);
}
//Download File
string fileToDownload = filePath + "spine-20220517.zip";
if (!File.Exists(fileToDownload))
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
using (FileStream fileStream = File.Create(filePath + "spine-20220517.zip"))
{
responseStream.CopyTo(fileStream);
}
response.Close();
}
catch (WebException ex)
{
Console.WriteLine("An error occurred while downloading the file: {0}", ex.Message);
}
}
else
{
Console.WriteLine("File already exists, skipping download.");
}
//UnZip File
string zipPath = filePath + "spine-20220517.zip";
string extractPath = filePath + "Spine/";
if (!Directory.Exists(extractPath) || !Directory.GetFiles(extractPath).Any())
{
try
{
ZipFile.ExtractToDirectory(zipPath, extractPath);
Console.WriteLine("Zip File extracted successfully");
}
catch (IOException ex)
{
Console.WriteLine("Zip File Extraction Error: " + ex.Message);
}
}
else
{
Console.WriteLine("Error: Target directory already exists and is not empty.");
}
}
}
}