From d6776ec2e953eead6bc0143d18b2aafcc24ebfb3 Mon Sep 17 00:00:00 2001
From: Smeulf <smeulf@team-mediaportal.com>
Date: Tue, 28 Feb 2012 03:06:46 +0100
Subject: [PATCH] Fix CustomAction for client and server auto attachment

---
 MediaPortal/Setup/CustomActions/CustomAction.cs |   52 ++++++++++++++++------
 1 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/MediaPortal/Setup/CustomActions/CustomAction.cs b/MediaPortal/Setup/CustomActions/CustomAction.cs
index 8b6394d..7d7c019 100644
--- a/MediaPortal/Setup/CustomActions/CustomAction.cs
+++ b/MediaPortal/Setup/CustomActions/CustomAction.cs
@@ -22,29 +22,38 @@
 
 #endregion
 
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-using MediaPortal.Common.Localization;
-using MediaPortal.Common.Services.Localization;
-using System.Text;
 using MediaPortal.Backend.Services.ClientCommunication;
 using MediaPortal.Backend.Services.SystemResolver;
-using MediaPortal.Common.General;
-using MediaPortal.Common.Services.Logging;
-using Microsoft.Deployment.WindowsInstaller;
 
 using MediaPortal.Common;
+using MediaPortal.Common.General;
 using MediaPortal.Common.Logging;
+using MediaPortal.Common.Localization;
 using MediaPortal.Common.PathManager;
-using MediaPortal.Common.Services.Settings;
+using MediaPortal.Common.PluginManager;
+using MediaPortal.Common.Registry;
+
+using MediaPortal.Common.Services.Logging;
+using MediaPortal.Common.Services.Registry;
+using MediaPortal.Common.Services.Localization;
 using MediaPortal.Common.Services.PathManager;
+using MediaPortal.Common.Services.PluginManager;
+using MediaPortal.Common.Services.Settings;
+
 using MediaPortal.Common.Settings;
 using MediaPortal.Common.SystemResolver;
+
 using MediaPortal.UI.ServerCommunication.Settings;
+
+using Microsoft.Deployment.WindowsInstaller;
 using Microsoft.Win32;
 
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.Text;
+
 namespace CustomActions
 {
   public class CustomActions
@@ -87,13 +96,14 @@ namespace CustomActions
         PathManager pathManager = new PathManager(); // Fake path manager to inject the settings path to SettingsManager
         ServiceRegistration.Set<IPathManager>(pathManager);
         ServiceRegistration.Set<ISettingsManager>(new SettingsManager());
-        ServiceRegistration.Set<ILocalization>(new StringManager());
 
         string clientApplicationPath = Path.Combine(session["INSTALLDIR_CLIENT"], "MP2-Client.exe");
         string serverApplicationPath = Path.Combine(session["INSTALLDIR_SERVER"], "MP2-Server.exe");
 
         pathManager.InitializeDefaults(clientApplicationPath);
 
+        //ServiceRegistration.Set<ILocalization>(new StringManager());
+
         ISettingsManager settingsManager = ServiceRegistration.Get<ISettingsManager>();
         ServerConnectionSettings serverConnectionSettings = settingsManager.Load<ServerConnectionSettings>();
 
@@ -104,17 +114,28 @@ namespace CustomActions
           pathManager.InitializeDefaults(serverApplicationPath);
           settingsManager.ClearCache(); // Force the settings manager to use the server path and re-load all settings objects
 
+          ServiceRegistration.Set<IRegistry>(new MediaPortal.Common.Services.Registry.Registry());
+
+          ServiceRegistration.Set<IPluginManager>(new PluginManager());
+
+          StringManager stringManager = new StringManager();
+          stringManager.Startup();
+          ServiceRegistration.Set<ILocalization>(stringManager);
+
           ISystemResolver backendSystemResolver = new SystemResolver();
 
           String serverSystemId = backendSystemResolver.LocalSystemId;
           session.Log("Using server's system ID '{0}'", serverSystemId);
 
+          //To be certain ILocalization service is not lost between client ans server paths, retrive FriendlyName and save it
+          String friendlyName = new LocalizedUPnPDeviceInformation().GetFriendlyName(CultureInfo.InvariantCulture);
+
           pathManager.InitializeDefaults(clientApplicationPath);
           settingsManager.ClearCache();
 
           serverConnectionSettings.HomeServerSystemId = serverSystemId;
           serverConnectionSettings.LastHomeServerSystem = SystemName.Loopback();
-          serverConnectionSettings.LastHomeServerName = new LocalizedUPnPDeviceInformation().GetFriendlyName(CultureInfo.InvariantCulture);
+          serverConnectionSettings.LastHomeServerName = friendlyName;
           settingsManager.Save(serverConnectionSettings);
         }
         else
@@ -122,6 +143,9 @@ namespace CustomActions
           session.Log("Client is already attached to server with system ID '{0}'", serverConnectionSettings.HomeServerSystemId);
         }
 
+        ServiceRegistration.RemoveAndDispose<ILocalization>();
+        ServiceRegistration.RemoveAndDispose<IPluginManager>();
+        ServiceRegistration.RemoveAndDispose<IRegistry>();
         ServiceRegistration.RemoveAndDispose<ISettingsManager>();
         ServiceRegistration.RemoveAndDispose<IPathManager>();
         ServiceRegistration.RemoveAndDispose<ILogger>();
@@ -155,7 +179,7 @@ namespace CustomActions
       string regValue = "INSTALLDIR_" + cs;
 
       // reading install dir from registry
-      RegistryKey masterKey = Registry.LocalMachine.OpenSubKey(regKey);
+      RegistryKey masterKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
       if (masterKey == null)
       {
         session.Log("RegKey HKLM\\{0} not opened/found.", regKey);
-- 
1.7.6.msysgit.0

