From 33b0dee8a8f36903ea0b1355e26c0634a8ffd64e Mon Sep 17 00:00:00 2001 From: Scarlet Rose <70824102+scarletquasar@users.noreply.github.com> Date: Fri, 1 Sep 2023 17:01:58 -0300 Subject: [PATCH] added move information --- .../Library/BindingsManager.cs | 581 +++++++++--------- 1 file changed, 291 insertions(+), 290 deletions(-) diff --git a/projects/native/MelonRuntime.Core/Library/BindingsManager.cs b/projects/native/MelonRuntime.Core/Library/BindingsManager.cs index 3e03fb8d..9190e568 100644 --- a/projects/native/MelonRuntime.Core/Library/BindingsManager.cs +++ b/projects/native/MelonRuntime.Core/Library/BindingsManager.cs @@ -17,294 +17,295 @@ namespace MelonRuntime.Core.Library { - public class BindingsManager - { - private readonly IMelon? _melon; - private static BindingsManager? _instance; - - private readonly DirectBindingFactories _bindingFactories; - - public static BindingsManager GetManager(IMelon melon) - { - _instance ??= new BindingsManager(melon); - return _instance; - } - - public BindingsManager(IMelon melon) - { - _melon = melon; - _bindingFactories = DirectBindingFactories.GetFactories(); - } - - public IDictionary GetBindings() - { - var allBindings = new List>() - { - GetSerializationBindings(), - GetDirectDatabaseProviderBindings(), - GetDirectBindingFactories(), - GetHttpClientBindings(), - GetThreadingBindings(), - GetRealmBindings(), - GetEnvironmentBindings(), - GetProcessBindings(), - GetFileSystemBindings(), - GetWebServiceApplicationBindings(), - GetTimeBindings() - }; - - return allBindings - .SelectMany(x => x) - .ToDictionary(pair => pair.Key, pair => pair.Value); - } - - private static Dictionary GetSerializationBindings() - { - return new() - { - ["Serialize"] = new Func(SerializationManager.Serialize), - ["Deserialize"] = new Func(SerializationManager.Deserialize), - }; - } - - private static Dictionary GetDirectDatabaseProviderBindings() - { - static Func getQueryFunction() where T : IDirectDatabaseProvider - { - var instance = (IDirectDatabaseProvider)Activator.CreateInstance(typeof(T))!; - return instance.ExecuteQuery; - } - - static Func getNonQueryFunction() where T : IDirectDatabaseProvider - { - var instance = (IDirectDatabaseProvider)Activator.CreateInstance(typeof(T))!; - return instance.ExecuteNonQuery; - } - - return new() - { - ["PostgreSQLBindingQuery"] = getQueryFunction(), - ["PostgreSQLBindingNonQuery"] = getNonQueryFunction(), - ["MySqlBindingQuery"] = getQueryFunction(), - ["MySqlBindingNonQuery"] = getNonQueryFunction(), - ["SqlServerBindingQuery"] = getQueryFunction(), - ["SqlServerBindingNonQuery"] = getNonQueryFunction(), - }; - } - - private Dictionary GetDirectBindingFactories() - { - var callStaticMethodBinding = - new Func(_bindingFactories.CallMethod); - - var getStaticProperty = - new Func(_bindingFactories.GetStaticProperty); - - var getTypes = new Func(_bindingFactories.GetTypes); - var loadAssembly = new Func(_bindingFactories.LoadAssembly); - var loadAssemblyAsync = new Func>(_bindingFactories.LoadAssemblyAsync); - - return new() - { - ["CallStaticMethod"] = callStaticMethodBinding, - ["GetStaticProperty"] = getStaticProperty, - ["GetTypes"] = getTypes, - ["LoadAssembly"] = loadAssembly, - ["LoadAssemblyAsync"] = loadAssemblyAsync - }; - } - - private static Dictionary GetHttpClientBindings() - { - var httpRequest = - new Func(HttpProvider.Request); - - var httpRequestAsync = - new Func>(HttpProvider.RequestAsync); - - var fetchRequest = new Func>(HttpProvider.Fetch); - - return new() - { - ["HttpRequest"] = httpRequest, - ["HttpRequestAsync"] = httpRequestAsync, - ["Fetch"] = fetchRequest - }; - } - - private Dictionary GetThreadingBindings() - { - Thread createThread(string identifier) - { - return ThreadingManager.CreateThread(identifier, _melon!); - } - - Task createTask(JsValue action) - { - return ThreadingManager.CreateTask(action, _melon!); - } - - return new() - { - ["CreateThread"] = new Func(createThread), - ["CreateTask"] = new Func>(createTask), - }; - } - - private Dictionary GetRealmBindings() - { - void createRealm(string name) - { - RealmManager.CreateRealm(name, _melon!); - } - - void deleteRealm(string name, int delay) - { - RealmManager.DeleteRealm(name, delay, _melon!); - } - - void setRealmScriptProperty(string realm, string key, dynamic value) - { - RealmManager.SetRealmPropertyFromScript(realm, key, value, _melon!); - } - - void setRealmInstanceProperty( - string realm, - string key, - string nSpace, - string type, - object[] parameters) - { - RealmManager.SetRealmPropertyFromInstance(realm, key, nSpace, type, parameters, _melon!); - } - - object? getRealmProperty(string realm, string key) - { - return RealmManager.GetRealmProperty(realm, key, _melon!); - } - - return new() - { - ["CreateRealm"] = new Action(createRealm), - ["DeleteRealm"] = new Action(deleteRealm), - ["GetRealmProperty"] = new Func(getRealmProperty), - ["SetRealmScriptProperty"] = new Action(setRealmScriptProperty), - ["SetRealmInstanceProperty"] = - new Action(setRealmInstanceProperty) - }; - } - - private Dictionary GetEnvironmentBindings() - { - return new() - { - ["LocalEnvironmentVariables"] = _melon!.GetEnvironmentVariables() - }; - } - - private Dictionary GetProcessBindings() - { - return new() - { - ["ProcessExit"] = new Action(x => Environment.Exit(x)) - }; - } - - private Dictionary GetFileSystemBindings() - { - return new() - { - { "ReadFileText", new Func(File.ReadAllText) }, - { - "ReadFileTextAsync", - new Func>(File.ReadAllTextAsync) - }, - { "WriteFileText", new Action(File.WriteAllText) }, - { - "WriteFileTextAsync", - new Func(File.WriteAllTextAsync) - }, - { "ReadFileBytes", new Func(File.ReadAllBytes) }, - { - "ReadFileBytesAsync", - new Func>(File.ReadAllBytesAsync) - }, - { "WriteFileBytes", new Action(File.WriteAllBytes) }, - { - "WriteFileBytesAsync", - new Func(File.WriteAllBytesAsync) - }, - { "DeleteFile", new Action(File.Delete) }, - { "CopyFile", new Action(File.Copy) }, - { "MoveFile", new Action(File.Move) }, - { "RenameFile", new Action(FileSystem.RenameFile) }, - { "RenameDirectory", new Action(FileSystem.RenameDirectory) }, - { "CreateDirectory", new Func(Directory.CreateDirectory) }, - { "DeleteDirectory", new Action(Directory.Delete) }, - }; - } - - private Dictionary GetWebServiceApplicationBindings() - { - void executeFromStringParameters(string parameters) - { - var deserialized = JsonConvert.DeserializeObject(parameters)!; - string deserializedRoutes = deserialized.Routes; - string deserializedEchoes = deserialized.Echoes; - - List endpointsBase = JsonConvert.DeserializeObject>(deserializedRoutes)!; - List echoesBase = JsonConvert.DeserializeObject>(deserializedEchoes)!; - - var endpoints = endpointsBase - .Select(x => new WebServiceApplicationEndpoint( - new HttpMethod((string)x.method), - (string)x.route, - (string)deserialized.Name, - _melon!)) - .ToList(); - - var echoes = echoesBase - .Select(x => new WebServiceApplicationEcho( - (string)x.host, - (int)x.port, - (bool)deserialized.EnableHttps)) - .ToList(); - - var application = new WebServiceApplication( - endpoints, - echoes, - (string)deserialized.Host, - (string)deserialized.Name, - (int)deserialized.Port, - (bool)deserialized.EnableHttps, - _melon!); - - application.Run(); - } - - return new() - { - ["SetupWebApplication"] = new Action(executeFromStringParameters) - }; - } - - private Dictionary GetTimeBindings() - { - void defineTimeoutOf(string targetName, int delay) - { - TimeManager.DefineTimeoutOf(_melon!, targetName, delay); - } - - void defineIntervalOf(string targetName, int delay) - { - TimeManager.DefineIntervalOf(_melon!, targetName, delay); - } - - return new() - { - ["DefineTimeoutOf"] = new Action(defineTimeoutOf), - ["DefineIntervalOf"] = new Action(defineIntervalOf) - }; - } - } + public class BindingsManager + { + private readonly IMelon? _melon; + private static BindingsManager? _instance; + + private readonly DirectBindingFactories _bindingFactories; + + public static BindingsManager GetManager(IMelon melon) + { + _instance ??= new BindingsManager(melon); + return _instance; + } + + public BindingsManager(IMelon melon) + { + _melon = melon; + _bindingFactories = DirectBindingFactories.GetFactories(); + } + + public IDictionary GetBindings() + { + var allBindings = new List>() + { + GetSerializationBindings(), + GetDirectDatabaseProviderBindings(), + GetDirectBindingFactories(), + GetHttpClientBindings(), + GetThreadingBindings(), + GetRealmBindings(), + GetEnvironmentBindings(), + GetProcessBindings(), + GetFileSystemBindings(), + GetWebServiceApplicationBindings(), + GetTimeBindings() + }; + + return allBindings + .SelectMany(x => x) + .ToDictionary(pair => pair.Key, pair => pair.Value); + } + + private static Dictionary GetSerializationBindings() + { + return new() + { + ["Serialize"] = new Func(SerializationManager.Serialize), + ["Deserialize"] = new Func(SerializationManager.Deserialize), + }; + } + + //MOVED TO Interoperability + private static Dictionary GetDirectDatabaseProviderBindings() + { + static Func getQueryFunction() where T : IDirectDatabaseProvider + { + var instance = (IDirectDatabaseProvider)Activator.CreateInstance(typeof(T))!; + return instance.ExecuteQuery; + } + + static Func getNonQueryFunction() where T : IDirectDatabaseProvider + { + var instance = (IDirectDatabaseProvider)Activator.CreateInstance(typeof(T))!; + return instance.ExecuteNonQuery; + } + + return new() + { + ["PostgreSQLBindingQuery"] = getQueryFunction(), + ["PostgreSQLBindingNonQuery"] = getNonQueryFunction(), + ["MySqlBindingQuery"] = getQueryFunction(), + ["MySqlBindingNonQuery"] = getNonQueryFunction(), + ["SqlServerBindingQuery"] = getQueryFunction(), + ["SqlServerBindingNonQuery"] = getNonQueryFunction(), + }; + } + + private Dictionary GetDirectBindingFactories() + { + var callStaticMethodBinding = + new Func(_bindingFactories.CallMethod); + + var getStaticProperty = + new Func(_bindingFactories.GetStaticProperty); + + var getTypes = new Func(_bindingFactories.GetTypes); + var loadAssembly = new Func(_bindingFactories.LoadAssembly); + var loadAssemblyAsync = new Func>(_bindingFactories.LoadAssemblyAsync); + + return new() + { + ["CallStaticMethod"] = callStaticMethodBinding, + ["GetStaticProperty"] = getStaticProperty, + ["GetTypes"] = getTypes, + ["LoadAssembly"] = loadAssembly, + ["LoadAssemblyAsync"] = loadAssemblyAsync + }; + } + + private static Dictionary GetHttpClientBindings() + { + var httpRequest = + new Func(HttpProvider.Request); + + var httpRequestAsync = + new Func>(HttpProvider.RequestAsync); + + var fetchRequest = new Func>(HttpProvider.Fetch); + + return new() + { + ["HttpRequest"] = httpRequest, + ["HttpRequestAsync"] = httpRequestAsync, + ["Fetch"] = fetchRequest + }; + } + + private Dictionary GetThreadingBindings() + { + Thread createThread(string identifier) + { + return ThreadingManager.CreateThread(identifier, _melon!); + } + + Task createTask(JsValue action) + { + return ThreadingManager.CreateTask(action, _melon!); + } + + return new() + { + ["CreateThread"] = new Func(createThread), + ["CreateTask"] = new Func>(createTask), + }; + } + + private Dictionary GetRealmBindings() + { + void createRealm(string name) + { + RealmManager.CreateRealm(name, _melon!); + } + + void deleteRealm(string name, int delay) + { + RealmManager.DeleteRealm(name, delay, _melon!); + } + + void setRealmScriptProperty(string realm, string key, dynamic value) + { + RealmManager.SetRealmPropertyFromScript(realm, key, value, _melon!); + } + + void setRealmInstanceProperty( + string realm, + string key, + string nSpace, + string type, + object[] parameters) + { + RealmManager.SetRealmPropertyFromInstance(realm, key, nSpace, type, parameters, _melon!); + } + + object? getRealmProperty(string realm, string key) + { + return RealmManager.GetRealmProperty(realm, key, _melon!); + } + + return new() + { + ["CreateRealm"] = new Action(createRealm), + ["DeleteRealm"] = new Action(deleteRealm), + ["GetRealmProperty"] = new Func(getRealmProperty), + ["SetRealmScriptProperty"] = new Action(setRealmScriptProperty), + ["SetRealmInstanceProperty"] = + new Action(setRealmInstanceProperty) + }; + } + + private Dictionary GetEnvironmentBindings() + { + return new() + { + ["LocalEnvironmentVariables"] = _melon!.GetEnvironmentVariables() + }; + } + + private Dictionary GetProcessBindings() + { + return new() + { + ["ProcessExit"] = new Action(x => Environment.Exit(x)) + }; + } + + private Dictionary GetFileSystemBindings() + { + return new() + { + { "ReadFileText", new Func(File.ReadAllText) }, + { + "ReadFileTextAsync", + new Func>(File.ReadAllTextAsync) + }, + { "WriteFileText", new Action(File.WriteAllText) }, + { + "WriteFileTextAsync", + new Func(File.WriteAllTextAsync) + }, + { "ReadFileBytes", new Func(File.ReadAllBytes) }, + { + "ReadFileBytesAsync", + new Func>(File.ReadAllBytesAsync) + }, + { "WriteFileBytes", new Action(File.WriteAllBytes) }, + { + "WriteFileBytesAsync", + new Func(File.WriteAllBytesAsync) + }, + { "DeleteFile", new Action(File.Delete) }, + { "CopyFile", new Action(File.Copy) }, + { "MoveFile", new Action(File.Move) }, + { "RenameFile", new Action(FileSystem.RenameFile) }, + { "RenameDirectory", new Action(FileSystem.RenameDirectory) }, + { "CreateDirectory", new Func(Directory.CreateDirectory) }, + { "DeleteDirectory", new Action(Directory.Delete) }, + }; + } + + private Dictionary GetWebServiceApplicationBindings() + { + void executeFromStringParameters(string parameters) + { + var deserialized = JsonConvert.DeserializeObject(parameters)!; + string deserializedRoutes = deserialized.Routes; + string deserializedEchoes = deserialized.Echoes; + + List endpointsBase = JsonConvert.DeserializeObject>(deserializedRoutes)!; + List echoesBase = JsonConvert.DeserializeObject>(deserializedEchoes)!; + + var endpoints = endpointsBase + .Select(x => new WebServiceApplicationEndpoint( + new HttpMethod((string)x.method), + (string)x.route, + (string)deserialized.Name, + _melon!)) + .ToList(); + + var echoes = echoesBase + .Select(x => new WebServiceApplicationEcho( + (string)x.host, + (int)x.port, + (bool)deserialized.EnableHttps)) + .ToList(); + + var application = new WebServiceApplication( + endpoints, + echoes, + (string)deserialized.Host, + (string)deserialized.Name, + (int)deserialized.Port, + (bool)deserialized.EnableHttps, + _melon!); + + application.Run(); + } + + return new() + { + ["SetupWebApplication"] = new Action(executeFromStringParameters) + }; + } + + private Dictionary GetTimeBindings() + { + void defineTimeoutOf(string targetName, int delay) + { + TimeManager.DefineTimeoutOf(_melon!, targetName, delay); + } + + void defineIntervalOf(string targetName, int delay) + { + TimeManager.DefineIntervalOf(_melon!, targetName, delay); + } + + return new() + { + ["DefineTimeoutOf"] = new Action(defineTimeoutOf), + ["DefineIntervalOf"] = new Action(defineIntervalOf) + }; + } + } } \ No newline at end of file