BadScript 2
Loading...
Searching...
No Matches
BadRuntimeBuilder.cs
Go to the documentation of this file.
1using System.IO.Compression;
2
13using BadScript2.IO;
17
18using Microsoft.AspNetCore.Components.CompilerServices;
19
20using Newtonsoft.Json;
22
23public static class BadRuntimeBuilder
24{
28 private const string SETTINGS_FILE = "Settings.json";
29
30
31 public static async Task<byte[]> GetWorkspace(string workspace, HttpClient client)
32 {
33 try
34 {
35 var result = await client.GetByteArrayAsync(workspace+".zip");
36 return result;
37 }
38 catch (Exception e)
39 {
40 BadConsole.WriteLine(e.ToString());
41 throw;
42 }
43 }
44 public static async Task<IFileSystem> BuildFileSystem(string workspace, byte[] data)
45 {
46 BadConsole.WriteLine("Building File System...");
48
49 fs.CreateDirectory($"/.workspace/{workspace}", true);
50 var meta = new
51 {
52 Workspace = workspace,
53 BasedOn = "default"
54 };
55 fs.WriteAllText($"/.workspace/{workspace}/meta.json", JsonConvert.SerializeObject(meta, Formatting.Indented));
56
57 var ms = new MemoryStream(data);
58 fs.ImportZip(ms);
59
60 return fs;
61 }
62
66 private static BadRuntime LoadConsoleSettings(this BadRuntime runtime, IFileSystem fs)
67 {
68 BadSettings consoleSettings = new BadSettings(string.Empty);
69 string rootDir = fs.GetStartupDirectory();
70 rootDir = rootDir.Remove(rootDir.Length - 1, 1);
71
72 consoleSettings.SetProperty("RootDirectory", new BadSettings(rootDir, string.Empty));
73 consoleSettings.SetProperty("DataDirectory", new BadSettings(Path.Combine(fs.GetStartupDirectory(), "data"), string.Empty));
74 BadSettingsProvider.RootSettings.SetProperty("Console", consoleSettings);
75
76 string settingsFile = Path.Combine(fs.GetStartupDirectory(), SETTINGS_FILE);
77 if (!fs.Exists(settingsFile))
78 {
79 return runtime;
80 }
81 return runtime.LoadSettings(settingsFile, fs);
82 }
83
84 public static Task<BadRuntime> BuildRuntime(IFileSystem fs)
85 {
87 // Build the runtime
88 BadConsole.WriteLine("Building Runtime...");
89 BadRuntime runtime = new BadRuntime()
90 .LoadConsoleSettings(fs)
91 .UseLogMask(mask)
93 .UseCommonInterop()
94 .UseCompressionApi()
95 .UseFileSystemApi(fs)
96 .UseHtmlApi()
97 .UseJsonApi()
98 .UseLinqApi()
99 .UseNetApi()
100 .UseNetHostApi()
102
103 return Task.FromResult(runtime);
104 }
105}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:28
BadRuntime UseLogMask(params BadLogMask[] mask)
Configures the Runtime to use the specified log masks.
BadRuntime UseConsoleLogWriter()
Configures the Runtime to use the default log writer implementation(log to console)
BadRuntime UseLocalModules(IFileSystem? fs=null)
Registers the Local Path Handler to the Module Importer.
BadRuntime LoadSettings(string settingsFile, IFileSystem? fileSystem=null)
Loads the specified settings file.
Implements a Mask for Log Messages.
Definition BadLogMask.cs:7
static readonly BadLogMask None
Static Helper for the "None" Mask.
Definition BadLogMask.cs:24
Wrapper class for the console abstraction.
Definition BadConsole.cs:12
static void WriteLine(string str)
Writes a string to the console and appends a newline.
Definition BadConsole.cs:76
Virtual File System Implementation for the BadScript Engine.
void CreateDirectory(string path, bool recursive=false)
Creates a new directory.
Public Api for the Settings System.
void SetProperty(string propertyName, BadSettings value, bool invokeOnChange=true)
Sets the Property with the given Name.
Helper class that can be used to automatically load a settings object from a file.
static BadSettings RootSettings
Returns the Root Settings Object.
static async Task< byte[]> GetWorkspace(string workspace, HttpClient client)
const string SETTINGS_FILE
Defines the Settings file.
static BadRuntime LoadConsoleSettings(this BadRuntime runtime, IFileSystem fs)
Loads the Settings.
static async Task< IFileSystem > BuildFileSystem(string workspace, byte[] data)
static Task< BadRuntime > BuildRuntime(IFileSystem fs)
Defines the interface for a file system.
Definition IFileSystem.cs:7
string GetStartupDirectory()
The Startup Directory of the Application.
bool Exists(string path)
Returns true if the given path is a file or directory.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains a Console Abstraction Layer to be able to Simulate Console Input/Output over the Network.
Definition BadConsole.cs:6
Contains the Implementation of the BadScript Virtual File System.
Contains IO Implementation for the BadScript2 Runtime.
Contains Common Interop Extensions and APIs for the BadScript2 Runtime.
Contains Compression Extensions and APIs for the BadScript2 Runtime.
Contains HTML Extensions and APIs for the BadScript2 Runtime.
Definition BadHtmlApi.cs:9
Contains IO Extensions and APIs for the BadScript2 Runtime.
Contains JSON Extensions and APIs for the BadScript2 Runtime.
Definition BadJson.cs:11
Contains Linq Extensions and APIs for the BadScript2 Runtime.
Contains Network Hosting Extensions and APIs for the BadScript2 Runtime.
Contains Networking Extensions and APIs for the BadScript2 Runtime.
Definition BadNetApi.cs:6
Contains the Settings Implementation.