Exemple #1
0
        /// <summary>
        /// Gets the a dictionary containing all ini files (file) and their number (key).
        /// </summary>
        /// <param name="TableFilename">The table filename.</param>
        /// <returns>Dictionary of ini files. Key is the ini file number, value is the ini file.</returns>
        public Dictionary <int, FileInfo> GetIniFilesDictionary(string TableFilename = "")
        {
            //Build the array of possible paths for the ini files

            List <string> LookupPaths = new List <string>();

            if (!IniFilesPath.IsNullOrWhiteSpace())
            {
                try
                {
                    DirectoryInfo DI = new DirectoryInfo(IniFilesPath);
                    if (DI.Exists)
                    {
                        LookupPaths.Add(DI.FullName);
                    }
                } catch (Exception E) {
                    Log.Exception("The specified IniFilesPath {0} could not be used due to a exception.".Build(IniFilesPath), E);
                };
            }


            if (!TableFilename.IsNullOrWhiteSpace())
            {
                try
                {
                    if (new FileInfo(TableFilename).Directory.Exists)
                    {
                        LookupPaths.Add(new FileInfo(TableFilename).Directory.FullName);
                    }
                }
                catch { }
            }

            if (GetGlobalConfigDirectory() != null)
            {
                LookupPaths.Add(GetGlobalConfigDirectory().FullName);
            }
            LookupPaths.Add(Directory.GetCurrentDirectory());

            if (!Assembly.GetExecutingAssembly().Location.IsNullOrEmpty())
            {
                LookupPaths.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            }

            //Build the dictionary of ini files

            Dictionary <int, FileInfo> IniFiles = new Dictionary <int, FileInfo>();

            bool FoundIt = false;

            string[] LedControlFilenames = { "directoutputconfig", "ledcontrol" };

            foreach (string LedControlFilename in LedControlFilenames)
            {
                foreach (string P in LookupPaths)
                {
                    DirectoryInfo DI = new DirectoryInfo(P);

                    List <FileInfo> Files = new List <FileInfo>();
                    foreach (FileInfo FI in DI.EnumerateFiles())
                    {
                        if (FI.Name.ToLower().StartsWith(LedControlFilename.ToLower()) && FI.Name.ToLower().EndsWith(".ini"))
                        {
                            Files.Add(FI);
                        }
                    }


                    foreach (FileInfo FI in Files)
                    {
                        if (string.Equals(FI.Name, "{0}.ini".Build(LedControlFilename), StringComparison.OrdinalIgnoreCase))
                        {
                            if (!IniFiles.ContainsKey(1))
                            {
                                IniFiles.Add(1, FI);
                                FoundIt = true;
                            }
                            else
                            {
                                Log.Warning("Found more than one ini file with for number 1. Likely you have a ini file without a number and and a second one with number 1.");
                            }
                        }
                        else
                        {
                            string F = FI.Name.Substring(LedControlFilename.Length, FI.Name.Length - LedControlFilename.Length - 4);
                            if (F.IsInteger())
                            {
                                int LedWizNr = -1;
                                if (int.TryParse(F, out LedWizNr))
                                {
                                    if (!IniFiles.ContainsKey(LedWizNr))
                                    {
                                        IniFiles.Add(LedWizNr, FI);
                                        FoundIt = true;
                                    }
                                    else
                                    {
                                        Log.Warning("Found more than one ini file with number {0}.".Build(LedWizNr));
                                    }
                                }
                            }
                        }
                    }
                    ;
                    if (FoundIt)
                    {
                        break;
                    }
                }
                if (FoundIt)
                {
                    break;
                }
            }



            return(IniFiles);
        }
