Compare commits
20 Commits
a8cfb012fc
...
master
Author | SHA1 | Date | |
---|---|---|---|
b1c36c08fb | |||
d2bd58568a | |||
15dd1c9aa4 | |||
21f3c580eb | |||
09b334ca58 | |||
4dc6691650 | |||
22b8c1838a | |||
b9012674d6 | |||
8a4edfcb79 | |||
8d6f96b99a | |||
a91c9741e4 | |||
9d7fbf9786 | |||
8e8edd8724 | |||
83ecbfbfd3 | |||
b5e2f4cc16 | |||
bacfcd550f | |||
f850030d10 | |||
d58efb13e2 | |||
c07c64be1e | |||
f8d81d9198 |
@@ -1,7 +1,5 @@
|
|||||||
---
|
---
|
||||||
description:
|
alwaysApply: false
|
||||||
globs:
|
|
||||||
alwaysApply: true
|
|
||||||
---
|
---
|
||||||
## RIPER-5 + O1 思维 + 代理执行协议
|
## RIPER-5 + O1 思维 + 代理执行协议
|
||||||
|
|
||||||
|
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[submodule "Convention/[FLEE]"]
|
||||||
|
path = Convention/[FLEE]
|
||||||
|
url = http://www.liubai.site:3000/ninemine/Flee.git
|
||||||
|
[submodule "Convention/[RScript]"]
|
||||||
|
path = Convention/[RScript]
|
||||||
|
url = http://www.liubai.site:3000/ninemine/RScript.git
|
@@ -5,7 +5,7 @@
|
|||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<RootNamespace>Convention</RootNamespace>
|
<RootNamespace>Convention</RootNamespace>
|
||||||
<UseWindowsForms>true</UseWindowsForms>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
1
Convention/[FLEE]
Submodule
1
Convention/[FLEE]
Submodule
Submodule Convention/[FLEE] added at 47b12f4bc0
1
Convention/[RScript]
Submodule
1
Convention/[RScript]
Submodule
Submodule Convention/[RScript] added at 4860aa251e
@@ -1,325 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace Convention.RScript
|
|
||||||
{
|
|
||||||
public partial class ScriptContent
|
|
||||||
{
|
|
||||||
public object RuntimeBindingTarget;
|
|
||||||
|
|
||||||
public struct RuntimeContext
|
|
||||||
{
|
|
||||||
public int iterator;
|
|
||||||
public bool isResetIterator;
|
|
||||||
|
|
||||||
public static RuntimeContext Create()
|
|
||||||
{
|
|
||||||
return new RuntimeContext()
|
|
||||||
{
|
|
||||||
iterator = 0,
|
|
||||||
isResetIterator = false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public RuntimeContext Clone()
|
|
||||||
{
|
|
||||||
return new()
|
|
||||||
{
|
|
||||||
iterator = iterator,
|
|
||||||
isResetIterator = isResetIterator
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class ScriptDataEntry
|
|
||||||
{
|
|
||||||
public string text;
|
|
||||||
|
|
||||||
public string command;
|
|
||||||
public string[] parameters;
|
|
||||||
public int iterator;
|
|
||||||
|
|
||||||
public Func<RuntimeContext, RuntimeContext> actor;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ScriptDataEntry> Commands = new();
|
|
||||||
|
|
||||||
// 预处理工具
|
|
||||||
|
|
||||||
public static int LastIndexOfNextTerminalTail(string text, int i, string terminal)
|
|
||||||
{
|
|
||||||
return text.IndexOf(terminal, i) + terminal.Length - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool StartWithSpecialTerminal(string text,int i)
|
|
||||||
{
|
|
||||||
switch (text[i])
|
|
||||||
{
|
|
||||||
case '{':
|
|
||||||
case '}':
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public const string SingleLineTerminal = "//";
|
|
||||||
public const string MultiLineBeginTerminal = "/*";
|
|
||||||
public const string MultiLineEndTerminal = "*/";
|
|
||||||
public const string TextTerminal = "\"";
|
|
||||||
|
|
||||||
public static bool StartWithSingleLineTerminal(string text, int i)
|
|
||||||
{
|
|
||||||
int length = SingleLineTerminal.Length;
|
|
||||||
if (text.Length - i < length)
|
|
||||||
return false;
|
|
||||||
return string.Compare(text, i, SingleLineTerminal, 0, length) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool StartWithMultiLineTerminal(string text, int i)
|
|
||||||
{
|
|
||||||
int length = MultiLineBeginTerminal.Length;
|
|
||||||
if (text.Length - i < length)
|
|
||||||
return false;
|
|
||||||
return string.Compare(text, i, MultiLineBeginTerminal, 0, length) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool StartWithTextTerminal(string text, int i)
|
|
||||||
{
|
|
||||||
int length = TextTerminal.Length;
|
|
||||||
if (text.Length - i < length)
|
|
||||||
return false;
|
|
||||||
return string.Compare(text, i, TextTerminal, 0, length) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int LastIndexOfNextTextTerminalTail(string text, int i,out string buffer)
|
|
||||||
{
|
|
||||||
char terminal = TextTerminal[0];
|
|
||||||
int __head = i + 1;
|
|
||||||
buffer = "";
|
|
||||||
for (int __tail = text.Length; __head < __tail && text[__head] != terminal;)
|
|
||||||
{
|
|
||||||
if (text[__head] == '\\')
|
|
||||||
{
|
|
||||||
switch (text[__head+1])
|
|
||||||
{
|
|
||||||
case 'n':
|
|
||||||
{
|
|
||||||
buffer += '\n';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
{
|
|
||||||
buffer += '\t';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
{
|
|
||||||
buffer += '\r';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '0':
|
|
||||||
{
|
|
||||||
buffer += '\0';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
buffer += text[__head + 1];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
__head += 2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer += text[__head];
|
|
||||||
__head++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return __head;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 文本预处理
|
|
||||||
private void ScriptTextPreprocessing(string script)
|
|
||||||
{
|
|
||||||
string buffer = "";
|
|
||||||
void PushBuffer()
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(buffer) == false)
|
|
||||||
{
|
|
||||||
Commands.Add(new()
|
|
||||||
{
|
|
||||||
text = buffer.Trim(),
|
|
||||||
iterator = Commands.Count
|
|
||||||
});
|
|
||||||
}
|
|
||||||
buffer = "";
|
|
||||||
}
|
|
||||||
Func<int, bool> loopPr = x => x < script.Length;
|
|
||||||
for (int i = 0; loopPr(i); i++)
|
|
||||||
{
|
|
||||||
// 切断语句\
|
|
||||||
if (script[i] == ';')
|
|
||||||
{
|
|
||||||
PushBuffer();
|
|
||||||
}
|
|
||||||
// 读入特殊标记符
|
|
||||||
else if (StartWithSpecialTerminal(script, i))
|
|
||||||
{
|
|
||||||
PushBuffer();
|
|
||||||
buffer += script[i];
|
|
||||||
PushBuffer();
|
|
||||||
}
|
|
||||||
// 跳过单行注释
|
|
||||||
else if (StartWithSingleLineTerminal(script, i))
|
|
||||||
{
|
|
||||||
i = LastIndexOfNextTerminalTail(script, i, "\n");
|
|
||||||
}
|
|
||||||
// 跳过多行注释
|
|
||||||
else if (StartWithMultiLineTerminal(script, i))
|
|
||||||
{
|
|
||||||
i = LastIndexOfNextTerminalTail(script, i, MultiLineEndTerminal);
|
|
||||||
}
|
|
||||||
// 读入文本
|
|
||||||
else if (StartWithTextTerminal(script, i))
|
|
||||||
{
|
|
||||||
i = LastIndexOfNextTextTerminalTail(script, i, out var temp);
|
|
||||||
buffer += temp;
|
|
||||||
}
|
|
||||||
// 读入
|
|
||||||
else if (loopPr(i))
|
|
||||||
buffer += script[i];
|
|
||||||
}
|
|
||||||
// 存在未完成的语句
|
|
||||||
if (string.IsNullOrEmpty(buffer) == false)
|
|
||||||
throw new ArgumentException("The script did not end with the correct terminator.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 指令预处理工具
|
|
||||||
|
|
||||||
private Dictionary<int, ScriptDataEntry> CommandIndexs = new();
|
|
||||||
private Dictionary<string, int> LabelIndexs = new();
|
|
||||||
|
|
||||||
public const string GotoTerminal = "goto";
|
|
||||||
public const string LabelTerminal = "label";
|
|
||||||
public const string IfTerminal = "if";
|
|
||||||
|
|
||||||
// 指令预处理
|
|
||||||
private void CommandPreprocessing()
|
|
||||||
{
|
|
||||||
static GroupCollection Match(ScriptDataEntry entry, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern)
|
|
||||||
{
|
|
||||||
Match match = Regex.Match(entry.text, pattern);
|
|
||||||
if (!match.Success)
|
|
||||||
throw new ArgumentException($"Script: \"{entry.text}\"<command iterator: {entry.iterator}> is invalid statement");
|
|
||||||
return match.Groups;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加入自由映射
|
|
||||||
foreach (var entry in Commands)
|
|
||||||
{
|
|
||||||
CommandIndexs.Add(entry.iterator, entry);
|
|
||||||
}
|
|
||||||
// 匹配
|
|
||||||
foreach (var entry in Commands)
|
|
||||||
{
|
|
||||||
if (
|
|
||||||
// goto label-name;
|
|
||||||
entry.text.StartsWith(GotoTerminal)||
|
|
||||||
// label label-name;
|
|
||||||
entry.text.StartsWith(LabelTerminal)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
var groups = Match(entry, @"^(\S*)\s+([^\s]+?)\s*;$");
|
|
||||||
entry.command = groups[1].Value;
|
|
||||||
entry.parameters = new string[] { groups[2].Value };
|
|
||||||
// touch label
|
|
||||||
if (entry.command == LabelTerminal)
|
|
||||||
{
|
|
||||||
LabelIndexs.Add(groups[2].Value, entry.iterator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (
|
|
||||||
// if(expr)
|
|
||||||
entry.text.StartsWith(IfTerminal)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
var groups = Match(entry, @"^(\S*)\s*(.*?)$");
|
|
||||||
entry.command = groups[1].Value;
|
|
||||||
entry.parameters = new string[] { groups[2].Value };
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var groups = Match(entry, @"^(\w+)\s*\(\s*(.*?)\s*\)\s*;$");
|
|
||||||
entry.command = groups[1].Value;
|
|
||||||
|
|
||||||
static string[] ParseArguments(string argumentsString)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(argumentsString))
|
|
||||||
return new string[0];
|
|
||||||
|
|
||||||
// 处理字符串字面量和普通参数的正则表达式
|
|
||||||
string argPattern = @"""(?:[^""\\]|\\.)*""|[^,]+";
|
|
||||||
|
|
||||||
return Regex.Matches(argumentsString, argPattern)
|
|
||||||
.Cast<Match>()
|
|
||||||
.Select(m => m.Value.Trim())
|
|
||||||
.Where(arg => !string.IsNullOrEmpty(arg))
|
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.parameters = ParseArguments(groups[2].Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 指令转化
|
|
||||||
|
|
||||||
private void LoopForTC2AC()
|
|
||||||
{
|
|
||||||
Dictionary<string, MethodInfo> cache = (from method in RuntimeBindingTarget.GetType().GetMethods()
|
|
||||||
select new KeyValuePair<string, MethodInfo>(method.Name, method)).ToDictionary();
|
|
||||||
|
|
||||||
for (int index = 0, e = Commands.Count; index < e; index++)
|
|
||||||
{
|
|
||||||
var entry = Commands[index];
|
|
||||||
// Keywords
|
|
||||||
if (entry.command == GotoTerminal)
|
|
||||||
{
|
|
||||||
entry.actor = context =>
|
|
||||||
{
|
|
||||||
var next = context.Clone();
|
|
||||||
next.iterator = LabelIndexs[entry.parameters[0]];
|
|
||||||
next.isResetIterator = true;
|
|
||||||
return next;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Custom Binding
|
|
||||||
else if(cache.TryGetValue(entry.command,out var methodInfo))
|
|
||||||
{
|
|
||||||
entry.actor = context =>
|
|
||||||
{
|
|
||||||
|
|
||||||
return context.Clone();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LoadScript(string script)
|
|
||||||
{
|
|
||||||
ScriptTextPreprocessing(script);
|
|
||||||
CommandPreprocessing();
|
|
||||||
LoopForTC2AC();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,29 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Convention.RScript
|
|
||||||
{
|
|
||||||
public class ScriptRunner
|
|
||||||
{
|
|
||||||
public ScriptContent BuildNewContent(object target)
|
|
||||||
{
|
|
||||||
return new()
|
|
||||||
{
|
|
||||||
RuntimeBindingTarget = target,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScriptContent BuildNewContent()
|
|
||||||
{
|
|
||||||
return BuildNewContent(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RunScriptFromContent(ScriptContent content)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,12 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Runtime.Serialization;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Xml;
|
||||||
|
|
||||||
namespace Convention
|
namespace Convention
|
||||||
{
|
{
|
||||||
@@ -66,7 +67,7 @@ namespace Convention
|
|||||||
public static T ConvertValue<T>(string str)
|
public static T ConvertValue<T>(string str)
|
||||||
{
|
{
|
||||||
Type type = typeof(T);
|
Type type = typeof(T);
|
||||||
var parse_method = type.GetMethod("Parse");
|
var parse_method = type.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null);
|
||||||
if (parse_method != null &&
|
if (parse_method != null &&
|
||||||
(parse_method.ReturnType.IsSubclassOf(type) || parse_method.ReturnType == type) &&
|
(parse_method.ReturnType.IsSubclassOf(type) || parse_method.ReturnType == type) &&
|
||||||
parse_method.GetParameters().Length == 1 &&
|
parse_method.GetParameters().Length == 1 &&
|
||||||
@@ -79,6 +80,39 @@ namespace Convention
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 序列化
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static byte[] Serialize<T>(T obj)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(obj);
|
||||||
|
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
DataContractSerializer ser = new DataContractSerializer(typeof(T));
|
||||||
|
ser.WriteObject(memoryStream, obj);
|
||||||
|
var data = memoryStream.ToArray();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 反序列化
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T Deserialize<T>(byte[] data)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(data);
|
||||||
|
|
||||||
|
using var memoryStream = new MemoryStream(data);
|
||||||
|
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());
|
||||||
|
DataContractSerializer ser = new DataContractSerializer(typeof(T));
|
||||||
|
var result = (T)ser.ReadObject(reader, true);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static object SeekValue(object obj, string name, BindingFlags flags, out bool isSucceed)
|
public static object SeekValue(object obj, string name, BindingFlags flags, out bool isSucceed)
|
||||||
{
|
{
|
||||||
Type type = obj.GetType();
|
Type type = obj.GetType();
|
||||||
@@ -409,5 +443,25 @@ namespace Convention
|
|||||||
{
|
{
|
||||||
return DateTime.Now.ToString(format);
|
return DateTime.Now.ToString(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class EnumerableClass : IEnumerable
|
||||||
|
{
|
||||||
|
private readonly IEnumerator ir;
|
||||||
|
|
||||||
|
public EnumerableClass(IEnumerator ir)
|
||||||
|
{
|
||||||
|
this.ir = ir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator GetEnumerator()
|
||||||
|
{
|
||||||
|
return ir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable AsEnumerable(this IEnumerator ir)
|
||||||
|
{
|
||||||
|
return new EnumerableClass(ir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -161,18 +161,7 @@ namespace Convention
|
|||||||
{
|
{
|
||||||
if (IsFile() == false)
|
if (IsFile() == false)
|
||||||
throw new InvalidOperationException("Target is not a file");
|
throw new InvalidOperationException("Target is not a file");
|
||||||
var file = this.OriginInfo as FileInfo;
|
return File.ReadAllBytes(FullPath);
|
||||||
const int BlockSize = 1024;
|
|
||||||
long FileSize = file.Length;
|
|
||||||
byte[] result = new byte[FileSize];
|
|
||||||
long offset = 0;
|
|
||||||
using (var fs = file.OpenRead())
|
|
||||||
{
|
|
||||||
fs.ReadAsync(result[(int)(offset)..(int)(offset + BlockSize)], 0, (int)(offset + BlockSize) - (int)(offset));
|
|
||||||
offset += BlockSize;
|
|
||||||
offset = System.Math.Min(offset, FileSize);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string[]> LoadAsCsv()
|
public List<string[]> LoadAsCsv()
|
||||||
@@ -245,7 +234,7 @@ namespace Convention
|
|||||||
|
|
||||||
public void SaveAsBinary(byte[] data)
|
public void SaveAsBinary(byte[] data)
|
||||||
{
|
{
|
||||||
SaveDataAsBinary(FullPath, data, (OriginInfo as FileInfo).OpenWrite());
|
File.WriteAllBytes(FullPath, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SaveAsCsv(List<string[]> csvData)
|
public void SaveAsCsv(List<string[]> csvData)
|
||||||
@@ -1113,4 +1102,32 @@ namespace Convention
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLE_UNSAFE
|
||||||
|
|
||||||
|
public static class UnsafeBinarySerializer
|
||||||
|
{
|
||||||
|
public static unsafe byte[] StructToBytes<T>(T structure) where T : unmanaged
|
||||||
|
{
|
||||||
|
int size = sizeof(T);
|
||||||
|
byte[] bytes = new byte[size];
|
||||||
|
|
||||||
|
fixed (byte* ptr = bytes)
|
||||||
|
{
|
||||||
|
*(T*)ptr = structure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe T BytesToStruct<T>(byte[] bytes) where T : unmanaged
|
||||||
|
{
|
||||||
|
fixed (byte* ptr = bytes)
|
||||||
|
{
|
||||||
|
return *(T*)ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -1,214 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Convention;
|
|
||||||
|
|
||||||
namespace Convention.Test
|
|
||||||
{
|
|
||||||
public class FileTest
|
|
||||||
{
|
|
||||||
public static void RunTests()
|
|
||||||
{
|
|
||||||
Console.WriteLine("=== ToolFile 功能测试 ===");
|
|
||||||
|
|
||||||
// 测试基本文件操作
|
|
||||||
TestBasicOperations();
|
|
||||||
|
|
||||||
// 测试文件加载和保存
|
|
||||||
TestLoadAndSave();
|
|
||||||
|
|
||||||
// 测试压缩和解压
|
|
||||||
TestCompression();
|
|
||||||
|
|
||||||
// 测试加密和解密
|
|
||||||
TestEncryption();
|
|
||||||
|
|
||||||
// 测试哈希计算
|
|
||||||
TestHash();
|
|
||||||
|
|
||||||
// 测试备份功能
|
|
||||||
TestBackup();
|
|
||||||
|
|
||||||
// 测试权限管理
|
|
||||||
TestPermissions();
|
|
||||||
|
|
||||||
Console.WriteLine("=== 所有测试完成 ===");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestBasicOperations()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 基本文件操作测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_file.txt");
|
|
||||||
testFile.SaveAsText("这是一个测试文件的内容");
|
|
||||||
|
|
||||||
Console.WriteLine($"文件存在: {testFile.Exists()}");
|
|
||||||
Console.WriteLine($"文件大小: {testFile.GetSize()} 字节");
|
|
||||||
Console.WriteLine($"文件名: {testFile.GetFilename()}");
|
|
||||||
Console.WriteLine($"文件扩展名: {testFile.GetExtension()}");
|
|
||||||
Console.WriteLine($"文件目录: {testFile.GetDir()}");
|
|
||||||
Console.WriteLine($"文件时间戳: {testFile.GetTimestamp()}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestLoadAndSave()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 文件加载和保存测试 ---");
|
|
||||||
|
|
||||||
// 测试 JSON 保存和加载
|
|
||||||
var jsonFile = new ToolFile("test_data.json");
|
|
||||||
var testData = new { name = "测试", value = 123, items = new[] { "item1", "item2" } };
|
|
||||||
jsonFile.SaveAsRawJson(testData);
|
|
||||||
|
|
||||||
var loadedData = jsonFile.LoadAsRawJson<dynamic>();
|
|
||||||
Console.WriteLine($"JSON 数据加载成功: {loadedData != null}");
|
|
||||||
|
|
||||||
// 测试 CSV 保存和加载
|
|
||||||
var csvFile = new ToolFile("test_data.csv");
|
|
||||||
var csvData = new List<string[]>
|
|
||||||
{
|
|
||||||
new[] { "姓名", "年龄", "城市" },
|
|
||||||
new[] { "张三", "25", "北京" },
|
|
||||||
new[] { "李四", "30", "上海" }
|
|
||||||
};
|
|
||||||
csvFile.SaveAsCsv(csvData);
|
|
||||||
|
|
||||||
var loadedCsv = csvFile.LoadAsCsv();
|
|
||||||
Console.WriteLine($"CSV 数据加载成功: {loadedCsv.Count} 行");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
jsonFile.Remove();
|
|
||||||
csvFile.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestCompression()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 压缩和解压测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_compress.txt");
|
|
||||||
testFile.SaveAsText("这是一个用于压缩测试的文件内容。".PadRight(1000, 'x'));
|
|
||||||
|
|
||||||
// 压缩文件
|
|
||||||
var compressedFile = testFile.Compress();
|
|
||||||
Console.WriteLine($"压缩成功: {compressedFile.Exists()}");
|
|
||||||
Console.WriteLine($"压缩后大小: {compressedFile.GetSize()} 字节");
|
|
||||||
|
|
||||||
// 解压文件
|
|
||||||
var decompressedDir = compressedFile.Decompress();
|
|
||||||
Console.WriteLine($"解压成功: {decompressedDir.Exists()}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
compressedFile.Remove();
|
|
||||||
if (decompressedDir.Exists())
|
|
||||||
decompressedDir.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestEncryption()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 加密和解密测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_encrypt.txt");
|
|
||||||
testFile.SaveAsText("这是一个用于加密测试的敏感数据");
|
|
||||||
|
|
||||||
// 加密文件
|
|
||||||
testFile.Encrypt("mysecretkey123");
|
|
||||||
Console.WriteLine($"加密成功: {testFile.Exists()}");
|
|
||||||
|
|
||||||
// 解密文件
|
|
||||||
testFile.Decrypt("mysecretkey123");
|
|
||||||
var decryptedContent = testFile.LoadAsText();
|
|
||||||
Console.WriteLine($"解密成功: {decryptedContent.Contains("敏感数据")}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestHash()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 哈希计算测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_hash.txt");
|
|
||||||
testFile.SaveAsText("这是一个用于哈希测试的文件内容");
|
|
||||||
|
|
||||||
// 计算不同算法的哈希值
|
|
||||||
var md5Hash = testFile.CalculateHash("MD5");
|
|
||||||
var sha256Hash = testFile.CalculateHash("SHA256");
|
|
||||||
|
|
||||||
Console.WriteLine($"MD5 哈希: {md5Hash}");
|
|
||||||
Console.WriteLine($"SHA256 哈希: {sha256Hash}");
|
|
||||||
|
|
||||||
// 验证哈希值
|
|
||||||
var isValid = testFile.VerifyHash(md5Hash, "MD5");
|
|
||||||
Console.WriteLine($"哈希验证: {isValid}");
|
|
||||||
|
|
||||||
// 保存哈希值到文件
|
|
||||||
var hashFile = testFile.SaveHash("MD5");
|
|
||||||
Console.WriteLine($"哈希文件保存: {hashFile.Exists()}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
hashFile.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestBackup()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 备份功能测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_backup.txt");
|
|
||||||
testFile.SaveAsText("这是一个用于备份测试的文件内容");
|
|
||||||
|
|
||||||
// 创建备份
|
|
||||||
var backupFile = testFile.CreateBackup();
|
|
||||||
Console.WriteLine($"备份创建成功: {backupFile.Exists()}");
|
|
||||||
|
|
||||||
// 列出备份
|
|
||||||
var backups = testFile.ListBackups();
|
|
||||||
Console.WriteLine($"备份数量: {backups.Count}");
|
|
||||||
|
|
||||||
// 恢复备份
|
|
||||||
var restoredFile = testFile.RestoreBackup(backupFile.GetFullPath(), "restored_file.txt");
|
|
||||||
Console.WriteLine($"备份恢复成功: {restoredFile.Exists()}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
backupFile.Remove();
|
|
||||||
restoredFile.Remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TestPermissions()
|
|
||||||
{
|
|
||||||
Console.WriteLine("\n--- 权限管理测试 ---");
|
|
||||||
|
|
||||||
// 创建测试文件
|
|
||||||
var testFile = new ToolFile("test_permissions.txt");
|
|
||||||
testFile.SaveAsText("这是一个用于权限测试的文件内容");
|
|
||||||
|
|
||||||
// 获取权限
|
|
||||||
var permissions = testFile.GetPermissions();
|
|
||||||
Console.WriteLine($"读取权限: {permissions["read"]}");
|
|
||||||
Console.WriteLine($"写入权限: {permissions["write"]}");
|
|
||||||
Console.WriteLine($"隐藏属性: {permissions["hidden"]}");
|
|
||||||
|
|
||||||
// 设置权限
|
|
||||||
testFile.SetPermissions(hidden: true);
|
|
||||||
var newPermissions = testFile.GetPermissions();
|
|
||||||
Console.WriteLine($"设置隐藏后: {newPermissions["hidden"]}");
|
|
||||||
|
|
||||||
// 检查权限
|
|
||||||
Console.WriteLine($"可读: {testFile.IsReadable()}");
|
|
||||||
Console.WriteLine($"可写: {testFile.IsWritable()}");
|
|
||||||
Console.WriteLine($"隐藏: {testFile.IsHidden()}");
|
|
||||||
|
|
||||||
// 清理
|
|
||||||
testFile.Remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,10 +1,62 @@
|
|||||||
using System;
|
using Convention;
|
||||||
using Convention.Test;
|
using Convention.EasySave;
|
||||||
|
using Convention.RScript;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
static class Test
|
||||||
|
{
|
||||||
|
public static object Func(object x)
|
||||||
|
{
|
||||||
|
Console.WriteLine(x);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
RScriptEngine engine = new();
|
||||||
|
RScriptImportClass import = new()
|
||||||
|
{
|
||||||
|
typeof(Math),
|
||||||
|
typeof(ExpressionMath),
|
||||||
|
typeof(Test)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var result = engine.Compile(@"
|
||||||
|
int i= 2;
|
||||||
|
int count = 0;
|
||||||
|
label(test);
|
||||||
|
goto(true,func1);
|
||||||
|
Func(i);
|
||||||
|
goto(100>i,test);
|
||||||
|
|
||||||
|
goto(context.ExistNamespace(""x""),end);
|
||||||
|
namespace(x)
|
||||||
|
{
|
||||||
|
Func(""xxx"");
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace(func1)
|
||||||
|
{
|
||||||
|
i = Pow(i,2);
|
||||||
|
count = count + 1;
|
||||||
|
Func(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
label(end);
|
||||||
|
", import);
|
||||||
|
var data = RScriptSerializer.SerializeClass(result);
|
||||||
|
var file = new ToolFile("F:\\test_after_run.dat");
|
||||||
|
file.SaveAsBinary(data);
|
||||||
|
data = file.LoadAsBinary();
|
||||||
|
engine.Run(RScriptSerializer.DeserializeClass(data), import);
|
||||||
|
return;
|
||||||
|
var data2 = RScriptSerializer.SerializeClass(engine.GetCompileResultFromCurrent());
|
||||||
|
var file2 = new ToolFile("F:\\test_after_run.dat");
|
||||||
|
file2.SaveAsBinary(data2);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user