Skip to content

Commit c4203d0

Browse files
committed
Merge branch 'develop' into release/v1.1.0
2 parents 32c457f + 4a1af81 commit c4203d0

File tree

10 files changed

+742
-415
lines changed

10 files changed

+742
-415
lines changed

src/ScriptEngine.HostedScript/Library/FixedStructureImpl.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public FixedStructureImpl(StructureImpl structure)
2020
{
2121
foreach (KeyAndValueImpl keyValue in structure)
2222
_structure.Insert(keyValue.Key.AsString(), keyValue.Value);
23-
2423
}
2524

2625
public FixedStructureImpl(string strProperties, params IValue[] values)
@@ -139,18 +138,10 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
139138
/// Создает фиксированную структуру по исходной структуре
140139
/// </summary>
141140
/// <param name="structure">Исходная структура</param>
142-
[ScriptConstructor(Name = "Из структуры")]
143-
public static FixedStructureImpl Constructor(IValue structure)
141+
//[ScriptConstructor(Name = "Из структуры")]
142+
private static FixedStructureImpl Constructor(StructureImpl structObject)
144143
{
145-
var structObject = structure.GetRawValue() as StructureImpl;
146-
if (structObject != null)
147-
{
148-
return new FixedStructureImpl(structObject);
149-
}
150-
else
151-
{
152-
throw new RuntimeException("В качестве параметра для конструктора можно передавать только Структура или Ключи и Значения");
153-
}
144+
return new FixedStructureImpl(structObject);
154145
}
155146

156147
/// <summary>
@@ -159,10 +150,20 @@ public static FixedStructureImpl Constructor(IValue structure)
159150
/// <param name="strProperties">Строка с именами свойств, указанными через запятую.</param>
160151
/// <param name="args">Значения свойств. Каждое значение передается, как отдельный параметр.</param>
161152
[ScriptConstructor(Name = "По ключам и значениям")]
162-
public static FixedStructureImpl Constructor(IValue strProperties, IValue[] args)
153+
public static FixedStructureImpl Constructor(IValue param1, IValue[] args)
163154
{
164-
return new FixedStructureImpl(strProperties.AsString(), args);
165-
}
155+
var rawArgument = param1.GetRawValue();
156+
if (rawArgument.DataType == DataType.String)
157+
{
158+
return new FixedStructureImpl(param1.AsString(), args);
159+
}
160+
else if (rawArgument is StructureImpl)
161+
{
162+
return new FixedStructureImpl(rawArgument as StructureImpl);
163+
}
166164

165+
throw new RuntimeException("В качестве параметра для конструктора можно передавать только Структура или Ключи и Значения");
167166
}
167+
168+
}
168169
}

src/ScriptEngine.HostedScript/Library/StructureImpl.cs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,19 @@ public StructureImpl()
2323
public StructureImpl(string strProperties, params IValue[] values)
2424
{
2525
var props = strProperties.Split(',');
26-
if (props.Length < values.Length)
27-
throw RuntimeException.InvalidArgumentValue();
2826

29-
for (int i = 0; i < props.Length; i++)
27+
for (int i = 0, nprop = 0; i < props.Length; i++)
3028
{
31-
props[i] = props[i].Trim();
32-
if (i < values.Length)
33-
{
34-
Insert(props[i], values[i]);
35-
}
36-
else
37-
{
38-
Insert(props[i], null);
39-
}
29+
var prop = props[i].Trim();
30+
if (prop.Equals(string.Empty))
31+
continue;
32+
33+
Insert(prop, nprop < values.Length ? values[nprop] : null);
34+
++nprop;
4035
}
4136
}
4237

