Multiple Game Instances [RESOLVED]

Hi there!,
Is there a way for RMXP to detect whether another copy of the game is running on the same machine?
My game is an Online one, and people are exploiting the PvP system by getting a dud character and sitting them
in the PvP area, killing them, and earning far too much money and exp for what its worth.

Possibly a script that can allow me to specify the executable name, so i can ban certain applications, not just multiple instances of Game.exe from running. (eg Cheat Engine)

But just multiple instances would be fine.
Any ideas would be greatly appreciated!
Thanks guys
Are you having them authenticate when they log in? If so, you can just deny any additional logs
Yes, there is definitely authentification, but that won't help in this matter.
Sadly, as Sweet Vengance is gone (programmer of the server I use), i don't have a server I can tinker with :\
So I have to just make do with what I have.
Well just check the IP. When a person logs in check if the same IP of a person is already online, if it is then deny access.

Also that gives you a great anti-hack if someone steals user data, when people can't login they will receive that message that the same IP is already logged in.

If this is implemented in authentication part then you should have NO problems. The only other way people should be able to log into your game is via a proxy, but since you're not probably allowing it to be used from within a game you should be safe.

But if you really want a one-instance script. Here:
  1.  
  2. #==============================================================================
  3. # OneInstance Script
  4. # by Drago del Fato
  5. # Version: 1.0.1
  6. # License: Free for non commercial
  7. # If you use it put me in credits.
  8. #==============================================================================
  9. # [ Instructions ]
  10. # Insert above main.
  11. # In Main Script post this:
  12. # 1. After begin put this:  
  13. #      $instance_test = OneInstance.new
  14. #
  15. # 2. After main loop put this, it should look like:
  16. #   while $scene != nil
  17. #    $scene.main
  18. #  end
  19. #  $instance_test.dispose
  20. #
  21. # 3. Before end put this:
  22. # rescue SystemExit
  23. #   $instance_test.dispose
  24. #
  25. #
  26. # And you're set. You can edit the name of the token file, and the message
  27. # which will be displayed if an user runs another instance of the game.
  28. # Token file cannot be deleted while the game is running, and after the game
  29. # closes token file is deleted. If people cannot run their game. Say that they,
  30. # need to delete it before running it if in any case it stays (sudden system
  31. # reboot, power failure or similar)
  32. #==============================================================================
  33. module OneInstanceConsts
  34.   MESSAGE = "This program is already opened. Closing now."
  35.   TOKEN_FILE = "token"
  36. end
  37.  
  38. class OneInstance
  39.  
  40.   def initialize
  41.      if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  42.        print OneInstanceConsts::MESSAGE
  43.        exit!
  44.      end
  45.      @fToken = File.open("./" + OneInstanceConsts::TOKEN_FILE, "w")
  46.    end
  47.    
  48.    def dispose
  49.      @fToken.close
  50.      File.delete("./" + OneInstanceConsts::TOKEN_FILE)
  51.    end
  52. end
  53.  
Expand to see the code.


This script creates an empty token file which is a flag that the game is open. Any new instance of the game will check if that file exists. And since it's open by the game you cannot delete it by any normal means.

Please note that this is kind of a quick way to solve a problem, one instance checking would require a bit of Win32API calls to get Process ID's, this is an easy way to fix the problem.
Until I get around to writing a IP checking section for the logging, i'll use this for a fix until then. Thankyou Drago del Fato!
That is a creative way of handling it. Just create a file and check for it. Fun stuff.
It just says it can't find the file :\
At which part of the script does it make the file?


Never mind. Fixed it.

Ahh, nope, another problem. I have buttons like the 'x' button. When u click it, it makes $scene = nil, right?
When I click it, it raises 'IO: stream closed.

