Beispiel #1
0
 def loadCardset(self,
                 cs,
                 id=0,
                 update=7,
                 progress=None,
                 tocache=False,
                 noprogress=False):
     # print 'loadCardset', cs.ident
     r = 0
     if cs is None or cs.error:
         return 0
     if cs is self.cardset:
         if not tocache:
             self.updateCardset(id, update=update)
         return 1
     # cache carsets
     # self.cardsets_cache:
     #   key: Cardset.type
     #   value: (Cardset.ident, Images, SubsampledImages)
     c = self.cardsets_cache.get(cs.type)
     if c and c[0] == cs.ident:
         # print 'load from cache', c
         self.images, self.subsampled_images = c[1], c[2]
         if not tocache:
             self.updateCardset(id, update=update)
             if self.menubar is not None:
                 self.menubar.updateBackgroundImagesMenu()
         return 1
     #
     if progress is None and not noprogress:
         self.wm_save_state()
         self.wm_withdraw()
         title = _("Loading cardset %s...") % cs.name
         color = self.opt.colors['table']
         if self.tabletile_index > 0:
             color = "#008200"
         progress = PysolProgressBar(self,
                                     self.top,
                                     title=title,
                                     color=color,
                                     images=self.progress_images)
     images = Images(self.dataloader, cs)
     try:
         if not images.load(app=self, progress=progress):
             raise Exception("Invalid or damaged cardset")
         simages = SubsampledImages(images)
         if tocache:
             simages.setNegative(self.opt.negative_bottom)
         # The save cardsets option is deprecated, and its existence
         # directly conflicts with the ability to allow previews of
         # other cardset types.
         # if self.opt.save_cardsets:
         c = self.cardsets_cache.get(cs.type)
         if c:
             # c[1].destruct()
             destruct(c[1])
         self.cardsets_cache[cs.type] = (cs.ident, images, simages)
         if not tocache:
             # elif self.images is not None:
             #    # self.images.destruct()
             #    destruct(self.images)
             #
             if self.cardset:
                 if self.cardset.ident != cs.ident:
                     if self.cardset.type == cs.type:
                         # clear saved games geometry
                         self.opt.games_geometry = {}
             # update
             self.images = images
             self.subsampled_images = simages
             self.updateCardset(id, update=update)
         r = 1
     except (Exception, TclError, UnpicklingError) as ex:
         traceback.print_exc()
         cs.error = 1
         # restore settings
         self.nextgame.cardset = self.cardset
         if self.cardset:
             self.cardset_manager.setSelected(self.cardset.index)
         # images.destruct()
         destruct(images)
         MfxExceptionDialog(self.top,
                            ex,
                            title=_("Cardset load error"),
                            text=_("Error while loading cardset"))
     self.intro.progress = progress
     if r and not tocache and self.menubar is not None:
         self.menubar.updateBackgroundImagesMenu()
     return r