Exemple #2
0
        /// <summary>
        /// Configures and initializes/starts and configures the Pinball object
        /// </summary>
        /// <param name="GlobalConfigFilename">The global config filename.</param>
        /// <param name="TableFilename">The table filename.</param>
        /// <param name="RomName">Name of the rom.</param>
        public void Init(string GlobalConfigFilename = "", string TableFilename = "", string RomName = "")
        {
            bool GlobalConfigLoaded = true;

            //Load the global config


            try
            {
                if (!GlobalConfigFilename.IsNullOrWhiteSpace())
                {
                    FileInfo GlobalConfigFile = new FileInfo(GlobalConfigFilename);


                    GlobalConfig = GlobalConfig.GetGlobalConfigFromConfigXmlFile(GlobalConfigFile.FullName);
                    if (GlobalConfig == null)
                    {
                        GlobalConfigLoaded = false;

                        //set new global config object if it config could not be loaded from the file.
                        GlobalConfig = new GlobalConfig();
                    }
                    GlobalConfig.GlobalConfigFilename = GlobalConfigFile.FullName;
                }
                else
                {
                    GlobalConfig = new GlobalConfig();
                    GlobalConfig.GlobalConfigFilename = GlobalConfigFilename;
                }
            }
            catch (Exception E)
            {
                throw new Exception("DirectOutput framework could initialize global config.\n Inner exception: {0}".Build(E.Message), E);
            }

            if (GlobalConfig.EnableLogging)
            {
                try
                {
                    Log.Filename = GlobalConfig.GetLogFilename((!TableFilename.IsNullOrWhiteSpace() ? new FileInfo(TableFilename).FullName : ""), RomName);
                    Log.Init();
                }
                catch (Exception E)
                {
                    throw new Exception("DirectOutput framework could initialize the log file.\n Inner exception: {0}".Build(E.Message), E);
                }
            }


            try
            {
                if (GlobalConfigLoaded)
                {
                    Log.Write("Global config loaded from: {0}".Build(GlobalConfigFilename));
                }
                else
                {
                    if (!GlobalConfigFilename.IsNullOrWhiteSpace())
                    {
                        Log.Write("Could not find or load theGlobalConfig file {0}".Build(GlobalConfigFilename));
                    }
                    else
                    {
                        Log.Write("No GlobalConfig file loaded. Using newly instanciated GlobalConfig object instead.");
                    }
                }



                Log.Write("Loading Pinball parts");



                //Load global script files
                Log.Write("Loading script files");
                Scripts.LoadAndAddScripts(GlobalConfig.GetGlobalScriptFiles());



                //Load table script files
                if (!TableFilename.IsNullOrWhiteSpace())
                {
                    Scripts.LoadAndAddScripts(GlobalConfig.GetTableScriptFiles(new FileInfo(TableFilename).FullName));
                }
                Log.Write("Script files loaded");


                Log.Write("Loading cabinet");
                //Load cabinet config
                Cabinet = null;
                FileInfo CCF = GlobalConfig.GetCabinetConfigFile();
                if (CCF != null)
                {
                    Log.Write("Will load cabinet config file: {0}".Build(CCF.FullName));
                    try
                    {
                        Cabinet = Cabinet.GetCabinetFromConfigXmlFile(CCF);
                        Cabinet.CabinetConfigurationFilename = CCF.FullName;
                        if (Cabinet.AutoConfigEnabled)
                        {
                            Log.Write("Cabinet config file has AutoConfig feature enable. Calling AutoConfig.");
                            Cabinet.AutoConfig();
                        }
                        Log.Write("Cabinet config loaded successfully from {0}".Build(CCF.FullName));
                    }
                    catch (Exception E)
                    {
                        Log.Exception("A exception occured when load cabinet config file: {0}".Build(CCF.FullName), E);
                    }
                }
                if (Cabinet == null)
                {
                    Log.Write("No cabinet config file loaded. Will use AutoConfig.");
                    //default to a new cabinet object if the config cant be loaded
                    Cabinet = new Cabinet();
                    Cabinet.AutoConfig();
                }

                Log.Write("Cabinet loaded");

                Log.Write("Loading table config");

                //Load table config

                Table = new DirectOutput.Table.Table();
                Table.AddLedControlConfig = true;

                if (!TableFilename.IsNullOrWhiteSpace())
                {
                    FileInfo TableFile = new FileInfo(TableFilename);
                    FileInfo TCF       = GlobalConfig.GetTableConfigFile(TableFile.FullName);
                    if (TCF != null)
                    {
                        Log.Write("Will load table config from {0}".Build(TCF.FullName));
                        try
                        {
                            Table = DirectOutput.Table.Table.GetTableFromConfigXmlFile(GlobalConfig.GetTableConfigFile(TableFile.FullName));
                            Table.TableConfigurationFilename = GlobalConfig.GetTableConfigFile(TableFile.FullName).FullName;
                            Log.Write("Table config loaded successfully from {0}".Build(TCF.FullName));
                        }
                        catch (Exception E)
                        {
                            Log.Exception("A exception occured when loading table config: {0}".Build(TCF.FullName), E);
                        }
                        if (Table.AddLedControlConfig)
                        {
                            Log.Write("Table config allows mix with ledcontrol configs.");
                        }
                    }
                    else
                    {
                        Log.Warning("No table config file found. Will try to load config from LedControl file(s).");
                    }
                }
                else
                {
                    Log.Write("No TableFilename specified, will use empty tableconfig");
                }
                if (Table.AddLedControlConfig)
                {
                    if (!RomName.IsNullOrWhiteSpace())
                    {
                        Log.Write("Will try to load configs from DirectOutput.ini or LedControl.ini file(s) for RomName {0}".Build(RomName));
                        //Load ledcontrol
                        LedControlConfigList L = new LedControlConfigList();
                        if (GlobalConfig.LedControlIniFiles.Count > 0)
                        {
                            Log.Write("Will try to load table config from LedControl  file(s) specified in global config.");
                            L.LoadLedControlFiles(GlobalConfig.LedControlIniFiles, false);
                        }
                        else
                        {
                            bool          FoundIt     = false;
                            List <string> LookupPaths = new List <string>();
                            if (!TableFilename.IsNullOrWhiteSpace())
                            {
                                if (new FileInfo(TableFilename).Directory.Exists)
                                {
                                    LookupPaths.Add(new FileInfo(TableFilename).Directory.FullName);
                                }
                            }
                            LookupPaths.AddRange(new string[] { GlobalConfig.GetGlobalConfigDirectory().FullName, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) });

                            LedControlIniFileList LedControlIniFiles = new LedControlIniFileList();

                            string[] LedControlFilenames = { "directoutputconfig", "ledcontrol" };

                            foreach (string LedControlFilename in LedControlFilenames)
                            {
                                foreach (string P in LookupPaths)
                                {
                                    DirectoryInfo DI = new DirectoryInfo(P);

                                    List <FileInfo> Files = new List <FileInfo>();
                                    foreach (FileInfo FI in DI.EnumerateFiles())
                                    {
                                        if (FI.Name.ToLower().StartsWith(LedControlFilename.ToLower()) && FI.Name.ToLower().EndsWith(".ini"))
                                        {
                                            Files.Add(FI);
                                        }
                                    }


                                    foreach (FileInfo FI in Files)
                                    {
                                        if (string.Equals(FI.Name, "{0}.ini".Build(LedControlFilename), StringComparison.OrdinalIgnoreCase))
                                        {
                                            LedControlIniFiles.Add(FI.FullName, 1);
                                            FoundIt = true;
                                        }
                                        else
                                        {
                                            string F = FI.Name.Substring(LedControlFilename.Length, FI.Name.Length - LedControlFilename.Length - 4);
                                            if (F.IsInteger())
                                            {
                                                int LedWizNr = -1;
                                                if (int.TryParse(F, out LedWizNr))
                                                {
                                                    if (!LedControlIniFiles.Contains(LedWizNr))
                                                    {
                                                        LedControlIniFiles.Add(FI.FullName, LedWizNr);
                                                        FoundIt = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    ;
                                    if (FoundIt)
                                    {
                                        break;
                                    }
                                }
                                if (FoundIt)
                                {
                                    break;
                                }
                            }



                            if (FoundIt)
                            {
                                L.LoadLedControlFiles(LedControlIniFiles, false);
                                Log.Write("{0} directoutput.ini or ledcontrol.ini files loaded.".Build(LedControlIniFiles.Count));
                            }
                            else
                            {
                                Log.Write("No directoutput.ini or ledcontrol.ini files found. No directoutput.ini or ledcontrol.ini configs will be loaded.");
                            }
                        }
                        if (!L.ContainsConfig(RomName))
                        {
                            Log.Write("No config for table found in LedControl data for RomName {0}.".Build(RomName));
                        }
                        else
                        {
                            Log.Write("Config for RomName {0} exists in LedControl data. Updating cabinet and config.".Build(RomName));

                            DirectOutput.LedControl.Setup.Configurator C = new DirectOutput.LedControl.Setup.Configurator();
                            C.EffectMinDurationMs    = GlobalConfig.LedControlMinimumEffectDurationMs;
                            C.EffectRGBMinDurationMs = GlobalConfig.LedControlMinimumRGBEffectDurationMs;
                            C.Setup(L, Table, Cabinet, RomName);
                            C = null;
                            //                        L.UpdateTableConfig(Table, RomName, Cabinet);
                        }
                        L = null;
                    }
                    else
                    {
                        Log.Write("Cant load config from directoutput.ini or ledcontrol.ini file(s) since no RomName was supplied. No ledcontrol config will be loaded.");
                    }
                }
                if (Table.TableName.IsNullOrWhiteSpace())
                {
                    if (!TableFilename.IsNullOrWhiteSpace())
                    {
                        Table.TableName = Path.GetFileNameWithoutExtension(new FileInfo(TableFilename).FullName);
                    }
                    else if (!RomName.IsNullOrWhiteSpace())
                    {
                        Table.TableName = RomName;
                    }
                }
                if (!TableFilename.IsNullOrWhiteSpace())
                {
                    Table.TableFilename = new FileInfo(TableFilename).FullName;
                }
                if (!RomName.IsNullOrWhiteSpace())
                {
                    Table.RomName = RomName;
                }
                Log.Write("Table config loading finished");



                Log.Write("Pinball parts loaded");

                Log.Write("Starting processes");
                InitStatistics();
                Cabinet.Init(this);
                Table.Init(this);
                Alarms.Init(this);
                Table.TriggerStaticEffects();
                Cabinet.Update();

                //Add the thread initializing the framework to the threadinfo list
                ThreadInfo TI = new ThreadInfo(Thread.CurrentThread);
                TI.HeartBeatTimeOutMs = 10000;
                TI.HostName           = "External caller";
                TI.HeartBeat();
                ThreadInfoList.Add(TI);



                InitMainThread();
                Log.Write("Framework initialized.");
                Log.Write("Have fun! :)");
            }
            catch (Exception E)
            {
                Log.Exception("A eception occured during initialization", E);
                throw new Exception("DirectOutput framework has encountered a exception during initialization.\n Inner exception: {0}".Build(E.Message), E);
            }
        }