Heres my Main:
  1. #==============================================================================
  2. # ** Netplay Main
  3. #------------------------------------------------------------------------------
  4. #  After defining each class, actual processing begins here.
  5. #==============================================================================
  6.  
  7. begin
  8.   $instance_test = OneInstance.new
  9.   $footsteps = false
  10.   Graphics.frame_rate = 100
  11.  
  12. =begin
  13. if $full == nil
  14.  $showm = Win32API.new 'user32', 'keybd_event', %w(l l l l), ''
  15. $showm.call(18,0,0,0)
  16. $showm.call(13,0,0,0)
  17. $showm.call(13,0,2,0)
  18. $showm.call(18,0,2,0)
  19. $full = 1
  20. end#  Resolution.initialize
  21. =end
  22.  
  23.    width = Screen_Data::Screen_Data[0].to_s.to_i
  24.    height = Screen_Data::Screen_Data[1].to_s.to_i
  25.    screen = Screen_Size.new
  26.    screen.change_screen_size(width, height)
  27.    
  28.   Network::Main.initialize
  29.   $game_language = Game_Language.new
  30.   $hudskin = true
  31.   $no_pvp = 0
  32.   $smooth_scroll = true
  33.   $Respawn = []
  34.   # Font used in windows
  35.   $defaultfonttype = $fontface = $fontname = Font.default_name = "Verdana"
  36.   # Font size used
  37.   $defaultfontsize = $fontsize = Font.default_size =  14
  38.   $mouse = $Mouse = Game_Mouse.new
  39.   $Mouse.visible
  40.   # Sets default font settings.
  41.   Font.default_name = "Verdana"
  42.   Font.default_size = 18
  43.   Font.default_color = Window_Edits::Normal_color
  44.   Font.default_bold = false
  45.   Font.default_italic = false
  46.   # Change the $fontface variable to change the font style
  47.   $fontface = "Verdana"
  48.   # Change the $fontsize variable to change the font size
  49.   $fontsize = 24
  50.   # Prepare for transition
  51.   Graphics.freeze
  52.   # Make scene object (connect screen)
  53.   $scene = Scene_Connect.new
  54.   # Call main method as long as $scene is effective
  55.   while $scene != nil
  56.       $scene.main
  57.     end
  58.   $instance_test.dispose
  59.   # Fade out
  60.   Network::Main.close_socket
  61.   Graphics.transition(25)
  62.   #exit
  63. rescue SystemExit
  64.     $instance_test.dispose
  65. rescue Errno::ENOENT
  66.   Network::Main.close_socket
  67.   # Supplement Errno::ENOENT exception
  68.   # If unable to open file, display message and end
  69.   filename = $!.message.sub("No such file or directory - ", "")
  70.   print("Unable to find file #{filename}.")
  71.   time = Time.now
  72.   time = time.strftime("%a %d %b %Y, %X")
  73.   File.open("ErrorLog.rxdata","a+"){ |fh| fh.puts("On <<#{time}>> the file <<#{filename}>> was missing." )}
  74. ensure
  75. #  $window.normal
  76.   Network::Main.close_socket if Network::Main.socket != nil
  77. end
  78.  
Expand to see the code.


