[mythtv] [patch] (mythgame) dl images #2

mian mythtv at mian.net.au
Mon Feb 2 05:51:25 EST 2004


> Please,  when  starting  a  new  thread, DON'T "start" it by replying to
another thread.  That way your mail ends up in the middle of another
thread. Instead use the button called "New Mail" in you e-mail client.

no prob, sorry about that.

> If you press F12 when playing a game (xmame) a screenshot is made and a 
romname.png file is automaticly placed in your sreens folder.

yeah i know, I just like having them all downloaded transparently for me.

..sorry, last patch had a bug with execl(), perl script is just a tad crude.
-------------- next part --------------
--- work-orig/mythgame/mythgame/mametypes.h	2004-02-02 16:26:27.000000000 +1030
+++ work-mameimagedler/mythgame/mythgame/mametypes.h	2004-02-02 17:08:39.000000000 +1030
@@ -32,7 +32,9 @@
 
     int show_disclaimer;
     int show_gameinfo;
-
+    int automatically_download_images;
+    
+    QString image_downloader;
     QString rom_url;
     QString screenshot_url;
     QString flyer_url;
--- work-orig/mythgame/mythgame/gamesettings.cpp	2004-02-02 16:26:27.000000000 +1030
+++ work-mameimagedler/mythgame/mythgame/gamesettings.cpp	2004-02-02 17:16:32.000000000 +1030
@@ -121,6 +121,26 @@
     };
 };
 
+class MameImageDownloader: public LineEditSetting, public GlobalSetting {
+public:
+    MameImageDownloader():
+        GlobalSetting("MameImageDownloader") {
+        setLabel(QObject::tr("MAME image downloader"));
+        setValue("");
+        setHelpText(QObject::tr("The path to the MAME image downloader helper."));
+    };
+};
+
+class MameAutomaticallyDownloadImages: public CheckBoxSetting, public GlobalSetting {
+public:
+    MameAutomaticallyDownloadImages():
+        GlobalSetting("MameAutomaticallyDownloadImages") {
+        setLabel(QObject::tr("Automatically download images"));
+        setValue(true);
+        setHelpText(QObject::tr("Attempt to automatically download ROM images if they don't exist."));
+    };
+};
+
 class MameShowDisclaimer: public CheckBoxSetting, public GlobalSetting {
 public:
     MameShowDisclaimer():
@@ -260,6 +280,8 @@
     mame2->addChild(new MameCabinetsLocation());
     mame2->addChild(new MameHistoryLocation());
     mame2->addChild(new MameCheatLocation());
+    mame2->addChild(new MameImageDownloader());
+    mame2->addChild(new MameAutomaticallyDownloadImages());
     mame2->addChild(new MameShowDisclaimer());
     mame2->addChild(new MameShowGameInfo());
     addChild(mame2);
--- work-orig/mythgame/mythgame/mamehandler.cpp	2004-02-02 16:26:27.000000000 +1030
+++ work-mameimagedler/mythgame/mythgame/mamehandler.cpp	2004-02-02 17:13:09.000000000 +1030
@@ -930,6 +930,8 @@
     general_prefs.cheat_file = gContext->GetSetting("MameCheatLocation");
     general_prefs.show_disclaimer = gContext->GetNumSetting("MameShowDisclaimer");
     general_prefs.show_gameinfo = gContext->GetNumSetting("MameShowGameInfo");
+    general_prefs.automatically_download_images = gContext->GetNumSetting("MameAutomaticallyDownloadImages");
+    general_prefs.image_downloader = gContext->GetSetting("MameImageDownloader");
 }
 
 void MameHandler::SetGameSettings(GameSettings &game_settings, MameRomInfo *rominfo)
--- work-orig/mythgame/mythgame/mamerominfo.cpp	2004-02-02 16:26:27.000000000 +1030
+++ work-mameimagedler/mythgame/mythgame/mamerominfo.cpp	2004-02-02 20:49:51.128156704 +1030
@@ -1,4 +1,9 @@
+#include <iostream>
 #include <qsqldatabase.h>
+#include <mythtv/mythcontext.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
 #include "mamerominfo.h"
 #include "mametypes.h"
 
@@ -15,7 +20,7 @@
 
     QString thequery = "SELECT manu,cloneof,romof,driver,"
                        "cpu1,cpu2,cpu3,cpu4,sound1,sound2,sound3,sound4,"
-                       "players,buttons FROM mamemetadata WHERE "
+                       "players,buttons,image_searched FROM mamemetadata WHERE "
                        "romname=\"" + romname + "\";";
 
     QSqlQuery query = db->exec(thequery);
@@ -37,6 +42,7 @@
         sound1 = query.value(11).toString();
         num_players = query.value(12).toInt();
         num_buttons = query.value(13).toInt();
+        image_searched = query.value(14).toInt();
     }
 }
 
@@ -107,11 +113,36 @@
     }
 
     /* if image does not exist launch, check if we want to automatically try download */