43-
public StructureImpl(IEnumerable<KeyAndValueImpl> structure)
38+
public StructureImpl(FixedStructureImpl structure)
4439
{
4540
foreach (KeyAndValueImpl keyValue in structure)
4641
{
@@ -51,6 +46,9 @@ public StructureImpl(IEnumerable<KeyAndValueImpl> structure)
5146
[ContextMethod("Вставить")]
5247
public void Insert(string name, IValue val = null)
5348
{
49+
if (!Utils.IsValidIdentifier(name))
50+
throw InvalidPropertyNameException(name);
51+
5452
var num = RegisterProperty(name);
5553
if (num == _values.Count)
5654
{
@@ -68,8 +66,20 @@ public void Insert(string name, IValue val = null)
6866
[ContextMethod("Удалить", "Delete")]
6967
public void Remove(string name)
7068
{
71-
var id = FindProperty(name);
72-
_values.RemoveAt(id);
69+
if (!Utils.IsValidIdentifier(name))
70+
throw InvalidPropertyNameException(name);
71+
72+
int propIndex;
73+
try
74+
{
75+
propIndex = FindProperty(name);
76+
}
77+
catch (PropertyAccessException)
78+
{
79+
return;
80+
}
81+
82+
_values.RemoveAt(propIndex);
7383
RemoveProperty(name);
7484
ReorderPropertyNumbers();
7585
}
@@ -78,7 +88,7 @@ public void Remove(string name)
7888
public bool HasProperty(string name, [ByRef] IVariable value = null)
7989
{
8090
if (!Utils.IsValidIdentifier(name))
81-
throw new RuntimeException("Задано неправильное имя атрибута структуры");
91+
throw InvalidPropertyNameException(name);
8292

8393
int propIndex;
8494
try
@@ -216,21 +226,41 @@ public static StructureImpl Constructor()
216226
return new StructureImpl();
217227
}
218228

229+
/// <summary>
230+
/// Создает структуру по фиксированной структуре
231+
/// </summary>
232+
/// <param name="fixedStruct">Исходная структура</param>
233+
//[ScriptConstructor(Name = "Из фиксированной структуры")]
234+
private static StructureImpl Constructor(FixedStructureImpl fixedStruct)
235+
{
236+
return new StructureImpl(fixedStruct);
237+
}
238+
219239
/// <summary>
220240
/// Создает структуру по заданному перечню свойств и значений
221241
/// </summary>
222242
/// <param name="strProperties">Строка с именами свойств, указанными через запятую.</param>
223243
/// <param name="args">Значения свойств. Каждое значение передается, как отдельный параметр.</param>
224244
[ScriptConstructor(Name = "По ключам и значениям")]
225-
public static StructureImpl Constructor(IValue strProperties, IValue[] args)
245+
public static StructureImpl Constructor(IValue param1, IValue[] args)
226246
{
227-
var rawArgument = strProperties.GetRawValue();
228-
if (rawArgument is IEnumerable<KeyAndValueImpl>)
247+
var rawArgument = param1.GetRawValue();
248+
if (rawArgument.DataType == DataType.String)
229249
{
230-
return new StructureImpl(rawArgument as IEnumerable<KeyAndValueImpl>);
250+
return new StructureImpl(rawArgument.AsString(), args);
231251
}
232-
return new StructureImpl(rawArgument.AsString(), args);
252+
else if (rawArgument is FixedStructureImpl)
253+
{
254+
return new StructureImpl(rawArgument as FixedStructureImpl);
255+
}
256+
257+
throw new RuntimeException("В качестве параметра для конструктора можно передавать только ФиксированнаяСтруктура или Ключи и Значения");
233258
}
234259

260+
261+
private static RuntimeException InvalidPropertyNameException( string name )
262+
{
263+
return new RuntimeException($"Задано неправильное имя атрибута структуры '{name}'");
264+
}
235265
}
236266
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
namespace ScriptEngine.HostedScript.Library.Zip
9+
{
10+
[EnumerationType("КодировкаИменФайловВZipФайле","FileNamesEncodingInZipFile")]
11+
public enum FileNamesEncodingInZipFile
12+
{
13+
[EnumItem("Авто")]
14+
Auto,
15+
16+
[EnumItem("UTF8")]
17+
Utf8,
18+
19+
[EnumItem("КодировкаОСДополнительноUTF8","OSEncodingWithUTF8")]
20+
OsEncodingWithUtf8
21+
}
22+
}

src/ScriptEngine.HostedScript/Library/Zip/ZipReader.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,22 @@ private void CheckIfOpened()
4444
/// <param name="filename">Имя ZIP файла, который требуется открыть для чтения.</param>
4545
/// <param name="password">Пароль к файлу, если он зашифрован.</param>
4646
[ContextMethod("Открыть","Open")]
47-
public void Open(string filename, string password = null)
47+
public void Open(string filename, string password = null, FileNamesEncodingInZipFile encoding = FileNamesEncodingInZipFile.Auto)
4848
{
49+
ZipFile.DefaultEncoding = Encoding.GetEncoding(866);
4950
// fuck non-russian encodings on non-ascii files
50-
_zip = ZipFile.Read(filename, new ReadOptions() { Encoding = Encoding.GetEncoding(866) });
51+
_zip = ZipFile.Read(filename, new ReadOptions() { Encoding = ChooseEncoding(encoding) });
5152
_zip.Password = password;
5253
}
5354

55+
private Encoding ChooseEncoding(FileNamesEncodingInZipFile encoding)
56+
{
57+
if(encoding != FileNamesEncodingInZipFile.OsEncodingWithUtf8)
58+
return Encoding.UTF8;
59+
60+
return null;
61+
}
62+
5463

5564
/// <summary>
5665
/// Извлечение всех файлов из архива

0 commit comments

Comments
 (0)