Whats the problem?
Hmm... here I modified it a bit.

  1.  
  2. #==============================================================================
  3. # OneInstance Script
  4. # by Drago del Fato
  5. # Version: 1.0.2
  6. # License: Free for non commercial
  7. # If you use it put me in credits.
  8. #==============================================================================
  9. # [ Instructions ]
  10. # Insert above main.
  11. # In Main Script post this:
  12. # 1. After begin put this:  
  13. #      $instance_test = OneInstance.new
  14. #
  15. # 2. After main loop put this, it should look like:
  16. #   while $scene != nil
  17. #    $scene.main
  18. #  end
  19. #  $instance_test.dispose
  20. #
  21. # 3. Before end put this:
  22. # rescue SystemExit
  23. #   $instance_test.dispose
  24. #
  25. #
  26. # And you're set. You can edit the name of the token file, and the message
  27. # which will be displayed if an user runs another instance of the game.
  28. # Token file cannot be deleted while the game is running, and after the game
  29. # closes token file is deleted. If people cannot run their game. Say that they,
  30. # need to delete it before running it if in any case it stays (sudden system
  31. # reboot, power failure or similar)
  32. #==============================================================================
  33. module OneInstanceConsts
  34.   MESSAGE = "This program is already opened. Closing now."
  35.   TOKEN_FILE = "token"
  36. end
  37.  
  38. class OneInstance
  39.  
  40.   def initialize
  41.      if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  42.        print OneInstanceConsts::MESSAGE
  43.        exit!
  44.      end
  45.      @fToken = File.open("./" + OneInstanceConsts::TOKEN_FILE, "w")
  46.    end
  47.    
  48.    def dispose
  49.       @fToken.close if !@fToken.closed?
  50.      File.delete("./" + OneInstanceConsts::TOKEN_FILE) if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  51.    end
  52. end
  53.  
Expand to see the code.


It shouldn't cause those problems now.
And since you're using ensure in that main class we can use it for this script too. Just put $instance_test.dispose there and remove:
rescue SystemExit
$instance_test.dispose

and $instance_test.dispose after:
while $scene != nil
$scene.main
end

That should definitely fix the problem if the above fix doesn't. :)
And it works. Thanks :D
Problem solved!
Oh, and 1 more thing, for others reading this thread who want to use this script (if there are any others)
This will attempt to delete the token upon startup, so if there was a power faliure or the game was closed via processes, it will still work.
Here it is:
  1. #==============================================================================
  2. # OneInstance Script
  3. # by Drago del Fato
  4. # Version: 1.0.1
  5. # License: Free for non commercial
  6. # If you use it put me in credits.
  7. #==============================================================================
  8. # [ Instructions ]
  9. # Insert above main.
  10. # In Main Script post this:
  11. # 1. After begin put this:  
  12. #      $instance_test = OneInstance.new
  13. #
  14. # 2. After main loop put this, it should look like:
  15. #   while $scene != nil
  16. #    $scene.main
  17. #  end
  18. #  $instance_test.dispose
  19. #
  20. # 3. Before end put this:
  21. # rescue SystemExit
  22. #   $instance_test.dispose
  23. #
  24. #
  25. # And you're set. You can edit the name of the token file, and the message
  26. # which will be displayed if an user runs another instance of the game.
  27. # Token file cannot be deleted while the game is running, and after the game
  28. # closes token file is deleted. If people cannot run their game. Say that they,
  29. # need to delete it before running it if in any case it stays (sudden system
  30. # reboot, power failure or similar)
  31. #==============================================================================
  32. module OneInstanceConsts
  33.   MESSAGE = "ERROR: Another instance of this program is already running."
  34.   TOKEN_FILE = "Active"
  35. end
  36.  
  37. class OneInstance
  38.  
  39.   def initialize
  40.     if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  41.       File.delete("./" + OneInstanceConsts::TOKEN_FILE) rescue
  42.       if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  43.         print OneInstanceConsts::MESSAGE
  44.         exit! if $DEBUG == false or $DEBUG == nil
  45.        else
  46.         @fToken = File.open("./" + OneInstanceConsts::TOKEN_FILE, "w")
  47.       end
  48.     end
  49.     @fToken = File.open("./" + OneInstanceConsts::TOKEN_FILE, "w")
  50.   end
  51.    
  52.    def dispose
  53.      @fToken.close if [email protected]?
  54.      File.delete("./" + OneInstanceConsts::TOKEN_FILE) if FileTest.exists?("./" + OneInstanceConsts::TOKEN_FILE)
  55.    end
  56. end
  57.  
Expand to see the code.


