forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateStorage.cs
More file actions
95 lines (78 loc) · 3.19 KB
/
TemplateStorage.cs
File metadata and controls
95 lines (78 loc) · 3.19 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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using OneScript.Contexts;
using OneScript.Contexts.Enums;
using OneScript.Exceptions;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
namespace ScriptEngine.HostedScript
{
/// <summary>
/// Контекст позволяет обращаться к макетам приложения
/// </summary>
[GlobalContext(Category = "Работа с макетами", ManualRegistration = true)]
public class TemplateStorage : GlobalContextBase<TemplateStorage>, IDisposable
{
private readonly ITemplateFactory _factory;
private readonly Dictionary<string, ITemplate> _templates = new Dictionary<string,ITemplate>();
public TemplateStorage(ITemplateFactory factory)
{
_factory = factory;
}
public void RegisterTemplate(string file, string name, TemplateKind kind)
{
if (_templates.ContainsKey(name))
throw RuntimeException.InvalidArgumentValue(name);
var template = _factory.CreateTemplate(file, kind);
_templates.Add(name, template);
}
public void RegisterTemplate(string name, ITemplate template)
{
if (_templates.ContainsKey(name))
throw RuntimeException.InvalidArgumentValue(name);
_templates.Add(name, template);
}
/// <summary>
/// Получает ранее зарегистрированный макет.
/// </summary>
/// <param name="templateName">Имя макета</param>
/// <returns>Строка или ДвоичныеДанные, в зависимости от типа макета.</returns>
[ContextMethod("ПолучитьМакет")]
public IValue GetTemplate(string templateName)
{
var template = _templates[templateName];
if (template.Kind == TemplateKind.File)
return ValueFactory.Create(template.GetFilename());
return template.GetBinaryData();
}
public IEnumerable<KeyValuePair<string, ITemplate>> GetTemplates()
{
return _templates;
}
public void Dispose()
{
foreach (var template in _templates.Values)
{
template.Dispose();
}
_templates.Clear();
}
}
/// <summary>
/// Тип макета в приложении. Значением макета в типе Файл является путь к файлу с данными.
/// </summary>
[EnumerationType("ТипМакета", "TemplateKind")]
public enum TemplateKind
{
[EnumValue("Файл", "File")]
File,
[EnumValue("ДвоичныеДанные", "BinaryData")]
BinaryData
}
}