Skip to content

Commit bfe145f

Browse files
Added Windows-OS Package
1 parent 7b8ea9f commit bfe145f

File tree

2 files changed

+200
-18
lines changed

2 files changed

+200
-18
lines changed

EZCode/EZHelp.cs

Lines changed: 197 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using static EZCodeLanguage.Parser;
22
using static EZCodeLanguage.Interpreter;
3+
using Timer = System.Timers.Timer;
34
using System.Data;
45
using System.Diagnostics;
56
using System.Timers;
6-
using Timer = System.Timers.Timer;
7+
using Microsoft.Win32;
8+
using System.Reflection;
79

810
namespace EZCodeLanguage
911
{
@@ -557,7 +559,7 @@ public bool IsType(object obj, object type)
557559
throw;
558560
}
559561
}
560-
public object ArrayParse(object array, object separator) => ObjectParse(array, "list").ToString().Split(StringParse(separator)).Select(x => x.Trim()).ToArray();
562+
public object ArrayParse(object array, object separator) => ObjectParse(array, "list").ToString().Split(StringParse(separator)).Select(x => x.Trim()).ToArray();
561563
public string ArrayStringParse(object array) => ObjectParse(array, "list", to_string: false, arraySeperator: ", ").ToString();
562564
public int ArrayLength(object array)
563565
{
@@ -824,6 +826,7 @@ public static T GetParameter<T>(object obj, string type)
824826

825827
return (T)e.ObjectParse(obj, type);
826828
}
829+
public static Exception ThrowError(string message) => ThrowError(new Exception(message));
827830
public static Exception ThrowError(Exception exception)
828831
{
829832
Error = exception.Message;
@@ -1175,5 +1178,197 @@ private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
11751178
_Timers[timer] = true;
11761179
}
11771180
public Dictionary<Timer, bool> _Timers = [];
1181+
1182+
// OS Package:
1183+
// include os
1184+
public void RegistrySetKey(object _keyName, object _key, object _value)
1185+
{
1186+
try
1187+
{
1188+
string keyName = StringParse(_keyName),
1189+
key = StringParse(_key);
1190+
object value = ObjectParse(_value, "str");
1191+
1192+
using (var v = Registry.CurrentUser.CreateSubKey(keyName))
1193+
{
1194+
Type type = value.GetType();
1195+
if (type == typeof(int))
1196+
{
1197+
value = $"_ {value}";
1198+
}
1199+
v.SetValue(key, value);
1200+
}
1201+
}
1202+
catch (Exception ex)
1203+
{
1204+
throw ThrowError(ex);
1205+
}
1206+
}
1207+
public object RegistryGetKey(object _keyName, object _key)
1208+
{
1209+
try
1210+
{
1211+
string keyName = StringParse(_keyName),
1212+
key = StringParse(_key);
1213+
1214+
using (var v = Registry.CurrentUser.OpenSubKey(keyName))
1215+
{
1216+
if (v != null)
1217+
{
1218+
return v.GetValue(key);
1219+
}
1220+
else
1221+
{
1222+
throw new Exception($"Could not find registry key at '{Path.Combine(keyName, key)}'");
1223+
}
1224+
}
1225+
}
1226+
catch (Exception ex)
1227+
{
1228+
throw ThrowError(ex);
1229+
}
1230+
}
1231+
public string RegistryLocalMachine() => Registry.LocalMachine.ToString();
1232+
public string RegistryCurrentUser() => Registry.CurrentUser.ToString();
1233+
public string RegistryUsers() => Registry.Users.ToString();
1234+
public string RegistryClassesRoot() => Registry.ClassesRoot.ToString();
1235+
public string RegistryPerformanceData() => Registry.PerformanceData.ToString();
1236+
public string RegistryCurrentConfig() => Registry.CurrentConfig.ToString();
1237+
public Process ProcessStart(object _path, object? _arguments)
1238+
{
1239+
try
1240+
{
1241+
string path = FixDirPath( StringParse(_path) );
1242+
object? arguments = Try(() => StringParse(_arguments));
1243+
1244+
if (arguments != null)
1245+
{
1246+
return Process.Start(path, arguments.ToString());
1247+
}
1248+
else
1249+
{
1250+
return Process.Start(path);
1251+
}
1252+
}
1253+
catch (Exception ex)
1254+
{
1255+
throw ThrowError(ex);
1256+
}
1257+
}
1258+
public string ProcessName(object _process) => (ObjectParse(_process, "process") as Process).ProcessName;
1259+
public Process ProcessGetCurrentProcess() => Process.GetCurrentProcess();
1260+
public Process ProcessGetProcessById(object _id, object? _machineName)
1261+
{
1262+
try
1263+
{
1264+
int id = IntParse(_id);
1265+
object? machineName = Try(() => StringParse(_machineName));
1266+
1267+
if (machineName != null)
1268+
{
1269+
return Process.GetProcessById(id, machineName.ToString());
1270+
}
1271+
else
1272+
{
1273+
return Process.GetProcessById(id);
1274+
}
1275+
}
1276+
catch (Exception ex)
1277+
{
1278+
throw ThrowError(ex);
1279+
}
1280+
}
1281+
public Process[] ProcessGetProcesses(object? _machineName)
1282+
{
1283+
try
1284+
{
1285+
object? machineName = Try(() => StringParse(_machineName));
1286+
1287+
if (machineName != null)
1288+
{
1289+
return Process.GetProcesses(machineName.ToString());
1290+
}
1291+
else
1292+
{
1293+
return Process.GetProcesses();
1294+
}
1295+
}
1296+
catch (Exception ex)
1297+
{
1298+
throw ThrowError(ex);
1299+
}
1300+
}
1301+
public Process[] ProcessGetProcessByName(object _name, object? _machineName)
1302+
{
1303+
try
1304+
{
1305+
string name = StringParse(_name);
1306+
object? machineName = Try(() => StringParse(_machineName));
1307+
1308+
if (machineName != null)
1309+
{
1310+
return Process.GetProcessesByName(name, machineName.ToString());
1311+
}
1312+
else
1313+
{
1314+
return Process.GetProcessesByName(name);
1315+
}
1316+
}
1317+
catch (Exception ex)
1318+
{
1319+
throw ThrowError(ex);
1320+
}
1321+
}
1322+
public void ProcessKill(object _process) => (ObjectParse(_process, "process") as Process).Kill();
1323+
public object? ProcessGetProperty(object _process, object _property)
1324+
{
1325+
try
1326+
{
1327+
var process = ObjectParse(_process, "process") as Process;
1328+
var property = StringParse(_property);
1329+
1330+
if (process == null)
1331+
{
1332+
throw new ArgumentNullException(nameof(_process), "The process object is null.");
1333+
}
1334+
1335+
PropertyInfo propertyInfo = typeof(Process).GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
1336+
if (propertyInfo == null)
1337+
{
1338+
throw new ArgumentException($"Property \"{property}\" not found on type \"Process\".");
1339+
}
1340+
1341+
return propertyInfo.GetValue(process);
1342+
}
1343+
catch (Exception ex)
1344+
{
1345+
throw ThrowError(ex);
1346+
}
1347+
}
1348+
public object? ProcessRunFunction(object _process, object _function)
1349+
{
1350+
try
1351+
{
1352+
var process = ObjectParse(_process, "process") as Process;
1353+
var function = StringParse(_function);
1354+
1355+
if (process == null)
1356+
{
1357+
throw new ArgumentNullException(nameof(_process), "The process object is null.");
1358+
}
1359+
1360+
MethodInfo methodInfo = typeof(Process).GetMethod(function, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
1361+
if (methodInfo == null)
1362+
{
1363+
throw new ArgumentException($"Method \"{function}\" not found on type \"Process\".");
1364+
}
1365+
1366+
return methodInfo.Invoke(process, null);
1367+
}
1368+
catch (Exception ex)
1369+
{
1370+
throw ThrowError(ex);
1371+
}
1372+
}
11781373
}
11791374
}

TestEnv/Code.ezcode

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
include main, time
1+
include os, time
22

3-
method start {
4-
print the timer started
3+
undefined v => process get-process-by-id : 2496
54

6-
timer time new : seconds:5
7-
time start
8-
9-
stopwatch stop new
10-
stop start
11-
12-
loop time is-not-done {
13-
float i new => stop elapsed-seconds
14-
print i
15-
}
16-
17-
print the timer ended
18-
}
5+
print v

0 commit comments

Comments
 (0)