forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptFileHelper.cs
More file actions
103 lines (83 loc) · 3.57 KB
/
ScriptFileHelper.cs
File metadata and controls
103 lines (83 loc) · 3.57 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
/*----------------------------------------------------------
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.IO;
using System.Net.Configuration;
using System.Reflection;
using System.Text;
using ScriptEngine.Environment;
using ScriptEngine.HostedScript;
namespace oscript
{
internal static class ScriptFileHelper
{
public static bool CodeStatisticsEnabled { get; private set; }
public static string CodeStatisticsFileName { get; private set; }
public static string CustomConfigPath(string scriptPath)
{
var dir = Path.GetDirectoryName(scriptPath);
var cfgPath = Path.Combine(dir, HostedScriptEngine.ConfigFileName);
return File.Exists(cfgPath) ? cfgPath : null;
}
public static void EnableCodeStatistics(string fileName)
{
CodeStatisticsEnabled = fileName != null;
CodeStatisticsFileName = fileName;
}
// http://www.cookcomputing.com/blog/archives/000556.html
public static bool SetAllowUnsafeHeaderParsing()
{
var aNetAssembly = Assembly.GetAssembly(typeof(SettingsSection));
if (aNetAssembly == null) return false;
var aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType == null) return false;
var anInstance = aSettingsType.InvokeMember("Section", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[]
{
});
if (anInstance == null) return false;
var aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
if (aUseUnsafeHeaderParsing == null) return false;
aUseUnsafeHeaderParsing.SetValue(anInstance, true);
return true;
}
private static bool ConvertSettingValueToBool(string value, bool defaultValue = false)
{
if (value == null) return defaultValue;
if (string.Compare(value, "true", StringComparison.InvariantCultureIgnoreCase) == 0) return true;
if (string.Compare(value, "1", StringComparison.InvariantCultureIgnoreCase) == 0) return true;
if (string.Compare(value, "yes", StringComparison.InvariantCultureIgnoreCase) == 0) return true;
if (string.Compare(value, "false", StringComparison.InvariantCultureIgnoreCase) == 0) return false;
if (string.Compare(value, "0", StringComparison.InvariantCultureIgnoreCase) == 0) return false;
if (string.Compare(value, "no", StringComparison.InvariantCultureIgnoreCase) == 0) return false;
return defaultValue;
}
public static void OnBeforeScriptRead(HostedScriptEngine engine)
{
var cfg = engine.GetWorkingConfig();
var openerEncoding = cfg["encoding.script"];
if (!string.IsNullOrWhiteSpace(openerEncoding))
if (StringComparer.InvariantCultureIgnoreCase.Compare(openerEncoding, "default") == 0)
engine.Loader.ReaderEncoding = FileOpener.SystemSpecificEncoding();
else
engine.Loader.ReaderEncoding = Encoding.GetEncoding(openerEncoding);
var strictWebRequest = ConvertSettingValueToBool(cfg["http.strictWebRequest"]);
if (!strictWebRequest)
SetAllowUnsafeHeaderParsing();
if (CodeStatisticsEnabled)
engine.EnableCodeStatistics();
}
public static void OnAfterScriptExecute(HostedScriptEngine engine)
{
if (CodeStatisticsEnabled)
{
var codeStat = engine.GetCodeStatData();
var statsWriter = new CodeStatWriter(CodeStatisticsFileName, CodeStatWriterType.JSON);
statsWriter.Write(codeStat);
}
}
}
}