Example #1
0
 protected override void AfterLoad()
 {
     Configuration.Servers.Clear();
     foreach (var server in servers)
     {
         var newServer = new Configuration.Server();
         server.AfterLoad(newServer);
         Configuration.Servers.Add(newServer);
     }
     Configuration.MainWindowVisible     = mainWindowVisible;
     Configuration.MainWindowPosition    = mainWindowPosition.AsRect().ToTuple();
     Configuration.InfoWindowVisible     = infoWindowVisible;
     Configuration.InfoWindowPosition    = infoWindowPosition.AsRect().ToTuple();
     Configuration.AutoStartServers      = autoStartServers;
     Configuration.AutoAcceptConnections = autoAcceptConnections;
     Configuration.ConfirmRemoveClient   = confirmRemoveClient;
     Configuration.PauseServerWithGame   = pauseServerWithGame;
     try {
         Logger.Level = (Logger.Severity)Enum.Parse(typeof(Logger.Severity), logLevel);
     } catch (ArgumentException) {
         Console.WriteLine(
             "[kRPC] Error parsing log level from configuration file. Got '" + logLevel + "'. " +
             "Defaulting to " + Logger.Severity.Info);
         Logger.Level = Logger.Severity.Info;
     }
     Configuration.VerboseErrors       = verboseErrors;
     ServicesChecker.CheckDocumented   = checkDocumented;
     Configuration.OneRPCPerUpdate     = oneRPCPerUpdate;
     Configuration.MaxTimePerUpdate    = maxTimePerUpdate;
     Configuration.AdaptiveRateControl = adaptiveRateControl;
     Configuration.BlockingRecv        = blockingRecv;
     Configuration.RecvTimeout         = recvTimeout;
     Logger.WriteLine("Loaded configuration", Logger.Severity.Debug);
 }
Example #2
0
 protected override void BeforeSave()
 {
     Logger.WriteLine("Saving configuration", Logger.Severity.Debug);
     servers.Clear();
     foreach (var server in Configuration.Servers)
     {
         var newServer = new Server();
         newServer.BeforeSave(server);
         servers.Add(newServer);
     }
     mainWindowVisible     = Configuration.MainWindowVisible;
     mainWindowPosition    = RectStorage.FromRect(Configuration.MainWindowPosition.ToRect());
     infoWindowVisible     = Configuration.InfoWindowVisible;
     infoWindowPosition    = RectStorage.FromRect(Configuration.InfoWindowPosition.ToRect());
     autoStartServers      = Configuration.AutoStartServers;
     autoAcceptConnections = Configuration.AutoAcceptConnections;
     confirmRemoveClient   = Configuration.ConfirmRemoveClient;
     pauseServerWithGame   = Configuration.PauseServerWithGame;
     logLevel            = Logger.Level.ToString();
     verboseErrors       = Configuration.VerboseErrors;
     checkDocumented     = ServicesChecker.CheckDocumented;
     oneRPCPerUpdate     = Configuration.OneRPCPerUpdate;
     maxTimePerUpdate    = Configuration.MaxTimePerUpdate;
     adaptiveRateControl = Configuration.AdaptiveRateControl;
     blockingRecv        = Configuration.BlockingRecv;
     recvTimeout         = Configuration.RecvTimeout;
 }
Example #3
0
 static void Error(string message)
 {
     Logger.WriteLine("Load API: " + message, Logger.Severity.Error);
     Compatibility.SpawnPopupDialog(new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), "krpc-api-loader",
                                    "kRPC API Loader", message, "OK", true, HighLogic.UISkin);
 }
Example #4
0
        public static Type Load(Type api, string assemblyName, string apiName, Version requiredVersion = null)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            // Find the assembly
            var assembly = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.assembly.GetName().Name == assemblyName);

            if (assembly == null)
            {
                Logger.WriteLine("Load API: " + assemblyName + " not found; skipping");
                return(null);
            }

            // Version check
            var version = new Version(assembly.versionMajor, assembly.versionMinor);

            if (requiredVersion != null)
            {
                if (version.CompareTo(requiredVersion) < 0)
                {
                    Error("Failed to load " + assemblyName + "; found version " + version + " but version >= " + requiredVersion + " is required");
                    return(null);
                }
            }

            // Get type of APIs static class
            var type = assembly.assembly.GetTypes().FirstOrDefault(t => t.FullName == apiName);

            if (type == null)
            {
                Error(apiName + " not found in " + assemblyName);
                return(null);
            }

            // Load the API methods
            var apiMethods = type.GetMethods();

            foreach (var property in api.GetProperties())
            {
                // Skip the property if it does not return a delegate
                if (!property.GetGetMethod().ReturnType.IsSubclassOf(typeof(Delegate)))
                {
                    continue;
                }

                // Assign the API method to the property
                var method = apiMethods.FirstOrDefault(m => m.Name.Equals(property.Name));
                if (method == null)
                {
                    Error("Method not found for " + property.Name);
                    return(null);
                }
                var f = Delegate.CreateDelegate(property.PropertyType, type, method.Name);
                property.SetValue(null, f, null);
            }

            Logger.WriteLine("Load API: Successfully loaded " + assemblyName + " version " + version);
            return(type);
        }