Beispiel #2
0
def pysol_init(app, args):

    # init commandline options (undocumented)
    opts = parse_option(args)
    if not opts:
        return 1
        sys.exit(1)
    opts, filename = opts
    if filename:
        app.commandline.loadgame = filename
    app.commandline.game = opts['game']
    if opts['gameid'] is not None:
        try:
            app.commandline.gameid = int(opts['gameid'])
        except ValueError:
            print_err(_('invalid game id: ') + opts['gameid'])

    # try to create the config directory
    for d in (
            app.dn.config,
            app.dn.savegames,
            os.path.join(app.dn.config, "music"),
            # os.path.join(app.dn.config, "screenshots"),
            os.path.join(app.dn.config, "tiles"),
            os.path.join(app.dn.config, "tiles", "stretch"),
            os.path.join(app.dn.config, "tiles", "save-aspect"),
            os.path.join(app.dn.config, "cardsets"),
            os.path.join(app.dn.config, "plugins"),
    ):
        if not os.path.exists(d):
            try:
                os.makedirs(d)
            except:
                traceback.print_exc()
                pass

    # init DataLoader
    f = os.path.join("html-src", "license.html")
    app.dataloader = DataLoader(args[0], f)

    # init toolkit 1)
    top = MfxRoot(className=TITLE)
    app.top = top
    app.top_bg = top.cget("bg")
    app.top_cursor = top.cget("cursor")

    # load options
    try:
        app.loadOptions()
    except:
        traceback.print_exc()
        pass

    # init toolkit 2)
    init_root_window(top, app)

    # prepare the progress bar
    app.loadImages1()
    if not app.progress_images:
        app.progress_images = (loadImage(app.gimages.logos[0]),
                               loadImage(app.gimages.logos[1]))
    app.wm_withdraw()

    # create the progress bar
    title = _("Welcome to %s") % TITLE
    color = app.opt.colors['table']
    if app.tabletile_index > 0:
        color = "#008200"
    app.intro.progress = PysolProgressBar(app,
                                          top,
                                          title=title,
                                          color=color,
                                          images=app.progress_images,
                                          norm=2.0)
    app.intro.progress.update(step=1)

    # init games database
    def progressCallback(*args):
        app.intro.progress.update(step=1)

    GAME_DB.setCallback(progressCallback)
    import pysollib.games
    if not opts['french-only']:
        import pysollib.games.ultra
        import pysollib.games.mahjongg
        import pysollib.games.special
        pysollib.games.special.no_use()

    # try to load plugins
    if not opts["noplugins"]:
        for dir in (os.path.join(app.dataloader.dir, "games"),
                    os.path.join(app.dataloader.dir,
                                 "plugins"), app.dn.plugins):
            try:
                app.loadPlugins(dir)
            except:
                pass
    GAME_DB.setCallback(None)

    # init audio 1)
    app.audio = None
    sounds = {
        'pss': PysolSoundServerModuleClient,
        'pygame': PyGameAudioClient,
        'oss': OSSAudioClient,
        'win': Win32AudioClient
    }
    if opts["nosound"] or SOUND_MOD == 'none':
        app.audio = AbstractAudioClient()
    elif opts['sound-mod']:
        c = sounds[opts['sound-mod']]
        app.audio = c()
    elif SOUND_MOD == 'auto':
        for c in (PyGameAudioClient, PysolSoundServerModuleClient,
                  OSSAudioClient, Win32AudioClient, AbstractAudioClient):
            try:
                app.audio = c()
                app.audio.startServer()
                app.audio.connectServer(app)
            except:
                pass
            else:
                # success
                break
    else:
        c = sounds[SOUND_MOD]
        app.audio = c()
        app.audio.startServer()
        app.audio.connectServer(app)

    # update sound_mode
    if isinstance(app.audio, PysolSoundServerModuleClient):
        app.opt.sound_mode = 1
    else:
        app.opt.sound_mode = 0

    # check games
    if len(app.gdb.getGamesIdSortedByName()) == 0:
        app.wm_withdraw()
        app.intro.progress.destroy()
        d = MfxMessageDialog(top,
                             title=_("%s installation error") % TITLE,
                             text=_('''
No games were found !!!

Main data directory is:
%s

Please check your %s installation.
''') % (app.dataloader.dir, TITLE),
                             bitmap="error",
                             strings=(_("&Quit"), ))
        return 1

    # init cardsets
    app.initCardsets()
    cardset = None
    c = app.opt.cardset.get(0)
    if c:
        cardset = app.cardset_manager.getByName(c[0])
        if cardset and c[1]:
            cardset.updateCardback(backname=c[1])
    if not cardset:
        cardset = app.cardset_manager.get(0)
    if app.cardset_manager.len() == 0 or not cardset:
        fatal_no_cardsets(app)
        return 3

    # init tiles
    manager = app.tabletile_manager
    tile = Tile()
    tile.color = app.opt.colors['table']
    tile.name = "None"
    tile.filename = None
    manager.register(tile)
    app.initTiles()
    if app.opt.tabletile_name:  # and top.winfo_screendepth() > 8:
        for tile in manager.getAll():
            if app.opt.tabletile_name == tile.basename:
                app.tabletile_index = tile.index
                break

    # init samples and music resources
    app.initSamples()
    app.initMusic()

    # init audio 2)
    if not app.audio.CAN_PLAY_SOUND:
        app.opt.sound = 0
    app.audio.updateSettings()
    # start up the background music
    if app.audio.CAN_PLAY_MUSIC:
        music = app.music_manager.getAll()
        if music:
            app.music_playlist = list(music)[:]
            app.miscrandom.shuffle(app.music_playlist)
            if 1:
                for m in app.music_playlist:
                    if m.name.lower() == "bye_for_now":
                        app.music_playlist.remove(m)
                        app.music_playlist.insert(0, m)
                        break
            app.audio.playContinuousMusic(app.music_playlist)

    # prepare other images
    app.loadImages2()
    app.loadImages3()
    app.loadImages4()

    # load cardset
    progress = app.intro.progress
    if not app.loadCardset(cardset, progress=progress, update=1):
        for cardset in app.cardset_manager.getAll():
            progress.reset()
            if app.loadCardset(cardset, progress=progress, update=1):
                break
        else:
            fatal_no_cardsets(app)
            return 3

    # ok
    return 0
