Compare commits
13 Commits
83ecbfbfd3
...
master
Author | SHA1 | Date | |
---|---|---|---|
b1c36c08fb | |||
d2bd58568a | |||
15dd1c9aa4 | |||
21f3c580eb | |||
09b334ca58 | |||
4dc6691650 | |||
22b8c1838a | |||
b9012674d6 | |||
8a4edfcb79 | |||
8d6f96b99a | |||
a91c9741e4 | |||
9d7fbf9786 | |||
8e8edd8724 |
@@ -5,6 +5,7 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Convention</RootNamespace>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
Submodule Convention/[FLEE] updated: c9e2493796...47b12f4bc0
Submodule Convention/[RScript] updated: e3589cf1ec...4860aa251e
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
@@ -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)
|
||||
{
|
||||
Type type = obj.GetType();
|
||||
@@ -409,5 +443,25 @@ namespace Convention
|
||||
{
|
||||
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)
|
||||
throw new InvalidOperationException("Target is not a file");
|
||||
var file = this.OriginInfo as FileInfo;
|
||||
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;
|
||||
return File.ReadAllBytes(FullPath);
|
||||
}
|
||||
|
||||
public List<string[]> LoadAsCsv()
|
||||
@@ -245,7 +234,7 @@ namespace Convention
|
||||
|
||||
public void SaveAsBinary(byte[] data)
|
||||
{
|
||||
SaveDataAsBinary(FullPath, data, (OriginInfo as FileInfo).OpenWrite());
|
||||
File.WriteAllBytes(FullPath, data);
|
||||
}
|
||||
|
||||
public void SaveAsCsv(List<string[]> csvData)
|
||||
@@ -1113,4 +1102,32 @@ namespace Convention
|
||||
|
||||
#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,37 +1,62 @@
|
||||
using Convention.RScript;
|
||||
using Flee.PublicTypes;
|
||||
using Convention;
|
||||
using Convention.EasySave;
|
||||
using Convention.RScript;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
public class Program
|
||||
{
|
||||
static class Test
|
||||
{
|
||||
public static object Func(object x)
|
||||
{
|
||||
Console.WriteLine(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
RScriptEngine engine = new();
|
||||
RScriptImportClass import = new()
|
||||
{
|
||||
typeof(Math)
|
||||
typeof(Math),
|
||||
typeof(ExpressionMath),
|
||||
typeof(Test)
|
||||
};
|
||||
var result = engine.Run(@"
|
||||
double i;
|
||||
i = 2.0;
|
||||
|
||||
|
||||
var result = engine.Compile(@"
|
||||
int i= 2;
|
||||
int count = 0;
|
||||
label(test);
|
||||
{
|
||||
goto(true,func1);
|
||||
goto(100>i,test);
|
||||
}
|
||||
double result;
|
||||
result = i;
|
||||
goto(true,func1);
|
||||
Func(i);
|
||||
goto(100>i,test);
|
||||
|
||||
goto(true,end);
|
||||
|
||||
label(func1);
|
||||
goto(context.ExistNamespace(""x""),end);
|
||||
namespace(x)
|
||||
{
|
||||
i = Pow(i,2.0);
|
||||
Func(""xxx"");
|
||||
}
|
||||
|
||||
namespace(func1)
|
||||
{
|
||||
i = Pow(i,2);
|
||||
count = count + 1;
|
||||
Func(count);
|
||||
}
|
||||
back(true);
|
||||
|
||||
label(end);
|
||||
", import);
|
||||
Console.WriteLine($"Script executed successfully. Result: {result["result"].data}");
|
||||
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