Skip to content

Commit c146334

Browse files
committed
libraries!
1 parent 7f1c3d2 commit c146334

20 files changed

Lines changed: 392 additions & 79 deletions
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

7Sharp/7Sharp.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Reference Include="CodingSeb.ExpressionEvaluator, Version=1.4.9.0, Culture=neutral, processorArchitecture=MSIL">
4040
<HintPath>..\packages\CodingSeb.ExpressionEvaluator.1.4.9\lib\net45\CodingSeb.ExpressionEvaluator.dll</HintPath>
4141
</Reference>
42+
<Reference Include="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
4243
<Reference Include="sly, Version=2.4.2.0, Culture=neutral, processorArchitecture=MSIL">
4344
<HintPath>..\packages\sly.2.4.2\lib\netstandard2.0\sly.dll</HintPath>
4445
</Reference>
@@ -48,6 +49,8 @@
4849
</Reference>
4950
<Reference Include="System.Core" />
5051
<Reference Include="System.Drawing" />
52+
<Reference Include="System.IO.Compression" />
53+
<Reference Include="System.IO.Compression.FileSystem" />
5154
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
5255
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
5356
</Reference>
@@ -76,13 +79,16 @@
7679
</Reference>
7780
</ItemGroup>
7881
<ItemGroup>
79-
<Compile Include="7sFunction.cs" />
82+
<Compile Include="Intrerpreter\7sFunction.cs" />
83+
<Compile Include="7sLib\7sLibManager.cs" />
84+
<Compile Include="7sLib\7sLibrary.cs" />
8085
<Compile Include="Editor\Editor.cs">
8186
<SubType>Form</SubType>
8287
</Compile>
8388
<Compile Include="Editor\Editor.Designer.cs">
8489
<DependentUpon>Editor.cs</DependentUpon>
8590
</Compile>
91+
<Compile Include="GlobalSuppressions.cs" />
8692
<Compile Include="Intrerpreter\FunctionDefinition.cs" />
8793
<Compile Include="Intrerpreter\Interpreter.cs" />
8894
<Compile Include="Intrerpreter\Sysfunctions.cs" />

