forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipelineController.NewAction.cs
97 lines (78 loc) · 3.27 KB
/
PipelineController.NewAction.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
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MonoGame.Tools.Pipeline
{
public partial class PipelineController
{
private class NewAction : IProjectAction
{
private readonly PipelineController _con;
private readonly string _name;
private readonly string _location;
private readonly ContentItemTemplate _template;
public NewAction(PipelineController controller, string name, string location, ContentItemTemplate template)
{
_con = controller;
_name = name;
_location = location;
_template = template;
}
public bool Do()
{
var ext = Path.GetExtension(_template.TemplateFile);
var filename = Path.ChangeExtension(_name, ext);
var fullpath = _con.GetFullPath(Path.Combine(_location, filename));
if (File.Exists(fullpath))
{
_con.View.ShowError("Error", string.Format("File already exists: '{0}'.", fullpath));
return false;
}
File.Copy(_template.TemplateFile, fullpath);
var parser = new PipelineProjectParser(_con, _con._project);
_con.View.BeginTreeUpdate();
if (parser.AddContent(fullpath, true))
{
var item = _con._project.ContentItems.Last();
item.Observer = _con;
item.ImporterName = _template.ImporterName;
item.ProcessorName = _template.ProcessorName;
item.ResolveTypes();
_con.View.AddTreeItem(item);
}
_con.View.EndTreeUpdate();
_con.ProjectDirty = true;
return true;
}
public bool Undo()
{
var ext = Path.GetExtension(_template.TemplateFile);
var filename = Path.ChangeExtension(_name, ext);
var fullpath = Path.Combine(_location, filename);
if (!File.Exists(fullpath))
{
_con.View.ShowError("Error", string.Format("File does not exist: '{0}'.", fullpath));
return false;
}
File.Delete(fullpath);
_con.View.BeginTreeUpdate();
for (var i = 0; i < _con._project.ContentItems.Count; i++)
{
var item = _con._project.ContentItems[i];
var path = Path.GetFullPath(_con._project.Location + "\\" + item.OriginalPath);
if (fullpath == path)
{
_con._project.ContentItems.Remove(item);
_con.View.RemoveTreeItem(item);
}
}
_con.View.EndTreeUpdate();
_con.ProjectDirty = true;
return true;
}
}
}
}