-    if (*result == "")
+    if (type == "screenshot" && *result == "")
     {
-        if (1)
+        /* if we are automatically downloading images and this rom has not been processed before */
+        if (general_prefs.automatically_download_images && !ImageSearched() &&
+            dirs.size() > 0 && !general_prefs.image_downloader.isEmpty() &&
+            !dirs[0].isEmpty()
+        )
         {
+            /* spawn background image downloader */
+            QString cmdline = general_prefs.image_downloader + " ";
 
+            cmdline += "\"" + dirs[0] + "\" ";
+            cmdline += "\"" + games[0] + "\" ";
+            cmdline += "\"" + gamename + "\"";
+
+            cout << "Launching: " + cmdline << endl;
+
+            if (fork() == 0)
+            {
+                ::system(cmdline.ascii());
+                exit(0);
+            }
+
+            /* update SQL */
+            QString theQuery = "UPDATE mamemetadata SET image_searched = 1 WHERE "
+                               "romname=\"" + romname + "\";";
+
+            QSqlDatabase* db = QSqlDatabase::database(); 
+            QSqlQuery query = db->exec(theQuery);
+            setImageSearched(true);
         }
     }    
 
--- work-orig/mythgame/mythgame/mamerominfo.h	2004-02-02 16:26:27.000000000 +1030
+++ work-mameimagedler/mythgame/mythgame/mamerominfo.h	2004-02-02 17:03:44.000000000 +1030
@@ -20,8 +20,9 @@
                 QString lsystem = "",
                 QString lgamename ="",
                 QString lgenre = "",
-                int lyear = 0) :
-            RomInfo(lromname, lsystem, lgamename, lgenre, lyear)
+                int lyear = 0,
+                bool limage_searched = false) :
+            RomInfo(lromname, lsystem, lgamename, lgenre, lyear, limage_searched)
             {}
     MameRomInfo(const MameRomInfo &lhs) :
                 RomInfo(lhs)
@@ -48,6 +49,7 @@
                 vector = lhs.vector;
                 favourite = lhs.favourite;
                 timesplayed = lhs.timesplayed;
+                image_searched = lhs.image_searched;
             }
     MameRomInfo(const RomInfo &lhs) :
                 RomInfo(lhs) {}
@@ -116,6 +118,9 @@
 
     bool Favourite() const { return favourite; }
     void setFavourite(bool lfavourite) { favourite = lfavourite; }
+ 
+    bool ImageSearched() const { return image_searched; }
+    void setImageSearched(bool limage_searched) { image_searched = limage_searched; }
 
     int Timesplayed() const { return timesplayed; }
     void setTimesplayed(int ltimesplayed) { timesplayed = ltimesplayed; }
@@ -146,6 +151,7 @@
     bool vector;
     bool favourite;
     int timesplayed;
+    bool image_searched;
 };
 
 #endif
-------------- next part --------------
#!/usr/bin/perl

use LWP::Simple;

sub findgame
{
    my $want = @_[0];
    my $games, $url, $gamename;

    $games = get("http://www.vgmuseum.com/test95.html");
    if ($games ne "")
    {
        foreach (split(/\n/, $games))
        {
            chop;
            if (@args = /^<li><a\shref=\"(.*?)\">(.*?)<\/a>/)
            {
                $url = "http://www.vgmuseum.com/" . @args[0];
                $gamename = @args[1];
 
                if ($gamename eq $want)
                {
                    return $url;
                }
            }
        }
    }

    return "";
}

sub getimage
{
    my $url = @_[0];
    my $images, $filename, @res;

    $images = get($url);
    if ($images ne "")
    {
        foreach (split(/\n/, $images))
        {
            chop;
            if (@args = /<img src=\"(.*?)\"/i)
            {
                $filename = @args[0];

                $imageurl = $url;
                $imageurl =~ s/\/[\w]+\.html/\/$filename/g;
                push(@res, $imageurl);
            }
        }
    }

    return @res;
}

my $url, $screenshot_dir, $romname, $gamename, @images, $count, $image, $path, $contents;

if ($#ARGV != 2)
{
    exit(1);
}

$screenshot_dir = @ARGV[0];
$romname = @ARGV[1];
$gamename = @ARGV[2];

$url = findgame($gamename);

if ($url ne "")
{
    @images = getimage($url);
    if (@images)
    {
        my $count = $#ARGV;
        my $n = int(rand($count+1));
        if (($image = @images[$n]) ne "")
        {
            $path = $screenshot_dir . "/" . $romname;
            $path .= ".gif" if ($image =~ /\.gif/i);
            $path .= ".png" if ($image =~ /\.png/i);
            $path .= ".jpg" if ($image =~ /\.jpg/i);

            $contents = get($image);
#            if ($contents ne "")
            {
                print("$count image(s) found ($path) ($image)\n");

                open(F, ">$path");
                binmode(F);
                print(F $contents);
                close(F);

                exit(0);
            }
        }
    }
}

exit(1);


More information about the mythtv-dev mailing list