7Sharp/7sLib/7sLibManager.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.IO;
6+
using System.IO.Compression;
7+
using System.Threading.Tasks;
8+
using System.Security.Cryptography;
9+
10+
namespace _7Sharp._7sLib
11+
{
12+
using static Console;
13+
internal class _7sLibManager
14+
{
15+
const int LIBVERSION = 1;
16+
17+
internal static void Save(string path)
18+
{
19+
try
20+
{
21+
//create directory
22+
string dir = GetTempDir();
23+
Directory.CreateDirectory(dir);
24+
//create files
25+
string sha1Text = WriteAndGetSHA1(dir + ".text", Program.shell.GetCode());
26+
_ = WriteAndGetSHA1(dir + ".head", "7SLIB\nV"+LIBVERSION+"\n"+sha1Text);
27+
//compress
28+
string output = path + ".7slib";
29+
ZipFile.CreateFromDirectory(dir, output);
30+
Directory.Delete(dir, true);
31+
}
32+
catch (Exception e)
33+
{
34+
Utils.PrintError(e);
35+
}
36+
}
37+
38+
private static string WriteAndGetSHA1(string path, string contents)
39+
{
40+
using (StreamWriter sw = new StreamWriter(path))
41+
{
42+
sw.Write(contents);
43+
}
44+
return GetSHA1(path);
45+
}
46+
47+
internal static _7sLibrary Load(string path)
48+
{
49+
try
50+
{
51+
using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Read))
52+
{
53+
if (archive.GetEntry(".head") != null)
54+
{
55+
if (archive.GetEntry(".text") != null)
56+
{
57+
//check header
58+
string[] header;
59+
using (StreamReader sr = new StreamReader(archive.GetEntry(".head").Open()))
60+
{
61+
header = sr.ReadToEnd().Split('\n');
62+
}
63+
if (header != null && header.Length >= 3)
64+
{
65+
if (header.ToList().FindIndex(x => x == null) > -1)
66+
{
67+
throw new FormatException("Invalid header!");
68+
}
69+
if (header[0] != "7SLIB")
70+
{
71+
throw new FormatException($"Invalid header! Expected 7SLIB at line 1! Got {header[0]}");
72+
}
73+
int v;
74+
if (!header[1].StartsWith("V") || !int.TryParse(header[1].Substring(1), out v))
75+
{
76+
throw new FormatException($"Invalid header! Expected V{LIBVERSION} or higher at line 2!");
77+
}
78+
if (v < LIBVERSION)
79+
{
80+
throw new FormatException($"Library version is INVALID! Expected {LIBVERSION} or more, got {v}");
81+
}
82+
}
83+
else
84+
{
85+
throw new FormatException("Invalid header!");
86+
}
87+
//check code
88+
string code;
89+
using (Stream entry = archive.GetEntry(".text").Open())
90+
{
91+
using (StreamReader sr = new StreamReader(entry))
92+
{
93+
string SHA1 = GetSHA1(entry);
94+
if (header[2] != SHA1)
95+
{
96+
throw new FormatException($"Invalid Code! SHA1 in header ({header[2]}) did NOT match SHA1 of .text! ({SHA1})");
97+
}
98+
}
99+
}
100+
using (Stream entry = archive.GetEntry(".text").Open())
101+
{
102+
using (StreamReader sr = new StreamReader(entry))
103+
{
104+
code = sr.ReadToEnd();
105+
}
106+
}
107+
var lib = new _7sLibrary();
108+
lib.Content = code;
109+
return lib;
110+
}
111+
else
112+
{
113+
throw new FormatException("The 7sLib file did not have a .text file!");
114+
}
115+
}
116+
else
117+
{
118+
throw new FormatException("The 7sLib file did not have a .head file!");
119+
}
120+
}
121+
}
122+
catch (Exception e)
123+
{
124+
Utils.PrintError(e);
125+
}
126+
return null;
127+
}
128+
129+
private static string GetSHA1(Stream s) => BitConverter.ToString(SHA1.Create().ComputeHash(s));
130+
131+
private static string GetSHA1(string path)
132+
{
133+
string o;
134+
using (Stream fs = new FileStream(path, FileMode.Open))
135+
{
136+
o = BitConverter.ToString(SHA1.Create().ComputeHash(fs)); //dont return here let using exit to close stream }
137+
}
138+
return o;
139+
}
140+
141+
private static string WriteBytesAndGetSHA1(string path, byte[] bytes)
142+
{
143+
using (FileStream fs = new FileStream(path, FileMode.Create))
144+
{
145+
foreach (byte b in bytes)
146+
{
147+
fs.WriteByte(b);
148+
}
149+
}
150+
return GetSHA1(path);
151+
}
152+
153+
private static string GetTempDir()
154+
{
155+
string dir;
156+
do
157+
{
158+
dir = Path.GetTempPath() + Path.GetRandomFileName().Split('.')[0] + Program.DirectorySeperator;
159+
}
160+
while (dir == null || Directory.Exists(dir));
161+
return dir;
162+
}
163+
}
164+
}

7Sharp/7sLib/7sLibrary.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace _7Sharp._7sLib
8+
{
9+
internal class _7sLibrary
10+
{
11+
public string Content;
12+
}
13+
}

7Sharp/App.config

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
55
</startup>
66
<runtime>
77
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
88
<dependentAssembly>
9-
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
10-
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
9+
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
10+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1"/>
1111
</dependentAssembly>
1212
<dependentAssembly>
13-
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
14-
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
13+
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
14+
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0"/>
1515
</dependentAssembly>
1616
<dependentAssembly>
17-
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
18-
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
17+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
18+
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0"/>
1919
</dependentAssembly>
2020
<dependentAssembly>
21-
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
22-
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
21+
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
22+
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
2323
</dependentAssembly>
2424
</assemblyBinding>
2525
</runtime>

7Sharp/GlobalSuppressions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+

2+
// This file is used by Code Analysis to maintain SuppressMessage
3+
// attributes that are applied to this project.
4+
// Project-level suppressions either have no target or are given
5+
// a specific target and scoped to a namespace, type, member, etc.
6+
7+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>", Scope = "type", Target = "~T:_7Sharp._7sLib._7sLibrary")]
8+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>", Scope = "type", Target = "~T:_7Sharp._7sLib._7sLibManager")]
9+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>", Scope = "type", Target = "~T:_7Sharp.Intrerpreter._7sFunction")]
10+
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ namespace _7Sharp.Intrerpreter
99
using static Techcraft7_DLL_Pack.Text.ColorConsoleMethods;
1010
using static ConsoleColor;
1111
using static Console;
12-
#pragma warning disable IDE1006 // Naming Styles
1312
public class _7sFunction
14-
#pragma warning restore IDE1006 // Naming Styles
1513
{
1614
public int NumberOfArguments;
1715
public string Name;

0 commit comments

Comments
 (0)