-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectFileManager.cs
More file actions
110 lines (96 loc) · 4.46 KB
/
ProjectFileManager.cs
File metadata and controls
110 lines (96 loc) · 4.46 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
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// BlockForge ProjectFileManager
/// Author: Angus Grewal
/// Date: Mar 25 2026
/// Source: Self-written, with AI coaching. All code submitted is human written, based on ChatGPT guidance.
/// </summary>
namespace COMP_3951_BlockForge_TechPro
{
/// <summary>
/// File IO handler to save and load a Project from application to disk or vice-versa.
/// </summary>
public class ProjectFileManager
{
/// <summary>
/// Requires a payload transformer to ensure our pipeline encrypts data at the file level.
/// </summary>
private PayloadTransformer _transformer;
/// <summary>
/// Constructor that will start the file manager with a supplied PayloadTransformer for encrypting or decrypting the data.
/// </summary>
/// <param name="transformer">which transformer to use.</param>
public ProjectFileManager(PayloadTransformer transformer)
{
_transformer = transformer;
}
/// <summary>
/// Saves the Project as a file on disk.
/// Project (DTO) -> JSON -> Encrypted -> UTF-8 encoded bytes -> [ProjectName].bfg
/// </summary>
/// <param name="project">project to save.</param>
/// <exception cref="InvalidOperationException">thrown when CodeBlocks fail to validate.</exception>
/// <exception cref="InvalidDataException">thrown if the project is attempted to be saved without a proper name.</exception>
public void SaveFile(Project project)
{
if (project.ProjectName == null)
{
throw new InvalidDataException("Project name cannot be null when saving.");
}
SaveFile(project, project.ProjectName + ".bfg");
}
/// <summary>
/// Saves the Project as a file on disk to an explicit path.
/// Project (DTO) -> JSON -> Encrypted -> UTF-8 encoded bytes -> filepath
/// </summary>
/// <param name="project">project to save.</param>
/// <param name="filepath">destination filepath for the saved project.</param>
/// <exception cref="InvalidOperationException">thrown when CodeBlocks fail to validate.</exception>
/// <exception cref="InvalidDataException">thrown if the project is attempted to be saved without a proper name.</exception>
public void SaveFile(Project project, string filepath)
{
List<string> errors = CodeBlockValidator.Validate(project.CodeBlocks);
if (errors.Count > 0)
{
string message = "Validation failed:\n" + string.Join("\n", errors);
throw new InvalidOperationException(message);
}
if (project.ProjectName == null)
{
throw new InvalidDataException("Project name cannot be null when saving.");
}
string json = ProjectSerializer.Serialize(project);
string scrambled = _transformer.Scramble(json);
byte[] encoded = Encoding.UTF8.GetBytes(scrambled);
File.WriteAllBytes(filepath, encoded);
}
/// <summary>
/// Loads a Project from a file on disk.
/// [ProjectName].bfg -> UTF-8 encoded bytes -> Encrypted -> JSON -> Project (DTO)
/// </summary>
/// <param name="filepath">the file to load.</param>
/// <returns>a reconstructed Project.</returns>
/// <exception cref="InvalidDataException">thrown if somehow a Project was saved without its name metadata.</exception>
/// <exception cref="InvalidOperationException">thrown when CodeBlocks fail to validate.</exception>
public Project LoadFile(string filepath)
{
byte[] data = File.ReadAllBytes(filepath);
string scrambled = Encoding.UTF8.GetString(data);
string json = _transformer.Unscramble(scrambled);
Project project = ProjectSerializer.Deserialize(json);
if (project.ProjectName == null)
{
throw new InvalidDataException("Project name was null when loading.");
}
List<string> errors = CodeBlockValidator.Validate(project.CodeBlocks);
if (errors.Count > 0)
{
string message = "Validation failed:\n" + string.Join("\n", errors);
throw new InvalidOperationException(message);
}
return project;
}
}
}