private void openProject() { if (this.isChanged) { MessageBoxResult ret = MessageBox.Show("当前项目已经被修改,在打开新项目前是否要保存当前修改?", "记忆工具", MessageBoxButton.YesNoCancel, MessageBoxImage.Question); if (ret == MessageBoxResult.Yes) { if (!this.saveProject()) { return; } } else if (ret == MessageBoxResult.No) { } else if (ret == MessageBoxResult.Cancel) { return; } } OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "记忆项目文件|*.mre"; if (dlg.ShowDialog().Value) { this.memorizeEntry = MemorizeEntry.Load(dlg.FileName); this.DataContext = this.memorizeEntry; this.projectFile = dlg.FileName; if (this.memorizeEntry.Stages.Count > 0 && this.memorizeEntry.Stages[0] is MemorizeMathStage) { MemorizeMathCreatorUserControl mathCreator = new MemorizeMathCreatorUserControl(this.memorizeEntry); this.creatorStackPanel.Children.Clear(); this.creatorStackPanel.Children.Add(mathCreator); } else { MemorizeCreatorUserControl generalCreator = new MemorizeCreatorUserControl(this.memorizeEntry); this.creatorStackPanel.Children.Clear(); this.creatorStackPanel.Children.Add(generalCreator); } foreach (var item in UIHelper.MemorizeTypeItems) { if (item.Type == this.memorizeEntry.SubType) { this.subTypeComboBox.SelectedItem = item; break; } } } }
private bool checkEntry(AppItem item) { if (item is MemorizeAppItem) { MemorizeAppItem memorizeAppItem = item as MemorizeAppItem; if (memorizeAppItem.MemorizeEntry == null) { try { memorizeAppItem.MemorizeEntry = MemorizeEntry.Load(memorizeAppItem.AppEntryFile); } catch (Exception ex) { Debug.Assert(false, ex.Message); } finally { } return(memorizeAppItem.MemorizeEntry != null); } } else if (item is AssessmentAppItem) { return(true); } else { if (item.Entry == null) { try { Assembly gadgetAssembly = Assembly.LoadFile(item.AppEntryFile); item.Entry = gadgetAssembly.CreateInstance(item.FullName) as IGadgetEntry; } catch (Exception ex) { Debug.Assert(false, ex.Message); } return(item.Entry != null); } } return(true); }
private void Window_Loaded(object sender, RoutedEventArgs e) { try { MemorizeEntry entry = MemorizeEntry.Load(this.projectFile); MemorizeControl.Instance.TestMode = false; MemorizeControl.Instance.Start(entry, "0"); this.rootGrid.Children.Add(MemorizeControl.Instance.StartupUI); } catch (Exception ex) { StringBuilder strBuilder = new StringBuilder(); strBuilder.AppendLine("测试失败"); strBuilder.AppendLine(ex.Message); MessageBox.Show(strBuilder.ToString(), "记忆工具", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void loadMreItem(string fileName) { this.memorizeEntry = MemorizeEntry.Load(fileName); this.titleTextBlock.Text = this.memorizeEntry.Title; this.descriptionTextBlock.Text = this.memorizeEntry.Description; this.thumbnailFile = this.memorizeEntry.Thumbnail; this.id = this.memorizeEntry.Id; foreach (string file in this.memorizeEntry.BackgroundImages) { this.additionalFileListBox.Items.Add(file); } foreach (string file in this.memorizeEntry.BackgroundMusics) { this.additionalFileListBox.Items.Add(file); } if (!string.IsNullOrEmpty(this.memorizeEntry.MemorizeItemBackground)) { this.additionalFileListBox.Items.Add(this.memorizeEntry.MemorizeItemBackground); } if (!string.IsNullOrEmpty(this.memorizeEntry.ItemClickMusic)) { this.additionalFileListBox.Items.Add(this.memorizeEntry.ItemClickMusic); } if (!string.IsNullOrEmpty(this.memorizeEntry.Thumbnail)) { this.additionalFileListBox.Items.Add(this.memorizeEntry.Thumbnail); } foreach (MemorizeItem item in this.memorizeEntry.Items) { this.getItemInfo(item.ObjectA); this.getItemInfo(item.ObjectB); } }
private static void loadMreItem(string fileName, string slpFile) { MemorizeEntry memorizeEntry = MemorizeEntry.Load(fileName); string title = memorizeEntry.Title; string description = memorizeEntry.Description; string thumbnailFile = memorizeEntry.Thumbnail; string id = memorizeEntry.Id; List <string> additionalFileList = new List <string>(); foreach (string file in memorizeEntry.BackgroundImages) { additionalFileList.Add(file); } foreach (string file in memorizeEntry.BackgroundMusics) { additionalFileList.Add(file); } if (!string.IsNullOrEmpty(memorizeEntry.MemorizeItemBackground)) { additionalFileList.Add(memorizeEntry.MemorizeItemBackground); } if (!string.IsNullOrEmpty(memorizeEntry.ItemClickMusic)) { additionalFileList.Add(memorizeEntry.ItemClickMusic); } if (!string.IsNullOrEmpty(memorizeEntry.Thumbnail)) { additionalFileList.Add(memorizeEntry.Thumbnail); } foreach (MemorizeItem item in memorizeEntry.Items) { getItemInfo(item.ObjectA, additionalFileList); getItemInfo(item.ObjectB, additionalFileList); } // Create package string packFile = slpFile; FileStream fs = File.OpenWrite(packFile); try { // Header Help.WriteString(fs, "{BE4A1507-5B37-42EA-9E08-43EF4F363C42}"); // AppId Help.WriteString(fs, id); // Title Help.WriteString(fs, title); Help.WriteString(fs, description); // Thumbnail ext Help.WriteString(fs, System.IO.Path.GetExtension(thumbnailFile)); byte[] emptyData = new byte[0]; // Thumbnail data if (File.Exists(thumbnailFile)) { Help.WriteBytes(fs, File.ReadAllBytes(thumbnailFile)); } else { Help.WriteBytes(fs, emptyData); } // Write a empty entry as Memorize App no entry and dll file. Help.WriteString(fs, System.IO.Path.GetFileName(fileName)); Help.WriteString(fs, string.Empty); // Type Help.WriteBytes(fs, BitConverter.GetBytes(102)); Help.WriteBytes(fs, BitConverter.GetBytes(memorizeEntry.SubType)); // Create Time Help.WriteString(fs, memorizeEntry.CreateDate.ToString()); // Creator Information Help.WriteString(fs, memorizeEntry.Creator); Help.WriteString(fs, memorizeEntry.CreatorLogo); Help.WriteString(fs, memorizeEntry.CreatorWebsite); string rootDir = System.IO.Path.GetDirectoryName(fileName); // File count Help.WriteString(fs, (additionalFileList.Count + 1).ToString()); string mreFile = fileName.Remove(0, rootDir.Length); Help.WriteString(fs, mreFile); Help.WriteBytes(fs, File.ReadAllBytes(fileName)); foreach (string temp in additionalFileList) { string name = temp.Remove(0, rootDir.Length); Help.WriteString(fs, name); if (File.Exists(temp)) { Help.WriteBytes(fs, File.ReadAllBytes(temp)); } else { Help.WriteBytes(fs, emptyData); } } } catch (Exception ex) { throw ex; } finally { if (fs != null) { fs.Close(); } } }
private void Install(AppInstallItem item) { FileStream fs = null; try { this.UpdateState(InstallState.Installing, item); this.UpdateProgress(0, item); fs = File.OpenRead(item.LocakPackFile); // Check File Header string fileHeader = this.ReadString(fs); this.VerifyFileHeader(fileHeader); // Check AppTitle and AppId string appId = this.ReadString(fs); string appTitle = this.ReadString(fs); if (appTitle != item.AppItem.Title || appId != item.AppItem.Id) { // throw new NotSupportedException(); } string appFolder = this.GetAppFolder(fileHeader, appId); // Description string description = this.ReadString(fs); // Logos string thumbnail = this.ExtractLogo(fs, appId); // Entry File; string appEntryFile = this.ReadString(fs); string fullName = this.ReadString(fs); int type = BitConverter.ToInt32(this.ReadBytes(fs), 0); int subType = BitConverter.ToInt32(this.ReadBytes(fs), 0); AppItem appItem = this.CreateAppItem(fileHeader, type, subType); appItem.Id = appId; appItem.Title = appTitle; appItem.Description = description; appItem.Thumbnail = thumbnail; appItem.AppEntryFile = appEntryFile; appItem.FullName = fullName; appItem.Type = type; appItem.SubType = subType; appItem.CreateDate = DateTime.Parse(this.ReadString(fs)); appItem.CreatorName = this.ReadString(fs); appItem.CreatorLogo = this.ReadString(fs); appItem.CreatorWebSite = this.ReadString(fs); appItem.AddedTime = DateTime.Now.ToUniversalTime(); // Get File Count string countString = this.ReadString(fs); int count = Convert.ToInt32(countString); for (int i = 0; i < count; i++) { // Get FileName and Data string fileName = this.ReadString(fs); byte[] fileData = this.ReadBytes(fs); if (string.IsNullOrEmpty(fileName)) { continue; } appItem.RelatedFiles.Add(fileName); // Create File Path string filePath = appFolder + Path.GetDirectoryName(fileName); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } // Create File Name filePath = Path.Combine(filePath, Path.GetFileName(fileName)); if (File.Exists(filePath)) { } try { if (fileData.Length > 0) { File.WriteAllBytes(filePath, fileData); } } catch { } if (item.State == InstallState.UserCancelling) { this.UpdateState(InstallState.UserCancelled, item); break; } this.UpdateProgress((int)((i + 1 * 100) / count), item); } if (appItem is MemorizeAppItem) { MemorizeAppItem memorizeAppItem = appItem as MemorizeAppItem; try { memorizeAppItem.MemorizeEntry = MemorizeEntry.Load(System.IO.Path.Combine(appFolder, System.IO.Path.GetFileName(appItem.AppEntryFile))); if (memorizeAppItem.MemorizeEntry.SubType == 0) { memorizeAppItem.SubType = 10299; } else { memorizeAppItem.SubType = memorizeAppItem.MemorizeEntry.SubType; } memorizeAppItem.Type = 102; } catch (Exception e) { this.UpdateState(InstallState.InstallFail, item); this.InvokeInstallCompletedEvent(item, e); return; } } else if (appItem is AssessmentAppItem) { AssessmentAppItem assessmentAppItem = appItem as AssessmentAppItem; try { string folder = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (UIHelper.Is64BitOS()) { SevenZipExtractor.SetLibraryPath(System.IO.Path.Combine(folder, "ThirdParty\\7z\\7z64.dll")); } else { SevenZipExtractor.SetLibraryPath(System.IO.Path.Combine(folder, "ThirdParty\\7z\\7z.dll")); } string archiveFile = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id + @"\" + assessmentAppItem.Id + ".zip"); assessmentAppItem.AppEntryFile = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id + @"\" + assessmentAppItem.Id + ".sla"); using (SevenZipExtractor extractor = new SevenZipExtractor(archiveFile, "$L&%$D123@3$5^7*")) { string targetFolder = Path.Combine(folder, @"data\assessment\" + assessmentAppItem.Id); for (int i = 0; i < extractor.FilesCount; i++) { string fileName = extractor.ArchiveFileNames[i]; if (!extractor.ArchiveFileNames[i].StartsWith("\\")) { fileName = "\\" + extractor.ArchiveFileNames[i]; } string targetFile = targetFolder + fileName; FileStream targetFs = File.Open(targetFile, FileMode.OpenOrCreate, FileAccess.ReadWrite); extractor.ExtractFile(i, targetFs); targetFs.Close(); } } File.Delete(archiveFile); } catch (Exception ex) { Debug.Assert(false, ex.Message); this.UpdateState(InstallState.InstallFail, item); this.InvokeInstallCompletedEvent(item, ex); return; } } if (DataMgr.Instance.getAppItemById(appItem.Id) == null) { DataMgr.Instance.insertAppItem(0, appItem); } this.UpdateProgress(100, item); this.UpdateState(InstallState.Done, item); this.InvokeInstallCompletedEvent(item, null); // this.Remove(item); DataMgr.Instance.AppService.IncreaseDownloadCount(appItem.Id, LoginInfo.GetMD5Hash("#$32*d_&")); } catch (Exception ex) { ProcessWriteFileEx(ex, item); this.InvokeInstallCompletedEvent(item, ex); } finally { if (fs != null) { fs.Close(); } try { File.Delete(item.LocakPackFile); } catch { } } }
internal MemorizeEntry Load(string file) { return(MemorizeEntry.Load(file)); }
private void loadGadgetWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; int index = 0; AppDomain exeDomain = AppDomain.CurrentDomain; Assembly assembly = Assembly.GetExecutingAssembly(); string exeAssembly = assembly.FullName; DirectoryInfo di = new DirectoryInfo(System.IO.Path.GetDirectoryName(assembly.Location)); DirectoryInfo memorizeDataDi = new DirectoryInfo(DataMgr.Instance.MemorizeDataPath); DirectoryInfo assessmentDataDi = new DirectoryInfo(DataMgr.Instance.AssessmentDataPath); try { DataMgr.Instance.preLoadApps(); } catch { } try { AppInstallMgr.Instance.Dispatcher = this.Dispatcher; AppInstallMgr.Instance.Init(); } catch { } var newAppQuery = from item in AppInstallMgr.Instance.AppInstallItems where item.State == InstallState.Done select item; #if _LAUNCH_APP_ #region Remote Loader FileInfo[] appFiles = di.GetFiles("*.dll"); this.totalDllCount = appFiles.Length; this.currentDllIndex = 0; foreach (FileInfo fi in appFiles) { this.currentDllIndex++; LocalLoader localLoader = null; try { localLoader = new LocalLoader(System.IO.Path.GetDirectoryName(assembly.Location)); localLoader.LoadAssembly(fi.FullName); foreach (string entryName in localLoader.GetSubclasses(typeof(MarshalByRefObject).FullName)) { MarshalByRefObject entryObject = localLoader.CreateInstance(entryName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, new object[] { }); if (entryObject is IGadgetEntry) { IGadgetEntry entry = entryObject as IGadgetEntry; if (DataMgr.Instance.GadgetItems.IsAppExist(entry.Id)) { worker.ReportProgress(index++, entry.Title); Thread.Sleep(1); break; } string name = string.Empty; string website = string.Empty; string logo = string.Empty; if (entry is IAuthorInfo) { IAuthorInfo authorInfo = entry as IAuthorInfo; name = authorInfo.Name; website = authorInfo.WebSite; logo = authorInfo.Logo; } string thumbnail = System.IO.Path.GetDirectoryName(assembly.Location); thumbnail = System.IO.Path.Combine(thumbnail, @"AppLogos\"); thumbnail += entry.Id; thumbnail += System.IO.Path.GetExtension(entry.Thumbnail); if (!File.Exists(thumbnail)) { ExtractLogo(entry.Thumbnail, entry.Id, assembly.Location); } AppItem item = (AppItem)exeDomain.CreateInstanceAndUnwrap( exeAssembly, typeof(AppItem).FullName, true, BindingFlags.Default, null, new object[] { entry.Id, entry.Title, entry.Description, entry.CreateDate, thumbnail, entry.Tag, entry.SubTag, entryName, name, website, logo }, null, null ); item.AppEntryFile = fi.FullName; // GadgetItem item = new GadgetItem(); DataMgr.Instance.GadgetItems.Add(item); worker.ReportProgress(index++, entry.Title); Thread.Sleep(1); break; } } } catch (Exception ex) { } finally { if (localLoader != null) { localLoader.Unload(); localLoader = null; } } } this.currentDllIndex = this.totalDllCount; #endregion #else ////////////////////////////////////////////////////////// FileInfo[] appFiles = di.GetFiles("*.dll"); FileInfo[] memorizeDataFiles = memorizeDataDi.GetFiles("*.mre", SearchOption.AllDirectories); FileInfo[] assessmentAppFiles = assessmentDataDi.GetFiles("*.sla", SearchOption.AllDirectories); this.totalDllCount = appFiles.Length + memorizeDataFiles.Length + assessmentAppFiles.Length; this.currentDllIndex = 0; foreach (FileInfo fi in appFiles) { try { this.currentDllIndex++; Assembly gadgetAssembly = Assembly.LoadFrom(fi.FullName); bool found = false; // Assembly gadgetAssembly = Assembly.LoadFile(fi.FullName); Module[] modules = gadgetAssembly.GetModules(); foreach (Module module in modules) { Type[] types = module.GetTypes(); foreach (Type type in types) { if (type.IsClass && !type.IsAbstract) { Type entryInterface = type.GetInterface("IGadgetEntry"); if (entryInterface != null) { IGadgetEntry entry = gadgetAssembly.CreateInstance(type.FullName) as IGadgetEntry; if (DataMgr.Instance.getAppItemById(entry.Id) != null) { worker.ReportProgress(index++, entry.Title); Thread.Sleep(1); found = true; break; } string name = string.Empty; string website = string.Empty; string logo = string.Empty; if (entry is IAuthorInfo) { IAuthorInfo authorInfo = entry as IAuthorInfo; name = authorInfo.Name; website = authorInfo.WebSite; logo = authorInfo.Logo; } string thumbnail = entry.Thumbnail; string thumbnailFile = System.IO.Path.GetDirectoryName(assembly.Location); thumbnailFile = System.IO.Path.Combine(thumbnail, @"AppLogos\"); thumbnailFile += entry.Id; thumbnailFile += System.IO.Path.GetExtension(entry.Thumbnail); if (File.Exists(thumbnailFile)) { try { File.Delete(thumbnailFile); } catch { } } // if (entry.Tag == 200 && // entry.SubTag == 202) // thumbnail = @"pack://application:,,,/Resources/Images/GradeMath.png"; AppItem item = null; if (entry.Tag == 200 && entry.SubTag == 207) { item = new MathFastAppItem(entry.Id, entry.Title, entry.Description, entry.CreateDate, thumbnail, entry.Tag, entry.SubTag, type.FullName, name, website, logo); } else { item = new DllAppItem(entry.Id, entry.Title, entry.Description, entry.CreateDate, thumbnail, entry.Tag, entry.SubTag, type.FullName, name, website, logo); } var matchAppId = from app in newAppQuery where app.AppItem.UniqueId == entry.Id select app.AppItem.Id; if (matchAppId.Count() > 0) { item.IsNew = true; } item.Entry = entry; item.AppEntryFile = fi.FullName; // GadgetItem item = new GadgetItem(); DataMgr.Instance.addAppItem(item); worker.ReportProgress(index++, entry.Title); Thread.Sleep(1); found = true; break; } // end if } // end if } // end foreach if (found) { break; } } } catch { } } foreach (FileInfo fi in memorizeDataFiles) { try { MemorizeEntry entry = MemorizeEntry.Load(fi.FullName); MemorizeAppItem item = new MemorizeAppItem(); item.Id = entry.Id; item.Title = entry.Title; item.Description = entry.Description; item.Thumbnail = entry.Thumbnail; item.CreateDate = entry.CreateDate; item.CreatorName = entry.Creator; item.CreatorLogo = entry.CreatorLogo; item.CreatorWebSite = entry.CreatorWebsite; item.MemorizeEntry = entry; if (entry.SubType == 0) { item.SubType = 10299; } else { item.SubType = entry.SubType; } item.Type = 102; item.AppEntryFile = fi.FullName; DataMgr.Instance.addAppItem(item); worker.ReportProgress(index++, entry.Title); Thread.Sleep(1); } catch (Exception ex) { Debug.Assert(false, ex.Message); } } foreach (FileInfo fi in assessmentAppFiles) { try { AssessmentApp app = SerializerHelper <AssessmentApp> .XmlDeserialize(fi.FullName); AssessmentAppItem item = new AssessmentAppItem(); item.Id = app.Id; item.Title = app.Name; item.Description = app.Description; item.Thumbnail = app.Thumbnail; item.Type = 200; // app.Type; item.SubType = 201; // app.SubType; item.CreateDate = app.CreateDate.ToLocalTime(); item.AppEntryFile = fi.FullName; item.CreatorName = app.Creator; item.CreatorWebSite = app.CreatorWebsite; item.CreatorLogo = app.CreatorLogo; DataMgr.Instance.addAppItem(item); worker.ReportProgress(index++, item.Title); Thread.Sleep(1); } catch (Exception ex) { Debug.Assert(false, ex.Message); } } this.currentDllIndex = this.totalDllCount; #endif try { DataMgr.Instance.MruItems.Load(); } catch { } }