Inheritance: CrossDomainObject, INotifyPropertyChanged
        public VersionPair CheckUpdates(GameModel model)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            string versionFile = ConfigurationManager.GetLocalVersionFile(model);
            if (File.Exists(ConfigurationManager.GetLocalVersionFile(model))) {
                int verCurrent = -1;
                int verRemote = -1;

                using (StreamReader streamReader = new StreamReader(versionFile)) {
                    verCurrent = GetVersion(streamReader.ReadToEnd());
                }

                using (WebClientEx webClient = new WebClientEx()) {
                    string result;
                    try {
                        result = webClient.DownloadString(ConfigurationManager.GetConfiguration(model).VersionRemoteURL);
                        verRemote = GetVersion(result);
                    } catch {
                        return null;
                    }

                    if (verRemote < 0 || verCurrent < 0) {
                        return null;
                    }
                    return new VersionPair(verCurrent, verRemote);
                }
            }
            return null;
        }
        public bool CheckLauncher(GameModel model)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            IConfiguration config = GetConfiguration(model);
            string pLauncherPath = GetLauncherPath(model);
            if (string.IsNullOrEmpty(pLauncherPath)) {
                return false;
            }

            new FileIOPermission(FileIOPermissionAccess.Read, pLauncherPath).Assert();
            if (!File.Exists(Path.Combine(pLauncherPath, config.LauncherExecutable))) {
                return false;
            }
            return true;
        }
 public bool CheckGame(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     IConfiguration config = GetConfiguration(model);
     string pGamePath = GetGamePath(model);
     if (string.IsNullOrEmpty(pGamePath)) {
         return false;
     }
     new FileIOPermission(FileIOPermissionAccess.Read, pGamePath).Assert();
     if (!File.Exists(Path.Combine(pGamePath, config.VersionLocalPath)) || !File.Exists(Path.Combine(pGamePath, config.GameExecutable))) {
         return false;
     }
     if (!File.Exists(Path.Combine(pGamePath, puPF)) || !File.Exists(Path.Combine(pGamePath, puHF))) {
         return false;
     }
     return true;
 }
        public bool DownloadUpdates(GameModel model, VersionPair versionPair)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            if (versionPair == null) {
                throw new ArgumentException("versionPair argument cannot be null");
            }
            bool updateSuccess = true;
            double downloadedContentLenght = 0;
            double WholeContentLength = 0;

            Dictionary<int, double> contentLenght = new Dictionary<int, double>();
            try {
                for (int i = versionPair.Local + 1; i <= versionPair.Remote; i++) {
                    double patchSize = GetFileLength(new Uri(string.Format(ConfigurationManager.GetConfiguration(model).PatchRemoteURL, i)));
                    WholeContentLength += patchSize;
                    contentLenght.Add(i, patchSize);
                }
            } catch (WebException) {
                return false;
            }

            for (int i = versionPair.Local + 1; i <= versionPair.Remote; i++) {
                Uri patchUri = new Uri(string.Format(ConfigurationManager.GetConfiguration(model).PatchRemoteURL, i));
                string packageFile = Path.Combine(ConfigurationManager.GetGamePath(model), string.Format("UPDATE{0}.zip", i));

                OnStatusChanged(UpdateStatusEventArgs.Stage.DOWNLOADING, i, versionPair.Remote, downloadedContentLenght, WholeContentLength, 0, 100);
                double CurrentContentLength = 0;
                if (!contentLenght.TryGetValue(i, out CurrentContentLength)) {
                    updateSuccess = false;
                    break;
                }

                int downloadAttempts = 5;
                bool patchSuccess = false;
                while (downloadAttempts > 0 && !patchSuccess) {
                    try {
                        if (File.Exists(packageFile)) {
                            File.Delete(packageFile);
                        }
                    } catch {
                        updateSuccess = false;
                        break;
                    }

                    using (WebClientEx webClient = new WebClientEx()) {
                        DownloadProgressChangedEventHandler progressChangedEventHandler = (s, e) => {
                            double dataReceived = (e.BytesReceived / (1024.0 * 1024.0));
                            double dataTotal = (e.TotalBytesToReceive / (1024.0 * 1024.0));
                            OnStatusChanged(UpdateStatusEventArgs.Stage.DOWNLOADING,
                                i, versionPair.Remote,
                                downloadedContentLenght + e.BytesReceived, WholeContentLength,
                                dataReceived, dataTotal);
                        };

                        webClient.DownloadProgressChanged += progressChangedEventHandler;
                        try {
                            webClient.DownloadFileAsync(patchUri, packageFile);
                            while (webClient.IsBusy) {
                                System.Threading.Thread.Sleep(100);
                            }
                            downloadedContentLenght += CurrentContentLength;
                        } catch {
                            downloadAttempts--;
                            continue;
                        } finally {
                            webClient.DownloadProgressChanged -= progressChangedEventHandler;
                        }
                    }
                    if (!ConfigurationManager.CheckUpdateAccess(model)) {
                        updateSuccess = false;
                        break;
                    }
                    if (ExtractUpdate(i, versionPair.Remote,
                        downloadedContentLenght, WholeContentLength,
                        packageFile, ConfigurationManager.GetGamePath(model), true)) {
                        try {
                            string versionFile = ConfigurationManager.GetLocalVersionFile(model);
                            string directory = Path.GetDirectoryName(versionFile);
                            if (!Directory.Exists(directory)) {
                                Directory.CreateDirectory(directory);
                            }
                            File.WriteAllLines(versionFile, new string[] { "[VERSION]", "version=" + i.ToString() });
                        } catch {
                            updateSuccess = false;
                            break;
                        }
                        patchSuccess = true;
                    }
                    downloadAttempts--;
                }
                if (!patchSuccess) {
                    updateSuccess = false;
                    break;
                }
            }
            return updateSuccess;
        }
 private IFileSystemManager GetFileSystem(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     IFileSystemManager fileSystem;
     if (FileSystems.TryGetValue(model, out fileSystem)) {
         return fileSystem;
     }
     fileSystem = App.Kernel.Get<IFileSystemManager>();
     FileSystems.TryAdd(model, fileSystem);
     fileSystem.WriteStatusChanged += (s, e) => {
         OnStatusChanged(UpdateStatusEventArgs.Stage.INSTALLING, 1, 1, e.FileNumber, e.FileCount, 0, 1);
     };
     return fileSystem;
 }
        public bool ImportPackages(GameModel model)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            if (Directory.Exists(ConfigurationManager.GetImportPath(model))) {
                IFileSystemManager fs = GetFileSystem(model);
                bool IsOpened = false;
                try {
                    IsOpened = fs.Open(FileAccess.Write, 16, ConfigurationManager.GetHFPath(model), ConfigurationManager.GetPFPath(model));
                } catch {
                    IsOpened = false;
                }

                if (!IsOpened) {
                    OnFileSystemOpenError();
                    return false;
                }
                try {
                    return fs.WriteDirectory(ConfigurationManager.GetImportPath(model), true);
                } finally {
                    fs.Close();
                }
            }
            return true;
        }
 /// <summary>
 /// Initializes a new <see cref="GameModel"/> based on another
 /// </summary>
 /// <param name="another">Source <see cref="GameModel"/></param>
 public GameModel(GameModel another)
 {
     this.Type = another.Type;
     this.GamePath = another.GamePath;
     this.LauncherPath = another.LauncherPath;
 }
 public string GetPFPath(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     string path = GetGamePath(model);
     if (string.IsNullOrEmpty(path)) {
         return null;
     }
     return Path.Combine(path, puPF);
 }
        public void UpdateRegistryPaths(GameModel model)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            IConfiguration config = GetConfiguration(model);
            string gamePath = GetGamePath(model);
            string launcherPath = GetGamePath(model);

            try {
                if (!string.IsNullOrEmpty(gamePath)) {
                    RegistryKey reg = Registry.CurrentUser.CreateSubKey(config.GamePathRegKey);
                    reg.SetValue(config.GamePathRegVal, gamePath);
                    reg.Close();
                }

                if (!string.IsNullOrEmpty(launcherPath)) {
                    RegistryKey reg = Registry.CurrentUser.CreateSubKey(config.LauncherPathRegKey);
                    reg.SetValue(config.LauncherPathRegVal, launcherPath);
                    reg.Close();
                }
            } catch (SecurityException) {
                // ignore
            }
        }
 public string GetLauncherPath(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     IConfiguration config = GetConfiguration(model);
     if (string.IsNullOrEmpty(model.LauncherPath)) {
         return GetLauncherPathFromRegistry(config);
     }
     return model.LauncherPath;
 }
 public string GetLocalVersionFile(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     IConfiguration config = GetConfiguration(model);
     string path = GetGamePath(model);
     if (string.IsNullOrEmpty(path)) {
         return null;
     }
     return Path.Combine(path, config.VersionLocalPath);
 }
 public string GetLauncherEXE(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     IConfiguration config = GetConfiguration(model);
     string path = GetLauncherPath(model);
     if (string.IsNullOrEmpty(path)) {
         return null;
     }
     return Path.Combine(path, config.LauncherExecutable);
 }
 public IConfiguration GetConfiguration(GameModel model)
 {
     if (model == null) {
         throw new ArgumentException("model argument cannot be null");
     }
     return GetConfiguration(model.Type);
 }
        public bool CheckUpdateAccess(GameModel model)
        {
            if (model == null) {
                throw new ArgumentException("model argument cannot be null");
            }
            string gameEXE = GetGameEXE(model);
            string pfFile = GetPFPath(model);
            string hfFile = GetHFPath(model);
            if (string.IsNullOrEmpty(gameEXE) || string.IsNullOrEmpty(pfFile) || string.IsNullOrEmpty(hfFile)) {
                return false;
            }

            new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, new string[] { gameEXE, pfFile, hfFile }).Assert();
            return !Utils.IsFileLocked(gameEXE) && !Utils.IsFileLocked(pfFile) && !Utils.IsFileLocked(hfFile);
        }
 public ProtectedProfile(Profile profile) {
     this.Id = profile.Id;
     this.Name = profile.Name;
     this.ImagePath = profile.ImagePath;
     this.LaunchMode = profile.LaunchMode;
     this.UpdateEngineEnabled = profile.UpdateEngineEnabled;
     this.KBLCServiceEnabled = profile.KBLCServiceEnabled;
     this.Rotation = new RotationData(profile.Rotation);
     this.News = new NewsData(profile.News);
     this.GameModel = new GameModel(profile.GameModel);
 }
Beispiel #16
0
 /// <summary>
 /// Initializes a new <see cref="GameModel"/> based on another
 /// </summary>
 /// <param name="another">Source <see cref="GameModel"/></param>
 public GameModel(GameModel another)
 {
     this.Type         = another.Type;
     this.GamePath     = another.GamePath;
     this.LauncherPath = another.LauncherPath;
 }