It's a tiny modification that goes a fair way :D
A much smaller version and just as efficient. No need to modify main either. VX Compatible as well.
  1. #===============================================================================
  2. # One Game Instance
  3. # Version 1.0
  4. # Author game_guy
  5. #-------------------------------------------------------------------------------
  6. # Intro:
  7. # Prevents players from opening multiple instances of your games.
  8. #
  9. # Features:
  10. # Prevents Multiple Instances
  11. # Custom File/Error
  12. # XP/VX Compatible
  13. # Allows multiple instances in debug mode.
  14. #
  15. # Instructions:
  16. # Place as first script.
  17. # Configure the 2 variables and thats all!
  18. #
  19. # Compatibility:
  20. # Works with everything.
  21. #
  22. # Credits:
  23. # game_guy ~ For creating it.
  24. # ZenVirZan ~ For requesting it.
  25. #===============================================================================
  26.  
  27. # Custom error message.
  28. ERROR = "Instance already running."
  29. # Custom file name, I'd recommend leaving the ENV[Appdata]
  30. TOKEN_FILE = ENV['APPDATA'] + "game_name.token"
  31.  
  32. begin
  33.   if FileTest.exists?(TOKEN_FILE)
  34.     begin
  35.       File.delete(TOKEN_FILE)
  36.     rescue Errno::EACCES
  37.       if $DEBUG == false || $DEBUG == nil || $TEST == false || $TEST == nil
  38.         print ERROR
  39.         exit
  40.       end
  41.     end
  42.   end
  43.   $token = File.open(TOKEN_FILE, "w")
  44. end
Expand to see the code.
Great that you want to make it more compact, but since you're already doing it you can delete lines 37 and 40 since they're pretty useless, since you made a if clause which is ALWAYS true, no matter what.

In most Interpreter languages (and also in Ruby) if a variable is not defined and you try to get value from it, you will get nil as value. The reason why Ruby shows errors when you use non-defined variables is when you try to use them with operators +, -, *, / and comparative operators > and <. They are not defined for nil class so when you try to use them you will get an error. Equality operator (==) checks if the value on the left is same as the value on the right, and that will always return false when the value on the left is nil. Only time the non-defined variable will return true is if you try $NONDEFINED == nil, that is always true.

I suppose you tried to make this in a way so that this code doesn't execute when the script is in debug mode (running inside IDE) you should make it like this:

  1.  
  2. #===============================================================================
  3. # One Game Instance
  4. # Version 1.0
  5. # Author game_guy
  6. #-------------------------------------------------------------------------------
  7. # Intro:
  8. # Prevents players from opening multiple instances of your games.
  9. #
  10. # Features:
  11. # Prevents Multiple Instances
  12. # Custom File/Error
  13. # XP/VX Compatible
  14. # Allows multiple instances in debug mode.
  15. #
  16. # Instructions:
  17. # Place as first script.
  18. # Configure the 2 variables and thats all!
  19. #
  20. # Compatibility:
  21. # Works with everything.
  22. #
  23. # Credits:
  24. # game_guy ~ For creating it.
  25. # ZenVirZan ~ For requesting it.
  26. #===============================================================================
  27.  
  28. # Custom error message.
  29. ERROR = "Instance already running."
  30. # Custom file name, I'd recommend leaving the ENV[Appdata]
  31. TOKEN_FILE = ENV['APPDATA'] + "game_name.token"
  32.  
  33. begin
  34.   if FileTest.exists?(TOKEN_FILE)
  35.     begin
  36.       File.delete(TOKEN_FILE)
  37.     rescue Errno::EACCES
  38.         if !$TEST && !$DEBUG
  39.           print ERROR
  40.           exit
  41.         end
  42.     end
  43.   end
  44.   $token = File.open(TOKEN_FILE, "w")
  45. end
  46.  
Expand to see the code.


This would be a right approach to the boolean checking problem. 🙂

Anyways, great way to solve this too. 🙂
Yeah, i'm happy with what I have now, so I'll stay with that
I've also already changed Drago's to disable when in Debug mode too.
Thanks anyway, game_guy!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=74963