Beispiel #1
0
        private AppItem CreateAppItem(string fileHeader, int type, int subType)
        {
            AppItem item = null;

            if (fileHeader == "{BE4A1507-5B37-42EA-9E08-43EF4F363C42}")
            {
                item = new MemorizeAppItem();
            }
            else if (fileHeader == "{8D2C2705-B49D-4730-BF39-B0E7E0E09172}")
            {
                item = new AssessmentAppItem();
            }
            else
            {
                if (type == 200 &&
                    subType == 207)
                {
                    item = new MathFastAppItem();
                }
                else
                {
                    item = new DllAppItem();
                }
            }

            return(item);
        }
        private void showApp()
        {
            try
            {
                this.appTipTImer.Stop();
                this.appTipBorder.Visibility = System.Windows.Visibility.Hidden;

                this.gadgetPanel.Child = null;
                if (this.currentAppItem is MemorizeAppItem)
                {
                    MemorizeAppItem appItem = this.currentAppItem as MemorizeAppItem;
                    MemorizeControl.Instance.Start(appItem.MemorizeEntry, DataMgr.Instance.LoginInfo.LoginId);
                    this.gadgetPanel.Child = MemorizeControl.Instance.StartupUI;

                    this.soundCheckBox.Visibility            = System.Windows.Visibility.Visible;
                    this.speechRecognizerCheckBox.Visibility = System.Windows.Visibility.Hidden;
                    AudioHelper.PlayBackgroundMusic(true);
                }
                else if (this.currentAppItem is AssessmentAppItem)
                {
                    AssessmentAppItem appItem = this.currentAppItem as AssessmentAppItem;
                    AssessmentAppControl.Instance.Start(appItem.AppEntryFile);

                    this.gadgetPanel.Child = AssessmentAppControl.Instance.StartupUI;
                }
                else
                {
                    this.gadgetPanel.Child = (UIElement)this.currentAppItem.Entry.GetStartupPage();
                    if ((this.currentAppItem.Entry.Capability & AppCapability.BackgroundMusic) != 0)
                    {
                        this.soundCheckBox.Visibility = System.Windows.Visibility.Visible;
                        AudioHelper.PlayBackgroundMusic(true);
                    }
                    else
                    {
                        this.soundCheckBox.Visibility = System.Windows.Visibility.Hidden;
                    }

                    if ((this.currentAppItem.Entry.Capability & AppCapability.SpeechRecognization) != 0)
                    {
                        this.speechRecognizerCheckBox.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        this.speechRecognizerCheckBox.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);

                MessageWindow msgWnd = new MessageWindow();
                msgWnd.ShowMessage(string.Format("启动应用{0}失败, 请与应用开发者联系。", this.currentAppItem.Title), MessageBoxButton.OK, (ok) =>
                {
                    this.goHome();
                });
            }
        }
        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);
        }
Beispiel #4
0
        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
                {
                }
            }
        }
        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
            {
            }
        }