2.1 ‐ Running in Ruby Runtime - Z-H-Sun/tswKai GitHub Wiki

In the previous article, you have been shown how to run the Ruby source codes using a minimal Ruby interpreter, tswKai3_rt.exe. However, if you would like to make full use of a complete Ruby environment (such as importing extension libraries), continue reading the sections below.

Prerequisites

  • Install Ruby runtime.

    • I recommend using the RubyInstaller provided here.

    • Choose any version between 1.8 and 2.7.2 (included).

      • Ruby versions higher than 2.7.3 (included) do not support the callback function of the win32-api library.
    • No need to install with the Devkit bundle (i.e., not RubyInstaller+Devkit).

  • Add the win32/api extension.

    • Run gem install win32-api

      • This extension dropped support for Ruby 1.8 and 1.9 since version 1.6.0. If you are using Ruby < 2, you must specify adding the extension with version 1.5.3:

        gem install win32-api -v 1.5.3

Run

  • Download the latest source code (or source codes of any releases or branches that are no earlier than v3.1) and unzip.

  • Double click on main.rbw, or run rubyw main.rbw. That's it!

    • In order to use the BGM enhancement function, you will need to download and extract the MP3 package to the root path of the tswKai3 source code repository.
    • To statically patch a TSW executable, run rubyw main.rbw <path/of/TSW/exe>.

Caveats

  • The Ruby executable from RubyInstaller does not come with a proper app manifest as required by tswKai, so certain features might not be optimally configured.

    Click here for more details.
    • It won't display the controls in the so-called XP-style fashion
    • For low Ruby versions, there is no UAC manifest either. As a result, for the tswModStatic module (i.e., to patch a TSW executable statically), if it is under an admin-privileged path (e.g., 'C:\Program Files (x86)\Tower of the Sorcerer'), then the output EXE file will be redirected to the VirtualStore folder by the Windows system, with the original file unmodified.
  • While the tswKai module is working (i.e., the cheat console is active), the user shall never close the console window, which will cause the whole app to ungracefully exit and also a lot of potential issues (e.g., the TSW window will remain disabled). Although I have intentionally tried to prevent this from happening, like disabling the 'Close' button of the console window, the user (if they really persists so) can still achieve this unwanted operation by Alt+F4 or Ctrl+Break.

    Click here for more details.
    • Theoretically, this issue can be solved by the SetConsoleCtrlHandler API. However, its implementation in the win32/api extension can easily cause stack overflow.

Solutions

  • For the first point, you can use the compile/manifest.rb tool. Then running tswKai3 in the future should experience no such issue.

    Click here for more details.
    • Run where rubyw to find the path of rubyw.exe, e.g., 'C:\Ruby27-x64\bin\rubyw.exe'. Backup this file!

    • cd to the compile folder; run ruby manifest.rb <rubyw_path> > manifest.txt, where <rubw_path> is the absolute path that you get from the previous step.

    • Check the output in the terminal and in the manifest.txt file:

      • If the terminal says 'Warning: No manifest resource found in this file', and the file is empty:

        Copy the entire file contents in tswKai3.exe.MANIFEST to manifest.txt.

      • If the terminal says 'Successfully obtained the data':

        The manifest resource contents have been written to manifest.txt, and we need to do some modifications to it.

        Open the file and add some lines as shown in the example below (ignore if the lines are already there):

         <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
         <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
        +<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        +<security>
        +<requestedPrivileges>
        +<requestedExecutionLevel level="asInvoker"/>
        +</requestedPrivileges>
        +</security>
        +</trustInfo>
         <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
         <application>
         <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
         <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
         <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
         <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> 
         <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> 
         </application>
         </compatibility>
         <dependency>
         <dependentAssembly>
         <assemblyIdentity version="1.0.0.0" type="win32" name="ruby_builtin_dlls" />
         </dependentAssembly>
         </dependency>
         <file name="x64-msvcrt-ruby#{VERSION}.dll"/>
        +<dependency>
        +<dependentAssembly>
        +<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
        +</dependentAssembly>
        +</dependency>
         </assembly>
      • Finally, run ruby manifest.rb <rubyw_path> manifest.txt, and the update of rubyw's app manifest is complete. Again, <rubyw_path> is the absolute path obtained in the first step. Should any issue arise in the future running other Ruby scripts, restore the original backup rubw.exe.

  • For the second point, I have added the function to the modified win32/api extension, and you can use the vendor/win32/compile.rb script to compile it. Then running tswKai3 in the future should experience no such issue.

    Click here for more details.
    • You need to install the Devkit for Ruby.

      • For RubyInstaller with Ruby < 2.4, use the 1st-generation Devkit.

        Download the devkit here, extract its contents to any folder (which you won't change in the future, e.g., C:\Ruby<version_number>\Devkit), and run ruby dk.rb init and then ruby dk.rb install.

        Before running ruby dk.rb install, you may want to check the contents of config.yml under the devkit path. The devkit might be unable to find the 64-bit Ruby path. In such a case, write the following line to that file: - <Ruby_Installation_Path> (e.g, - C:/Ruby23-x64).

      • For RubyInstaller with Ruby >= 2.4, use MSYS2 with the corresponding compiler toolchain.

        Run ridk install.

    • For Ruby 1.8, you also need rake. The latest version that supports Ruby 1.8.7 is 10.5.0, so run gem install rake -v 10.5.0

    • Navigate to the vendor/win32 folder and double click on vendor/win32/compile.rb. That's the end of it!

  • Alternatively, use tswKai3_rt.exe to solve both issues as introduced in the Playground Mode section.

Customized Corrections of TSW

The TSW executables provided in the all-in-one package come with certain corrections for better gaming experience.

If you would like to restore or modify these revisions or apply them to unpatched TSW executables, you can make use of the tswRev.rb script. Its function is similar to the Config function of the tswLauncher, but it offers more flexibility and tunability by making use of the Ruby script language; however, you may want to edit the contents of the file to achieve what you desire.

  • Please edit the file according to the comments and examples there. For example, you can:
    • Alter $font to change the display font of the game;
    • Alter $pause to change the keyboard lag when you hold an arrow key;
    • etc. (See comments there for more information.)
  • Then Run it with ruby (or tswKai3_rt.exe if you don't have Ruby installed).

Conclusion

Hope you've enjoyed the journey up to here. By the end of this article, I hope you have successfully run the Ruby code. And if you feel like publishing the codes into standalone executables, then stay with me and set out for the next article, Publishing using Precompiled Exerb Header!

⚠️ **GitHub.com Fallback** ⚠️