Beispiel #3
0
 def loadCardset(self, cs, id=0, update=7, progress=None):
     # print 'loadCardset', cs.ident
     r = 0
     if cs is None or cs.error:
         return 0
     if cs is self.cardset:
         self.updateCardset(id, update=update)
         return 1
     # cache carsets
     # self.cardsets_cache:
     #   key: Cardset.type
     #   value: (Cardset.ident, Images, SubsampledImages)
     c = self.cardsets_cache.get(cs.type)
     if c and c[0] == cs.ident:
         # print 'load from cache', c
         self.images, self.subsampled_images = c[1], c[2]
         self.updateCardset(id, update=update)
         if self.menubar is not None:
             self.menubar.updateBackgroundImagesMenu()
         return 1
     #
     if progress is None:
         self.wm_save_state()
         self.wm_withdraw()
         title = _("Loading %s %s...") % (CARDSET, cs.name)
         color = self.opt.colors['table']
         if self.tabletile_index > 0:
             color = "#008200"
         progress = PysolProgressBar(self,
                                     self.top,
                                     title=title,
                                     color=color,
                                     images=self.progress_images)
     images = Images(self.dataloader, cs)
     try:
         if not images.load(app=self, progress=progress):
             raise Exception("Invalid or damaged " + CARDSET)
         simages = SubsampledImages(images)
         if self.opt.save_cardsets:
             c = self.cardsets_cache.get(cs.type)
             if c:
                 # c[1].destruct()
                 destruct(c[1])
             self.cardsets_cache[cs.type] = (cs.ident, images, simages)
         elif self.images is not None:
             # self.images.destruct()
             destruct(self.images)
         #
         if self.cardset:
             if self.cardset.ident != cs.ident:
                 if self.cardset.type == cs.type:
                     # clear saved games geometry
                     self.opt.games_geometry = {}
         # update
         self.images = images
         self.subsampled_images = simages
         self.updateCardset(id, update=update)
         r = 1
     except (Exception, TclError, UnpicklingError) as ex:
         traceback.print_exc()
         cs.error = 1
         # restore settings
         self.nextgame.cardset = self.cardset
         if self.cardset:
             self.cardset_manager.setSelected(self.cardset.index)
         # images.destruct()
         destruct(images)
         MfxExceptionDialog(self.top,
                            ex,
                            title=CARDSET + _(" load error"),
                            text=_("Error while loading ") + CARDSET)
     self.intro.progress = progress
     if r and self.menubar is not None:
         self.menubar.updateBackgroundImagesMenu()
     return r