Major code cleanup

This commit is contained in:
punkrockguy318 2008-06-17 15:46:41 +00:00
parent e6cc29b7d2
commit cff7eb225a
1 changed files with 207 additions and 202 deletions

409
gfceu
View File

@ -31,65 +31,28 @@ import shutil
from optparse import OptionParser
from subprocess import Popen
# # # # # # # #
# Messaging Functions
def gfceu_print(text, use_gtk=False):
"""
gfceu_print()
This function prints messages to the user. This is generally used for status
messages. If a GTK message_box is requried, the use_gtk flag can be enabled.
"""
print text
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(text)
msgbox.run()
msgbox.destroy()
def gfceu_error(message, code, use_gtk=True, fatal=True):
"""
gfceu_error()
Presents the user with an error message and optionally quits the program.
"""
print title + ' error code '+str(code)+': ' + message
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(title + ' ERROR Code '+str(code)+':\n'+message)
msgbox.run()
msgbox.destroy()
if fatal:
sys.exit(code)
# # # # # # # #
# Import libraries
try:
import pytgtk
pygtk.require("2.12")
except:
pass
try:
import pygtk
pygtk.require("2.0")
import gtk
except ImportError:
gfceu_error('The PyGTK libraries cannot be found.\n\
print "The PyGTK libraries cannot be found.\n\
Ensure that PyGTK (>=2.12) is installed on this system.\n\
On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgtk2.0-0', 1, False)
sudo apt-get install python-gtk2 libgtk2.0-0"
try:
import gnomevfs
have_gnomevfs = True
except ImportError:
gfceu_error('The gnomevfs libraries cannot be found.\n\
print "The gnomevfs libraries cannot be found.\n\
To enable ROM loading over the network, ensure that gnomevfs is installed on\
this system.\n\
On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgnomevfs2-0', 5, False, False)
sudo apt-get install python-gtk2 libgnomevfs2-0"
have_gnomevfs = False
# # # # # # # #
@ -145,7 +108,7 @@ def give_widgets():
This function takes data from the options struct and relays it to
the GTK window
"""
global xml, options
global options, widgets
try:
widgets.get_object("rom_entry").set_text(options.romfile)
@ -173,27 +136,11 @@ def give_widgets():
widgets.get_object("host_port").set_value(float(options.host_port))
widgets.get_object("host_pass").set_text(options.host_pass)
except:
except AttributeError:
# When new widgets are added, old pickle files might break.
options = game_options()
give_widgets()
def setup_environment ():
"""
Configures the environment if this is the first time the application
has been run. For instance, it checks for the options file and creates
it if it doesn't exist. It also converts between the old version and
the new version of this application, which stores the options file in
a separate directory.
"""
global appconfigdir, optionsfile
if not os.path.exists(appconfigdir):
# this is the first time the application is run.
# create the directory
gfceu_print("Creating application settings directory")
os.mkdir(appconfigdir)
def set_options():
"""
@ -202,7 +149,6 @@ def set_options():
This function grabs all of the data from the GTK widgets
and stores it in the options object.
"""
global xml
options.romfile = widgets.get_object("rom_entry").get_text()
# sound
@ -229,105 +175,7 @@ def set_options():
def launch(rom_name, local=False):
global xml, options, fceu_server_binary, fceux_binary
set_options()
sound_options = ''
if options.sound_check:
sound_options += '--sound 1 '
else:
sound_options += '--sound 0 '
if options.soundq_check:
sound_options += '--soundq 1 '
else:
sound_options += '--soundq 0 '
if options.soundrate_entry:
sound_options += '--soundrate ' + options.soundrate_entry + ' '
else:
soundrate = ' '
# video
video_options = ''
if options.fullscreen_check:
video_options += '--fullscreen 1 '
else:
video_options += '--fullscreen 0 '
if options.opengl_check:
video_options += '--opengl 1 '
else:
video_options += '--opengl 0 '
video_options += ' --xscale ' + str(options.xscale_spin)
video_options += ' --yscale ' + str(options.yscale_spin)
video_options += ' '
# Netplay is fucked right now
"""
if options.join_radio:
if options.join_pass == '':
netpass = ''
else:
netpass = '--pass ' + '"' + options.join_pass + '" '
network = '-net "' + options.join_add + '"'\
' --port '+ str(options.join_port) + ' ' + netpass
else:
network = ''
if options.host_radio:
if options.host_pass == '':
netpass = ' '
else:
netpass = ' --pass ' + '"' + options.host_pass + '" '
network = '--net localhost --port '+\
str(options.host_port) + netpass + ' '
if local:
network = ''
"""
network = ''
command = fceux_binary + ' ' + sound_options + video_options +\
network + options.extra_entry + ' '+ rom_name
gfceu_print('Command: ' + command)
# more code to disable because netplay is fucked
"""
if options.host_radio:
xterm_binary = find_binary("xterm")
if xterm_binary == None:
gfceu_error("Cannot find xterm on this system. You will not \n\
be informed of server output.", 102, True, False)
args = [fceu_server_binary]
else:
args = [xterm_binary, "-e", fceu_server_binary]
args.append('--port')
args.append(str(options.host_port))
if options.host_pass:
args.append("--password")
args.append(options.host_pass)
pid = Popen(args).pid
"""
widgets.get_object("main_window").hide()
# os.system() is a blocker, so we must force
# gtk to process our events.
while gtk.events_pending():
gtk.main_iteration_do()
os.system(command)
widgets.get_object("main_window").show()
# another part of netplay code
"""
if options.host_radio:
os.kill(pid, 9)
"""
def find_binary(file):
@ -358,14 +206,86 @@ def find_binary(file):
options = None
appconfigdir = os.getenv('HOME') + '/.'+ title
optionsfile = appconfigdir + 'gfceu_options.dat'
fceux_binary = None
fceu_server_binary = None
widgets = None
class WidgetsWrapper:
class GfceuApp:
def __init__(self):
# Search for the glade file
# Check first in the directory of this script.
self.fceux_binary = self.find_fceu()
self.load_ui()
self.setup_environment()
options = game_options()
load_options()
give_widgets()
try:
gtk.main()
except KeyboardInterrupt:
sys.exit(0)
def msg(self, text, use_gtk=False):
"""
GfceuApp.msg()
This function prints messages to the user. This is generally used for status
messages. If a GTK message_box is requried, the use_gtk flag can be enabled.
"""
print text
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(text)
msgbox.run()
msgbox.destroy()
def print_error(self, message, code, use_gtk=True, fatal=True):
"""
GfceuApp.error()
Presents the user with an error message and optionally quits the program.
"""
print title + ' error code '+str(code)+': ' + message
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(title + ' ERROR Code '+str(code)+':\n'+message)
msgbox.run()
msgbox.destroy()
if fatal:
sys.exit(code)
def setup_environment (self):
"""
Configures the environment if this is the first time the application
has been run. For instance, it checks for the options file and creates
it if it doesn't exist. It also converts between the old version and
the new version of this application, which stores the options file in
a separate directory.
"""
global appconfigdir, optionsfile
if not os.path.exists(appconfigdir):
# this is the first time the application is run.
# create the directory
self.msg("Creating application settings directory")
os.mkdir(appconfigdir)
def find_fceu(self):
bin = find_binary('fceux')
if bin == None:
gfceu_error('Could not find the fceu binary.\n\
Ensure that FCE Ultra is installed and in the $PATH.\n\
On Debian based systems (like Ubuntu), try the following command:\n\
sudo apt-get install fceu', 4, True)
else:
self.msg('Using: ' + bin)
return bin
def load_ui(self):
global widgets
""" Search for the glade XML file and load it """
# Check first in the directory of this script.
if os.path.isfile('gfceu.xml'):
glade_file = 'gfceu.xml'
# Then check to see if its installed on a *nix system
@ -383,13 +303,117 @@ class WidgetsWrapper:
widgets.add_from_file(glade_file)
widgets.connect_signals(self)
except:
gfceu_error("Couldn't load the glade UI file", 24)
self.print_error("Couldn't load the glade UI file", 24)
widgets.get_object("main_window").show_all()
def launch(self, rom_name, local=False):
global options
set_options()
sound_options = ''
if options.sound_check:
sound_options += '--sound 1 '
else:
sound_options += '--sound 0 '
if options.soundq_check:
sound_options += '--soundq 1 '
else:
sound_options += '--soundq 0 '
if options.soundrate_entry:
sound_options += '--soundrate ' + options.soundrate_entry + ' '
else:
soundrate = ' '
# video
video_options = ''
if options.fullscreen_check:
video_options += '--fullscreen 1 '
else:
video_options += '--fullscreen 0 '
if options.opengl_check:
video_options += '--opengl 1 '
else:
video_options += '--opengl 0 '
video_options += ' --xscale ' + str(options.xscale_spin)
video_options += ' --yscale ' + str(options.yscale_spin)
video_options += ' '
# Netplay is fucked right now
"""
if options.join_radio:
if options.join_pass == '':
netpass = ''
else:
netpass = '--pass ' + '"' + options.join_pass + '" '
network = '-net "' + options.join_add + '"'\
' --port '+ str(options.join_port) + ' ' + netpass
else:
network = ''
if options.host_radio:
if options.host_pass == '':
netpass = ' '
else:
netpass = ' --pass ' + '"' + options.host_pass + '" '
network = '--net localhost --port '+\
str(options.host_port) + netpass + ' '
if local:
network = ''
"""
network = ''
command = self.fceux_binary + ' ' + sound_options + video_options +\
network + options.extra_entry + ' '+ rom_name
self.msg('Command: ' + command)
# more code to disable because netplay is fucked
"""
if options.host_radio:
xterm_binary = find_binary("xterm")
if xterm_binary == None:
gfceu_error("Cannot find xterm on this system. You will not \n\
be informed of server output.", 102, True, False)
args = [self.server_binary]
else:
args = [xterm_binary, "-e", self.server_binary]
args.append('--port')
args.append(str(options.host_port))
if options.host_pass:
args.append("--password")
args.append(options.host_pass)
pid = Popen(args).pid
"""
widgets.get_object("main_window").hide()
# os.system() is a blocker, so we must force
# gtk to process our events.
while gtk.events_pending():
gtk.main_iteration_do()
os.system(command)
widgets.get_object("main_window").show()
# another part of netplay code
"""
if options.host_radio:
os.kill(pid, 9)
"""
### Callbacks
def launch_button_clicked(self, arg1):
global xml
global options
options.romfile = widgets.get_object("rom_entry").get_text()
if widgets.get_object("rom_entry").get_text() == '':
gfceu_print('Please specify a ROM to open in the main tab.', True)
self.msg('Please specify a ROM to open in the main tab.', True)
return
if options.network_rom:
try:
@ -418,16 +442,15 @@ class WidgetsWrapper:
else:
romfile = options.romfile
launch('"'+romfile+'"')
self.launch('"'+romfile+'"')
def about_button_clicked(self, menuitem, data=None):
global xml
widgets.get_object("about_dialog").set_name('GNOME FCE Ultra '+version)
widgets.get_object("about_dialog").run()
widgets.get_object("about_dialog").hide()
def browse_button_clicked(self, menuitem, data=None):
global xml,options
global options
set_options()
chooser = gtk.FileChooserDialog("Open...", None,
gtk.FILE_CHOOSER_ACTION_OPEN,
@ -506,10 +529,10 @@ class WidgetsWrapper:
options.no_network_radio = False
def host_radio_clicked(self, menuitem, data=None):
global fceu_server_binary
if widgets.get_object("host_radio").get_active():
fceu_server_binary = find_binary('fceu-server')
if fceu_server_binary == None:
self.server_binary = find_binary('fceu-server')
if self.server_binary == None:
if os.name == 'nt':
gfceu_error("The fceu server software cannot be found. \n\
Ensure that it is installed in the same directory as \n\
@ -522,7 +545,6 @@ class WidgetsWrapper:
options.no_network_radio = True
return False
gfceu_print("Using: "+fceu_server_binary)
widgets.get_object("join_frame").set_sensitive(False)
widgets.get_object("host_frame").set_sensitive(True)
options.join_radio = False
@ -537,38 +559,21 @@ class WidgetsWrapper:
options.no_network_radio = True
def end(self, menuitem, data=None):
global xml, options, optionsfile
global options, optionsfile
set_options()
save_options()
gtk.main_quit()
widgets = None
# # # # # # # #
# main
if __name__ == '__main__':
# Parse options
fceux_binary = find_binary('fceux')
parser = OptionParser(version='%prog '+ version)
parser.add_option('-b', '--binary', action="store", type="string", dest="fceux_binary")
parser.parse_args()
if fceux_binary == None:
gfceu_error('Could not find the fceu binary.\n\
Ensure that FCE Ultra is installed and in the $PATH.\n\
On Debian based systems (like Ubuntu), try the following command:\n\
sudo apt-get install fceu', 4, True)
else:
gfceu_print('Using: '+fceux_binary)
wrap = WidgetsWrapper()
widgets.get_object("main_window").show_all()
setup_environment()
options = game_options()
load_options()
give_widgets()
try:
gtk.main()
except KeyboardInterrupt:
sys.exit(0)
app = GfceuApp()