-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.txt
More file actions
203 lines (167 loc) · 6.15 KB
/
Files.txt
File metadata and controls
203 lines (167 loc) · 6.15 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
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
Ok so let's build the definition of a sprite before we acually build a sprite.
A sprite should be a folder inside of the project's "items" folder.
In case you don't remember, a project file (*.xPaint) is a zip file with a project.json file,
a header.json file, and a "items", "compiled", and now a sprites folder inside of the items folder.
In the "sprites" folder there should be, well... Sprites. So anyway a sprite is a folder (inside of the sprites folder) containing
the following:
Sprite.json (Attributes), Sprite.wxa (Animation or images), Sprite.png (Thumbnail), and Sprite.pss (PaintScript), then next to those files
should be a items folder. When opening a sprite in the editor this is what the explorer should show,
the root of the items folder in the sprite folder, no longer the "items" folder in the root of
the project.
So when a sprite is exported, it's just a zip file containing everthing (*.pSprite).
Just to be clear, what I mean by opening a sprite is selecting a sprite in the sprite manager area.
When you do that, the explorer and editor should then show, and the workspace directory will change
depending on the sprite, but of course the root of the full project stays the same.
using System;
using System.IO;
namespace PaintPower.ProjectSystem;
public class TempWorkspace
{
public string Root { get; }
public string ItemsDir => Path.Combine(Root, "items");
public TempWorkspace()
{
Root = Path.Combine(Path.GetTempPath(), "PaintPower_" + Guid.NewGuid());
Directory.CreateDirectory(Root);
Directory.CreateDirectory(ItemsDir);
}
public string MapToTemp(string projectRelativePath)
{
return Path.Combine(ItemsDir, projectRelativePath.Replace("/", "\\"));
}
public void ImportFile(string sourcePath, string relativePath)
{
string dest = MapToTemp(relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
File.Copy(sourcePath, dest, overwrite: true);
}
public void SaveFile(string relativePath, string content)
{
string dest = MapToTemp(relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
File.WriteAllText(dest, content);
}
public void SaveBinary(string relativePath, byte[] data)
{
string dest = MapToTemp(relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
File.WriteAllBytes(dest, data);
}
public byte[] LoadBinary(string relativePath)
{
string path = MapToTemp(relativePath);
return File.Exists(path) ? File.ReadAllBytes(path) : Array.Empty<byte>();
}
public string LoadText(string relativePath)
{
string path = MapToTemp(relativePath);
return File.Exists(path) ? File.ReadAllText(path) : "";
}
public void Delete(string relativePath)
{
string path = MapToTemp(relativePath);
if (File.Exists(path))
File.Delete(path);
}
public void Cleanup()
{
if (Directory.Exists(Root))
Directory.Delete(Root, recursive: true);
}
}
using System;
using System.IO;
using System.IO.Compression;
using System.Text.Json;
namespace PaintPower.ProjectSystem;
public class PaintProject
{
public string ProjectPath { get; set; } = ""; // Path to zip file.
public TempWorkspace Workspace { get; }
public ProjectMetadata Metadata { get; private set; }
public string ProjectName { get; private set; } = string.Empty;
public PaintProject()
{
Workspace = new TempWorkspace();
Metadata = new ProjectMetadata();
}
// -------------------------
// CREATE NEW PROJECT
// -------------------------
public void CreateNew(string projectPath, string Name)
{
ProjectPath = projectPath;
Metadata = new ProjectMetadata
{
name = Name,
OpenFile = null
};
SaveMetadata();
SaveToDisk();
}
// -------------------------
// LOAD EXISTING PROJECT
// -------------------------
public void Load(string projectPath)
{
ProjectPath = projectPath;
if (Directory.Exists(Workspace.Root)) { }
// Extract ZIP into temp workspace
ZipFile.ExtractToDirectory(projectPath, Workspace.Root, overwriteFiles: true);
// Load metadata
string metaPath = Path.Combine(Workspace.Root, "project.json");
if (File.Exists(metaPath))
{
string json = File.ReadAllText(metaPath);
Metadata = JsonSerializer.Deserialize<ProjectMetadata>(json) ?? new ProjectMetadata();
}
else
{
Metadata = new ProjectMetadata();
}
}
// -------------------------
// SAVE PROJECT AS ZIP
// -------------------------
public void SaveToZip()
{
if (string.IsNullOrWhiteSpace(ProjectPath))
throw new Exception("ProjectPath is not set.");
// Update metadata file
SaveMetadata();
// Recreate ZIP
if (File.Exists(ProjectPath))
File.Delete(ProjectPath);
ZipFile.CreateFromDirectory(Workspace.Root, Path.Combine(Workspace.Root, "..", "Paintfile", "file.zip"));
}
public void ClearTempZip() {
File.Delete(Path.Combine(Workspace.Root, "..", "Paintfile", "file.zip"));
}
// -------------------------
// SAVE PROJECT
// -------------------------
public void SaveToDisk()
{
if (string.IsNullOrWhiteSpace(ProjectPath))
throw new Exception("ProjectPath is not set.");
// Update metadata file
SaveMetadata();
// Recreate ZIP
if (File.Exists(ProjectPath))
File.Delete(ProjectPath);
ZipFile.CreateFromDirectory(Workspace.Root, ProjectPath);
}
private void SaveMetadata()
{
string json = JsonSerializer.Serialize(Metadata, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(Path.Combine(Workspace.Root, "project.json"), json);
}
}
// -------------------------
// PROJECT METADATA STRUCT
// -------------------------
public class ProjectMetadata
{
public string name { get; set; } = "Untitled Project";
public string? OpenFile { get; set; }
}