-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaScript.cs
129 lines (118 loc) · 4.05 KB
/
LuaScript.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Loader
{
[Serializable]
internal class LuaScript
{
private readonly Dictionary<string, LuaScript.RequirementState> _requirements = new Dictionary<string, LuaScript.RequirementState>();
private bool _load;
private string _scriptName;
private LuaScript.RequirementState _state;
public string ScriptName
{
get
{
return _scriptName;
}
set
{
_scriptName = value;
}
}
public bool LoadScript
{
get
{
return _load;
}
set
{
_load = value;
}
}
public bool HasConfig { get; private set; }
public string ScriptDescription { get; private set; }
public Dictionary<string, LuaScript.RequirementState> Requirements
{
get
{
return _requirements;
}
}
public LuaScript.RequirementState State
{
get
{
return _state;
}
private set
{
if (_state == LuaScript.RequirementState.StateUnknown)
return;
_state = value;
}
}
public LuaScript(string name)
{
HasConfig = false;
_scriptName = name;
CheckRequirements();
}
public override string ToString()
{
return _scriptName.Substring(_scriptName.LastIndexOf(Path.DirectorySeparatorChar) + 1);
}
private void CheckRequirements()
{
try
{
string[] strArray = File.ReadAllLines(Path.Combine("Scripts", _scriptName + ".lua"));
Regex regexConfig = new Regex("ScriptConfig\\.new\\(", RegexOptions.Compiled);
using (IEnumerator<string> enumerator = Enumerable.Where<string>(strArray, line => regexConfig.IsMatch(line)).GetEnumerator())
{
if (enumerator.MoveNext())
{
string current = enumerator.Current;
HasConfig = true;
}
}
Regex regex1 = new Regex("--<<(.*?)>>", RegexOptions.Compiled);
ScriptDescription = strArray.Length <= 0 || !regex1.IsMatch(strArray[0]) ? string.Empty : regex1.Match(strArray[0]).Groups[1].Value;
Regex regex = new Regex("require[ ]*\\([ ]*\"libs\\.([A-Za-z0-9]+)\"[ ]*\\)", RegexOptions.Compiled);
List<string> list = Enumerable.ToList<string>(Enumerable.Select<string, string>(Enumerable.Where<string>(strArray, line => regex.IsMatch(line)), line => regex.Match(line).Groups[1].Value));
_state = LuaScript.RequirementState.StateOkay;
foreach (string index in list)
{
_requirements[index] = LuaScript.RequirementState.StateOkay;
try
{
if (!File.Exists(Path.Combine("Scripts", "libs", index + ".lua")))
{
_requirements[index] = LuaScript.RequirementState.StateError;
_state = LuaScript.RequirementState.StateError;
}
}
catch (Exception)
{
_requirements[index] = LuaScript.RequirementState.StateUnknown;
_state = LuaScript.RequirementState.StateUnknown;
}
}
}
catch (Exception)
{
_state = LuaScript.RequirementState.StateUnknown;
}
}
public enum RequirementState
{
StateUnknown,
StateOkay,
StateError,
}
}
}